diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-02-07 11:21:23 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-02-07 11:21:23 -0500 |
| commit | 39020cf5ae3030bc15035925a0c72eb44eea67b7 (patch) | |
| tree | fb82b339c1e5c14334f8d9839f8d836b120fbb08 /gen/dp.py | |
| parent | d524da9bd072ad1be4ac0d633e3e783094ddc2d7 (diff) | |
Added gen_exps.py script.
Diffstat (limited to 'gen/dp.py')
| -rw-r--r-- | gen/dp.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/gen/dp.py b/gen/dp.py new file mode 100644 index 0000000..0ac8cce --- /dev/null +++ b/gen/dp.py | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | from __future__ import division | ||
| 2 | |||
| 3 | class DesignPointGenerator(object): | ||
| 4 | '''Iterates over all combinations of values specified in options. | ||
| 5 | Shamelessly stolen (and simplified) from bcw.''' | ||
| 6 | def __init__(self, options): | ||
| 7 | self.point_idx = 0 # Current point | ||
| 8 | self.options = options | ||
| 9 | self.total = 1 | ||
| 10 | for x in options.itervalues(): | ||
| 11 | self.total *= len(x) | ||
| 12 | |||
| 13 | def __iter__(self): | ||
| 14 | return self | ||
| 15 | |||
| 16 | def next(self): | ||
| 17 | while True: | ||
| 18 | if self.point_idx == self.total: | ||
| 19 | raise StopIteration | ||
| 20 | else: | ||
| 21 | point = {} | ||
| 22 | |||
| 23 | divisor = 1 | ||
| 24 | for key in sorted(self.options.keys()): | ||
| 25 | size = len(self.options[key]) | ||
| 26 | |||
| 27 | option_idx = int(self.point_idx / divisor) % size | ||
| 28 | point[key] = self.options[key][option_idx] | ||
| 29 | |||
| 30 | divisor *= size | ||
| 31 | self.point_idx += 1 | ||
| 32 | |||
| 33 | return point | ||
