Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [Puzzle] How to do parameter variation?

Reply
Thread Tools

[Puzzle] How to do parameter variation?

 
 
Gary Boone
Guest
Posts: n/a
 
      12-07-2006

Suppose you want to do several runs of a program or function, varying
the parameters each time. You start with a hash of parameter lists:

params = { speed=>[1,2,3], beta=>[0.1, 0.002, 0.4], x=>['a', 'b'] }

Given this set, you'll call the test function 18 times:

test_function( {speed=>1, beta=>0.1, x=>'a'} )
test_function( {speed=>1, beta=>0.1, x=>'b'} )
test_function( {speed=>1, beta=>0.002, x=>'a'} )
... etc

So the puzzle is how to take the original params list and generate a
list of all the combinations of the parameters? The resulting parameter
sets will be passed one at a time to the function as shown above.

Notes:
- I used hashes and lists to explain the problem. Maybe there's a better
way.
- You don't know how many parameters will be given.
- The number of values for each parameter is unknown in advance.
- The parameters have different types, as shown above. Each parameter's
values are all the same type, though.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Gregory Seidman
Guest
Posts: n/a
 
      12-07-2006
On Thu, Dec 07, 2006 at 04:25:43PM +0900, Gary Boone wrote:
}
} Suppose you want to do several runs of a program or function, varying
} the parameters each time. You start with a hash of parameter lists:
}
} params = { speed=>[1,2,3], beta=>[0.1, 0.002, 0.4], x=>['a', 'b'] }
}
} Given this set, you'll call the test function 18 times:
}
} test_function( {speed=>1, beta=>0.1, x=>'a'} )
} test_function( {speed=>1, beta=>0.1, x=>'b'} )
} test_function( {speed=>1, beta=>0.002, x=>'a'} )
} ... etc
}
} So the puzzle is how to take the original params list and generate a
} list of all the combinations of the parameters? The resulting parameter
} sets will be passed one at a time to the function as shown above.
}
} Notes:
} - I used hashes and lists to explain the problem. Maybe there's a better
} way.
} - You don't know how many parameters will be given.
} - The number of values for each parameter is unknown in advance.
} - The parameters have different types, as shown above. Each parameter's
} values are all the same type, though.

params = { :speed => [1,2,3], :beta => [0.1, 0.002, 0.4], => ['a', 'b'] }

all = params.inject([{}]) do |perms,(k,list)|
perms.inject([]) do |expanded,base|
list.each do |v|
expanded << base.merge(k => v)
end
expanded
end
end

require 'yaml'
puts all.size
puts all.to_yaml

--Greg


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      12-07-2006
On Dec 7, 2006, at 1:25 AM, Gary Boone wrote:

>
> Suppose you want to do several runs of a program or function, varying
> the parameters each time. You start with a hash of parameter lists:
>
> params = { speed=>[1,2,3], beta=>[0.1, 0.002, 0.4], x=>['a',
> 'b'] }
>
> Given this set, you'll call the test function 18 times:
>
> test_function( {speed=>1, beta=>0.1, x=>'a'} )
> test_function( {speed=>1, beta=>0.1, x=>'b'} )
> test_function( {speed=>1, beta=>0.002, x=>'a'} )
> ... etc


See if this gives you some ideas:

#!/usr/bin/env ruby -w

def call_with(meth, params, selected = Hash.new)
params = params.dup
key = params.keys.sort_by { |k| k.to_s }.first
choices = params.delete(key)

choices.each do |choice|
selected = selected.merge(key => choice)
if params.empty?
send(meth, selected)
else
call_with(meth, params, selected)
end
end
end

def print_args(args)
p args
end

call_with rint_args, :speed => [1, 2, 3],
:beta => [0.1, 0.002, 0.4],
=> ["a", "b"]
# >> {:speed=>1, :beta=>0.1, =>"a"}
# >> {:speed=>1, :beta=>0.1, =>"b"}
# >> {:speed=>2, :beta=>0.1, =>"a"}
# >> {:speed=>2, :beta=>0.1, =>"b"}
# >> {:speed=>3, :beta=>0.1, =>"a"}
# >> {:speed=>3, :beta=>0.1, =>"b"}
# >> {:speed=>1, :beta=>0.002, =>"a"}
# >> {:speed=>1, :beta=>0.002, =>"b"}
# >> {:speed=>2, :beta=>0.002, =>"a"}
# >> {:speed=>2, :beta=>0.002, =>"b"}
# >> {:speed=>3, :beta=>0.002, =>"a"}
# >> {:speed=>3, :beta=>0.002, =>"b"}
# >> {:speed=>1, :beta=>0.4, =>"a"}
# >> {:speed=>1, :beta=>0.4, =>"b"}
# >> {:speed=>2, :beta=>0.4, =>"a"}
# >> {:speed=>2, :beta=>0.4, =>"b"}
# >> {:speed=>3, :beta=>0.4, =>"a"}
# >> {:speed=>3, :beta=>0.4, =>"b"}

James Edward Gray II


 
Reply With Quote
 
Gary Boone
Guest
Posts: n/a
 
      12-08-2006

Cool. While I study the others, here's my solution. Looks like it's
similar to Greg's.

--Gary


# expand_hash_lists
#
# note that accumulating lists must be passed in as the first argument
#
def expand_hash_lists(a1, a2)
a1.inject([]){|acc,f| a2.inject(acc){|a,b| a << [b]+
(f.is_a?(Hash)?[f]:f)}}
end

if __FILE__ == $0

# tests of expand_hash_lists
ah=[{'a'=>1}, {'a'=>2}, {'a'=>3}]
bh=[{'b'=>21}, {'b'=>22}, {'b'=>23}]
ch=[{'c'=>31}, {'c'=>32}, {'c'=>33}]
dh=[{'d'=>41}, {'d'=>42}, {'d'=>43}]

abh = expand_hash_lists(ah, bh)
abch = expand_hash_lists(abh, ch)
p expand_hash_lists(abch, dh).inspect

# Additional tests
p "----"
p [[{'a'=>1}], [{'b'=>2}]].inject{|acc, vl| expand_hash_lists(acc,
vl)}.inspect
p "----"
p [[{'a'=>1}, {'a'=>3}], [{'b'=>2}]].inject{|acc, vl|
expand_hash_lists(acc, vl)}.inspect
p "----"
p [[{'a'=>1}, {'a'=>3}], [{'b'=>2}, {'b'=>4}]].inject{|acc, vl|
expand_hash_lists(acc, vl)}.inspect
end

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
without declare parameter [double square(parameter)] return 0 in main WanHongbin@gmail.com C Programming 5 10-01-2008 03:31 AM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57