-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhawk dove game.py
259 lines (181 loc) · 8.25 KB
/
hawk dove game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from random import choice, randint
import time
STARTING_DOVES = 100
STARTING_HAWKS = 100
STARTING_POPULATION = STARTING_HAWKS + STARTING_DOVES
ROUNDS = 100
STARTING_ENERGY = 100;
MIN_FOOD_PER_ROUND = 20
MAX_FOOD_PER_ROUND = 70
ENERGY_REQUIRED_FOR_REPRODUCTION = 250
ENERGY_LOSS_PER_ROUND = 2
ENERGY_COST_OF_BLUFFING = 10
ENERGY_LOSS_FROM_FIGHTING = 200
ENERGY_REQUIRED_FOR_LIVING = 20
STATUS_ACTIVE = "active"
STATUS_ASLEEP = "asleep"
TYPE_HAWK = "hawk"
TYPE_DOVE = "dove"
agents = []
# Graph stuff
graph_hawk_points = []
graph_dove_points = []
# Profiling
class Agent:
id = 0
agent_type = None
status = STATUS_ACTIVE
energy = STARTING_ENERGY
def main():
init()
current_round = 1
death_count = 0
dead_hawks = 0
dead_doves = 0
breed_count = 0
main_tic = time.process_time()
while current_round <= ROUNDS and len(agents) > 2:
tic = time.process_time()
awakenAgents()
food = getFood()
# This could be optimized further by creating a list every time
# that only has active agents, so it isn't iterating over entire list every time
while True:
agent, nemesis = getRandomAgents()
if agent is None or nemesis is None: break
compete(agent, nemesis, food)
# Energy cost of 'living'
for agent in agents:
agent.energy += ENERGY_LOSS_PER_ROUND
round_dead_hawks, round_dead_doves = cull()
round_hawk_babies, round_dove_babies = breed()
death_count += (round_dead_hawks + round_dead_doves)
breed_count += (round_hawk_babies + round_dove_babies)
toc = time.process_time()
print("ROUND %d" % current_round)
print("Food produced : %d" % food)
print("Population : Hawks-> %d, Doves-> %d" % (getAgentCountByType(TYPE_HAWK), getAgentCountByType(TYPE_DOVE)))
print("Dead hawks : %d" % round_dead_hawks)
print("Dead doves : %d" % round_dead_doves)
print("Hawk babies : %s" % round_hawk_babies)
print("Dove babies : %s" % round_dove_babies)
print("Hawks : %s" % getPercByType(TYPE_HAWK))
print("Doves : %s" % getPercByType(TYPE_DOVE))
print("----")
print("Round Processing time : %s" % getTimeFormatted(toc - tic))
print("Elapsed time : %s\n" % getTimeFormatted(time.process_time() - main_tic))
# Plot
graph_hawk_points.append(getAgentCountByType(TYPE_HAWK))
graph_dove_points.append(getAgentCountByType(TYPE_DOVE))
current_round += 1
main_toc = time.clock()
print("=============================================================")
print("Total dead agents : %d" % death_count)
print("Total breeding agents : %d" % breed_count)
print("Total rounds completed : %d" % (current_round - 1))
print("Total population size : %s" % len(agents))
print("Hawks : %s" % getPercByType(TYPE_HAWK))
print("Doves : %s" % getPercByType(TYPE_DOVE))
print("Processing time : %s" % getTimeFormatted(main_toc - main_tic))
print("=============================================================")
def init():
for x in range(0,STARTING_DOVES):
a = Agent()
a.agent_type = TYPE_DOVE
agents.append(a)
for x2 in range(0,STARTING_HAWKS):
a2 = Agent()
a2.agent_type = TYPE_HAWK
agents.append(a2)
def getAvgFromList(list):
return float( sum(list) / len(list) )
def getTimeFormatted(seconds):
m, s = divmod(seconds, 60)
return "%02d:%02d" % (m, s)
def getFood():
return randint(MIN_FOOD_PER_ROUND, MAX_FOOD_PER_ROUND)
def getPercByType(agent_type):
perc = float(getAgentCountByType(agent_type)) / float(len(agents))
return '{percent:.2%}'.format(percent=perc)
def getAliveAgentsCount():
return getAgentCountByStatus(STATUS_ACTIVE) + getAgentCountByStatus(STATUS_ASLEEP)
def getRandomAgents():
nemesis = None
active_agents = list(generateAgentsByStatus(STATUS_ACTIVE))
if len(active_agents) < 2:
return None, None
max_index = len(active_agents) - 1
agent = active_agents[ randint(0, max_index) ]
while nemesis is None:
n = active_agents[ randint(0, max_index) ]
if n is not agent:
nemesis = n
return agent, nemesis
def awakenAgents():
for agent in agents:
agent.status = STATUS_ACTIVE
def generateAgentsByType(agent_type):
for agent in agents:
if agent.agent_type == agent_type:
yield agent
def generateAgentsByStatus(status):
for agent in agents:
if agent.status == status:
yield agent
def getEnergyFromFood(food):
return food # 1 to 1
def getAgentCountByStatus(status):
count = len( list(generateAgentsByStatus(status)) )
return count
def getAgentCountByType(agent_type):
return len( list(generateAgentsByType(agent_type)) )
def compete(agent, nemesis, food):
winner = choice([agent, nemesis])
loser = agent if (winner is nemesis) else nemesis
if agent.agent_type == TYPE_HAWK and nemesis.agent_type == TYPE_HAWK:
# Random winner chosen, loser gets injured, winner gets food
winner.energy += getEnergyFromFood(food)
loser.energy -= ENERGY_LOSS_FROM_FIGHTING
if agent.agent_type == TYPE_HAWK and nemesis.agent_type == TYPE_DOVE:
agent.energy += getEnergyFromFood(food)
nemesis.energy -= ENERGY_COST_OF_BLUFFING
if agent.agent_type == TYPE_DOVE and nemesis.agent_type == TYPE_HAWK:
nemesis.energy += getEnergyFromFood(food)
agent.energy -= ENERGY_COST_OF_BLUFFING
if agent.agent_type == TYPE_DOVE and nemesis.agent_type == TYPE_DOVE:
winner.energy += getEnergyFromFood(food)
loser.energy -= ENERGY_COST_OF_BLUFFING
nemesis.status = agent.status = STATUS_ASLEEP
def getNewAgent(agent_type, starting_energy=STARTING_ENERGY, status=STATUS_ASLEEP):
agent = Agent()
agent.agent_type = agent_type
agent.status = status
agent.energy = starting_energy
return agent
def breed():
"""
If agent can breed, it halves its energy and produces
two babies with starting energy (parent energy / 2)
"""
hawk_babies = 0
dove_babies = 0
for agent in agents:
if agent.energy > ENERGY_REQUIRED_FOR_REPRODUCTION:
baby_agent_a = getNewAgent(agent.agent_type, (agent.energy/2))
baby_agent_b = getNewAgent(agent.agent_type, (agent.energy/2))
agents.append(baby_agent_b)
agents.append(baby_agent_a)
agent.energy /= 2
if agent.agent_type == TYPE_DOVE: dove_babies += 2
if agent.agent_type == TYPE_HAWK: hawk_babies += 2
return hawk_babies, dove_babies
def cull():
dead_hawks = 0
dead_doves = 0
for index, agent in enumerate(agents):
if agent.energy < ENERGY_REQUIRED_FOR_LIVING:
if agent.agent_type == TYPE_DOVE: dead_doves += 1
if agent.agent_type == TYPE_HAWK: dead_hawks += 1
del agents[index]
return dead_hawks, dead_doves
main()