-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactions_module.py
More file actions
138 lines (104 loc) · 4.95 KB
/
actions_module.py
File metadata and controls
138 lines (104 loc) · 4.95 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
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
from cells_classes_module import *
from in_out_module import *
import numpy as np
ind = None
def probability_to_multiply():
"""Function returns float from (0, 1) """
return random.uniform(0, 1)
def change_age_step(list_cell, status):
""":arg : list_cell - type : list. Contains all elements with type 'Cell'
status - type : int. Status of the button
Function changes age step.
"""
if status == 1:
delta = 0.001
else:
delta = -0.001
for i in range(len(list_cell)):
list_cell[i].age_step += delta
def change_multiply_skill(list_cells, status):
""":arg : list_cell - type : list. Contains all elements with type 'Cell'
status - type : int. Status of the button
Function changes multiply skill.
"""
if status == 1:
delta = 0.02
else:
delta = -0.02
for i in range(len(list_cells)):
list_cells[i].multiply_skill += delta
def kill_the_cell(list_cell, cell, time):
""":arg: list_cell - type : list. Contains all elements with type 'Cell'
cell - type : Cell. A cell that was killed
time - type : float. Moment of the time when the cell was killed
Function removes cell from the list_cell and writes data about it
"""
if cell in list_cell:
list_cell.remove(cell) # A cell removed from the list
def born_the_cell(list_cell, cell, time):
""":arg: list_cell - type : list. Contains all elements with type 'Cell'
cell - type : Cell. A cell that was born
time - type : float. Moment of the time when the cell was born
Function adds cell to the list_cell and writes data about it
"""
list_cell.append(cell) # A cell added to the list
def multiply(list_cells, time, parameters):
""":arg: list_cells - type : list. Contains all elements with type 'Cell'
time - type : float. How much time passed since the beginning of the simulation
Function adds new cell (cells multiply) and kills cell if it is too old or too hungry
"""
for cell in list_cells:
p = probability_to_multiply() # The chance of a cell to multiply
if cell.age >= 100 or cell.satiety <= 0: # A cell dies if it is too old or too hungry
kill_the_cell(list_cells, cell, time)
if (p > cell.multiply_skill
# Cells have to be if reproductive age to multiply :
and cell.reproductive_age[0] <= cell.age <= cell.reproductive_age[1]
# Cells could not multiply each moment of time :
and cell.age - cell.age_of_last_multiplication > cell.reproductive_waiting
# Cells have to have a lot of satiety to multiply :
and cell.satiety >= 0.5):
new_cell = cell.multiply(list_cells, parameters) # new_cell = Cell() or 0
# Born the cell :
if new_cell != 0:
born_the_cell(list_cells, new_cell, time)
def update(list_cells, meal_list, time):
""":arg: list_cells - type : list. Contains all elements with type 'Cell'
meal_list - type : list. Contains all elements with type 'Meal'
time - type : float. The moment of time when update is called
Updates of cell's and meal's positions on the screen;
kills the peaceful cells if they are attacked by predator cell;
adds age of cell and gets lower satiety of cell
"""
victim_list = [cell for cell in list_cells if not cell.predator] # List of the peaceful cells
for cell in list_cells:
cell.calc_forces(meal_list, list_cells) # Calculates forces
cell.update() # Updates a position of the cells
# Is a peaceful cell was eaten by a predator :
if cell.predator:
if len(victim_list) != 0:
for victim in victim_list:
# They are have to be close to each other :
if vec_module(find_vector(cell, victim)) <= victim.size:
# Kills a cell :
kill_the_cell(list_cells, victim, time)
# Adds satiety :
cell.satiety += victim.richness
# Satiety have to be from 0 to 1 :
cell.satiety = min(cell.satiety, 1)
cell.age += cell.age_step
cell.satiety -= cell.satiety_step
# It checks was meal eaten by peaceful cell or not:
for meal in meal_list:
meal_eaten = False # Special that responsible for eaten meal
for cell in list_cells:
if meal.eaten(cell):
if not cell.predator: # Meal could be eaten only by peaceful cell
cell.satiety += meal.richness
cell.satiety = min(cell.satiety, 1)
# Meal removed from the list when it is eaten :
meal_list.remove(meal)
meal_eaten = True
break
if meal_eaten:
break