checkIsFile($filepath); $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);
<?php foreach (file('new_condorcet_simulation_count') as $line) { $parts = explode(' ', $line); $winner = $parts[0]; $file = trim(array_pop($parts)); $approv = findApproval($file); print $file . " condorcet:" . $winner . " approval:" . join(',', $approv); if (in_array($winner, $approv)) { if (1 == count($approv)) { $flag = "EQUAL"; } else { $flag = "IN SET"; } } else { $flag = "NO"; } print " " . $flag . "\n"; } function findApproval($filename) { $seats = 12; $data = json_decode(file_get_contents($filename)); $cands = $data[0]; natsort($cands); $tallies = array(); foreach ($data as $ballot) { foreach (range(0, $seats - 1) as $num) { if (isset($ballot[$num])) { $cname = $ballot[$num]; if (!isset($tallies[$cname])) {
<?php $candidates = 30; $seats = 12; //foreach (new DirectoryIterator('elections') as $fileInfo) { // if($fileInfo->isDot()) { // continue; // } else { $fn = 'sim_ballots_uneven/000/ballot_0000000.json'; $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;
<?php $candidates = 30; $seats = 8; foreach (file('new_condorcet_simulation_count') as $line) { $parts = explode(' ', $line); $winner = $parts[0]; $file = trim(array_pop($parts)); //APPROVAL $approv = findApproval($file, $seats); //print_r($approv); exit; $committee = array(); foreach ($approv as $eid => $tally) { $committee[] = $eid . ":" . $tally; } 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();