'; echo ' Shapley Value Calculator '; // This is the reference to our CSS style sheet.' echo ''; echo ''; echo '

Shapley Value Calculator

'; ## Pointer to this file $thisFile = "index.php"; ## Decide the Number of Players $numPlayers = $_POST["numplayers"] ?? 3; // It sees what number of players the user has selected. // If it's the first time the user comes to the webpage, the default will be 3 echo "
"; echo " "; echo ""; echo "
"; ## Power set of all players for($n = 1; $n <= $numPlayers; $n++){ $setOfPlayers[$n] = $n; } $powerSet = powerSet($setOfPlayers); $i = 0; foreach($powerSet as $set){ $powerSetAsStrings[$i] = setToString($set); $i++; } ## Forms for the value of the coalition $getCoalitionNum = $_POST['CoalitionNum']?? 0; if($getCoalitionNum == 0){ //That is, if we haven't yet posted anything to ourselves regarding the size of the coalition echo '
'; echo '
'; echo ''; $i = 0; foreach($powerSetAsStrings as $setAsString){ echo '

'; echo ''; echo ''; echo '

'; $i++; } echo '
'; echo '
'; echo ''; echo '
'; }else{ echo '
'; echo ''; echo '
'; $i = 0; foreach($powerSetAsStrings as $setAsString){ echo '

'; echo ''; echo ''; echo '

'; $i++; } echo '
'; echo '
'; echo ''; echo '
'; ## Compute the Shapley values // Now, we have posted our data to ourselves. Note that we're still inside the else{} part of the loop // All that remains is to do the actual calculations. $i = 0; /* Reminder numPlayers: number of players setOfPlayers: Numbers from 1 to n powerSet: The power set of the above. All the different possible combinations. getCoalitionNum: The value of coalitions 1 through 2^# */ $i = 0; for($n = 0; $n<$numPlayers; $n++){ $Impact[$n] = 0; } foreach($powerSet as $set){ // in_array() function: will be useful. https://www.php.net/manual/es/function.in-array.php $size = sizeof($set); if($size !=0){ foreach($set as $player){ $marginalImpact = $getCoalitionNum[$i] - $getCoalitionNum[$i - pow(2,($player-1) ) ]; $Impact[$player-1] += $marginalImpact / choose($size-1,$numPlayers-1); } } $i++; } echo '

'; for($n=1; $n <=$numPlayers; $n++){ echo "The Shapley value of player ".$n." is: ".$Impact[$n-1]/$numPlayers."
"; } echo '

'; } // this is the end of the else{} part of the "have we posted any data to ourselves yet" question. Only executes if we indeed have. ## More HTML flavor //echo '

Also of interest:

// Shapley value resources
' echo '

'; echo '

'; echo 'Other things I have done
'; echo '

'; echo ''; echo ''; ## Functions function powerSet($array){ $results = array(array()); foreach($array as $element){ foreach($results as $combination){ array_push($results, array_merge(array($element), $combination)); } } return $results; // https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm } function setToString($set){ $size = sizeof($set); if($size == 0){ return "{}"; }else{ $return = "{"; for($i=0; $i<$size-1; $i++){ $return .= $set[$i].", "; } $return .= $set[$i]."}"; return $return; } } function factorial($n){ if($n == 0){ return 1; }else{ $f = 1; for($i=$n; $i>=1;$i--){ $f *= $i; } return $f; } } function choose($m, $n){ // Choose n objects among m choices return factorial($n) / (factorial($m)*factorial($n-$m)); } ?>