Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // download php on : https://www.php.net/downloads.php
- // each vote will be stocked within a single .csv file.
- // for example, if you have a proposition "prop1", and vote :
- // 24% on fuck no
- // 30% on meh
- // 47% on okay
- // 50% on good
- // 68% on pretty good
- // 98% on hell yeah
- // then you'll have a line in "prop1.csv" where it's written :
- // 24,30,47,50,68,98
- // each vote will be stocked in arrays, which look like :
- /* [24,30,47,50,68,98] */
- // with each being percentages for, from left-to-right :
- // fuck no
- // meh
- // okay
- // good
- // pretty good
- // hell yeah
- function csv2arr ($path) {
- /* description */
- // converts a csv file's content in a 2d array (table),
- // csv files being big tables of data separated by commas.
- /* use */
- // we'll stock votes from prop1.csv to propn.csv for n=2, or 3, ...
- // we'll have to convert them into 2d arrays (tables) to use them.
- /* parameter(s) */
- // $path
- // path of the csv file.
- // here it's just "1.csv", "2.csv", etc.
- // that's bcs they're all in the same folder as this program.
- $file = file($path);
- // file($path) creates an array with each line,
- // and $file = file($path); assigns the array of
- // file($path) to $file.
- // however, right now it's still not very usable,
- // because it thinks these are just big strings
- // of characters with lots of commas in there.
- // we therefore need to convert those strings
- // into arrays, which is what we'll do here :
- foreach ($file as $index => $eval) {
- // foreach ($path as $index => $eval) runs through the $file array,
- // and at each step it assigns the index to $index and the string
- // content to $eval (which i called like that bcs they'll have to
- // be evaluated).
- eval("\$ret[$index] = [$eval];");
- // in those csv files, there are just numbers.
- // in php, we can define an array $array of numbers as such :
- /* $array = [24,30,47,50,68,98]; */
- // eval() evaluates the expression within the input, and if,
- // for example, $eval = "24,30,47,50,68,98", then "[$eval]"
- // which will be evaluated as :
- /* [24,30,47,50,68,98] */
- // hence, "\$ret[$index] = [$eval];" will assign the evaluated
- // array eval("[$eval]") to $ret[$index]. in this example,
- // it will assign [24,30,47,50,68,98] to $ret[$index].
- }
- return $ret;
- // the resulting array of arrays / 2d array / table will be returned
- // by the function. so for example, if we have :
- /* $ret[0] = [24,30,47,50,68,98] */
- // we can then reach the first value, 24, doing $ret[0][0], the
- // second with $ret[0][1], the third with $ret[0][2], etc.
- }
- $dir = scandir(".");
- // takes all the files in the folder and puts their name and extension in
- // an array. if you put the csv files in a specific subfolder, replace "."
- // by the name of said folder in quotation marks.
- foreach ($dir as $fileNameExt) {
- // runs through each file, and assigns to $file the name and extension of
- // each file in the folder at each step.
- if (strpos($fileNameExt,".csv") !== false) {
- // if the file is a .csv file...
- $propositions[str_replace(".csv","",$fileNameExt)] = csv2arr($fileNameExt);
- // each proposition voting tables/.csv files will be assigned their
- // .csv table converted into arrays, so we can actually use them.
- // the str_replace(".csv","",$fileNameExt) removes the extension to
- // $fileNameExt, which is basically just the file name then lol.
- }
- }
- function linear_interpolation ($array) {
- /* description */
- // maps each percentage $array[0..5] to $ret[0], $ret[20], $ret[40],
- // $ret[60], $ret[80], $ret[100], and use linear interpolation to fill
- // in the gaps, and rounding all the values to the nearest integer.
- // basically the v function which was presented in the video.
- /* use */
- // we will use it along with condorcet's method to determine the condorcet
- // winning percentage which will be assigned to the proposition.
- /* parameter(s) */
- // $array
- // one of the arrays from $propositions[..] tables.
- foreach ($array as $score => $percentage) {
- // runs through all the percentages and scores given.
- $ret[$score*20] = round($percentage);
- // maps each percentage $array[0..5] to $ret[0], $ret[20], $ret[40],
- // $ret[60], $ret[80], $ret[100].
- if ($score < 5)
- for ($i = $score*20+1; $i < ($score+1)*20; $i++)
- $ret[$i] = round(($array[$score+1]-$percentage)
- * ($i-$score*20)/20
- + $percentage)+rand(-50,49)/100;
- // that's the linear interpolation part, we fill the gaps for indices
- // other than 0, 20, 40, 60, 80 or 100, and rounds the values. the
- // rand(-50,49)/100 value added gives a random value betwen -0.5 and
- // +0.49 in order to randomly sort tying rounded values in (*).
- }
- return $ret;
- }
- // <minor function>
- function array_of_indices (&$array) {
- $ret = [];
- foreach ($array as $index => $content)
- $ret[count($ret)] = $index;
- $array = $ret;
- // pretty self-explainatory
- }
- // </minor function>
- foreach ($propositions as $fileName => $votes) {
- $preferences[$fileName] = [];
- foreach ($votes as $index => $vote) {
- for ($i = 0; $i < max($vote); $i++) {
- // we will count each vote as if it were as many votes as the maximum
- // percentage of all the ones given in the vote.
- $tmp = linear_interpolation($vote);
- // we stock the array of this linearly interpolated vote temporarily.
- arsort($tmp); // (*)
- array_of_indices($tmp);
- $preferences[$fileName][count($preferences[$fileName])] = $tmp;
- }
- }
- }
- function maximal_lotteries ($votes) {
- /* description */
- // we use condorcet's method first, aka we take each percentages on 1-to-1
- // contests with each other and make an array of all the
- /* use */
- // we will use it along with condorcet's method to determine the condorcet
- // winning percentage which will be assigned to the proposition.
- /* parameter(s) */
- // $votes
- // self-explainatory.
- foreach ($votes as $vote) {
- for ($percentage1 = 0; $percentage1 <= 100; $percentage1 += 1) {
- // |
- // you can give higher
- // values to faster, but
- // you will lose accuracy
- // |
- for ($percentage2 = 0; $percentage2 <= 100; $percentage2 += 1) {
- if ($percentage1 != $percentage2)
- $ranking[$percentage1] += intval(array_search($percentage1,$vote)
- <= array_search($percentage2,$vote));
- }}
- }
- $condorcet = [];
- for ($percentage = 0; $percentage <= 100; $percentage++) {
- if (max($ranking) == $ranking[$percentage])
- $condorcet[count($condorcet)] = $percentage;
- }
- return $condorcet[rand(0,count($condorcet)-1)];
- }
- print "\n Results (can take a while) :\n";
- foreach ($preferences as $proposition => $votes) {
- print " - $proposition : ".maximal_lotteries($votes)." %\n";
- // gives the final results
- }
- print "\n If there is a tie, you can :
- - Run the program again
- - Pick a random winner in the ones with the highest percentage\n";
- system("pause>nul");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement