Skip to content

swarmrl.training_routines.genetic_algorithm Module API Reference

Class for the genertic algorithm training routine.

GeneticTraining

Class for the genetic training routine.

Source code in swarmrl/training_routines/genetic_algorithm.py
 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
class GeneticTraining:
    """
    Class for the genetic training routine.
    """

    def __init__(
        self,
        trainer: ContinuousTrainer,
        simulation_runner_generator: callable,
        n_episodes: int = 100,
        episode_length: int = 20,
        number_of_generations: int = 10,
        population_size: int = 10,
        number_of_parents: int = 2,
        parent_selection_method: str = "sum",
        output_directory: str = ".",
        routine_name: str = "genetic_algorithm",
        parallel_jobs: int = None,
        cluster: JobQueueCluster = None,
    ):
        """
        Constructor for the genetic training routine.

        Parameters
        ----------
        n_episodes : int
            Number of episodes in each lifespan
        number_of_generations : int (default: 10)
            Number of generations to run
        population_size : int (default: 10)
            Number of individuals in the population
        number_of_parents : int (default: 2)
            Number of parents to select for each generation
        parent_selection_method : str
            How to reduce the life reward. Either sum or mean.
        output_directory : str (default: ".")
            Output directory of the run.
        routine_name : str (default: "genetic_algorithm")
            Name of the training routine.
        cluster : JobQueueCluster
            The cluster to run the jobs on.
            If None, the jobs will be run locally.
        parallel_jobs : int
            The number of parallel jobs to run.
            If None, the population size is used.

        Notes
        -----
        Currently the client is fixed to run on the local machine. This will be
        changed to a parameter in the future. The problem lies in espresso not being
        able to handle multiple threads and us not being able to force Dask to refresh
        a worker after each training is finished.
        """
        self.trainer = trainer
        self.simulation_runner_generator = simulation_runner_generator
        self.n_episodes = n_episodes
        self.episode_length = episode_length
        self.number_of_generations = number_of_generations
        self.population_size = population_size
        self.number_of_parents = number_of_parents
        self.output_directory = Path(f"{output_directory}/{routine_name}")

        if parallel_jobs is None:
            parallel_jobs = population_size
        self.parallel_jobs = parallel_jobs

        # Use default local cluster if None is given.
        if cluster is None:
            cluster = LocalCluster(
                processes=True,
                threads_per_worker=2,
                silence_logs=logging.ERROR,
                resources={"espresso": 1},
            )

        self.cluster = cluster

        self.client = Client(cluster)

        self.cluster.scale(n=self.parallel_jobs)
        webbrowser.open(self.client.dashboard_link)

        # Decide on parent splits
        self.identifiers = range(population_size)

        lazy_splits = np.array_split(np.ones(population_size), number_of_parents)
        self.split_lengths = [len(split) for split in lazy_splits]

        # set the select function
        if parent_selection_method == "sum":
            self._select_fn = onp.sum
        elif parent_selection_method == "mean":
            self._select_fn = onp.mean
        elif parent_selection_method == "max":
            self._select_fn = onp.max

        # Create the output directory
        os.mkdir(Path(self.output_directory))

    @staticmethod
    def _train_network(
        name: Path,
        load_directory: str = None,
        trainer: ContinuousTrainer = None,
        runner_generator: callable = None,
        select_fn: callable = None,
        episode_length: int = None,
        n_episodes: int = None,
    ) -> tuple:
        """
        Train the network.

        Parameters
        ----------
        name : Path
            Name of the network and where to save the data.
        load_directory : str (default: None)
            Directory to load the model from. If None, a new model will be created.
        trainer : ContinuousTrainer
                Trainer to use for training.
        runner_generator : callable
                Function that returns a system_runner.
        select_fn : callable
                Function for reducing training rewards to a single
                number.
        episode_length : int
                Length of one episode.
        n_episodes : int
                Number of episodes in the lifespan of the child.

        Returns
        -------
        reduced_rewards : float
            The reduced rewards of the agent.
        model_id : str
            The id of the model.
        """
        model_id = name.as_posix().split("_")[-1]
        os.makedirs(name)
        os.chdir(name)

        system_runner = runner_generator()  # get the runner

        if load_directory is None:
            trainer.initialize_models()
        else:
            trainer.restore_models(load_directory)

        rewards = trainer.perform_rl_training(
            system_runner,
            episode_length=episode_length,
            n_episodes=n_episodes,
            load_bar=False,
        )
        trainer.export_models()

        return (select_fn(rewards), model_id)

    def _deploy_jobs(
        self,
        child_names: List[Path],
        load_paths: List[Path],
    ) -> List[float]:
        """
        Function to send jobs to the cluster.

        Parameters
        ----------
        child_names : List[Path]
            List of paths to save the models to.
        load_paths : List[Path, None]
            List of paths to load the models from.

        Returns
        -------
        Returns the outcome of the job deployment.
        """
        futures = []

        for i in range(self.population_size // self.parallel_jobs):
            block = self.client.map(
                self._train_network,
                child_names[i * self.parallel_jobs : (i + 1) * self.parallel_jobs],
                load_paths[i * self.parallel_jobs : (i + 1) * self.parallel_jobs],
                [deepcopy(self.trainer)] * self.parallel_jobs,
                [self.simulation_runner_generator] * self.parallel_jobs,
                [self._select_fn] * self.parallel_jobs,
                [self.episode_length] * self.parallel_jobs,
                [self.n_episodes] * self.parallel_jobs,
                resources={"espresso": 1},
            )
            _ = wait(block)
            futures += self.client.gather(block)
            # Restart and wait for workers
            _ = self.client.restart(wait_for_workers=False)
            _ = self.client.wait_for_workers(self.parallel_jobs)

        return futures

    def _run_generation(
        self, generation: int, seed: bool = False, parent_ids: list = None
    ) -> List:
        """
        Run a generation of the training.

        Parameters
        ----------
        generation : int
            The number of the generation to run.
        seed : bool (default: False)
            Whether to seed the generation or not.
        parent_ids : list (default: None)
            The ids of the parents to use for the generation. If None, it should
            be seeded.

        Returns
        -------
        generation_outputs : list
                A list of rewards values form which parents will be chosen.
        """
        # Create the children directories
        children_names = [
            (
                self.output_directory / f"_generation_{generation}" / f"_child_{i}"
            ).resolve()
            for i in self.identifiers
        ]
        # deploy the jobs
        if seed:
            generation_outputs = self._deploy_jobs(
                children_names, [None] * self.population_size
            )

        else:
            # get load paths for each parent
            load_paths = []
            for i, index in enumerate(parent_ids):
                load_paths += [
                    self.output_directory
                    / f"_generation_{generation - 1}"
                    / f"_child_{index}"
                    / "Models"
                ] * self.split_lengths[i]

            load_paths = [item.resolve().as_posix() for item in load_paths]

            generation_outputs = self._deploy_jobs(children_names, load_paths)

        return generation_outputs

    def _select_parents(self, generation_outputs: np.ndarray) -> list:
        """
        Select the parents for the next generation.

        Parameters
        ----------
        generation_outputs : np.ndarray (n_individuals, )
            The outputs of the generation.

        Returns
        -------
        ids : list
            The ids of the parents.
        chosen_reward: float
            Reward of the chosen child.
        """
        rewards = [item[0] for item in generation_outputs]
        ids = [item[1] for item in generation_outputs]

        # First get best parent
        max_reward_index = np.argmax(np.array(rewards))
        chosen_id = ids[max_reward_index]

        # Pick mutations
        if self.number_of_parents == 1:
            return [chosen_id], rewards[max_reward_index]
        else:
            random_ids = onp.random.choice(
                ids, size=self.number_of_parents - 1, replace=False
            )
            return [chosen_id] + list(random_ids), rewards[max_reward_index]

    def train_model(self):
        """
        Train the model.
        """
        generation = 0
        # Seed genetic process
        seed_outputs = self._run_generation(generation=generation, seed=True)
        parents, reward = self._select_parents(seed_outputs)

        # Loop over generations

        progress = Progress(
            "Generation: {task.fields[generation]}",
            BarColumn(),
            "Best generation reward: {task.fields[best_reward]:.2f} ",
            TimeRemainingColumn(),
        )
        with progress:
            task = progress.add_task(
                "Genetic training",
                total=self.number_of_generations - 1,
                generation=generation,
                best_reward=reward,
            )
            for _ in range(self.number_of_generations - 1):
                generation += 1  # Update the generation
                generation_outputs = self._run_generation(
                    generation=generation, parent_ids=parents
                )
                parents, reward = self._select_parents(generation_outputs)
                progress.update(
                    task,
                    advance=1,
                    generation=generation,
                    best_reward=reward,
                )

        best_model_path = (
            self.output_directory / f"_generation_{generation}" / f"_child_{parents[0]}"
        )
        print(f"Best Model: {best_model_path.as_posix()}")
        print(f"Best Reward: {reward:.2f}")

        # Shutdown the cluster
        self.cluster.close()
        self.client.close()

        return best_model_path

__init__(trainer, simulation_runner_generator, n_episodes=100, episode_length=20, number_of_generations=10, population_size=10, number_of_parents=2, parent_selection_method='sum', output_directory='.', routine_name='genetic_algorithm', parallel_jobs=None, cluster=None)

Constructor for the genetic training routine.

Parameters

n_episodes : int Number of episodes in each lifespan number_of_generations : int (default: 10) Number of generations to run population_size : int (default: 10) Number of individuals in the population number_of_parents : int (default: 2) Number of parents to select for each generation parent_selection_method : str How to reduce the life reward. Either sum or mean. output_directory : str (default: ".") Output directory of the run. routine_name : str (default: "genetic_algorithm") Name of the training routine. cluster : JobQueueCluster The cluster to run the jobs on. If None, the jobs will be run locally. parallel_jobs : int The number of parallel jobs to run. If None, the population size is used.

Notes

Currently the client is fixed to run on the local machine. This will be changed to a parameter in the future. The problem lies in espresso not being able to handle multiple threads and us not being able to force Dask to refresh a worker after each training is finished.

Source code in swarmrl/training_routines/genetic_algorithm.py
 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
def __init__(
    self,
    trainer: ContinuousTrainer,
    simulation_runner_generator: callable,
    n_episodes: int = 100,
    episode_length: int = 20,
    number_of_generations: int = 10,
    population_size: int = 10,
    number_of_parents: int = 2,
    parent_selection_method: str = "sum",
    output_directory: str = ".",
    routine_name: str = "genetic_algorithm",
    parallel_jobs: int = None,
    cluster: JobQueueCluster = None,
):
    """
    Constructor for the genetic training routine.

    Parameters
    ----------
    n_episodes : int
        Number of episodes in each lifespan
    number_of_generations : int (default: 10)
        Number of generations to run
    population_size : int (default: 10)
        Number of individuals in the population
    number_of_parents : int (default: 2)
        Number of parents to select for each generation
    parent_selection_method : str
        How to reduce the life reward. Either sum or mean.
    output_directory : str (default: ".")
        Output directory of the run.
    routine_name : str (default: "genetic_algorithm")
        Name of the training routine.
    cluster : JobQueueCluster
        The cluster to run the jobs on.
        If None, the jobs will be run locally.
    parallel_jobs : int
        The number of parallel jobs to run.
        If None, the population size is used.

    Notes
    -----
    Currently the client is fixed to run on the local machine. This will be
    changed to a parameter in the future. The problem lies in espresso not being
    able to handle multiple threads and us not being able to force Dask to refresh
    a worker after each training is finished.
    """
    self.trainer = trainer
    self.simulation_runner_generator = simulation_runner_generator
    self.n_episodes = n_episodes
    self.episode_length = episode_length
    self.number_of_generations = number_of_generations
    self.population_size = population_size
    self.number_of_parents = number_of_parents
    self.output_directory = Path(f"{output_directory}/{routine_name}")

    if parallel_jobs is None:
        parallel_jobs = population_size
    self.parallel_jobs = parallel_jobs

    # Use default local cluster if None is given.
    if cluster is None:
        cluster = LocalCluster(
            processes=True,
            threads_per_worker=2,
            silence_logs=logging.ERROR,
            resources={"espresso": 1},
        )

    self.cluster = cluster

    self.client = Client(cluster)

    self.cluster.scale(n=self.parallel_jobs)
    webbrowser.open(self.client.dashboard_link)

    # Decide on parent splits
    self.identifiers = range(population_size)

    lazy_splits = np.array_split(np.ones(population_size), number_of_parents)
    self.split_lengths = [len(split) for split in lazy_splits]

    # set the select function
    if parent_selection_method == "sum":
        self._select_fn = onp.sum
    elif parent_selection_method == "mean":
        self._select_fn = onp.mean
    elif parent_selection_method == "max":
        self._select_fn = onp.max

    # Create the output directory
    os.mkdir(Path(self.output_directory))

train_model()

Train the model.

Source code in swarmrl/training_routines/genetic_algorithm.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
def train_model(self):
    """
    Train the model.
    """
    generation = 0
    # Seed genetic process
    seed_outputs = self._run_generation(generation=generation, seed=True)
    parents, reward = self._select_parents(seed_outputs)

    # Loop over generations

    progress = Progress(
        "Generation: {task.fields[generation]}",
        BarColumn(),
        "Best generation reward: {task.fields[best_reward]:.2f} ",
        TimeRemainingColumn(),
    )
    with progress:
        task = progress.add_task(
            "Genetic training",
            total=self.number_of_generations - 1,
            generation=generation,
            best_reward=reward,
        )
        for _ in range(self.number_of_generations - 1):
            generation += 1  # Update the generation
            generation_outputs = self._run_generation(
                generation=generation, parent_ids=parents
            )
            parents, reward = self._select_parents(generation_outputs)
            progress.update(
                task,
                advance=1,
                generation=generation,
                best_reward=reward,
            )

    best_model_path = (
        self.output_directory / f"_generation_{generation}" / f"_child_{parents[0]}"
    )
    print(f"Best Model: {best_model_path.as_posix()}")
    print(f"Best Reward: {reward:.2f}")

    # Shutdown the cluster
    self.cluster.close()
    self.client.close()

    return best_model_path