Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: Basic prisoner's dilemma? Date: Mon, 18 Apr 2011 19:22:53 +0200 Organization: A noiseless patient Spider Lines: 72 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 18 Apr 2011 17:22:49 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="TdHps3CX+e9UB6Yx/BzU0w"; logging-data="15408"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+gXuz8JOs4+ysY6cByMLd" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: Cancel-Lock: sha1:9vruEbtXurtEsuANDAyGlpKUrwE= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3110 On 18/04/2011 04:51, theglazeb allegedly wrote: > Hi there! > > I was wondering if you were able to help with me a basic prisoner's > dilemma strategy? How would I implement a basic tit for tat strategy? > And perhaps try to avoid the "death spiral" from occurring when two > players use the tit for tat strategy? > > See below > > Thanks! > > /* > * File: PDStrategy.java > * --------------------- > * This class encapsulates the strategy for an Iterated Prisoner's > * Dilemma game. To enter the contest, all you have to do is change > * the definition of chooseAction so that it takes account of the > * history and does something more clever than cooperating (or > * defecting) all the time. > */ > > import java.util.*; > > class PDStrategy { > > /* > * Method: chooseAction > * -------------------- > * This method determines the player's action for the current > * game, based on two ArrayLists containing the history of play: > * one for your algorithm and one for the opponent. Each of these > * ArrayLists contains strings, which are guaranteed to be one of > * the uppercase strings "COOPERATE" or "DEFECT". Your method > * should look at the history and return one of those two strings > * representing the action you take on the current round. > */ > > public String chooseAction(ArrayList myHistory, > ArrayList oppHistory) { > return "COOPERATE"; //currently, it is only cooperating > } > > } Basic tit-for-tat is as follows: - if it's the first time you encounter that opponent (this implies you keep a 'memory' of previous encounters), cooperate. - otherwise, replicate that opponent's last move. In code (assuming oppHistory contains only the history with "this"): public String chooseAction( ArrayList myHistory, ArrayList oppHistory) { return oppHistory.isEmpty() ? "COOPERATE" : oppHistory.get( oppHistory.size()); } (or oppHistory.get(0), depending on whether it's used as a FIFO or a LIFO). You can add various tricks, but don't expect to get far with them, because tit-for-tat is the evolutionary stable strategy. If you're interested in the topic apart from the programming aspect, I *very* strongly suggest you read Dawkins' "Selfish Gene" and "Blind Watchmaker". Even if you're not, by the way -- those are IMHO must-reads. -- DF. An escaped convict once said to me: "Alcatraz is the place to be"