| 1 | package Vote::Count::Start; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use 5.024; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 1 | ▶ | use feature qw/postderef signatures/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 1 | ▶ | no warnings qw/experimental/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use Path::Tiny 0.108; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | # use Data::Dumper; | ||||||||||||||||||
| 12 | # use Vote::Count::Method::CondorcetDropping; | ||||||||||||||||||
| 13 | 1 | ▶ | use Vote::Count; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 1 | ▶ | use Vote::Count::ReadBallots 'read_ballots'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 1 | our $VERSION='2.02'; | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | Vote::Count::Start | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | =head1 VERSION 2.02 | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | =cut | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | # ABSTRACT: Vote::Count Common Setup | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | use Vote::Count::Start; | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | my $Election = StartElection( | ||||||||||||||||||
| 33 | BallotFile => $filepath, | ||||||||||||||||||
| 34 | FloorRule => 'TopCount', | ||||||||||||||||||
| 35 | FloorValue => 2, | ||||||||||||||||||
| 36 | LogPath -> '/some/path', | ||||||||||||||||||
| 37 | ... | ||||||||||||||||||
| 38 | ); | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | $Election->WriteLog(); | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | =head1 Description | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | Does common startup steps useful accross methods. It includes a lot of the boiler plate for common usage. Use for resolving elections where the rules don't require customization, or as an example for writing more customized methods. | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | =over | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | * Reads Ballots from a file/path | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | * Calculates and logs Top Count | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | * Calculates and logs Approval | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | * Applies a Floor Rule | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | * Calculatures and logs a Borda Count | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | * Generates a Condorcet Matrix and logs the Win/Loss Summary and the Scores | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | * Conducts IRV (default options) and logs the result | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | * Returns a Vote::Count Object | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =back | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | =head1 Method StartElection | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | Returns a Vote::Count object performing the above operations. | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | =head2 Parameter BallotSet or BallotFile | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | It is mandatory to provide either a reference to a BallotSet or to provide a BallotFile for ReadBallots to create a BallotSet. | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | =head2 Paramater FloorRule, FloorValue (optional) | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | A FloorRule and optional value (see Vote::Count::Floor). If no FloorRule is provide none will be used. | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | =head2 Other Options | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | Any other option to Vote::Count can just be passed in the arguments list | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | =cut | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 1 | ▶ | use Exporter::Easy ( EXPORT => ['StartElection'] ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | # checks for ballotfile and updates the ballotset in | ||||||||||||||||||
| 87 | # args. no return value because %ARGS is passed by reference | ||||||||||||||||||
| 88 | # and updated directly if needed. | ||||||||||||||||||
| 89 | ▶ | sub _ballotset( $ARGS ) { | |||||||||||||||||
|
Subroutine
_ballotset: called CC=3 SCAR=11.4
| |||||||||||||||||||
| 90 | 2 | ▶ | if ( $ARGS->{'BallotFile'} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 91 | 1 | $ARGS->{'BallotSet'} = read_ballots $ARGS->{'BallotFile'}; | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | # If | ||||||||||||||||||
| 94 | 2 | ▶ | unless ( defined( $ARGS->{'BallotSet'}{'choices'} ) ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 95 | croak "A Valid BallotSet or BallotFile was not provided " | ||||||||||||||||||
| 96 | 0 | . $ARGS->{'BallotFile'} . "\n"; | |||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | ▶ | sub _dofloor ( $self, %ARGS ) { | |||||||||||||||||
|
Subroutine
_dofloor: called CC=5 SCAR=17.4
| |||||||||||||||||||
| 101 | 2 | ▶ | unless ( defined $ARGS{'FloorRule'} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | 1 | return $self->Active(); | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | 1 | $self->logv(''); # log a blank line. | |||||||||||||||||
| 105 | 1 | my $flr = $ARGS{'FloorRule'}; | |||||||||||||||||
| 106 | 1 | my $floorset = {}; | |||||||||||||||||
| 107 | 1 | ▶ | if ( $flr eq 'TopCount' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 108 | 0 | $floorset = $self->TopCountFloor( $ARGS{'FloorValue'} ); | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | elsif ( $flr eq 'TCA' ) { | ||||||||||||||||||
| 111 | 0 | $floorset = $self->TCA(); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | elsif ( $flr eq 'Approval' ) { | ||||||||||||||||||
| 114 | 1 | $floorset = $self->ApprovalFloor( $ARGS{'FloorValue'} ); | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | else { | ||||||||||||||||||
| 117 | 0 | croak "Undefined Floor rule $flr.\n"; | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | 1 | $self->logv(''); # add blank line to output | |||||||||||||||||
| 120 | 1 | return $floorset; | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | ▶ | sub _do_plurality ( $Election ) { | |||||||||||||||||
|
Subroutine
_do_plurality: called CC=2 SCAR=7.5
| |||||||||||||||||||
| 124 | 2 | my $Plurality = $Election->TopCount(); | |||||||||||||||||
| 125 | 2 | $Election->logv( | |||||||||||||||||
| 126 | ' ', | ||||||||||||||||||
| 127 | 'Initial Top Count (Plurality)', | ||||||||||||||||||
| 128 | $Plurality->RankTable() | ||||||||||||||||||
| 129 | ); | ||||||||||||||||||
| 130 | 2 | my $PluralityTop = $Plurality->Leader(); | |||||||||||||||||
| 131 | 2 | ▶ | if ( $PluralityTop->{'winner'} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 132 | 2 | $Election->logt( "Plurality Winner: " . $PluralityTop->{'winner'} ); | |||||||||||||||||
| 133 | 2 | return $PluralityTop->{'winner'}; | |||||||||||||||||
| 134 | } | ||||||||||||||||||
| 135 | else { | ||||||||||||||||||
| 136 | $Election->logt( | ||||||||||||||||||
| 137 | 0 | "Plurality Tie: " . join( ', ', $PluralityTop->{'tied'}->@* ) ); | |||||||||||||||||
| 138 | 0 | return ''; | |||||||||||||||||
| 139 | } | ||||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | ▶ | sub _do_approval ( $Election ) { | |||||||||||||||||
|
Subroutine
_do_approval: called CC=2 SCAR=7.5
| |||||||||||||||||||
| 143 | 2 | my $Approval = $Election->Approval(); | |||||||||||||||||
| 144 | 2 | $Election->logv( "\nApproval", $Approval->RankTable() ); | |||||||||||||||||
| 145 | 2 | my $AWinner = $Approval->Leader(); | |||||||||||||||||
| 146 | 2 | ▶ | if ( $AWinner->{'winner'} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 147 | 0 | $Election->logt( "Approval Winner: " . $AWinner->{'winner'} ); | |||||||||||||||||
| 148 | 0 | return $AWinner->{'winner'}; | |||||||||||||||||
| 149 | } | ||||||||||||||||||
| 150 | else { | ||||||||||||||||||
| 151 | $Election->logt( | ||||||||||||||||||
| 152 | 2 | "Approval Tie: " . join( ', ', $AWinner->{'tied'}->@* ) ); | |||||||||||||||||
| 153 | 2 | return ''; | |||||||||||||||||
| 154 | } | ||||||||||||||||||
| 155 | } | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | ▶ | sub _do_borda ( $Election ) { | |||||||||||||||||
|
Subroutine
_do_borda: called CC=2 SCAR=7.5
| |||||||||||||||||||
| 158 | 2 | my $Borda = $Election->Approval(); | |||||||||||||||||
| 159 | 2 | $Election->logv( "Borda Count", $Borda->RankTable(), ); | |||||||||||||||||
| 160 | 2 | my $AWinner = $Borda->Leader(); | |||||||||||||||||
| 161 | 2 | ▶ | if ( $AWinner->{'winner'} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 162 | 0 | $Election->logt( "Borda Winner: " . $AWinner->{'winner'}, '' ); | |||||||||||||||||
| 163 | 0 | return $AWinner->{'winner'}; | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | else { | ||||||||||||||||||
| 166 | 2 | $Election->logt( "Borda Tie: " . join( ', ', $AWinner->{'tied'}->@* ), | |||||||||||||||||
| 167 | '' ); | ||||||||||||||||||
| 168 | 2 | return ''; | |||||||||||||||||
| 169 | } | ||||||||||||||||||
| 170 | } | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | ▶ | sub _do_majority( $Election) { | |||||||||||||||||
|
Subroutine
_do_majority: called CC=2 SCAR=8.4
| |||||||||||||||||||
| 173 | 2 | my $majority = $Election->TopCountMajority(); | |||||||||||||||||
| 174 | 2 | ▶ | if ( $majority->{'winner'} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 175 | 0 | $Election->logv( "Majority Winner: " . $majority->{'winner'} ); | |||||||||||||||||
| 176 | 0 | return $majority->{'winner'}; | |||||||||||||||||
| 177 | } | ||||||||||||||||||
| 178 | 2 | else { return ''; } | |||||||||||||||||
| 179 | } | ||||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | ▶ | sub _do_matrix( $Election) { | |||||||||||||||||
|
Subroutine
_do_matrix: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 182 | 2 | my $matrix = $Election->PairMatrix(); | |||||||||||||||||
| 183 | 2 | $Election->logv( | |||||||||||||||||
| 184 | "Pairing Results:", | ||||||||||||||||||
| 185 | $matrix->MatrixTable(), | ||||||||||||||||||
| 186 | "\nSmith Set: " . join( ', ', sort( keys $matrix->SmithSet()->%* ) ) | ||||||||||||||||||
| 187 | ); | ||||||||||||||||||
| 188 | 2 | ▶ | if ( $matrix->CondorcetWinner() ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 189 | 1 | $Election->logv( "Condoret Winner: " . $matrix->CondorcetWinner() ); | |||||||||||||||||
| 190 | 1 | return $matrix->CondorcetWinner(); | |||||||||||||||||
| 191 | } | ||||||||||||||||||
| 192 | 1 | else { return '' } | |||||||||||||||||
| 193 | } | ||||||||||||||||||
| 194 | |||||||||||||||||||
| 195 | ▶ | sub _do_irv ( $Election, $floorset ) { | |||||||||||||||||
|
Subroutine
_do_irv: called CC=1 SCAR=0.4
| |||||||||||||||||||
| 196 | 2 | ▶ | my $IRVResult = try { $Election->RunIRV() } | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 197 | 2 | ▶ | catch { croak "RunIRV exploded" }; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||
| 198 | } | ||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | ▶ | sub StartElection ( %ARGS ) { | |||||||||||||||||
|
Subroutine
StartElection: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 201 | 2 | my $winners = {}; | |||||||||||||||||
| 202 | 2 | _ballotset( \%ARGS ); | |||||||||||||||||
| 203 | 2 | delete $ARGS{'BallotFile'}; | |||||||||||||||||
| 204 | 2 | my $FloorValue = delete $ARGS{'FloorValue'}; | |||||||||||||||||
| 205 | 2 | my $FloorRule = delete $ARGS{'FloorRule'}; | |||||||||||||||||
| 206 | 2 | my $Election = Vote::Count->new(%ARGS); | |||||||||||||||||
| 207 | 2 | $winners->{'plurality'} = _do_plurality($Election); | |||||||||||||||||
| 208 | 2 | $winners->{'approval'} = _do_approval($Election); | |||||||||||||||||
| 209 | 2 | my $floorset = _dofloor( $Election, 'FloorRule' => $FloorRule, 'FloorValue' => $FloorValue ); | |||||||||||||||||
| 210 | 2 | $Election->SetActive($floorset); | |||||||||||||||||
| 211 | 2 | $winners->{'majority'} = _do_majority($Election); | |||||||||||||||||
| 212 | 2 | $winners->{'borda'} = _do_borda($Election); | |||||||||||||||||
| 213 | 2 | $winners->{'condorcet'} = _do_matrix($Election); | |||||||||||||||||
| 214 | 2 | $winners->{'irv'} = _do_irv( $Election, $floorset ); | |||||||||||||||||
| 215 | # todo generate a summary from the winners hash. | ||||||||||||||||||
| 216 | 2 | $Election->{'startdata'} = $winners; | |||||||||||||||||
| 217 | # Active gets modified from default, so reset to floorset | ||||||||||||||||||
| 218 | 2 | $Election->SetActive($floorset); | |||||||||||||||||
| 219 | 2 | return ($Election); | |||||||||||||||||
| 220 | } | ||||||||||||||||||
| 221 | |||||||||||||||||||
| 222 | #FOOTER | ||||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | =pod | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | BUG TRACKER | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | L<https://github.com/brainbuz/Vote-Count/issues> | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | AUTHOR | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||||||||||||||||||
| 233 | |||||||||||||||||||
| 234 | CONTRIBUTORS | ||||||||||||||||||
| 235 | |||||||||||||||||||
| 236 | Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||||||||||||||||||
| 237 | |||||||||||||||||||
| 238 | LICENSE | ||||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>. | ||||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | SUPPORT | ||||||||||||||||||
| 243 | |||||||||||||||||||
| 244 | This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author. | ||||||||||||||||||
| 245 | |||||||||||||||||||
| 246 | =cut | ||||||||||||||||||
| 247 | |||||||||||||||||||