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.º 2
0
function rerunForNeitherOnBallot($cands, $pairs, $ballots)
{
    $old_pairs = $pairs;
    $new_pairs = $pairs;
    foreach ($ballots as $ballot) {
        $cands_missing_on_this_ballot = array();
        foreach ($cands as $miss) {
            if (!in_array($miss, $ballot)) {
                $cands_missing_on_this_ballot[] = $miss;
            }
        }
        foreach ($cands_missing_on_this_ballot as $I) {
            foreach ($cands_missing_on_this_ballot as $J) {
                if ($I != $J) {
                    $ballots_with_neither = array();
                    foreach ($ballots as $bal) {
                        if (!in_array($I, $bal) and !in_array($J, $bal)) {
                            $ballots_with_neither[] = $bal;
                        }
                    }
                    //per Tse-min algorithm
                    $V = count($ballots);
                    $M = floor($V / 2) + 1;
                    $C = count($cands);
                    $R = count($ballots) - count($ballots_with_neither);
                    $S = count($ballots_with_neither);
                    $T = $old_pairs[$I][$J];
                    $IbeatJ = $old_pairs[$I][$J];
                    $JbeatI = $old_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;
                    }
                    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";
                    }
                    if (flip($new_prob)) {
                        $new_pairs[$I][$J] += 1;
                    } else {
                        //		$new_pairs[$J][$I] += 1;
                    }
                }
            }
        }
    }
    return $new_pairs;
}