-
Notifications
You must be signed in to change notification settings - Fork 0
/
clockwise_migration.py
62 lines (44 loc) · 1.95 KB
/
clockwise_migration.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
from pygenalgo.engines.auxiliary import SubPopulation
from pygenalgo.operators.migration.migration_operator import MigrationOperator
class ClockwiseMigration(MigrationOperator):
"""
Description:
Clockwise Migration implements a "very basic" migration policy in which
each island migrates its best chromosome to the population on its right,
following a "clockwise" rotation movement.
"""
def __init__(self, migration_probability: float = 0.95):
"""
Construct a 'ClockwiseMigration' object with a given probability value.
:param migration_probability: (float) in [0, 1].
"""
# Call the super constructor with the provided probability value.
super().__init__(migration_probability)
# _end_def_
def migrate(self, islands: list[SubPopulation]) -> None:
"""
Perform the migration operation on the list of SubPopulations.
:param islands: list[SubPopulation].
:return: None.
"""
# If we have only one active population exit without migration.
if len(islands) == 1:
return None
# _end_if_
# First find the best individual chromosome FROM EACH island.
best_chromosomes = [max(island_i.population, key=lambda c: c.fitness)
for island_i in islands]
# Go through all the islands.
for i, island_i in enumerate(islands):
# Perform the migration with a predefined probability.
if self.probability > self.rng.random():
# Select randomly one individual chromosome.
idx = self.rng.integers(0, len(island_i.population))
# Replace the chromosome with the best one from its left.
island_i.population[idx] = best_chromosomes[i-1].clone()
# _end_if_
# _end_for_
# Increase the migration counter.
self.inc_counter()
# _end_def_
# _end_class_