-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSim.py
194 lines (166 loc) · 6.91 KB
/
Sim.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
from Food import food
from Animal import animal
import numpy as np
from Queue import queue
import random
import json
class sim:
"""Class to run the simulation"""
def __init__(self, pop, reps):
self.WIDTH = 20
self.HEIGHT = 20
self.REPETITIONS = reps
self.initial_pop = pop
self.cycles = 0
self.population = 0
self.animals = []
self.board = np.zeros((self.WIDTH, self.HEIGHT), dtype=int)
self.foods = []
self.cycles = 0
self.restricted_spots = {}
self.datasets = []
self.food_amount = 100
def create_food(self, range):
print ("creating food")
restricted_spots = {}
for i in range:
for food_item in self.foods:
restricted_spots[food_item.x] = food_item.y
for animal in self.animals:
restricted_spots[animal.x] = animal.y
x = random.randint(0, self.WIDTH - 2)
y = random.randint(0, self.HEIGHT - 2)
for j in restricted_spots:
if x == j:
x += 1
if y == restricted_spots[j]:
y += 1
if x > self.WIDTH - 1:
x = 1
if y > self.HEIGHT - 1:
y = 1
self.foods.append(food(x, y))
print ("done")
def create_initial_population(self, pop):
restricted_spots = {}
for i in pop:
x = random.randint(0, self.WIDTH - 1)
y = random.randint(0, self.HEIGHT - 1)
for j in restricted_spots:
if x == j:
x += 1
if y == restricted_spots[j]:
y += 1
restricted_spots[x] = y
speed = random.randint(1, 10)
range = random.randint(5, 10)
self.animals.append(animal(x, y, range, speed))
def get_restricted_spots(self):
self.restricted_spots = {}
for food_item in self.foods:
self.restricted_spots[food_item.x] = food_item.y
for animal in self.animals:
self.restricted_spots[animal.x] = animal.y
def run(self):
#Create initial assets for simulation
self.create_initial_population(range(self.initial_pop))
self.create_food(range(self.food_amount))
## Repeat the simulation for the number of cycles specified ##
while self.cycles < self.REPETITIONS:
print (f"📘📘📘Population: {len(self.animals)}")
print (f"📘📘📘Food: {len(self.foods)}")
#If there are no more animals, end the simulation
if len(self.animals) < 1:
break
####################Create the board###########################
self.board = np.zeros((self.WIDTH, self.HEIGHT), dtype=int)
if len(self.foods) < self.food_amount - 10:
food_needed = range(self.food_amount - 1 - len(self.foods))
self.create_food(food_needed)
for food in self.foods:
self.board[food.x][food.y] = 1
#If an animal has not found food,
#Get animals to find new food when food is added
new_animal_array = []
for animal in self.animals:
if not animal.food_near:
animal.find_food(self.board)
for food in self.foods:
self.board[food.x][food.y] = 1
food = 0
###############################################################
new_animal_array = []
###Using the animals###
for animal in self.animals:
###Find food###
if animal.searching_for_food:
print ("📔📔📔Searching for food")
animal.find_food(self.board)
if animal.food_near != []:
animal.find_best_path(animal.food_near[0], self.WIDTH, self.HEIGHT)
else:
animal.move_queue = queue()
for i in range(animal.speed):
animal.move_queue.add(random.choice(["L", "R", "U", "D"]))
animal.searching_for_food = True
animal.move_to_food()
else:
if animal.move_queue.get_q() == []:
animal.searching_for_food = True
#Get method to return index for food, then pop in main.py
self.foods, animal.searching_for_food = animal.eat(self.foods)
animal.move_to_food()
if animal.hunger > 20:
animal.hunger = int(animal.hunger / 2)
self.get_restricted_spots()
new_animals = animal.reproduce(self.restricted_spots, self.WIDTH, len(self.animals))
print ("reproduce")
for new_animal in new_animals:
new_animal.find_food(self.board)
new_animal_array.append(new_animal)
animal.hunger -= 1
new_animal_array.append(animal)
if animal.hunger <= 0:
print (f"📕📕📕Rip {animal}")
new_animal_array.pop(new_animal_array.index(animal))
############################################################################
self.animals = new_animal_array
self.cycles += 1
print (self.cycles)
## Create a new dataset for the cycle ##
data = {}
animals_data = {}
## Set all the data categories to be an empty array ##
animals_data["speed"] = []
animals_data["range"] = []
animals_data["info"] = []
## Record the data of each animal ##
for animal in self.animals:
animals_data["speed"].append(animal.speed)
animals_data["range"].append(animal.animal_range)
animal_info = {
"x": animal.x,
"y": animal.y,
"hunger": animal.hunger
}
animals_data["info"].append(animal_info)
## Record the data of each food ##
food_data = []
for food in self.foods:
food = {
"x": food.x,
"y": food.y
}
food_data.append(food)
## Record other data points ##
data["cycle"] = self.cycles
data["pop"] = len(self.animals)
data["animals"] = animals_data
data["food_data"] = food_data
## Add the cycle's dataset to the simulation's dataset ##
self.datasets.append(data)
if __name__ == '__main__':
s = sim(20, 200)
s.run()
#with open("data.json", "w") as f:
#json.dump(s.datasets, f)