-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·163 lines (122 loc) · 4.64 KB
/
main.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
#!/usr/bin/env python3
# Many thanks to this guy:
# http://www.akeric.com/blog/?p=1527
import sys
import random
import copy
import pyglet
from pyglet.window import key
from vector import Vector
from ant import Ant
from food import Food
import ga
from chromosome import Chromosome
class SpriteWindow(pyglet.window.Window):
def __init__(self, size, caption="Pyglet game", fps=60):
super(SpriteWindow, self).__init__(width=size[0], height=size[1],
caption=caption)
self.width = size[0]
self.height = size[1]
# Set the background color
self.setBackgroundColor()
self.sprite_batch = pyglet.graphics.Batch()
self.ants = []
self.food = []
self.num_food = 20
self.num_ants = 25
self.growFood()
self.step = 0
self.generations = 0
self.population_change_steps = 1000
pyglet.clock.schedule_interval(self.update, 1.0/fps)
self.ga = ga.RealGeneticAlg(perturbation_bounds = (0.01, 0.3),
crossover_rate = 0.6,
mutation_rate = 0.2,
elite = 6
)
@staticmethod
def setBackgroundColor(color=(0.9, 0.9, 0.9, 1)):
if len(color) != 4:
raise ValueError("Color must be of length 4!")
pyglet.gl.glClearColor(*color)
@staticmethod
def run():
pyglet.app.run()
def on_draw(self):
self.clear()
self.sprite_batch.draw()
def on_text(self, symbol):
if symbol == "s":
print("Saving weights")
with open("net_weights.bak", "w") as f:
for a in self.ants:
f.write(",".join(map(str, a.neural_net.GetWeights())) + "\n")
if symbol == "l":
print("Loading weights")
with open("net_weights.bak") as f:
for ant in self.ants:
ant.neural_net.SetWeights(
[float(v) for v in f.readline().split(",")])
if symbol == "f":
self.fastMode(2)
if symbol == "v":
self.fastMode(10)
def fastMode(self, generations = 1):
stop_generation = self.generations + generations
while self.generations < stop_generation:
self.update(1)
# dt is "delta time" since last update
def update(self, dt):
self.step += 1
for ant in self.ants:
ant.update(0.5, self.food)
for food in self.food:
if not food.eaten and ant.collidesWith(food):
food.eaten = True
ant.score += 1
self.food = [f for f in self.food if not f.eaten]
self.growFood()
if self.step > self.population_change_steps:
self.generations += 1
self.printStats()
self.step = 0
self.updateNets(self.ga.NewPopulation(self.chromosomesFromAnts()))
def printStats(self):
avg_score = 0
max_score = 0
min_score = None
for a in self.ants:
avg_score += a.score
if a.score > max_score:
max_score = a.score
if min_score == None or a.score < min_score:
min_score = a.score
print("{:3d} {:3d} {:3d} {:.4f}".format(
self.generations, min_score, max_score, avg_score / self.num_ants))
def chromosomesFromAnts(self):
return [Chromosome(a.neural_net.GetWeights(), a.score) for a in self.ants]
def updateNets(self, chromosomes):
for i in range(self.num_ants):
self.ants[i].score = 0
#self.ants[i].updateImage(chromosomes[i].is_top_performer)
self.ants[i].neural_net.SetWeights(chromosomes[i])
def getRandomPoint(self, margin = 0):
return (random.randrange(margin ,self.width - margin),
random.randrange(margin, self.height - margin))
def growFood(self):
while len(self.food) < self.num_food:
#point = (random.randrange(0,100), random.randrange(0,100))
point = self.getRandomPoint(10)
self.food.append(Food(self, point, self.sprite_batch))
def initAnts(self):
for i in range(self.num_ants):
a = random.randrange(0, 359)
self.ants.append(Ant(self, self.getRandomPoint(),
Vector(20, a), self.sprite_batch))
if __name__ == "__main__":
import cProfile
window = SpriteWindow((400,400), fps = 240)
window.initAnts()
#cProfile.run("window.update(1, 20)")
#window.fastMode(10)
sys.exit(window.run())