-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathneuroevolution.py
More file actions
238 lines (192 loc) · 7.71 KB
/
Copy pathneuroevolution.py
File metadata and controls
238 lines (192 loc) · 7.71 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from copy import deepcopy
import multiprocessing
import numbers
import random
from typing import Callable, List, Optional
from gridmind.algorithms.evolutionary_rl.neuroevolution.neuro_agent import NeuroAgent
from gridmind.algorithms.evolutionary_rl.neuroevolution.neuroevolution_util import (
NeuroEvolutionUtil,
)
from gridmind.policies.parameterized.discrete_action_mlp_policy import (
DiscreteActionMLPPolicy,
)
from gridmind.utils.evo_util.selection import Selection
from gridmind.algorithms.evolutionary_rl.base_evo_rl_algorithm import BaseEvoRLAlgorithm
from gymnasium import Env
import torch
from tqdm import trange
import numpy as np
import gymnasium as gym
class NeuroEvolution(BaseEvoRLAlgorithm):
def __init__(
self,
env: Env,
population: Optional[List[NeuroAgent]] = None,
mu: int = 5,
_lambda: int = 20,
mutation_mean: float = 0,
mutation_std: float = 0.1,
feature_constructor: Optional[Callable] = None,
num_processes: Optional[int] = None,
stopping_fitness: Optional[float] = None,
summary_dir: Optional[str] = None,
write_summary: bool = False,
):
super().__init__(
name="NeuroEvolution",
env=env,
summary_dir=summary_dir,
write_summary=write_summary,
)
self.mu = mu
self._lambda = _lambda
self.mutation_mean = mutation_mean
self.mutation_std = mutation_std
self.feature_constructor = feature_constructor
self.observation_shape = (
self.env.observation_space.shape
if feature_constructor is None
else self._determine_observation_shape()
)
self.highest_possible_fitness = stopping_fitness
self.num_processes = (
num_processes
if num_processes is not None
else multiprocessing.cpu_count() // 2
)
self.num_actions = self.env.action_space.n
self.best_agent = None
self.population = (
population if population is not None else self.initialize_population()
)
self._generation = 0
@property
def generation(self):
return self._generation
def get_best(self, unwrapped: bool = True):
assert (
self.best_agent is not None
), "No best agent found. Train the algorithm first."
if unwrapped:
return self.best_agent.policy
return self.best_agent
def initialize_population(self):
population = []
for _ in range(self._lambda):
population.append(self.spawn_individual())
return population
def spawn_individual(self):
network = DiscreteActionMLPPolicy(
observation_shape=self.observation_shape,
num_actions=self.num_actions,
num_hidden_layers=2,
)
spawned_individual = NeuroAgent(network=network)
return spawned_individual
def _determine_observation_shape(self):
observation, _ = self.env.reset()
features = self.feature_constructor(observation)
shape = features.shape
return shape
def _preprocess(self, obs):
if self.feature_constructor is not None:
obs = self.feature_constructor(obs)
if isinstance(obs, numbers.Number):
obs = torch.tensor(obs, dtype=torch.float32).unsqueeze(0)
else:
obs = torch.tensor(obs, dtype=torch.float32)
return obs
def _get_state_value_fn(self, force_functional_interface: bool = True):
if not force_functional_interface:
return self.value_estimator
return lambda s: self.value_estimator(s).cpu().detach().item()
def _get_state_action_value_fn(self, force_functional_interface: bool = True):
raise Exception()
def _get_policy(self):
return self.get_best(unwrapped=True)
def set_policy(self, policy, **kwargs):
raise NotImplementedError()
def mutate(self, network, mean, std):
chromosome = NeuroEvolutionUtil.get_parameters_vector(network)
noise = np.random.normal(loc=mean, scale=std, size=chromosome.shape)
mutated_chromosome = chromosome + noise
return mutated_chromosome
@torch.no_grad()
def evaluate_fitness(
self, policy: DiscreteActionMLPPolicy, average_over_episodes: int = 3
):
sum_episode_return = 0.0
for i in range(average_over_episodes):
obs, info = self.env.reset()
done = False
while not done:
obs = self._preprocess(obs)
action = policy.get_action(obs)
obs, reward, terminated, truncated, info = self.env.step(action)
sum_episode_return += reward
done = terminated or truncated
return sum_episode_return / average_over_episodes
def _train(self, num_generations: int, *args, **kwargs):
for num_gen in trange(num_generations):
agent_to_assess_fitness = []
for agent in self.population:
if agent.fitness is None:
agent_to_assess_fitness.append(agent)
fitness_scores = [
self.evaluate_fitness(agent.policy) for agent in agent_to_assess_fitness
]
for agent, fitness in zip(agent_to_assess_fitness, fitness_scores):
agent.fitness = fitness
if self.best_agent is None or agent.fitness > self.best_agent.fitness:
self.best_agent = agent
if (
self.highest_possible_fitness is not None
and self.best_agent.fitness >= self.highest_possible_fitness
):
self.logger.info(
f"Stopping fitness reached: {self.best_agent.fitness}"
)
self.summary_writer.add_scalar(
"Best_Agent_Fitness",
self.best_agent.fitness,
global_step=self.generation,
)
return self.best_agent
average_fitness = sum([agent.fitness for agent in self.population]) / len(
self.population
)
self.logger.info(
f"Generation: {self.generation}, Average Fitness: {average_fitness}"
)
self.summary_writer.add_scalar(
"Population_Average_Fitness",
average_fitness,
global_step=self.generation,
)
if self.best_agent is not None:
self.logger.info(f"Best Agent Fitness: {self.best_agent.fitness}")
self.summary_writer.add_scalar(
"Best_Agent_Fitness",
self.best_agent.fitness,
global_step=self.generation,
)
# Select parents
parents = Selection.truncation_selection(
population=self.population, num_selection=self.mu
)
self.population = deepcopy(parents)
# Mutation
for parent in parents:
for _ in range(self._lambda // self.mu):
mutated_param_vector = self.mutate(
network=parent.policy,
mean=self.mutation_mean,
std=self.mutation_std,
)
child = self.spawn_individual()
NeuroEvolutionUtil.set_parameters_vector(
child.policy, mutated_param_vector
)
self.population.append(child)
self._generation += 1
return self.best_agent