1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import random |
6
|
|
|
import numpy as np |
7
|
|
|
|
8
|
|
|
from ._evolutionary_algorithm import EvolutionaryAlgorithmOptimizer |
9
|
|
|
from ._individual import Individual |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class EvolutionStrategyOptimizer(EvolutionaryAlgorithmOptimizer): |
13
|
|
|
name = "Evolution Strategy" |
14
|
|
|
_name_ = "evolution_strategy" |
15
|
|
|
__name__ = "EvolutionStrategyOptimizer" |
16
|
|
|
|
17
|
|
|
optimizer_type = "population" |
18
|
|
|
computationally_expensive = False |
19
|
|
|
|
20
|
|
View Code Duplication |
def __init__( |
|
|
|
|
21
|
|
|
self, |
22
|
|
|
search_space, |
23
|
|
|
initialize={"grid": 4, "random": 2, "vertices": 4}, |
24
|
|
|
constraints=[], |
25
|
|
|
random_state=None, |
26
|
|
|
rand_rest_p=0, |
27
|
|
|
nth_process=None, |
28
|
|
|
population=10, |
29
|
|
|
offspring=20, |
30
|
|
|
replace_parents=False, |
31
|
|
|
mutation_rate=0.7, |
32
|
|
|
crossover_rate=0.3, |
33
|
|
|
): |
34
|
|
|
super().__init__( |
35
|
|
|
search_space=search_space, |
36
|
|
|
initialize=initialize, |
37
|
|
|
constraints=constraints, |
38
|
|
|
random_state=random_state, |
39
|
|
|
rand_rest_p=rand_rest_p, |
40
|
|
|
nth_process=nth_process, |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
self.population = population |
44
|
|
|
self.offspring = offspring |
45
|
|
|
self.replace_parents = replace_parents |
46
|
|
|
self.mutation_rate = mutation_rate |
47
|
|
|
self.crossover_rate = crossover_rate |
48
|
|
|
|
49
|
|
|
self.individuals = self._create_population(Individual) |
50
|
|
|
self.optimizers = self.individuals |
51
|
|
|
|
52
|
|
|
def _cross(self): |
53
|
|
|
while True: |
54
|
|
|
if len(self.individuals) > 2: |
55
|
|
|
rnd_int2 = random.choice( |
56
|
|
|
[ |
57
|
|
|
i |
58
|
|
|
for i in range(0, self.n_ind - 1) |
59
|
|
|
if i not in [self.rnd_int] |
60
|
|
|
] |
61
|
|
|
) |
62
|
|
|
else: |
63
|
|
|
rnd_int2 = random.choice( |
64
|
|
|
[i for i in range(0, self.n_ind) if i not in [self.rnd_int]] |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
p_sec = self.pop_sorted[rnd_int2] |
68
|
|
|
p_worst = self.pop_sorted[-1] |
69
|
|
|
|
70
|
|
|
two_best_pos = [self.p_current.pos_current, p_sec.pos_current] |
71
|
|
|
pos_new = self.discrete_recombination(two_best_pos) |
72
|
|
|
|
73
|
|
|
self.p_current = p_worst |
74
|
|
|
p_worst.pos_new = pos_new |
75
|
|
|
|
76
|
|
|
if self.conv.not_in_constraint(pos_new): |
77
|
|
|
return pos_new |
78
|
|
|
|
79
|
|
|
return self.p_current.move_climb(pos_new) |
80
|
|
|
|
81
|
|
|
@EvolutionaryAlgorithmOptimizer.track_new_pos |
82
|
|
|
def init_pos(self): |
83
|
|
|
nth_pop = self.nth_trial % len(self.individuals) |
84
|
|
|
|
85
|
|
|
self.p_current = self.individuals[nth_pop] |
86
|
|
|
return self.p_current.init_pos() |
87
|
|
|
|
88
|
|
View Code Duplication |
@EvolutionaryAlgorithmOptimizer.track_new_pos |
|
|
|
|
89
|
|
|
def iterate(self): |
90
|
|
|
self.n_ind = len(self.individuals) |
91
|
|
|
|
92
|
|
|
if self.n_ind == 1: |
93
|
|
|
self.p_current = self.individuals[0] |
94
|
|
|
return self.p_current.iterate() |
95
|
|
|
|
96
|
|
|
self.sort_pop_best_score() |
97
|
|
|
self.rnd_int = random.randint(0, len(self.pop_sorted) - 1) |
98
|
|
|
self.p_current = self.pop_sorted[self.rnd_int] |
99
|
|
|
|
100
|
|
|
total_rate = self.mutation_rate + self.crossover_rate |
101
|
|
|
rand = np.random.uniform(low=0, high=total_rate) |
102
|
|
|
|
103
|
|
|
if rand <= self.mutation_rate: |
104
|
|
|
return self.p_current.iterate() |
105
|
|
|
else: |
106
|
|
|
return self._cross() |
107
|
|
|
|
108
|
|
|
@EvolutionaryAlgorithmOptimizer.track_new_score |
109
|
|
|
def evaluate(self, score_new): |
110
|
|
|
self.p_current.evaluate(score_new) |
111
|
|
|
|