# Kann die evolutionäre Spieltheorie die Entstehung von Kooperation erklären? Studie über die Schwächen eines formalen Ansatzes

Eckhart Arnold

##### Listing: Beispiel_StagHunt_1.py

```import random
import Gfx
from Compatibility import *
GfxDriver = GetDriver()

StagHuntGame = [[[3, 3], [4, 0]],\
[[0, 4], [5, 5]]]

NEIGHBOURHOOD = (1,1) #change this to (2,2) to see the difference!

class Cell:
"""Cell Player: Either a cooperator or a defector.
"""
def __init__(self):
self.cooperate = 0
self.update = 0     # cooperate in the next round
self.neighbours = []
self.reference = []  # usually the same as neighbours
self.score = 0.0

def play(self, Game = StagHuntGame):
"""Play two person Stag Hunt with all neighbours.
"""
self.score = 0.0
for n in self.neighbours:
self.score += Game[self.cooperate][n.cooperate][0]
self.score /= float(len(self.neighbours))

def copyTheBest(self):
"""Set the strategy (to cooperate or not to cooperate) to
the strategy of the best player in the neighbourhood.
"""
score = self.score
self.update = self.cooperate
for n in self.reference:
if n.score > score:
score = n.score
self.update = n.cooperate
return self.update != self.cooperate

def commit(self):
self.cooperate = self.update

class PlayField:
"""Playfield, where players are placed.
"""
def __init__(self, gfx, size = (40, 30), nhood = (1,1),
game = StagHuntGame):
self.gfx = gfx
self.size = size
self.game = game
self.changeOccured = True
self.field = [[Cell() for x in xrange(size[0])] \
for y in xrange(size[1])]
self.genNhoods(nhood[0], nhood[1])
self.clearSetup()

def clearSetup(self):
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
self.field[row][col].cooperate = 0

def randomSetup(self, weight=0.5):
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
self.field[row][col].cooperate = int(random.random()+weight)

def blockSetup(self, x, y, d = 1):
for row in xrange(y-d, y+d):
for col in xrange(x-d, x+d):
self.field[row%self.size[1]][col%self.size[0]].cooperate = 1

def spotSetup(self, x, y, d = 1):
if d < 1: return
else:
self.field[y % self.size[1]][x % self.size[0]].cooperate = 1
if d > 1:
self.spotSetup(x-1, y, d-1)
self.spotSetup(x+1, y, d-1)
self.spotSetup(x, y-1, d-1)
self.spotSetup(x, y+1, d-1)

def invertSetup(self):
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
if self.field[row][col].cooperate == 1:
self.field[row][col].cooperate = 0
else:
self.field[row][col].cooperate = 1

def nHood(self, x, y, d = 1):
n = []
for dy in xrange(-d, d+1):
for dx in xrange(-d, d+1):
if not (dx == 0 and dy == 0):
n.append(self.field[(y+dy) % self.size[1]] \
[(x+dx) % self.size[0]])
return n

def genNhoods(self, dn = 1, dr = 1):
for y in xrange(self.size[1]):
for x in xrange(self.size[0]):
self.field[y][x].neighbours = self.nHood(x, y, dn)
if dn == dr:
self.field[y][x].reference = self.field[y][x].neighbours
else:
self.field[y][x].reference = self.nHood(x, y, dr)

def playRound(self):
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
self.field[row][col].play(self.game)
self.changeOccured = False
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
if self.field[row][col].copyTheBest():
self.changeOccured = True
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
self.field[row][col].commit()

def plot(self):
width, height = self.gfx.getSize()
pen = Gfx.RED_PEN
self.gfx.applyPen(pen)
for row in xrange(self.size[1]):
for col in xrange(self.size[0]):
x = width * col / self.size[0]
y = height * row / self.size[1]
w = width * (col+1) / self.size[0] - x + 1
h = height * (row+1) / self.size[1] - y + 1
if self.field[row][col].cooperate == 1 and \
pen != Gfx.GREEN_PEN:
pen = Gfx.GREEN_PEN
self.gfx.applyPen(pen)
elif self.field[row][col].cooperate == 0 and \
pen != Gfx.RED_PEN:
pen = Gfx.RED_PEN
self.gfx.applyPen(pen)
self.gfx.fillRect(x, y, w, h)

def RunStagHuntGame():
"""A simulation of the Stag Hunt game (2 player) on a 2D plane.
"""

# Open a window for graphics output.

gfx = GfxDriver.Window(size=(800,600), title="2 Person Stag Hunt")

#  Setup play field

playfield = PlayField(gfx, size=(80,60), nhood=NEIGHBOURHOOD)
playfield.clearSetup()
playfield.spotSetup(10, 10, 5)
playfield.plot()
gfx.refresh()

# run the simulation

while playfield.changeOccured:
playfield.playRound()
playfield.plot()
gfx.refresh()

# wait until user closes the window

gfx.waitUntilClosed()

if __name__ == "__main__":
print __doc__
RunStagHuntGame()

```