<commit_before>package com.evolutionandgames.repeatedgames.evolution;

import java.util.ArrayList;

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;

import com.evolutionandgames.jevodyn.utils.Random;

public class RepeatedGame {

	private RealMatrix stageGame;
	private double continuationProbability;

	// keeping the last histories
	private ArrayList<Action> actionsPlayerOne = new ArrayList<Action>();
	private ArrayList<Action> actionsPlayerTwo = new ArrayList<Action>();

	public RepeatedGame(double reward, double sucker, double temptation,
			double punishment, double continuationProbability) {
		super();
		double[][] raw = { { reward, sucker }, { temptation, punishment } };
		this.stageGame = new Array2DRowRealMatrix(raw);
		this.continuationProbability = continuationProbability;
	}

	public double getReward() {
		return this.stageGame.getEntry(0, 0);
	}

	public double getSucker() {
		return this.stageGame.getEntry(0, 1);
	}

	public double getTemptation() {
		return this.stageGame.getEntry(1, 0);
	}

	public double getPunishment() {
		return this.stageGame.getEntry(1, 1);
	}
	
	public double getContinuationProbability() {
		return this.continuationProbability;
	}
	
	

	/**
	 * Payoff with a lot of noise, just one interaction takes place.
	 * 
	 * @param playerOne
	 * @param playerTwo
	 * @return
	 */
	public double[] playOnce(RepeatedStrategy playerOne,
			RepeatedStrategy playerTwo) {
		return playOnce(playerOne, playerTwo, 0.0);
	}

	// TODO: these methods should act on an interface and not on explicit
	// automata.
	/**
	 * Payoff with a lot of noise, just one interaction takes place.
	 * 
	 * @param playerOne
	 * @param playerTwo
	 * @param mistakeProbability
	 * @return
	 */
	public double[] playOnce(RepeatedStrategy playerOne,
			RepeatedStrategy playerTwo, double mistakeProbability) {
		// reset the machines
		playerOne.reset();
		playerTwo.reset();

		double payoffOne = 0.0;
		double payoffTwo = 0.0;
		// We start with empty histories
		actionsPlayerOne.clear();
		actionsPlayerTwo.clear();

		// STEP
		// first action happens for sure
		double[] stageResult = stage(playerOne, playerTwo, actionsPlayerOne,
				actionsPlayerTwo, mistakeProbability);
		payoffOne = payoffOne + stageResult[0];
		payoffTwo = payoffTwo + stageResult[1];

		while (Random.bernoulliTrial(continuationProbability)) {
			stageResult = stage(playerOne, playerTwo, actionsPlayerOne,
					actionsPlayerTwo, mistakeProbability);
			payoffOne = payoffOne + stageResult[0];
			payoffTwo = payoffTwo + stageResult[1];
		}

		double[] ans = new double[2];
		ans[0] = (1.0 - continuationProbability) * payoffOne;
		ans[1] = (1.0 - continuationProbability) * payoffTwo;
		return ans;
	}

	/**
	 * Looks up payoff combinations in the matrix of the game. Cooperate has<commit_msg>Use geometric sampling in generic game to speed up computation.<commit_after>package com.evolutionandgames.repeatedgames.evolution;

import java.util.ArrayList;

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;

import com.evolutionandgames.jevodyn.utils.Random;

public class RepeatedGame {

	private RealMatrix stageGame;
	private double continuationProbability;

	// keeping the last histories
	private ArrayList<Action> actionsPlayerOne = new ArrayList<Action>();
	private ArrayList<Action> actionsPlayerTwo = new ArrayList<Action>();

	public RepeatedGame(double reward, double sucker, double temptation,
			double punishment, double continuationProbability) {
		super();
		double[][] raw = { { reward, sucker }, { temptation, punishment } };
		this.stageGame = new Array2DRowRealMatrix(raw);
		this.continuationProbability = continuationProbability;
	}

	public double getReward() {
		return this.stageGame.getEntry(0, 0);
	}

	public double getSucker() {
		return this.stageGame.getEntry(0, 1);
	}

	public double getTemptation() {
		return this.stageGame.getEntry(1, 0);
	}

	public double getPunishment() {
		return this.stageGame.getEntry(1, 1);
	}
	
	public double getContinuationProbability() {
		return this.continuationProbability;
	}
	
	

	/**
	 * Payoff with a lot of noise, just one interaction takes place.
	 * 
	 * @param playerOne
	 * @param playerTwo
	 * @return
	 */
	public double[] playOnce(RepeatedStrategy playerOne,
			RepeatedStrategy playerTwo) {
		return playOnce(playerOne, playerTwo, 0.0);
	}

	/**
	 * Payoff with a lot of noise, just one interaction takes place.
	 * 
	 * @param playerOne
	 * @param playerTwo
	 * @param mistakeProbability
	 * @return
	 */
	public double[] playOnce(RepeatedStrategy playerOne,
			RepeatedStrategy playerTwo, double mistakeProbability) {
		// reset the machines
		playerOne.reset();
		playerTwo.reset();

		double payoffOne = 0.0;
		double payoffTwo = 0.0;
		// We start with empty histories
		actionsPlayerOne.clear();
		actionsPlayerTwo.clear();

		// STEP
		// first action happens for sure
		double[] stageResult = stage(playerOne, playerTwo, actionsPlayerOne,
				actionsPlayerTwo, mistakeProbability);
		payoffOne = payoffOne + stageResult[0];
		payoffTwo = payoffTwo + stageResult[1];
		
		int rounds = Random.simulateGeometricDistribution(1.0-continuationProbability);
		for (int i = 0; i < rounds; i++) {
			stageResult = stage(playerOne, playerTwo, actionsPlayerOne,
					actionsPlayerTwo, mistakeProbability);
			payoffOne = payoffOne + stageResult[0];
			payoffTwo = payoffTwo + stageResult[1];
		}

		double[] ans = new double[2];
		ans[0] = (1.0 - continuationProbability) * payoffOne;
		ans[1] = (1.0 - continuationProbability) * payoffTwo;
		return ans;
	}

	/**
	 * Looks up payoff combinations in the matrix of the game. Cooperate has
{'fraction_non_alphanumeric': 0.10266689907617221, 'fraction_numerical': 0.010632734878856544, 'mean_word_length': 4.392857142857143, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943859', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1693, 'n_words': 457}