0

When trying to find optimal parameters for some program, it would be handy if the code could be automatically executed for all possible values of a parameter in a certain range, without having to manually add for loops everywhere. Let's explain:

Let prms be a dict of parameters. If each value of this dict is not a list, then the following code should be normally executed, like this:

prms = dict()
prms['param1'] = 3
prms['param2'] = 4
prms['param3'] = -17

do_something(prms)

But if each parameter is a list, then the program should be re-executed for each value of the list. Example:

prms = dict()
prms['param1'] = [3, 7]
prms['param2'] = [4]
prms['param3'] = [-17, 2]

should give:

p = dict()
for p['param1'] in prms['param1']:
  for p['param2'] in prms['param2']:
    for p['param3'] in prms['param3']:
        do_something(p)              

Is there a programming pattern / nice way to do this?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Well, write your test recursively. Also, what is optimal supposed to mean? – timgeb Dec 09 '15 at 12:55
  • I don' get the should part: for prms['param1'] in temp_param1: how does it make sense, they are equal right? also what does your do_something takesas a parameter? – Scientist1642 Dec 09 '15 at 13:20

2 Answers2

1

As I can't comment I will drop this as an answer. I guess you are looking for itertools.product. It should let you create all possible sets of parameters for your program. I don't know yet how to combine dictionary.values() with itertools.product but it's something to start with.

Another question is finding optimal set of parameters. You should probably try creating test function and using chi-squared tests.

Hipnotyzer
  • 41
  • 1
  • 3
  • Thanks a lot, it helped me much in writing the solution that I'm going to post here. – Basj Dec 09 '15 at 14:22
0

Here is the solution, thanks to this related question.

import itertools    

prms = dict()
prms['param1'] = [3, 7]
prms['param2'] = [4]
prms['param3'] = [-17, 2]

for p in (dict(itertools.izip(prms, x)) for x in itertools.product(*prms.itervalues())):
    print p

which outputs:

{'param3': -17, 'param2': 4, 'param1': 3}
{'param3': -17, 'param2': 4, 'param1': 7}
{'param3': 2, 'param2': 4, 'param1': 3}
{'param3': 2, 'param2': 4, 'param1': 7}

Conclusion: This single line allows to test all the possible combinations of parameters made entered in prms. Useful when the program you write has many parameters that need to be optimized!

Example of use case: should I take FFT of length 128, 256, 512, 1024? Should I take overlap factor = 2, 4? Using this cartesian product, we can test all the possible combinations.

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673