-
Notifications
You must be signed in to change notification settings - Fork 0
/
animals.py
338 lines (291 loc) · 11.4 KB
/
animals.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
# Import
from random import randint, random
from constants import *
from visual import *
from textures import *
import Image
from time import sleep
# Functions
def dna_to_int(dna):
return int("".join(str(i) for i in dna), 2)
def random_dna():
dna = []
for i in range(4):
dna.append(randint(0,1))
return dna
def random_pos():
return [randint(0,SIZE_AFRICA-1), randint(0,SIZE_AFRICA-1)]
def is_neighbour(ani1, ani2):
if abs(ani1.pos[0] - ani2.pos[0]) <= 2 and \
abs(ani1.pos[1] - ani2.pos[1]) <= 2:
return True
else:
return False
##############################################################################
# Classes
class Animal:
def __init__(self, dna, pos):
self.animal_name =''
self.dna = dna
self.pos = pos
self.nrj_max = 250 + randint(-50,50)
self.nrj = self.nrj_max/2
self.life_expect = 1000 + randint(-200,200) # In number of iterations
self.desire = randint(20,50)
dnaint = dna_to_int(self.dna)
self.speed = dnaint + 1
self.vision = dnaint*4 + 1
self.food_eaten = dnaint/2 + 1
self.nrj_consum = float(dnaint)/4 + 1
self.waste_level = 0
# Graphic model
self.model = box(pos=(self.pos[0]*VCOEFF - 5, self.pos[1]*VCOEFF -5, 10),
length=8, height = 8, width = 8)
def alive(self):
return self.nrj > 0
def reproduct(self, pop):
libido = self.desire
child_dna = None
if self.nrj < self.food_eaten*4:
libido /= 2
# TODO penalty on libido if tiger
for ani in pop:
if ani.id != self.id:
if not ani.is_dead():
if is_neighbour(self, ani):
libido += ani.desire
libido -= abs(dna_to_int(self.dna) - dna_to_int(ani.dna))*4
if libido > 100:
self.desire = 0
ani.desire = 0
child_dna = self.crossover(ani)
break
if child_dna !=None:
print self.animal_name, self.id,' DNA ',self.dna,' and ',ani.animal_name, ani.id,'DNA', ani.dna,'had a child!'
return child_dna
def crossover(self, animal2):
dim = len(self.dna)
new_dna = []
fixed_point = randint(0,dim-1)
for i in range(dim):
if i < fixed_point:
new_dna.append(self.dna[i])
else:
new_dna.append(animal2.dna[i])
return new_dna
def move_model(self, x, y):
for i in range(VCOEFF):
self.model.pos=(self.pos[0]+x, self.pos[1]+y, 5)
def live(self):
self.nrj -= self.nrj_consum
self.nrj = min(self.nrj_max, self.nrj)
self.life_expect -= 1
if self.nrj <= 0 or self.life_expect <= 0:
self.die()
def make_waste(self, matrix_waste, waste_models):
if (random.randint(1,35) == 1) and self.waste_level > 0 :
if (self.pos[0], self.pos[1]) in waste_models.keys():
waste_models[(self.pos[0], self.pos[1])].axis += (0, 0, self.waste_level/WASTE_SCALE)
else:
waste = cone(pos=(self.pos[0]*VCOEFF, self.pos[1]*VCOEFF, 0),
axis=(0,0,self.waste_level/WASTE_SCALE), radius = 5,
color = color.magenta)
waste_models[(self.pos[0], self.pos[1])] = waste
matrix_waste[self.pos[0]][self.pos[1]] += self.waste_level
self.waste_level = 0
def is_dead(self):
return self.speed <= 0
def disappear(self):
self.model.visible = False
del self.model
##############################################################################
class Zebra(Animal):
class_counter = 0
def __init__(self,dna,pos):
Animal.__init__(self,dna,pos)
self.animal_name = 'Zebra'
dna = dna_to_int(dna)
self.death = False
self.death_by_tiger = False
self.model.color = color.white
self.id = Zebra.class_counter
Zebra.class_counter +=1
self.model.material=textures_zebra[dna]
print 'Zebra', self.id, 'DNA', self.dna, 'appeared !'
def move(self, mat, matwaste, popzebras, poptigers):
# Dirty hack, die if near tiger
for tig in poptigers:
if tig.pos == self.pos and random.random() > 0.5:
self.die()
print 'Tiger ', tig.id, ' just killed Zebra', self.id
# Look for point with most food
best_point = [-1,-1]
best_point_quality = 0
for i in range(self.pos[0]-self.vision, self.pos[0]+self.vision):
for j in range(self.pos[1]-self.vision, self.pos[1]+self.vision):
if i >= 0 and i < SIZE_AFRICA and \
j >= 0 and j < SIZE_AFRICA:
current_quality = mat[i][j]
# Lower quality if tiger nearby
# for tig in poptigers:
# if abs(tig.pos[0] - i <= 2) or \
# abs(tig.pos[1] - j <= 2):
# current_quality -= 20
# Lower quality if waste nearby
waste_near = 0
for ii in range(i-2, i+2):
for jj in range(j-2, j+2):
if ii >= 0 and ii < SIZE_AFRICA and \
jj >= 0 and jj < SIZE_AFRICA:
waste_near += matwaste[ii][jj]
current_quality -= waste_near
if current_quality > best_point_quality:
best_point = [i,j]
best_point_quality = current_quality
# Move toward this point
# TODO flee tiger
# TODO flee waste
if best_point == [-1,-1]:
# Random move
best_point= [randint(0,SIZE_AFRICA), randint(0,SIZE_AFRICA)]
moved = 0
while moved <= self.speed:
if self.pos[0] < best_point[0]:
self.move_model(+1, 0)
self.pos[0] += 1
if self.pos[0] > best_point[0]:
self.move_model(-1, 0)
self.pos[0] -= 1
if self.pos[1] < best_point[1]:
self.move_model(0, +1)
self.pos[1] += 1
if self.pos[1] > best_point[1]:
self.move_model(0, -1)
self.pos[1] -= 1
self.model.pos=(self.pos[0]*VCOEFF, self.pos[1]*VCOEFF, 5)
moved += 1
# Stay in an empty space
while self.is_alone(popzebras) == False:
movx = randint(-1,1) # Random move between -1, 0, or 1
movy = randint(-1,1)
self.pos[0] += movx
self.pos[1] += movy
self.move_model(movx, movy)
self.model.pos=(self.pos[0]*VCOEFF, self.pos[1]*VCOEFF, 5)
def is_alone(self, popzebras):
samepos = 0
for zeb in popzebras:
if self.pos == zeb.pos:
samepos +=1
return True if samepos == 1 else False
def eat(self, mat_food, mat_waste, dict_resources):
i, j = self.pos[0], self.pos[1]
if i >= 0 and i < SIZE_AFRICA and \
j >= 0 and j < SIZE_AFRICA:
if mat_food[i][j] > 0:
eaten = min(mat_food[i][j], self.food_eaten)
mat_food[i][j] -= eaten*3
self.waste_level += eaten
self.nrj += eaten
dict_resources[(i, j)].axis -= (0,0,eaten)
if i >= 0 and i < SIZE_AFRICA and \
j >= 0 and j < SIZE_AFRICA:
if mat_food[i][j] <= 0:
mat_food[i][j] = 0
dict_resources[(i, j)].visible = False
del dict_resources[(i, j)]
return True
else:
return False
def die(self):
# When a zebra dies, it stops moving but stays as food for tigers
if not self.death :
print 'Snif snif, Zebra ', self.id, ' died.'
self.death= True
self.speed = -1
# self.model.material = None
# self.model.color = color.black
self.nrj = 0
def clean(self):
# TODO find better name
if self.nrj <= 0:
self.disappear()
return True
##############################################################################
class Tiger(Animal):
class_counter=0
def __init__(self,dna,pos):
Animal.__init__(self,dna,pos)
self.animal_name = 'Tiger'
dna = dna_to_int(dna)
self.model.color = color.orange
self.id = Tiger.class_counter
Tiger.class_counter += 1
self.model.material=textures_tiger[dna]
print 'Tiger', self.id, 'DNA', self.dna, 'appeared !'
def move(self, popzebras, poptigers):
# Look for point with most food
best_point = [-1,-1]
for i in range(self.pos[0]-self.vision, self.pos[0]+self.vision):
for j in range(self.pos[1]-self.vision, self.pos[1]+self.vision):
for zeb in popzebras:
if [i, j] == zeb.pos:
best_point = [i,j]
break
# Move toward this point
# TODO if several zebras in sigth, choose one randomly (or closest)
if best_point == [-1,-1]:
# Random move
best_point= [randint(0,SIZE_AFRICA), randint(0,SIZE_AFRICA)]
moved = 0
while moved <= self.speed:
if self.pos[0] < best_point[0]:
self.move_model(1, 0)
self.pos[0] += 1
if self.pos[0] > best_point[0]:
self.move_model(-1, 0)
self.pos[0] -= 1
if self.pos[1] < best_point[1]:
self.move_model(0, 1)
self.pos[1] += 1
if self.pos[1] > best_point[1]:
self.move_model(0, -1)
self.pos[1] -= 1
self.model.pos=(self.pos[0]*VCOEFF, self.pos[1]*VCOEFF, 5)
moved += 1
# Stay in an empty space
while self.is_alone(poptigers) == False:
movx = randint(-1,1) # Random move between -1, 0, or 1
movy = randint(-1,1)
self.pos[0] += movx
self.pos[1] += movy
self.move_model(movx, movy)
self.model.pos=(self.pos[0]*VCOEFF, self.pos[1]*VCOEFF, 5)
def is_alone(self, poptigers):
samepos = 0
for tig in poptigers:
if self.pos == tig.pos:
samepos +=1
return True if samepos == 1 else False
def eat(self, popzebras):
for zeb in popzebras:
# if self.pos == zeb.pos:
if abs(self.pos[0] - zeb.pos[0]) <= 2 and \
abs(self.pos[1] - zeb.pos[1]) <= 2:
zeb.die()
if not zeb.death_by_tiger:
zeb.death_by_tiger = True
print 'Tiger ', self.id, ' just killed Zebra', zeb.id
eaten = min(self.food_eaten, zeb.nrj)
self.waste_level += eaten
zeb.nrj -= eaten
self.nrj += eaten
return True
break;
return False
def die(self):
# TODO When a tiger dies, transform in waste
print 'Snif snif, Tiger ', self.id, ' died.'
self.speed = -1
self.nrj = -1
self.disappear()