forked from benjamin22-314/evolving-simple-organisms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate.py
More file actions
83 lines (59 loc) · 2.69 KB
/
simulate.py
File metadata and controls
83 lines (59 loc) · 2.69 KB
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
from numpy.random import shuffle
import plotting
import nn_maths_functions
from make_gif import make_gif
from tqdm import tqdm
def simulate(settings, organisms, foods, gen):
total_time_steps = settings['time_steps']
# --- CYCLE THROUGH EACH TIME STEP ---------------------+
for t_step in tqdm(range(0, total_time_steps, 1)):
# SHUFFLE ORGANISMS (works inplace)
# I don't want to favour the orgs at the beginning of the list
shuffle(organisms)
# PLOT SIMULATION FRAME
plot_final_generation = (settings['plot'] is True) and (gen == settings['gens']-1)
plot_this_generation = (settings['plot'] is True) and (gen in settings['plot_generations'])
if plot_final_generation or plot_this_generation:
plotting.plot_frame(settings, organisms, foods, gen, t_step)
# FOR EACH ORGANISM
for org1 in organisms:
# find closest food
closest_dist_so_far = 10000
# get the closest food
for food in foods:
food_org_dist = nn_maths_functions.dist_to_food(org1, food)
# update closest food if necessary
# if food_org_dist < org1.d_food:
if food_org_dist < closest_dist_so_far:
# org1.d_food = food_org_dist
closest_dist_so_far = food_org_dist
org1.x_distance_to_food, org1.y_distance_to_food = nn_maths_functions.xy_dist_to_food(org1, food)
# UPDATE FITNESS FUNCTION
if food_org_dist <= 0.075:
org1.fitness += food.energy
# SET FOOD AS 'EATEN'
food.energy = 0
org1.d_food = 10000
# find closest org
closest_dist_so_far = 10000
# get the closest neighbour
for org2 in organisms:
if org1 is org2:
pass
org_org_dist = nn_maths_functions.dist_to_neighbour(org1, org2)
# update closest food if necessary
if org_org_dist < closest_dist_so_far:
closest_dist_so_far = org_org_dist
org1.x_distance_to_neighbour, org1.y_distance_to_neighbour = nn_maths_functions.xy_dist_to_neighbour(org1, org2)
# UPDATE FITNESS FUNCTION
if org_org_dist <= 0.10:
org1.fitness -= 0.05 #
# GET FOOD RESPONSE
for food in foods:
food.respawn(settings)
# GET ORGANISM RESPONSE
for org in organisms:
org.think()
if plot_final_generation or plot_this_generation:
make_gif(settings, gen)
return organisms