CoopSim - A Computer Simulation of the Evolution of Cooperation. User's Manual

1 Introduction
2 Aknowledgements
3 License
4 Installation
5 First Steps - A guided tour through CoopSim
6 Comprehensive Overview
7 Advanced Topics
    7.1 Technical notes on the dynamical model
    7.2 Programming user strategies
        7.2.1 Preface
        7.2.2 The Strategy class
    7.3 Defining user setups
8 Further Reading

7.2.2 The Strategy class

Any strategy in the game must be derived from class Strategies.Strategy. This class defines the two methods firstMove(self) and nextMove(self, myMoves, opMoves). Both methods must return either 0 or 1, where 0 means “defection” and 1 stands for “cooperation”. firstMove does not take any parameters except “self”, while nextMove gets the list of its own moves during the previous rounds of the current match as well as a list of its opponents previous moves. Both lists are list of zeros and ones. Implementing your own user strategies is pretty easy now: Simply derive a class from class Strategies.Strategy and define the methods firstMove and nextMove, nothing else is needed, neither a constructor, nor is it necessary to worry about a name for the strategy, because the class name is automatically used as the strategies' name. All state saving variables of your class must be reset in method firstmove! This is necessary, because the same strategy object is used in all matches of the tournament. Here is a simple example of a custom strategy class:

class LesserTFT(Strategy):    
    """Retailiate only when not having retailiated in    
    the last round already.    
    def firstMove(self):
        return 1  # start friendly
    def nextMove(self, myMoves, opMoves):
        if opMoves[-1] == 0 and myMoves[-1] != 0:
            return 0  # retailiate
            return 1  # cooperate

# Do not forget to instantiate your class, otherwise
# it will not appear among the available strategies!

lesser_tft = LesserTFT()

t g+ f @