Class: Equalshares::Rules::MethodOfEqualShares

Inherits:
Base
  • Object
show all
Defined in:
lib/equalshares/rules/method_of_equal_shares.rb

Overview

The Method of Equal Shares for approval ballots — the equalshares.net rule, including its completion methods (none/utilitarian/add1/...) and comparison step. Faithful port of equalShares() in js/methodOfEqualSharesWorker.js.

Cost and budget are treated as floats at this level (statistics, comparison, utilitarian/greedy committees, the everything-affordable check); only the inner fixed-budget MES loop switches to exact rationals for accuracy "fractions".

Constant Summary collapse

ADD1_COMPLETIONS =
%w[add1 add1e add1u add1eu].freeze
UTILITARIAN_COMPLETIONS =
%w[utilitarian add1u].freeze

Instance Method Summary collapse

Methods inherited from Base

call, #initialize

Constructor Details

This class inherits a constructor from Equalshares::Rules::Base

Instance Method Details

#callObject



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
58
# File 'lib/equalshares/rules/method_of_equal_shares.rb', line 16

def call
  start = now
  voter_ids = election.voter_ids
  project_ids = election.project_ids
  approvers = election.approvers

  # Strings for the MES loop (it wraps them per accuracy); floats for the
  # completion/comparison/statistics bookkeeping (as the JS does).
  cost_source = project_ids.to_h { |c| [c, instance.projects[c]["cost"]] }
  cost = election.float_costs
  b_float = election.float_budget

  mes = run_mes(cost_source, instance.budget)
  winners = mes.fetch(:winners)
  report = mes.fetch(:report)
  notes = {
    endowment: report[:endowment],
    money_behind_candidate: report[:money_behind_candidate],
    effective_vote_count: report[:effective_vote_count]
  }

  # utilitarian completion if needed
  if UTILITARIAN_COMPLETIONS.include?(params.completion)
    completion = Completion.utilitarian(voter_ids, project_ids, cost, approvers, b_float, winners)
    winners = completion.fetch(:winners)
    notes[:added_by_utilitarian_completion] = completion.fetch(:added)
  end

  # comparison step
  greedy = Completion.utilitarian(voter_ids, project_ids, cost, approvers, b_float, []).fetch(:winners)
  unless params.comparison == "none"
    cmp = Comparison.comparison_step(voter_ids, approvers, greedy, winners, params)
    unless cmp[:stick_to_mes]
      winners = greedy
      notes[:comparison] =
        "The committee chosen by the greedy algorithm is preferred by #{cmp[:prefers_greedy]} voters, " \
        "while the committee chosen by the method of equal shares is preferred by #{cmp[:prefers_mes]} voters."
    end
  end

  notes[:greedy_stats] = election.statistics(greedy)
  result(winners, start, notes)
end