-
Notifications
You must be signed in to change notification settings - Fork 33
/
pole_run.py
38 lines (27 loc) · 977 Bytes
/
pole_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
import logging
import gym
import torch
import neat.population as pop
import neat.experiments.pole_balancing.config as c
from neat.visualize import draw_net
from neat.phenotype.feed_forward import FeedForwardNet
logger = logging.getLogger(__name__)
logger.info(c.PoleBalanceConfig.DEVICE)
neat = pop.Population(c.PoleBalanceConfig)
solution, generation = neat.run()
if solution is not None:
logger.info('Found a Solution')
draw_net(solution, view=True, filename='./images/pole-balancing-solution', show_disabled=True)
# OpenAI Gym
env = gym.make('LongCartPole-v0')
done = False
observation = env.reset()
fitness = 0
phenotype = FeedForwardNet(solution, c.PoleBalanceConfig)
while not done:
env.render()
input = torch.Tensor([observation]).to(c.PoleBalanceConfig.DEVICE)
pred = round(float(phenotype(input)))
observation, reward, done, info = env.step(pred)
fitness += reward
env.close()