-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
54 lines (31 loc) · 1.04 KB
/
run.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
from Cannon import Cannon
from Projectile import Projectile
from Physics import Physics
import numpy as np
from Visualiser import Visualiser
from Castle import Castle
from Genetics import generateInitialPopulation, evolve
def score(cannon):
global ball, physics, castle
projectile = ball.copy()
cannon.shoot(projectile)
rel_pos = []
while projectile.pos[1] >= castle.pos[1]:
physics.timestep(projectile)
rel_pos.append(physics.distance(projectile, castle))
scores = (2 - np.array(rel_pos)) / 2
return max(scores) if len(rel_pos) > 0 else 0.0
ball = Projectile(2.0)
impulse = np.array([0.08, 0.05])
castle = Castle()
physics = Physics(0.001)
cannons = generateInitialPopulation(100)
for i in range(100):
scores = []
for cannon in cannons:
scores.append(score(cannon))
if i % 10 == 0:
a = Visualiser(np.random.choice(cannons), ball.copy(), castle, physics)
a.run()
print("Min score for generation %i is %f" % (i, min(scores)))
cannons = evolve(cannons, score)