$ballots = json_decode(file_get_contents($filepath), 1); $ballots = evenBallotLength($ballots); $dir_path = '8seats_uneven/' . $dir; if (!is_dir($dir_path)) { mkdir($dir_path); } $result_path = $dir_path . '/' . $ballot_set_name . '.json'; if (is_file($result_path)) { continue; } //check out file_put_contents($result_path, '-'); $result = array(); $result['condorcet'] = findCondorcet($filepath); $result['approv'] = findApproval($ballots, $seats); $result['borda'] = findBorda($ballots, $seats); $result['stv'] = findStv($ballots, $seats); $result_json = json_encode($result); file_put_contents($result_path, $result_json); $now = microtime(1); $num_done = $j + 1; $per_iter = ($now - $start) / $num_done; $left_to_do = $iters - $num_done; $time_remaining = round($per_iter * $left_to_do / 60); print $j . " iterations completed "; print $time_remaining . " minutes to go\n"; } function findCondorcet($filepath) { $ballots = json_decode(file_get_contents($filepath), 1); $cands = getCandidates($ballots);
$seats = $elec_data['ELECTION']['seats']; $year = $elec_data['ELECTION']['id']; $ballots = file2data($fn); //$candidates = count($elec_data['CANDIDATES']); //APPROVAL /* $approv = findApproval($ballots,$seats); $out = ''; foreach ($approv as $eid => $tally) { $out .= $eid.",".$tally."\n"; } $outfile = 'historical_approval/approval_'.$year.'.csv'; file_put_contents($outfile,$out); */ //BORDA $borda = findBorda($ballots, $seats); $out = ''; foreach ($borda as $eid => $tally) { $out .= $eid . "," . $tally . "\n"; } $outfile = 'new_historical_borda/borda_' . $year . '.csv'; file_put_contents($outfile, $out); } } function findBorda($ballots, $seats) { $cand_list = getCandidates($ballots); $cand_num = count($cand_list); $tallies = array(); foreach ($ballots as $ballot) { foreach (range(0, $cand_num - 1) as $num) {
$ballots = json_decode(file_get_contents($fn), 1); $seats = 12; $ballots = evenBallotLength($ballots); $candidates = count(getCandidates($ballots)); $num_ballots = count($ballots); //APPROVAL $approv = findApproval($ballots, $seats); $out = ''; foreach ($approv as $eid => $tally) { $out .= $eid . "," . $tally . "\n"; } //print json_encode($approv); exit; //$outfile = 'historical_approval/approval_'.$year.'.csv'; //file_put_contents($outfile,$out); //BORDA $borda = findBorda($ballots, $seats, $candidates); $out = ''; foreach ($borda as $eid => $tally) { $out .= $eid . "," . $tally . "\n"; } print json_encode($borda); exit; //$outfile = 'historical_borda/borda_'.$year.'.csv'; //file_put_contents($outfile,$out); //} //} function getCandidates($ballots) { $cands = array(); foreach ($ballots as $ballot) { foreach ($ballot as $cand) {
} print $file . " condorcet:" . $winner . " approval:" . join(',', $committee); if (in_array($winner, array_keys($approv))) { $place = array_search($winner, array_keys($approv)) + 1; $perc = $approv[$winner]; if (1 == count($approv)) { $flag = "EQUAL"; } else { $flag = "YES IN APPROVAL SET - place: {$perc}"; } } else { $flag = "NO IN APPROVAL SET - place: 0"; } print " " . $flag . "\n"; //BORDA $borda = findBorda($file, $seats, $candidates); $committee = array(); foreach ($borda as $eid => $tally) { $committee[] = $eid . ":" . $tally; } print $file . " condorcet:" . $winner . " borda:" . join(',', $borda); if (in_array($winner, array_keys($borda))) { $place = array_search($winner, array_keys($borda)) + 1; $perc = $borda[$winner]; if (1 == count($borda)) { $flag = "EQUAL"; } else { $flag = "YES IN BORDA SET - place: {$perc}"; } } else { $flag = "NO IN BORDA SET - place: 0";