-
Notifications
You must be signed in to change notification settings - Fork 3
/
Classes_Agents.py
491 lines (426 loc) · 18.6 KB
/
Classes_Agents.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 21 21:18:38 2017
@author: andrea
"""
#For predator modified f3
#Changed the features in preceive
#Removed log in reward
#Added exp in features
from builtins import map
import numpy as np
from GrassAgent import Grass
class Prey :
ptype = -1 #1 if predator, -1 for prey
age = 0
epsilon = 0.2
def __init__(self, x_position, y_position, ID, lastAte, father, reproduction_age,
death_rate, reproduction_rate, weights, learning_rate,discount_factor, hunger_minimum):
self.x_position = x_position
self.y_position = y_position
self.ID = ID
self.lastAte = lastAte
self.father = father
self.reproduction_age = reproduction_age
self.death_rate = death_rate
self.reproduction_rate = reproduction_rate
self.weights = weights
self.learning_rate = learning_rate
self.discount_factor = discount_factor
self.hunger_minimum = hunger_minimum
self.q = 0
def compute_how_many(self,matrix):
"""
Returns the number of the different agents for each neighbor cell
"""
how_many = np.zeros([3, 9]) # Grass is nr 0, prey 1 and predator 2.
iMoore = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
x_target = (self.x_position + i) % matrix.xDim
y_target = (self.y_position + j) % matrix.yDim
for agents in matrix.grid[x_target][y_target]:
if type(agents) is Grass:
how_many[0][iMoore] += 1
if type(agents) is Prey:
how_many[1][iMoore] += 1
if type(agents) is Predator:
how_many[2][iMoore] += 1
iMoore += 1
return how_many
def perceive(self,x,y,matrix):
"""
Returns the features for a position x,y as a matrix 9x3
"""
#Row 1: grass, Row 2: prey, Row 3: predators
features=np.zeros(12)
# Count all predators and prey in the world
nr_grass=matrix.numGrass
nr_prey=matrix.numPrey
nr_pred=matrix.numPred
# How many agents are at each spot?
how_many=np.zeros([3,9]) # Grass is nr 0, prey 1 and predator 2.
iMoore=0
for i in [-1,0,1]:
for j in [-1,0,1]:
x_target = (x+i)%matrix.xDim
y_target = (y+j)%matrix.yDim
for agents in matrix.grid[x_target][y_target]:
if type(agents) is Grass:
how_many[0][iMoore]+= 1
elif type(agents) is Prey:
how_many[1][iMoore]+= 1
else:
how_many[2][iMoore]+= 1
iMoore +=1
# Calculate features. Note: this is for a prey
if nr_pred != 0:
features[0]=sum(how_many[2][:])/nr_pred #pred
else:
features[0] = 0
if nr_prey != 0:
features[1]=sum(how_many[1][:])/nr_prey # prey
else:
features[1] = 0
if nr_grass != 0:
features[2]=sum(how_many[0][:])/nr_grass # grass
else:
features[2] = 0
if sum(how_many[2][:])==0:
features[3:12]=0
else:
features[3]=how_many[2][0]/sum(how_many[2][:])
features[4]=how_many[2][1]/sum(how_many[2][:])
features[5]=how_many[2][2]/sum(how_many[2][:])
features[6]=how_many[2][3]/sum(how_many[2][:])
features[7]=how_many[2][4]/sum(how_many[2][:])
features[8]=how_many[2][5]/sum(how_many[2][:])
features[9]=how_many[2][6]/sum(how_many[2][:])
features[10]=how_many[2][7]/sum(how_many[2][:])
features[11]=how_many[2][8]/sum(how_many[2][:])
return features
def Cells_Evaluation(self,matrix):
"""
Evaluate the neighbooring cells
"""
x=self.x_position
y=self.y_position
iMoore=0
score = np.empty([3, 9])
for i_x_Moore in [-1,0,1] :
for i_y_Moore in [-1,0,1] :
x_eval = np.mod(x+i_x_Moore,matrix.xDim) # eval case 0 if agent in case 50
y_eval = np.mod(y+i_y_Moore,matrix.yDim)
f_i = self.perceive(x_eval,y_eval,matrix)
cell_score = np.dot(f_i,self.weights)
score[0][iMoore]=x_eval #gives the score and the absolute position
score[1][iMoore]=y_eval
score[2][iMoore]=cell_score
iMoore +=1
return score
def Change_Position(self, matrix):
"""
Perform action (i.e. movement) of the agent depending on its evaluations
"""
r = np.random.rand()
if r < 1 - self.epsilon:
score = self.Cells_Evaluation(matrix)
best_score_index = np.argmax(score[2, :]) # select the line with the best score
x_new = score[0, best_score_index]
y_new = score[1, best_score_index]
self.q = score[2, best_score_index]
else:
x_new = (self.x_position + np.random.randint(-1, 2) )%matrix.xDim
y_new = (self.y_position + np.random.randint(-1, 2) )%matrix.yDim
features = self.perceive(x_new, y_new, matrix)
self.q = np.dot(features, self.weights)
new_position = np.array([x_new, y_new])
return new_position
def Aging(self, i):
self.age += 1
self.epsilon = 1 / i
if i <= 501:
self.learning_rate = 0.05 - 0.0001 * (i - 1)
else:
self.learning_rate = 0
self.lastAte += 1
return
#---------------------------Learning part-------------------------------#
def Get_Reward(self,matrix):
"""
opponent :number of the other species type within the agent’s Moore
neighborhood normalized by the number of total
type is 1 for predator and −1 for prey
same = {0, 1} for if the opponent is on the same location
"""
type_animal = self.ptype
how_many = self.compute_how_many(matrix)
x = self.x_position
y = self.y_position
features = self.perceive(x,y,matrix)
feature_wanted = features[0]
opponent = feature_wanted
same = how_many[2][4]>0
reward = opponent*type_animal + 2*same*type_animal
return reward
def Get_QFunction(self,features):
weights = self.weights
Q = 0
for i in range(len(weights)):
Q = Q + weights[i]*features[i]
return Q
def Update_Weight(self, reward, matrix, Q_value):
weights = self.weights
learning_rate = self.learning_rate
discount_factor = self.discount_factor
#Compute the Q'-table:
Q_prime = []
for i in [-1,0,1]:
for j in [-1,0,1]:
x_target = (self.x_position+i)%matrix.xDim
y_target = (self.y_position+j)%matrix.yDim
features = self.perceive(x_target,y_target,matrix)
Q_prime.append(self.Get_QFunction(features))
#Update the weights:
Q_prime_max = max(Q_prime)
for i in range(0,len(weights)):
if i <3:
c = 9/(matrix.xDim*matrix.yDim)
else:
c = 1/9
w = weights[i]
f = features[i]
f = np.exp(-0.5*(f-c)**2)
weights[i] = w + learning_rate*(reward +discount_factor*Q_prime_max - Q_value)*f
self.weights= weights
return
def Eat(self, agentListAtMatrixPos):
for agent in agentListAtMatrixPos: #Not selected randomly at the moment, just eats the first prey in the list
if type(agent) is Grass:
killFoodSource = agent.consume()
self.lastAte = 0
if killFoodSource == 0:
return agent.ID
return -1
def Starve(self):
if self.lastAte > self.hunger_minimum:
pdeath = self.lastAte*self.death_rate
r = np.random.rand()
if r < pdeath:
return self.ID
return -1
def Reproduce(self):
offspring = 0
if self.age >= self.reproduction_age:
r = np.random.rand()
if r < self.reproduction_rate:
offspring = Prey(self.x_position, self.y_position, -1, 0, self.ID, self.reproduction_age,
self.death_rate, self.reproduction_rate, self.weights, self.learning_rate,
self.discount_factor, self.hunger_minimum) #ID is changed in Grid.update()
return offspring
class Predator:
ptype = 1 #1 if predator, -1 for prey
age = 0
epsilon = 0.2
def __init__(self, x_position, y_position, ID, lastAte, father, reproduction_age,
death_rate, reproduction_rate, weights, learning_rate,discount_factor, hunger_minimum):
self.x_position = x_position
self.y_position = y_position
self.ID = ID
self.lastAte = lastAte
self.father = father
self.reproduction_age = reproduction_age
self.death_rate = death_rate;
self.reproduction_rate = reproduction_rate
self.weights = weights
self.learning_rate = learning_rate
self.discount_factor = discount_factor
self.hunger_minimum = hunger_minimum
self.q = 0
def compute_how_many(self,matrix):
"""
Returns the number of the different agents for each neighbor cell
"""
how_many = np.zeros([3, 9]) # Grass is nr 0, prey 1 and predator 2.
iMoore = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
x_target = (self.x_position + i) % matrix.xDim
y_target = (self.y_position + j) % matrix.yDim
for agents in matrix.grid[x_target][y_target]:
if type(agents) is Grass:
how_many[0][iMoore] += 1
if type(agents) is Prey:
how_many[1][iMoore] += 1
if type(agents) is Predator:
how_many[2][iMoore] += 1
iMoore += 1
return how_many
def perceive(self,x,y,matrix):
"""
Returns the features for a position (x,y) as a matrix 9x3
"""
#Row 1: grass, Row 2: prey, Row 3: predators
features=np.zeros(12)
# Count all predators and prey in the world
nr_grass=matrix.numGrass
nr_prey=matrix.numPrey
nr_pred=matrix.numPred
# How many agents are at each spot?
how_many=np.zeros([3,9]) # Grass is nr 0, prey 1 and predator 2.
iMoore=0
for i in [-1,0,1]:
for j in [-1,0,1]:
x_target = (x+i)%matrix.xDim
y_target = (y+j)%matrix.yDim
for agents in matrix.grid[x_target][y_target]:
if type(agents) is Grass:
how_many[0][iMoore]+= 1
elif type(agents) is Prey:
how_many[1][iMoore]+= 1
else:
how_many[2][iMoore]+= 1
iMoore +=1
# Calculate features. Note: this is for a predator
if nr_prey != 0:
features[0]=sum(how_many[1][:])/nr_prey #prey
else:
features[0] = 0
if nr_pred != 0:
features[1]=sum(how_many[2][:])/nr_pred # predators
else:
features[1] = 0
if nr_grass != 0:
features[2]=sum(how_many[0][:])/nr_grass # grass
else:
features[2] = 0
if sum(how_many[1][:])==0:
features[3:12]=0
else:
features[3]=how_many[1][0]/sum(how_many[1][:])
features[4]=how_many[1][1]/sum(how_many[1][:])
features[5]=how_many[1][2]/sum(how_many[1][:])
features[6]=how_many[1][3]/sum(how_many[1][:])
features[7]=how_many[1][4]/sum(how_many[1][:])
features[8]=how_many[1][5]/sum(how_many[1][:])
features[9]=how_many[1][6]/sum(how_many[1][:])
features[10]=how_many[1][7]/sum(how_many[1][:])
features[11]=how_many[1][8]/sum(how_many[1][:])
return features
def Cells_Evaluation(self,matrix):
"""
Evaluate the neighbooring cells
"""
x=self.x_position
y=self.y_position
iMoore=0
score = np.empty([3, 9])
for i_x_Moore in [-1,0,1] :
for i_y_Moore in [-1,0,1] :
x_eval = np.mod(x+i_x_Moore,matrix.xDim) # eval case 0 if agent in case 50
y_eval = np.mod(y+i_y_Moore,matrix.yDim)
f_i = self.perceive(x_eval,y_eval,matrix) #self.perceive or perceive ?
cell_score = np.dot(f_i,self.weights)
score[0][iMoore]=x_eval #gives the score and the absolute position
score[1][iMoore]=y_eval
score[2][iMoore]=cell_score
iMoore += 1
return score
def Change_Position(self,matrix):
"""
Perform action (i.e. movement) of the agent depending on its evaluations
"""
r=np.random.rand()
if r < 1 - self.epsilon:
score = self.Cells_Evaluation(matrix)
best_score_index = np.argmax(score[2,:]) #select the line with the best score
x_new = score[0, best_score_index]
y_new = score[1, best_score_index]
self.q = score[2, best_score_index]
else :
x_new = (self.x_position + np.random.randint(-1, 2) )%matrix.xDim
y_new = (self.y_position + np.random.randint(-1, 2) )%matrix.yDim
features = self.perceive(x_new, y_new, matrix)
self.q = np.dot(features, self.weights)
new_position = np.array([x_new, y_new])
return new_position
def Aging(self, i):
self.age +=1
self.epsilon = 1/i
if i <= 501:
self.learning_rate = 0.05 - 0.0001*(i - 1)
else:
self.learning_rate = 0
self.lastAte +=1
return
#---------------------------Learning part-------------------------------#
def Get_Reward(self,matrix):
"""
opponent :number of the other species type within the agent’s Moore
neighborhood normalized by the number of total
type is 1 for predator and −1 for prey
same = {0, 1} for if the opponent is on the same location
"""
type_animal = self.ptype
how_many = self.compute_how_many(matrix)
x = self.x_position
y = self.y_position
features = self.perceive(x,y,matrix)
feature_wanted = features[0]
opponent = feature_wanted
same = how_many[1][4]>0
reward = opponent*type_animal + 2*same*type_animal
return reward
def Get_QFunction(self,features):
weights = self.weights
Q = 0
for i in range(len(weights)):
Q = Q + weights[i]*features[i]
return Q
def Update_Weight(self, reward, matrix, Q_value):
weights = self.weights
learning_rate = self.learning_rate
discount_factor = self.discount_factor
#Compute the Q'-table:
Q_prime = []
for i in [-1,0,1]:
for j in [-1,0,1]:
x_target = (self.x_position+i)%matrix.xDim
y_target = (self.y_position+j)%matrix.yDim
features = self.perceive(x_target,y_target,matrix)
Q_prime.append(self.Get_QFunction(features))
#Update the weights:
Q_prime_max = max(Q_prime)
for i in range(0,len(weights)):
if i<3:
c = 9/(matrix.xDim*matrix.yDim)
else:
c=1/9
w = weights[i]
f = features[i]
f = np.exp(-0.5*(f-c)**2)
weights[i] = w + learning_rate*(reward +discount_factor*Q_prime_max - Q_value)*f
self.weights= weights
return
def Eat(self, agentListAtMatrixPos):
for agent in agentListAtMatrixPos:
if type(agent) is Prey: #Not selected randomly at the moment, just eats the first prey in the list
self.lastAte = 0
return agent.ID
return -1
def Starve(self):
if self.lastAte > self.hunger_minimum:
pdeath = self.lastAte*self.death_rate
r = np.random.rand()
if r < pdeath:
return self.ID
return -1
def Reproduce(self):
offspring = 0
if self.age >= self.reproduction_age:
r = np.random.rand()
if r < self.reproduction_rate:
offspring = Predator(self.x_position, self.y_position, -1, 0, self.ID, self.reproduction_age,
self.death_rate, self.reproduction_rate, self.weights, self.learning_rate,
self.discount_factor, self.hunger_minimum) #ID is changed in Grid.update()
return offspring