-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
103 lines (85 loc) · 2.58 KB
/
Main.py
File metadata and controls
103 lines (85 loc) · 2.58 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
# ---------------------------------------------------------------------------
# Author: Jacen Williams
# Assignment: Final Project
# Class: Machine Learning
# Due Data: 5/7/19
# ---------------------------------------------------------------------------
import Game
import math
import pygame as pg
import numpy as np
import matplotlib.pyplot as plt
# simulation variables
initial_generations = 50
initial_cell_count = 200
initial_food_count = 200
food_spawn_rate = 1
max_generation_lifespan = 1000
# physics variables
width = 1600
height = 900
# genetic algorithm variables
crossover_rate = 1
mutation_rate = .001
# neural network variables
num_inputs = 20
num_hidden_neurons = 10
num_outputs = 2
# statistical variables
mean_lifespans = []
lifespans = []
scores = []
mean_scores = []
# init
pg.init()
game = Game.Game(width, height, initial_cell_count, initial_food_count, food_spawn_rate, num_inputs,
num_hidden_neurons, num_outputs, crossover_rate, mutation_rate)
# prerender loop
generation = 0
while generation < initial_generations:
print("Generation: ", generation)
counter = 0
while counter < max_generation_lifespan and not game.get_terminate():
game.update()
counter += 1
generation += 1
lifespans.append([game.get_lifespans()])
mean_lifespans.append(np.mean(game.get_lifespans()))
scores.append([game.get_scores()])
mean_scores.append([np.mean(game.get_scores())])
game.reinitialize()
lifespan_plt_x = []
lifespan_plt_y = []
score_plt_x = []
score_plt_y = []
for i, mean in enumerate(mean_scores):
score_plt_x.append(i)
score_plt_y.append(mean)
# graph generation, uncomment to generate graph of scores
# plt.plot(score_plt_x, score_plt_y)
# plt.show()
screen = pg.display.set_mode((width, height))
clock = pg.time.Clock()
terminate = False
image_num = 0
# render loop
while True:
# framerate limiter
clock.tick(30)
for event in pg.event.get():
if event.type == pg.QUIT:
quit()
if not terminate:
screen.fill((255, 255, 255))
game.update()
terminate = game.get_terminate()
# draw loops
for cell in game.cells:
pg.draw.circle(screen, (0, 255, 0), [math.floor(cell.position.x), math.floor(cell.position.y)], cell.r)
for food in game.food:
pg.draw.circle(screen, (0, 155, 0), [math.floor(food.position.x), math.floor(food.position.y)], food.r)
pg.display.flip()
str_num = "000" + str(image_num)
file_name = "image" + str_num[-4:] + ".png"
pg.image.save(screen, file_name)
image_num += 1