forked from ddehueck/pytorch-neat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountain_climb_run.py
42 lines (32 loc) · 1.01 KB
/
mountain_climb_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.mountain_climbing.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.MountainClimbConfig.DEVICE)
neat = pop.Population(c.MountainClimbConfig)
solution, generation = neat.run()
if solution is not None:
logger.info("Found a Solution")
draw_net(
solution,
view=True,
filename="./images/mountain-climb-solution",
show_disabled=True,
)
# OpenAI Gym
env = gym.make("MountainCarContinuous-v0")
done = False
observation = env.reset()
fitness = 0
phenotype = FeedForwardNet(solution, c.MountainClimbConfig)
while not done:
env.render()
input = torch.Tensor([observation]).to(c.MountainClimbConfig.DEVICE)
pred = [round(float(phenotype(input)))]
observation, reward, done, info = env.step(pred)
fitness += reward
env.close()