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 void createTable()
        {
            //Clear the score table view
            dataGridView.Rows.Clear();

            //Create score table
            scoreTable = UpdateLogic.scoreTable(nrrounds, nrrestarts, strategies, noise);
            initDataGrid();

            //Also calls setup
            Setup();
        }
        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);
        }
        //Event called every tick
        private void TickTimer_Tick(object sender, EventArgs e)
        {
            //Update proportions for next time step using replicator equation
            allproportions = UpdateLogic.replicator(allproportions, scoreTable, birthrate);

            tickCount++;
            this.tickLabel.Text = String.Format("Ticks = {0}", tickCount);
            //Update plot
            updatePlot();

            //Update proportions in textbox
            updateTextBox();
        }
        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);
        }