forked from ddehueck/pytorch-neat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpole_run.py
42 lines (32 loc) · 1015 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
39
40
41
42
import logging
import gym
import torch
import neat.experiments.pole_balancing.config as c
import neat.population as pop
from neat.phenotype.feed_forward import FeedForwardNet
from neat.visualize import draw_net
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()