-
Notifications
You must be signed in to change notification settings - Fork 33
/
mountain_climb_run.py
38 lines (27 loc) · 999 Bytes
/
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
import logging
import gym
import torch
import neat.population as pop
import neat.experiments.mountain_climbing.config as c
from neat.visualize import draw_net
from neat.phenotype.feed_forward import FeedForwardNet
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()