shapleyvalue.com

Attribute value according to a parsimonious game theory solution
Log | Files | Refs

publicGoodsGame.php (4939B)


      1 
      2 <?php
      3 
      4 /*
      5 
      6 *	File: /shapleyvalue.com/publicGoodsGame.php
      7 *	By: Nuño Sempere
      8 * 	Date: 23 Sept 2019
      9 * 	 
     10 *	This file applies the Shapley value to a public goods game with 6 players.
     11 
     12 */
     13 
     14 
     15 ## HTML Flavor 
     16 
     17 echo '<html>';
     18 
     19 echo '<head>
     20 	<title>Shapley Value Calculator</title>
     21 	<meta name="copyright" content="Nuño Sempere López-Hidalgo">
     22 	<meta name="keywords" content="Shapley Value, Shapley value, calculate Shapley value, calculate Shapley value online free, Shapley value program">
     23 	<meta name="description" content="Shapley value calculator">
     24 	<link rel="stylesheet" href="CSS/main.css" type="text/css">'; 
     25 	// This is the reference to our CSS style sheet.'
     26 echo '</head>';
     27 
     28 echo '<body>';
     29 
     30 echo '<h1> Shapley Value Calculator </h1>';
     31 
     32 
     33 ## Pointer to this file
     34 
     35 $thisFile = "publicGoodsGame.php";
     36 
     37 
     38 ## Power set of all players
     39 
     40 $numPlayers = 6;
     41 	## Initially, the number of players is 6.
     42 for($n = 1; $n <= $numPlayers; $n++){
     43 	$setOfPlayers[$n] = $n;
     44 }
     45 
     46 $powerSet = powerSet($setOfPlayers);
     47 
     48 $i = 0;
     49 foreach($powerSet as $set){
     50 	
     51 	$powerSetAsStrings[$i] = setToString($set);
     52 	$i++;
     53 }
     54 
     55 ## Check if the thing has been posted
     56 
     57 $getContributionPlayer = $_POST['ContributionPlayer']?? -1;
     58 
     59 $getMultiplier = $_POST['Multiplier'] ?? -1;
     60 
     61 if($getContributionPlayer == -1){
     62 
     63 	## Get the contributions of each player to the common pot
     64 	echo '<form action='.$thisFile.' method = "post">';
     65 	echo '<div class="form">';
     66 
     67 	echo '<input type="hidden" name="numplayers" value='.$numPlayers.' />';
     68 	$i = 0;
     69 
     70 	for($i = 0; $i<$numPlayers; $i++){
     71 		echo '<p>';
     72 			echo '<label>Contribution of Player #'.($i+1).': &nbsp</label>';
     73 			echo '<input type="number" name="ContributionPlayer['.($i).']" value = "0" class = "Box">';
     74 		echo '</p>';
     75 	}
     76 	echo '<p></br></p>';
     77 	echo '<p>';
     78 		echo '<label>Multiplier = &nbsp </label>';
     79 		echo '<input type="number" name="Multiplier" value = "0" step="0.01" class = "Box">';
     80 	echo '</p>';
     81 	
     82 	echo '</br>';
     83 
     84 	echo '</div>';
     85 		echo '<input type="submit" value="Compute" class="Buttons" >';
     86 	echo '</form>';
     87 
     88 }else{
     89 
     90 
     91 	echo '<form action='.$thisFile.' method = "post">';
     92 	echo '<div class="form">';
     93 
     94 	echo '<input type="hidden" name="numplayers" value='.$numPlayers.' />';
     95 	$i = 0;
     96 
     97 	for($i = 0; $i<$numPlayers; $i++){
     98 		echo '<p>';
     99 			echo '<label>Contribution of Player #'.($i+1).': &nbsp</label>';
    100 			echo '<input type="number" name="ContributionPlayer['.($i).']" value ='.$getContributionPlayer[($i)].' class = "Box">';
    101 		echo '</p>';
    102 	}
    103 
    104 	echo '<p></br></p>';
    105 	echo '<p>';
    106 		echo '<label>Multiplier = &nbsp </label>';
    107 		echo '<input type="number" name="Multiplier" value ='.$getMultiplier.' step = "0.01" class = "Box">';
    108 	echo '</p>';
    109 	
    110 	echo '</br>';
    111 
    112 	echo '</div>';
    113 		echo '<input type="submit" value="Compute" class="Buttons" >';
    114 	echo '</form>';
    115 
    116 	## Now, the Shapley value in a public good game is simply going to be contribution*multiplier
    117 	## The classical reward is simply aggregate*multiplier/number of players.
    118 	## Let D = Shapley value - Classical reward
    119 	## Then it would be interesting to check what happens when the payout is
    120 	## P = Classical Reward + Alpha*D,
    121 	## Where alpha ranges from 0 to 1.
    122 	## I think that just comparing alpha = 0, alpha = 1/3, alpha = 2/3, (alpha = 1) would be interesting.
    123 
    124 	$sumContributions = array_sum($getContributionPlayer);
    125 	
    126 	echo '<p>';
    127 		echo 'Sum of the contributions = &nbsp'.$sumContributions;
    128 	echo '</p>';
    129 	echo '<p>';
    130 		echo 'Sum*Multiplier = &nbsp'.($sumContributions*$getMultiplier);
    131 	echo '</p>';
    132 	echo '<p>';
    133 		$payout = ($sumContributions*$getMultiplier/$numPlayers);
    134 		echo 'Payout = Sum*Multiplier/ Number of Players = &nbsp'.$payout;
    135 	echo '</p>';
    136 	
    137 	## What is the difference between the payout and the Shapley value?
    138 
    139 	echo '<div class="centeredExample">';
    140 	echo '<p>';
    141 	echo "<h3>For each player, what is the difference between the payout and the Shapley value? </h3>";
    142 	for($i = 0; $i<$numPlayers; $i++){
    143 		echo '<p>';
    144 			echo 'Difference for Player #'.($i+1).'&nbsp = '.($getMultiplier*$getContributionPlayer[$i]-$payout);
    145 		echo '</p>';
    146 	}
    147 	echo '</p>';
    148 	echo '</div>';
    149 }
    150 ## Footer
    151 echo '<p><a href="/">Go back</a></p></br>';
    152 
    153 echo '</body>';
    154 echo '</html>';
    155 
    156 ## Functions
    157 
    158 function powerSet($array){
    159 	
    160 	$results = array(array());
    161 	foreach($array as $element){
    162 		foreach($results as $combination){
    163 			array_push($results, array_merge(array($element), $combination));
    164 		}
    165 	}
    166 	return $results;
    167 	// https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm
    168 }
    169 
    170 
    171 
    172 function setToString($set){
    173 	
    174 	$size = sizeof($set);
    175 	
    176 	if($size == 0){
    177 	
    178 		return "{}";
    179 	
    180 	}else{
    181 		$return = "{";
    182 		for($i=0; $i<$size-1; $i++){
    183 			$return .= $set[$i].", ";
    184 		}
    185 		$return .= $set[$i]."}";
    186 		return $return;
    187 	}
    188 }
    189 
    190 function factorial($n){
    191 	if($n == 0){
    192 		return 1;
    193 	}else{
    194 
    195 		$f = 1;
    196 		for($i=$n; $i>=1;$i--){
    197 			$f *= $i;
    198 		}
    199 		return $f;
    200 	}	
    201 }
    202 
    203 function choose($m, $n){
    204 	// Choose n objects among m choices
    205 	return factorial($n) / (factorial($m)*factorial($n-$m));
    206 
    207 }