-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_lunar_lander.py
55 lines (33 loc) · 1.25 KB
/
main_lunar_lander.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
46
47
48
49
50
51
52
53
54
55
import neat
import numpy as np
import run_neat_base
def eval_network(net, net_input):
activation = net.activate(net_input)
return np.argmax(activation)
def eval_single_genome(genome, genome_config):
net = neat.nn.FeedForwardNetwork.create(genome, genome_config)
total_reward = 0.0
for i in range(run_neat_base.n):
# print("--> Starting new episode")
observation = run_neat_base.env.reset()
action = eval_network(net, observation)
done = False
t = 0
while not done:
# run_neat_base.env.render()
observation, reward, done, info = run_neat_base.env.step(action)
# print("\t Reward {}: {}".format(t, reward))
# print("\t Action {}: {}".format(t, action))
action = eval_network(net, observation)
total_reward += reward
t += 1
if done:
# print("<-- Episode finished after {} timesteps with reward {}".format(t + 1, genome.fitness))
break
return total_reward / run_neat_base.n
def main():
run_neat_base.run(eval_network,
eval_single_genome,
environment_name="LunarLander-v2")
if __name__ == '__main__':
main()