Class: PlanOut::WeightedChoice
- Defined in:
- lib/plan_out/op_random.rb
Constant Summary
Constants inherited from OpRandom
Instance Attribute Summary
Attributes inherited from Operator
Instance Method Summary collapse
Methods inherited from OpRandom
#get_hash, #get_uniform, #get_unit
Methods inherited from OpSimple
Methods inherited from Operator
Constructor Details
This class inherits a constructor from PlanOut::Operator
Instance Method Details
#simple_execute ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/plan_out/op_random.rb', line 54 def simple_execute choices = @parameters[:choices] weights = @parameters[:weights] return [] if choices.length() == 0 cum_weights = choices.zip(weights) cum_sum = 0.0 cum_weights.each do |choice, weight| cum_sum += weight cum_weights[choice] = cum_sum end stop_value = get_uniform(0.0, cum_sum) cum_weights.each do |choice, cum_weight| choice if stop_value <= cum_weight end end |