-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunar_lander.py
143 lines (99 loc) · 3.63 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import random
import torch
import numpy as np
from classes.NEAT import *
random.seed(14)
torch.manual_seed(14)
np.random.seed(14)
n_networks = 200
# Fitness:
c1 = 1
c2 = 1
c3 = 0.5
distance_delta = 2
weight_magnitude = 1.5 # std of weight mutation
# Mutation
mutate_weight_prob = 0.8
mutate_weight_perturb = 0.8
mutate_weight_random = 1 - mutate_weight_perturb
mutate_add_node_prob = 0.02
mutate_remove_node_prob = 0.02
mutate_add_link_prob_large_pop = 0.3
mutate_add_link_prob = 0.3
mutate_remove_link_prob = 0.3
offspring_without_crossover = 0.05
interspecies_mate_rate = 0.001
fitness_survival_rate = 0.2
interspecies_mate_rate = 0.001
node_gene_history = Node_Gene_History()
connection_gene_history = Connection_Gene_History()
genotypes = []
for _ in range(n_networks):
node_genes = []
for i in range(9):
node_genes.append(Node_Gene(None, None, node_gene_history, add_initial=True, add_initial_node_level=0, initial_node_id=i))
for i in range(9,13):
node_genes.append(Node_Gene(None, None, node_gene_history, add_initial=True, add_initial_node_level=1, initial_node_id=i))
connection_genes = []
for i in range(9):
for j in range(9,13):
connection_genes.append(Connection_Gene(i, j, np.random.normal(), False, connection_gene_history))
genotype = Genotype(
node_genes, connection_genes, node_gene_history, connection_gene_history,
mutate_weight_prob, mutate_weight_perturb, mutate_weight_random, mutate_add_node_prob,mutate_remove_node_prob, mutate_add_link_prob,mutate_remove_link_prob, weight_magnitude,
c1, c2, c3)
genotypes.append(genotype)
import gymnasium as gym
#env = gym.make("LunarLander-v2")
def lunar_fitness(genotype_and_env, inputs, targets):
#error = 0
genotype, env = genotype_and_env
network = NeuralNetwork(genotype)
fitness = 0
num_tries = 5
for _ in range(num_tries):
observation, info = env.reset()
terminated, truncated = False, False
while not terminated and not truncated:
input = {
0:torch.tensor([1.0]),# bias
1:torch.tensor([observation[0]]),
2:torch.tensor([observation[1]]),
3:torch.tensor([observation[2]]),
4:torch.tensor([observation[3]]),
5:torch.tensor([observation[4]]),
6:torch.tensor([observation[5]]),
7:torch.tensor([observation[6]]),
8:torch.tensor([observation[7]]),
}
actions = network.forward(input)
actions = torch.tensor(actions)
action = torch.argmax(actions).item()
observation, reward, terminated, truncated, info = env.step(action)
fitness += reward
fitness /= num_tries
env.close()
return fitness
initial_species = Species(np.random.choice(genotypes), genotypes, distance_delta)
import os
import datetime
now = datetime.datetime.now()
folder = os.path.join('runs', 'lunar', str(now))
evolved_species, solutions = evolve(
features=None,
target=None,
fitness_function=lunar_fitness,
stop_at_fitness=1000,
n_generations=10000,
species=[initial_species],
fitness_survival_rate=fitness_survival_rate,
interspecies_mate_rate=interspecies_mate_rate,
distance_delta=distance_delta,
largest_species_linkadd_rate=mutate_add_link_prob_large_pop,
eliminate_species_after_n_generations=20,
run_folder=folder,
n_workers=16,
gymnasium_env=[gym.make("LunarLander-v2") for _ in range(n_networks)],
elitism=True
)
print(solutions)