Created
April 25, 2014 16:19
-
-
Save blahah/11295062 to your computer and use it in GitHub Desktop.
Recursive, on-demand yielding, lazy parameter sweep generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Combinator | |
include Enumerable | |
def initialize parameters | |
@parameters = parameters | |
end | |
def generate_combinations(index, opts, &block) | |
if index == @parameters.length | |
block.call opts.clone | |
return | |
end | |
# recurse | |
key = @parameters.keys[index] | |
@parameters[key].each do |value| | |
opts[key] = value | |
generate_combinations(index + 1, opts, &block) | |
end | |
end | |
def each &block | |
generate_combinations(0, {}, &block) | |
end | |
end | |
parameters = { | |
:K=>[21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, | |
55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81], | |
:M=>[0, 1, 2, 3], | |
:d=>[0, 1, 2, 3, 4, 5, 6], | |
:D=>[0, 1, 2, 3, 4, 5, 6], | |
:G=>[50, 75, 100, 125, 150, 175, 200, 225, 250], | |
:L=>[50, 100, 150, 200, 250], | |
:e=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | |
:t=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | |
} | |
c = Combinator.new parameters | |
d = c.to_enum | |
600_000.times do | |
a = d.next | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also https://gist.github.com/cboursnell/011edf5c2aed2c65dba9