-
Notifications
You must be signed in to change notification settings - Fork 0
/
Biotope.py
executable file
·391 lines (345 loc) · 14.3 KB
/
Biotope.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
from basic_tools import Matrix, is_number
from random import shuffle, choice
from math import sqrt
from basic_tools import is_function
from basic_tools import print_methods_names
class Feature(object): # A float variable
def __init__(self, feature_name, feature_settings, parent_ecosystem):
self.feature_name = feature_name
self.parent_ecosystem = parent_ecosystem
functions_dict = parent_ecosystem.function_maker\
.turn_settings_into_functions(
feature_settings,
caller='#ecosystem'
)
self.current_value = functions_dict['initial value'](
self.parent_ecosystem)
try:
self.calculate_value_after_update = functions_dict[
# we take the function without calling it
'value after updating']
try:
self.update_once_every = functions_dict[
'update once every'](self.parent_ecosystem)
except Exception:
self.update_once_every = 1
self.time_of_next_update = \
self.parent_ecosystem.time + self.update_once_every
except Exception:
pass
def update(self): # or def evolve(self):
# print 'Updating', self.feature_name # ***
if (
hasattr(self, 'time_of_next_update') and
self.parent_ecosystem.time > self.time_of_next_update
):
self.current_value = self.calculate_value_after_update(
self.parent_ecosystem)
self.time_of_next_update += self.update_once_every
def get_value(self):
return self.current_value
def set_value(self, new_value):
self.current_value = new_value
return new_value
def modify_proportionally(self, proportion):
value = self.get_value()
increment = proportion * value
self.set_value(value + increment)
return increment
def modify(self, increment):
value = self.get_value() + increment
self.set_value(value)
return increment
class FeatureMap(object): # A function f(x, y)
def __init__(self, feature_name, feature_settings, parent_ecosystem):
self.feature_name = feature_name
self.parent_ecosystem = parent_ecosystem
functions_dict = parent_ecosystem.function_maker.\
turn_settings_into_functions(
feature_settings,
caller='#ecosystem'
)
(self.size_x, self.size_y) = feature_settings['matrix size']
(size_x, size_y) = (self.size_x, self.size_y)
self.current_value = Matrix(size_x, size_y)
for i in range(size_x):
for j in range(size_y):
self.current_value[i, j] = \
functions_dict['initial value'](
self.parent_ecosystem,
float(i) / size_x,
float(j) / size_y)
try:
self.calculate_value_after_update = functions_dict[
'value after updating'
] # we take the function without calling it
try:
self.update_once_every = functions_dict['update once every'](
self.parent_ecosystem)
except Exception:
self.update_once_every = 1
self.time_of_next_update = (
self.parent_ecosystem.time + self.update_once_every)
except Exception:
pass
def update(self): # or def evolve(self):
# print 'Updating', self.feature_name # ***
if (
hasattr(self, 'time_of_next_update') and
self.parent_ecosystem.time > self.time_of_next_update
):
new_value = Matrix(self.size_x, self.size_y)
for i in range(self.size_x):
for j in range(self.size_y):
new_value[i, j] = \
self.calculate_value_after_update(
self.parent_ecosystem,
float(i) / self.size_x,
float(j) / self.size_y)
self.current_value = new_value
self.time_of_next_update += self.update_once_every
def get_value(self, x, y):
# PRECONDITION: 0 <= x <= 1, 0 <= y <= 1
for n in (x, y, self.size_x, self.size_y):
if not is_number(n):
print n, 'is not a FLOAT!!!' # ***
exit()
return self.current_value[
int(round(x * self.size_x)),
int(round(y * self.size_y))
]
def set_value(self, x, y, new_value):
# PRECONDITION: 0 <= x <= 1, 0 <= y <= 1
self.current_value[
int(round(x * self.size_x)),
int(round(y * self.size_y))
] = new_value
return new_value
def modify_proportionally(self, x, y, proportion):
# PRECONDITION: 0 <= x <= 1, 0 <= y <= 1
value = self.get_value(x, y)
increment = proportion * value
self.set_value(x, y, value + increment)
return increment
def modify(self, x, y, increment):
# PRECONDITION: 0 <= x <= 1, 0 <= y <= 1
value = self.get_value(x, y) + increment
self.set_value(x, y, value)
return increment
class Biotope(object):
class random_free_locations_list(object):
# warning: this is not the __init__ method of Biotope class!
def __init__(self, parent_biotope):
self.parent_biotope = parent_biotope
self.reset()
def reset(self):
size = self.parent_biotope['size']
self.list = [(x, y) for x in range(size[0])
for y in range(size[1])]
shuffle(self.list)
self.last_location_index = len(self.list) - 1
def get_new_free_location(self):
if len(self.list) == 0:
return None
i = (self.last_location_index + 1) % len(self.list)
while (
self.parent_biotope.organisms_matrix[self.list[i]] is not None
and i != self.last_location_index
):
i = (i + 1) % len(self.list)
if self.parent_biotope.organisms_matrix[self.list[i]] is None:
self.last_location_index = i
return self.list[i]
else:
# Error: Full biotope. There're no more free locations
return None
def __init__(self, settings, parent_ecosystem):
self.settings = settings
self.parent_ecosystem = parent_ecosystem
self.organisms_matrix = Matrix(*self.settings['size'])
self.initialize_biotope_features()
self.random_free_locations = self.random_free_locations_list(self)
# The 'distance' between two points A and B is subjective. Depends on
# the topology of the biotope (currently it's a flat torus) and the
# metric we use (euclidean, chess, taxicab,...). So, we define:
try:
self.set_distance(self.settings['distance'])
except Exception:
self.set_distance('euclidean distance')
def __getitem__(self, keys):
return self.settings[keys]
def __setitem__(self, keys, value):
self.settings[keys] = value
def __str__(self):
return str(self.organisms_matrix)
def size_x(self):
return self.settings['size'][0]
def size_y(self):
return self.settings['size'][1]
def print_matrix(self):
for y in range(self.size_y()):
print [
0
if self.organisms_matrix[x, y] is None
else 1 for x in range(self.size_x())
]
def add_feature(self, feature_name, feature_settings):
# print 'add_feature:', feature_name # ***
self.biotope_features[feature_name] = Feature(
feature_name, feature_settings, self.parent_ecosystem)
def add_feature_map(self, feature_name, feature_settings):
# print 'add_feature_map:', feature_name # ***
self.biotope_features[feature_name] = FeatureMap(
feature_name, feature_settings, self.parent_ecosystem)
def initialize_biotope_features(self):
"""
We don't need to initialize features in a particular
order. That's why we don't initialize them the same way as genes or
new operators. Features can refer to each other and can even refer to
themselves, because we built feature operators (such as '#biotope
seasons speed' or 'extract #biotope nutrient A') before initializing
features themselves. We built them out of features' names.
"""
if print_methods_names:
print 'Biotope.py: initialize_biotope_features' # ***
self.biotope_features = {}
if 'biotope features' in self.settings:
for feature_name in self.settings['biotope features']:
if 'matrix size' in self.settings[
'biotope features'][
feature_name
]:
self.add_feature_map(
feature_name,
self.settings['biotope features'][feature_name]
)
else:
self.add_feature(
feature_name,
self.settings['biotope features'][feature_name])
if print_methods_names:
print 'initialize_biotope_features done!!' # ***
def get_organism(self, location):
return self.organisms_matrix[location]
def add_organism(self, organism, location='find location'):
if location == 'find location':
try:
location = organism['location']
except Exception:
location = self.seek_free_location()
if location is not None and self.organisms_matrix[location] is None:
# this way we assure that everything is in its place
organism['location'] = location
self.organisms_matrix[location] = organism
# print "in", location, "there is", organism # ***
return 'success'
else:
return 'fail'
def move_organism(self, old_location, new_location):
if old_location != new_location:
self.organisms_matrix[
new_location] = self.organisms_matrix[old_location]
self.organisms_matrix[old_location] = None
def delete_organism(self, location):
if location:
self.organisms_matrix[location] = None
def seek_free_location(self):
"""
This method return a random free position
(None if not possible)
"""
return self.random_free_locations.get_new_free_location()
def list_of_locations_close_to(
self,
center,
radius,
condition=lambda x: (x is None)
):
(xc, yc) = center
# borders of a square around center (xc, yc):
left = int(round(xc - radius))
# we write + 1 because range(a, b+1) = [a, a+1, a+1, ..., b] = [a, ...,
# b]
right = int(round(xc + radius)) + 1
up = int(round(yc - radius))
# we write + 1 because range(a, b+1) = [a, a+1, a+1, ..., b] = [a, ...,
# b]
down = int(round(yc + radius)) + 1
return [
(x, y)
for x in range(left, right)
for y in range(up, down)
if (
condition(self.organisms_matrix[x, y]) and
self.distance(center, (x, y)) <= radius
)
]
def seek_free_location_close_to(self, center, radius):
"""
This method return a random free position close to a center within
a radius (None if not possible)
"""
list_of_free_locations = self.list_of_locations_close_to(
center,
radius,
lambda x: (x is None)
)
if list_of_free_locations == []:
return None
else:
(x, y) = choice(list_of_free_locations)
return (x % self['size'][0], y % self['size'][1])
def seek_organism_close_to(self, center, radius, condition=None):
def default_condition(organism):
return (organism is not None) and (organism['location'] != center)
if condition is None:
condition = default_condition
list_of_locations = self.list_of_locations_close_to(
center, radius, condition)
if list_of_locations == []:
return None
else:
(x, y) = choice(list_of_locations)
return (x % self['size'][0], y % self['size'][1])
def calculate_distance(self, A, B, distance='euclidean distance'):
"""
Gives the distance from the location A to the location B, taking
into account that coordinates are taken (x % size_x, y % size_y)
and, thus, the location (size_x, size_y) is equivalent to (0, 0),
so the distance between the locations (0, 0) and
(size_x - 1, size_y - 1) is really small
"""
if (
hasattr(A, '__iter__') and
hasattr(B, '__iter__') and
len(A) == 2 and
len(B) == 2
):
size_x, size_y = self['size']
Ax, Ay, Bx, By = A[0] % size_x, A[
1] % size_y, B[0] % size_x, B[1] % size_y
dif_x = min(abs(Bx - Ax), size_x - abs(Bx - Ax))
dif_y = min(abs(By - Ay), size_y - abs(By - Ay))
if distance in {'square', 'chess', 'chess distance'}:
return max(dif_x, dif_y)
elif distance in {'circle', 'euclidean', 'euclidean distance'}:
return sqrt(dif_x**2 + dif_y**2)
elif distance in {
'tilted square',
'taxicab',
'taxist',
'taxist distance',
'taxicab distance'
}:
return dif_x + dif_y
else:
return None
def set_distance(self, distance):
if is_function(distance):
self.distance = distance
elif isinstance(distance, str):
self.distance = lambda A, B: self.calculate_distance(
A, B, distance)
def evolve(self):
for feature in self.biotope_features:
self.biotope_features[feature].update()