Module: Equalshares::FixedBudget

Defined in:
lib/equalshares/fixed_budget.rb

Overview

The fixed-budget Method of Equal Shares loop.

This unifies the two near-identical JS implementations (equalSharesFixedBudgetFractions and equalSharesFixedBudgetFloats in js/methodOfEqualSharesWorker.js) into a single loop driven by the numeric type of the values passed in:

- fractions mode: `cost` values and `b_total` are Rational  -> exact arithmetic
- floats mode:    `cost` values and `b_total` are Float      -> IEEE-754 arithmetic

The arithmetic (order and types) is kept bit-identical to the JS tool; the class below only reorganises the control flow into evaluate/charge steps.

Defined Under Namespace

Classes: Loop, Report

Class Method Summary collapse

Class Method Details

.run(voter_ids, project_ids, cost_source, approvers, budget_source, params, report_details: false, progress: nil) ⇒ Object

Dispatch on params.accuracy, preparing cost/budget in the right numeric type. cost_source maps project_id => original cost string; budget_source is the budget string. This mirrors JS where costs come from parseFloat, then (in the fractions path) get wrapped by new Fraction(...).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/equalshares/fixed_budget.rb', line 22

def run(voter_ids, project_ids, cost_source, approvers, budget_source, params,
        report_details: false, progress: nil)
  case params.accuracy
  when "fractions"
    cost = cost_source.transform_values { |c| Election.rational_of(c) }
    b_total = Election.rational_of(budget_source)
  when "floats"
    cost = cost_source.transform_values { |c| Float(c) }
    b_total = Float(budget_source)
  else
    raise ComputeError, "Unknown accuracy parameter"
  end
  Loop.new(voter_ids, project_ids, cost, approvers, b_total, params,
           report_details: report_details, progress: progress).run
end