module Apportion::Algorithm::EqualProportions
Selects the next recipient
Constants
- BIG_FIXNUM
Public Instance Methods
next_recipient(weights, portions)
click to toggle source
Selects the next recipient by sorting the equal proportions rank-index of the recipients
see Balinski, M. and H. Young, The Quota Method of Apportionment, Amer. Math. Monthly 82 (1975) 701-730.
@param weights [Hash] relative integer proportions @param portions [Hash] @return [Symbol] recipient having the highest equal proportions rank-index @example
next_recipient({a: 41, b: 32, c: 27}, {a: 4, b: 3, c: 2}) # => :c
# File lib/apportion/algorithm/equal_proportions.rb, line 21 def next_recipient(weights, portions) weights.max_by { |k, v| recipient_rank(v, portions[k]) }[0] end
recipient_rank(weight, portion)
click to toggle source
# File lib/apportion/algorithm/equal_proportions.rb, line 25 def recipient_rank(weight, portion) return zero_rank(weight) if portion == 0 weight / Math.sqrt(portion * (portion + 1)).to_f end
zero_rank(weight)
click to toggle source
protect from division by zero
# File lib/apportion/algorithm/equal_proportions.rb, line 31 def zero_rank(weight) weight * BIG_FIXNUM end