-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESoptimization.py
More file actions
78 lines (65 loc) · 2.33 KB
/
Copy pathESoptimization.py
File metadata and controls
78 lines (65 loc) · 2.33 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
""" Fabio D'Isidoro - ETH Zurich - September 2017
Tests on self-adaptive evolutionary strategy optimizer.
"""
import numpy as np
from test_functions import generate_3dlandscape, rastring_cost_function, test_function2D, rosenbrock_cost_function, ackley_function, plot_optimization_result2D
from evolutionary_strategy2_with_plots_sigma_variation_restart_option import EvolutionaryStrategy
# choose cost function from the imported ones
cost_function = ackley_function
# choose optimizer parameters for the chosen cost function
if cost_function == test_function2D:
mu = 2
domain = np.array([[-40.0,40.0],[-40.0,40.0]])
accuracies = [0.01, 0.01]
max_numb_generations = 100
with_plots = True
with_sigma_mutation = True
self_adaptivity = 1.0
elif cost_function == rastring_cost_function:
ndim = 6
mu = 3
domain = np.array([[-5.12,5.12],]*ndim)
accuracies = [0.01,]*ndim
max_numb_generations = 100
with_plots = True
with_sigma_mutation = True
self_adaptivity = 1.0
elif cost_function == rosenbrock_cost_function:
ndim = 2
mu = 3
domain = np.array([[-2.048,2.048],]*ndim)
accuracies = [0.01,]*ndim
max_numb_generations = 200
with_plots = True
with_sigma_mutation = True
self_adaptivity = 1.0
with_restart = True
elif cost_function == ackley_function:
ndim = 6
mu = 3
domain = np.array([[-32.768,32.768],]*ndim)
accuracies = [0.01,]*ndim
max_numb_generations = 200
with_plots = True
with_sigma_mutation = True
self_adaptivity = 0.5
with_restart = True
# plot landscape
dim1 = 1
dim2 = 2
npoints = 50
landscape = generate_3dlandscape(cost_function, domain, dim1, dim2, npoints)
# optimizer
optimizer = EvolutionaryStrategy(cost_function, domain, mu, accuracies, var = 1.0, rho = 2, max_numb_generations = max_numb_generations,
with_plots = with_plots, with_sigma_mutation = with_sigma_mutation, self_adaptivity = self_adaptivity, with_restart = with_restart)
# run
optimizer.run()
# print
print(" \n Found optimal parameters are ")
print(optimizer.solution.par)
print(" Best fitness found is ")
print(optimizer.solution.fit)
print(" The best fitness was found at generation ")
print(optimizer.solution_generation)
# plot results
plot_optimization_result2D(landscape, domain, optimizer.solution.par, dim1, dim2)