public double getValue(List <int> myhistory, List <int> yourhistory, int action) { int nrOfRounds = myhistory.Count(); int roundsAction = 0; double rewardAction = 0; // Calculate the sum of payoffs, earned when playing action. for (int i = 0; i < nrOfRounds; i++) { if (myhistory[i] == action) { rewardAction += UpdateLogic.getPayoff(myhistory[i], yourhistory[i]); roundsAction += 1; } } // Returns the average payoff of action. if (roundsAction == 0) { // set the initial estimate to an unrealistic high value to make the probability that each action is tried in the beginning high. return(6); } else { return(rewardAction / roundsAction); } }
public double getReward(List <int> myActions, List <int> yourActions) { int nrOfRounds = myActions.Count(); double reward = 0; // This calculates the sum of the rewards earned in each round in history. for (int i = 0; i < nrOfRounds; i++) { reward += UpdateLogic.getPayoff(myActions[i], yourActions[i]); } return(reward); }
public int getAction(List <int> myhistory, List <int> yourhistory) { int action = 0; // Find the number of times the opponent defected and the total number of played rounds. int timesOpponentDefect = yourhistory.FindAll(x => x == 1).Count(); int nrOfPlayedRounds = yourhistory.Count(); if (nrOfPlayedRounds > 0) { // Calculate the mixed strategy of your opponent. double[] opponentStrategy = { 1 - (timesOpponentDefect / nrOfPlayedRounds), (timesOpponentDefect / nrOfPlayedRounds) }; // Calculate the expected reward of your actions, given the strategy of the opponent. double expectedRewardDefect = opponentStrategy[0] * UpdateLogic.getPayoff(1, 0) + opponentStrategy[1] * UpdateLogic.getPayoff(1, 1); double expectedRewardCooperate = opponentStrategy[0] * UpdateLogic.getPayoff(0, 0) + opponentStrategy[1] * UpdateLogic.getPayoff(0, 1); // Choose an action proportional to the expected reward of that action. double probabilityDefect = expectedRewardDefect / (expectedRewardDefect + expectedRewardCooperate); action = randomAction.proportionalAction(probabilityDefect); } return(action); }