-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_neat_vision.py
45 lines (37 loc) · 1.33 KB
/
game_neat_vision.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
import time
from random import choices
import numpy as np
from game import Game
class GameNeatVision(Game):
"""
This agent sucks ass, do not use it. That is all.
"""
def __init__(self, board: np.array = None, network=None, render=False):
super(GameNeatVision, self).__init__(board=board, network=network, render=render)
def neatstep(self):
activation = self.network.activate(
self.observations().flatten()
)
choice = self._stepfromactivation(activation)
self.current.pos_log.append((choice, tuple(self.current.pos()), self.current.direction))
if self.render:
self.paint(choice, activation)
def neatplay(self, tickdelay=0.1, timeout=60):
self.started_at = time.time()
self.tickdelay = tickdelay
self.timetick()
while (not self.gameover) and time.time() - self.started_at < timeout:
self.neatstep()
return self.fitness
def _stepfromactivation(self, activation):
if not self.ticking:
self.timetick()
if sum(activation) == 0:
activation = [1] * 6
choice = choices(
population=['left', 'right', 'rotate', 'drop', 'nop', 'hold'],
weights=activation,
k=1
)[0]
self.game_step(choice)
return choice