print "working on {$filepath}\n"; 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) {
<?php foreach (range(0, 100000) as $j) { $ballot_set_name = 'ballot_' . sprintf("%07d", $j); $dir = substr($ballot_set_name, -3); $filepath = 'sim_ballots/' . $dir . '/' . $ballot_set_name . '.json'; findCondorcet($filepath); } //$filepath = 'sim_ballots/484/ballot_0000484.json'; //findCondorcet($filepath); function findCondorcet($filename) { $data = json_decode(file_get_contents($filename)); $cands = $data[0]; natsort($cands); $pairs = array(); foreach ($cands as $c) { $new_cands = $cands; unset($new_cands[array_search($c, $new_cands)]); foreach ($new_cands as $nc) { $pairs[$c][$nc] = 0; } } foreach ($data as $ballot) { foreach ($cands as $cand1) { foreach ($cands as $cand2) { if ($cand1 != $cand2) { $cand1rank = array_search($cand1, $ballot); $cand2rank = array_search($cand2, $ballot); if ($cand1rank < $cand2rank) { $pairs[$cand1][$cand2] += 1;