Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/pbvoting/pabutools
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-utke committed Sep 22, 2023
2 parents eee4576 + 6b9636a commit 1db584b
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 150 deletions.
4 changes: 3 additions & 1 deletion docs/_modules/pabutools/analysis/category.html
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ <h1>Source code for pabutools.analysis.category</h1><div class="highlight"><pre>
<span class="n">app_cost_per_category</span><span class="p">[</span><span class="n">category</span><span class="p">]</span> <span class="o">+=</span> <span class="n">project</span><span class="o">.</span><span class="n">cost</span>
<span class="k">for</span> <span class="n">category</span> <span class="ow">in</span> <span class="n">categories</span><span class="p">:</span>
<span class="n">proportional_app_cost_per_category</span><span class="p">[</span><span class="n">category</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span>
<span class="n">app_cost_per_category</span><span class="p">[</span><span class="n">category</span><span class="p">]</span> <span class="o">/</span> <span class="n">app_total_cost</span> <span class="o">*</span> <span class="n">profile</span><span class="o">.</span><span class="n">multiplicity</span><span class="p">(</span><span class="n">ballot</span><span class="p">)</span>
<span class="n">app_cost_per_category</span><span class="p">[</span><span class="n">category</span><span class="p">]</span>
<span class="o">/</span> <span class="n">app_total_cost</span>
<span class="o">*</span> <span class="n">profile</span><span class="o">.</span><span class="n">multiplicity</span><span class="p">(</span><span class="n">ballot</span><span class="p">)</span>
<span class="p">)</span>
<span class="k">for</span> <span class="n">category</span> <span class="ow">in</span> <span class="n">categories</span><span class="p">:</span>
<span class="n">proportional_app_cost_per_category</span><span class="p">[</span><span class="n">category</span><span class="p">]</span> <span class="o">/=</span> <span class="n">profile</span><span class="o">.</span><span class="n">num_ballots</span><span class="p">()</span>
Expand Down
2 changes: 1 addition & 1 deletion docs/_modules/pabutools/analysis/votersatisfaction.html
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ <h1>Source code for pabutools.analysis.votersatisfaction</h1><div class="highlig
<span class="n">hist_data</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sat_profile</span><span class="o">.</span><span class="n">multiplicity</span><span class="p">(</span><span class="n">ballot</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">hist_data</span><span class="p">[</span>
<span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">satisfaction</span><span class="o">*</span><span class="p">(</span><span class="n">num_bins</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">max_satisfaction</span><span class="p">)</span>
<span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">satisfaction</span> <span class="o">*</span> <span class="p">(</span><span class="n">num_bins</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">max_satisfaction</span><span class="p">)</span>
<span class="p">]</span> <span class="o">+=</span> <span class="n">sat_profile</span><span class="o">.</span><span class="n">multiplicity</span><span class="p">(</span><span class="n">ballot</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">hist_data</span><span class="p">)):</span>
<span class="n">hist_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">hist_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="n">profile</span><span class="o">.</span><span class="n">num_ballots</span><span class="p">()</span>
Expand Down
12 changes: 7 additions & 5 deletions docs/_modules/pabutools/election/instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ <h1>Source code for pabutools.election.instance</h1><div class="highlight"><pre>
<span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">cost</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">projects</span><span class="p">)</span></div>


<div class="viewcode-block" id="max_budget_allocation_cardinality"><a class="viewcode-back" href="../../../reference/election/instance.html#pabutools.election.instance.max_budget_allocation_cardinality">[docs]</a><span class="k">def</span> <span class="nf">max_budget_allocation_cardinality</span><span class="p">(</span><span class="n">projects</span><span class="p">:</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">Project</span><span class="p">],</span> <span class="n">budget_limit</span><span class="p">:</span> <span class="n">Number</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<div class="viewcode-block" id="max_budget_allocation_cardinality"><a class="viewcode-back" href="../../../reference/election/instance.html#pabutools.election.instance.max_budget_allocation_cardinality">[docs]</a><span class="k">def</span> <span class="nf">max_budget_allocation_cardinality</span><span class="p">(</span>
<span class="n">projects</span><span class="p">:</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">Project</span><span class="p">],</span> <span class="n">budget_limit</span><span class="p">:</span> <span class="n">Number</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the maximum number of projects that can be chosen with respect to the budget limit.</span>

Expand Down Expand Up @@ -396,7 +398,9 @@ <h1>Source code for pabutools.election.instance</h1><div class="highlight"><pre>
<span class="k">return</span> <span class="n">selected</span></div>


<div class="viewcode-block" id="max_budget_allocation_cost"><a class="viewcode-back" href="../../../reference/election/instance.html#pabutools.election.instance.max_budget_allocation_cost">[docs]</a><span class="k">def</span> <span class="nf">max_budget_allocation_cost</span><span class="p">(</span><span class="n">projects</span><span class="p">:</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">Project</span><span class="p">],</span> <span class="n">budget_limit</span><span class="p">:</span> <span class="n">Number</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Number</span><span class="p">:</span>
<div class="viewcode-block" id="max_budget_allocation_cost"><a class="viewcode-back" href="../../../reference/election/instance.html#pabutools.election.instance.max_budget_allocation_cost">[docs]</a><span class="k">def</span> <span class="nf">max_budget_allocation_cost</span><span class="p">(</span>
<span class="n">projects</span><span class="p">:</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">Project</span><span class="p">],</span> <span class="n">budget_limit</span><span class="p">:</span> <span class="n">Number</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Number</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the maximum total cost over all subsets of projects with respect to the budget limit.</span>

Expand All @@ -420,9 +424,7 @@ <h1>Source code for pabutools.election.instance</h1><div class="highlight"><pre>
<span class="p">}</span>
<span class="k">if</span> <span class="n">p_vars</span><span class="p">:</span>
<span class="n">mip_model</span><span class="o">.</span><span class="n">objective</span> <span class="o">=</span> <span class="n">maximize</span><span class="p">(</span><span class="n">xsum</span><span class="p">(</span><span class="n">p_vars</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">*</span> <span class="n">p</span><span class="o">.</span><span class="n">cost</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">projects</span><span class="p">))</span>
<span class="n">mip_model</span> <span class="o">+=</span> <span class="p">(</span>
<span class="n">xsum</span><span class="p">(</span><span class="n">p_vars</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">*</span> <span class="n">p</span><span class="o">.</span><span class="n">cost</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">projects</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">budget_limit</span>
<span class="p">)</span>
<span class="n">mip_model</span> <span class="o">+=</span> <span class="n">xsum</span><span class="p">(</span><span class="n">p_vars</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">*</span> <span class="n">p</span><span class="o">.</span><span class="n">cost</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">projects</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">budget_limit</span>
<span class="n">mip_model</span><span class="o">.</span><span class="n">optimize</span><span class="p">()</span>
<span class="n">max_cost</span> <span class="o">=</span> <span class="n">mip_model</span><span class="o">.</span><span class="n">objective</span><span class="o">.</span><span class="n">x</span>
<span class="k">return</span> <span class="n">frac</span><span class="p">(</span><span class="n">max_cost</span><span class="p">)</span>
Expand Down
12 changes: 9 additions & 3 deletions docs/_modules/pabutools/election/pabulib.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ <h1>Source code for pabutools.election.pabulib</h1><div class="highlight"><pre>
<span class="n">CumulativeProfile</span><span class="p">,</span>
<span class="n">AbstractCumulativeProfile</span><span class="p">,</span>
<span class="n">OrdinalProfile</span><span class="p">,</span>
<span class="n">AbstractOrdinalProfile</span>
<span class="n">AbstractOrdinalProfile</span><span class="p">,</span>
<span class="p">)</span>

<span class="kn">import</span> <span class="nn">urllib.request</span>
Expand Down Expand Up @@ -531,6 +531,7 @@ <h1>Source code for pabutools.election.pabulib</h1><div class="highlight"><pre>
<span class="sd"> file_path: str</span>
<span class="sd"> The path to the output file.</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="k">def</span> <span class="nf">update_meta_value</span><span class="p">(</span><span class="n">meta_dict</span><span class="p">,</span> <span class="n">inst_meta</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">mandatory</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">inst_meta</span><span class="p">:</span>
<span class="n">meta_dict</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="n">inst_meta</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
Expand Down Expand Up @@ -664,10 +665,15 @@ <h1>Source code for pabutools.election.pabulib</h1><div class="highlight"><pre>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s2">;</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;PROJECTS</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="s2">&quot;;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">project_keys</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">project_dict</span> <span class="ow">in</span> <span class="n">project_dicts</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">project_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s2">&quot;None&quot;</span><span class="p">))</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">project_keys</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
<span class="s2">&quot;;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">project_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s2">&quot;None&quot;</span><span class="p">))</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">project_keys</span><span class="p">])</span>
<span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
<span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;VOTES</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="s2">&quot;;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">vote_keys</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">vote_dict</span> <span class="ow">in</span> <span class="n">vote_dicts</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">vote_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s2">&quot;None&quot;</span><span class="p">))</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">vote_keys</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
<span class="s2">&quot;;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">vote_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s2">&quot;None&quot;</span><span class="p">))</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">vote_keys</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
<span class="p">)</span>
</pre></div>

</article>
Expand Down
Loading

0 comments on commit 1db584b

Please sign in to comment.