function processBallotSet($filepath, $ballot_set_name, $pairs) { global $PRINT; $data = json_decode(file_get_contents($filepath), 1); foreach ($data as $ballot) { $pairs = runBallot($pairs, $ballot); } //printFormattedPairs($pairs); $cands = array_keys($pairs); asort($cands); $new_cands = array(); foreach ($cands as $cc) { $new_cands[$cc]['beat'] = array(); $new_cands[$cc]['lost_to'] = array(); $new_cands[$cc]['tied'] = array(); } foreach ($cands as $c1) { foreach ($cands as $c2) { if ($c1 != $c2) { if ($pairs[$c1][$c2] > $pairs[$c2][$c1]) { // print "$c1 beats $c2\n"; $new_cands[$c1]['beat'][] = $c2; $new_cands[$c2]['lost_to'][] = $c1; } if ($pairs[$c1][$c2] < $pairs[$c2][$c1]) { // print "$c2 beats $c1\n"; } if ($pairs[$c1][$c2] == $pairs[$c2][$c1]) { $new_cands[$c2]['tied'][] = $c1; } } } } $condorcets = array(); foreach ($new_cands as $ocand => $result) { foreach ($result as $stat => $subcands) { //print "$ocand $stat ".count($subcands)." candidates\n"; if ('lost_to' == $stat && 0 == count($subcands)) { print "{$ocand} is CONDORCET on ballot {$ballot_set_name}\n"; $condorcets[] = "{$ocand} is CONDORCET on ballot {$ballot_set_name}\n"; } } } }
function getResults($result, $filepath) { global $PRINT; $cands = getCandidates($filepath); $pairs = getAllPairs($cands); $data = json_decode(file_get_contents($filepath), 1); $year = $data['ELECTION']['id']; if ($PRINT) { print "\n\n ----- {$year} -----\n\n"; } foreach ($data['BALLOTS'] as $ballot) { $pairs = runBallot($cands, $pairs, $ballot); } $pairs = rerunForNeitherOnBallot($cands, $pairs, $data['BALLOTS']); //printFormattedPairs($pairs); $new_cands[$year] = array(); foreach ($cands as $cc) { $new_cands[$year][$cc]['beat'] = array(); $new_cands[$year][$cc]['lost_to'] = array(); $new_cands[$year][$cc]['tied'] = array(); } foreach ($cands as $c1) { foreach ($cands as $c2) { if ($c1 != $c2) { if ($pairs[$c1][$c2] > $pairs[$c2][$c1]) { // print "$c1 beats $c2\n"; $new_cands[$year][$c1]['beat'][] = $c2; $new_cands[$year][$c2]['lost_to'][] = $c1; } if ($pairs[$c1][$c2] < $pairs[$c2][$c1]) { // print "$c2 beats $c1\n"; } if ($pairs[$c1][$c2] == $pairs[$c2][$c1]) { $new_cands[$year][$c2]['tied'][] = $c1; } } } } //print "\n\n ----- $year Summary -----\n\n"; //printFormattedResult($new_cands); return $new_cands; }