Created
December 10, 2014 20:23
-
-
Save pkazmierczak/dff35a81231efe1ef85c to your computer and use it in GitHub Desktop.
stagHunt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import random | |
class Agent(): | |
def __init__(self, | |
location={'x': 0, 'y': 0}, | |
vision=1, | |
strategy='defect', | |
fitness=0, | |
EndofTheWorld=32): | |
self.location = location | |
self.vision = vision | |
self.strategy = strategy | |
self.fitness = fitness | |
self.sexyness = 0 | |
self.neighborhood = [{'x': i, 'y': j} | |
for i in range(self.location['x'] - self.vision, | |
self.location['x'] + self.vision + 1) | |
if i >= 0 and i <= EndofTheWorld | |
for j in range(self.location['y'] - self.vision, | |
self.location['y'] + self.vision + 1) | |
if j >= 0 and j <= EndofTheWorld] | |
def __str__(self): | |
return 'Strategy: ' + str(self.strategy) | |
def moore_agents(self, agents): | |
return [agent for agent in agents if | |
agent.location in self.neighborhood and agent != self] | |
def play(self, agents, pay_offs): | |
self.sexyness = numpy.sum([pay_offs[self.strategy + '-' + agent.strategy] | |
for agent in self.moore_agents(agents)]) | |
self.fitness += self.sexyness | |
def learn(self, agents): | |
agents_to_eyeup = self.moore_agents(agents) | |
sexiest_agents = [a for a in agents_to_eyeup | |
if a.sexyness == max([b.sexyness for b in agents_to_eyeup])] | |
agent_to_consider_copying = random.choice(sexiest_agents) | |
if agent_to_consider_copying.sexyness > self.sexyness: | |
self.strategy = agent_to_consider_copying.strategy | |
pay_offs = {'cooperate-cooperate': 4, | |
'cooperate-defect': 0, | |
'defect-cooperate': 3, | |
'defect-defect': 1} | |
a = Agent(location={'x': 1, 'y': 1}) | |
b = Agent(location={'x': 2, 'y': 1}, strategy='cooperate') | |
agents = [a,b] | |
print b.fitness, b.strategy | |
a.play(agents,pay_offs) | |
b.play(agents,pay_offs) | |
b.learn(agents) | |
print b.fitness, b.strategy | |
b.play(agents,pay_offs) | |
print b.fitness, b.strategy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment