Class: PlanOut::WeightedChoice

Inherits:
OpRandom show all
Defined in:
lib/plan_out/op_random.rb

Constant Summary

Constants inherited from OpRandom

OpRandom::LONG_SCALE

Instance Attribute Summary

Attributes inherited from Operator

#args

Instance Method Summary collapse

Methods inherited from OpRandom

#get_hash, #get_uniform, #get_unit

Methods inherited from OpSimple

#execute

Methods inherited from Operator

#execute, #initialize

Constructor Details

This class inherits a constructor from PlanOut::Operator

Instance Method Details

#simple_executeObject



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