Exemplo n.º 1
0
function runElections()
{
    $num_cands = 30;
    $cands = array();
    foreach (range(1, $num_cands) as $num) {
        $cands[] = 'cand' . $num;
    }
    $pairs = getAllPairs($cands);
    $result = array();
    $condorcets = array();
    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';
        processBallotSet($filepath, $ballot_set_name, $pairs);
    }
}
function runBallots($cands, $ballots)
{
    $cprobs = array();
    $final_probs = array();
    $pairs = getAllPairs($cands);
    foreach ($ballots as $b) {
        $pairs = getBallotPairs($cands, $b, $pairs);
    }
    $ballots_with_neither = array();
    foreach ($ballots as $ballot) {
        foreach ($cands as $I) {
            foreach ($cands as $J) {
                if ($I != $J) {
                    if (!isset($ballots_with_neither[$I])) {
                        $ballots_with_neither[$I] = array();
                    }
                    if (!isset($ballots_with_neither[$I][$J])) {
                        $ballots_with_neither[$I][$J] = 0;
                    }
                    if (!in_array($I, $ballot) and !in_array($J, $ballot)) {
                        $ballots_with_neither[$I][$J] += 1;
                    }
                }
            }
        }
    }
    //print_r($ballots_with_neither);exit;
    foreach ($ballots as $ballot) {
        foreach ($cands as $I) {
            $cprobs[$I] = array();
            foreach ($cands as $J) {
                if ($I != $J) {
                    $cprobs[$I][$J] = '';
                    //per Tse-min algorithm
                    $V = count($ballots);
                    $M = floor($V / 2) + 1;
                    $C = count($cands);
                    $R = count($ballots) - $ballots_with_neither[$I][$J];
                    $S = $ballots_with_neither[$I][$J];
                    $T = $pairs[$I][$J];
                    $IbeatJ = $pairs[$I][$J];
                    $JbeatI = $pairs[$J][$I];
                    $prob = $IbeatJ / ($IbeatJ + $JbeatI);
                    $Q = $prob;
                    $k_range = $M - $T - 1;
                    if ($M - $S <= $T && $T < $M) {
                        $sum = 0;
                        foreach (range(0, $k_range) as $k) {
                            $sum += binomial($S, $Q, $k);
                        }
                        $new_prob = 1 - $sum;
                    }
                    if ($T + $S < $M) {
                        $new_prob = 0;
                    }
                    if ($T >= $M) {
                        $new_prob = 1;
                    }
                    $cprobs[$I][$J] = $new_prob;
                    global $PRINT;
                    if ($PRINT) {
                        print "\n----------------------\n";
                        print "I is {$I}\n";
                        print "J is {$J}\n";
                        print "V is {$V}\n";
                        print "M is {$M}\n";
                        print "C is {$C}\n";
                        print "R is {$R}\n";
                        print "S is {$S}\n";
                        print "T is {$T}\n";
                        print "Q is {$Q}\n";
                        print "k range is {$k_range}\n";
                        print "calculated probability is {$new_prob}\n";
                    }
                }
                // I != J
            }
            // foreach J
        }
        //foreach I
        foreach ($cprobs as $I => $J_array) {
            $final_probs[$I] = round(array_product($J_array), 4);
        }
    }
    return $final_probs;
}
Exemplo n.º 3
0
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;
}