-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environment.py
30 lines (25 loc) · 936 Bytes
/
Environment.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
from typing import *
from Population import *
from main import CLOCK
class Environment:
def __init__(self, no_populations: int, pop_size: int):
self.no_generations = 0
self.best_snake_len = 4
self.populations = List[Population]
self.populations = [Population(pop_size) for _ in range(no_populations)]
def is_pop_extinct(self):
for pop in self.populations:
if not pop.is_extinct():
return False
return True
def update(self):
for pop in self.populations:
pop.update_alive()
self.best_snake_len = pop.best_snake_len
def run_genetic(self):
# Runs when whole population is dead
for pop in self.populations:
pop.clear_snakes()
pop.calc_score() # calculate fitness for each snake in each population
pop.do_natural_selection()
self.no_generations += 1