-
Notifications
You must be signed in to change notification settings - Fork 3
/
apply_metaheuristics.py
167 lines (144 loc) · 4.8 KB
/
apply_metaheuristics.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
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
#!python3
# Copyright (C) 2020 Victor O. Costa
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Python standard library
import sys
import time
# 3rd party
from deap.benchmarks import ackley
# Own
import ant_colony_for_continuous_domains
import particle_swarm_optimization
import simulated_annealing
if len(sys.argv) != 2:
print("Please enter \"%s {number of function evaluations}\"" % sys.argv[0])
exit(-1)
num_func_evals = int(sys.argv[1])
print("Num. FE = %d" % num_func_evals)
ranges = [[-5,5],
[-5,5],
[-5,5]]
is_bounded = [True, True, True]
def flatten_cost(cost_function):
def flattened_cost(x):
return cost_function(x)[0]
return flattened_cost
# ACOr
# Total # of function evaluations: archive_size + population_size * num_iterations
print("ACOr")
# Parameters
k = 50; pop_size = 10; q = 1e-2; xi = 0.85
# Configure and run
colony = ant_colony_for_continuous_domains.ACOr()
colony.set_verbosity(False)
colony.set_cost(flatten_cost(ackley))
colony.set_parameters(pop_size, k, q, xi, [num_func_evals])
colony.define_variables(ranges, is_bounded)
solution = colony.optimize()
print(solution)
# AELACOr
# Total # of function evaluations: archive_size + population_size * num_iterations
print("AELACOr")
# Parameters
k = 50; pop_size = 10; xi = 0.85
min_q = 1e-2
max_q = 1.0
# Configure and run
colony = ant_colony_for_continuous_domains.AELACOr()
colony.set_verbosity(False)
colony.set_cost(flatten_cost(ackley))
colony.set_parameters(pop_size, k, xi, min_q, max_q, 'exp', [num_func_evals])
colony.define_variables(ranges, is_bounded)
solution = colony.optimize()
print(solution)
# AGDACOr
# Total # of function evaluations: archive_size + population_size * num_iterations
print("AGDACOr")
# Parameters
k = 50; pop_size = 10; q = 0.01
min_xi = 0.1
max_xi = 0.93
# Configure and run
colony = ant_colony_for_continuous_domains.AGDACOr()
colony.set_verbosity(False)
colony.set_cost(flatten_cost(ackley))
colony.set_parameters(pop_size, k, q, min_xi, max_xi, 'sig', [num_func_evals])
colony.define_variables(ranges, is_bounded)
solution = colony.optimize()
print(solution)
# BAACOr
print("BAACOr")
# Parameters
k = 50; pop_size = 10;
min_q = 1e-2
max_q = 1.0
min_xi = 0.1
max_xi = 0.93
# Configure and run
colony = ant_colony_for_continuous_domains.BAACOr()
colony.set_verbosity(False)
colony.set_cost(flatten_cost(ackley))
colony.set_parameters(pop_size, k, min_q, max_q, min_xi, max_xi, 'exp', 'sig', [num_func_evals])
colony.define_variables(ranges, is_bounded)
solution = colony.optimize()
print(solution)
# SA
print("SA")
# Parameters
initial_temperature = 10.0; cooling_constant = 0.99; step_size = 1e-2;
local_iterations = 100
# Configure and run
sa = simulated_annealing.SA()
sa.set_verbosity(False)
sa.set_cost(flatten_cost(ackley))
sa.set_parameters(initial_temperature, cooling_constant, step_size, local_iterations, [num_func_evals])
sa.define_variables(ranges, is_bounded)
solution = sa.optimize()
print(solution)
# ACFSA
print("ACFSA")
# Parameters
initial_temperature = 10.0; cooling_constant = 0.99
local_iterations = 100
# Configure and run
acfsa = simulated_annealing.ACFSA()
acfsa.set_verbosity(False)
acfsa.set_cost(flatten_cost(ackley))
acfsa.set_parameters(initial_temperature, cooling_constant, local_iterations, [num_func_evals])
acfsa.define_variables(ranges, is_bounded)
solution = acfsa.optimize()
print(solution)
# PSO
print("PSO")
# Parameters
swarm = particle_swarm_optimization.PSO()
swarm_size = 20; personal_acceleration = 2; global_acceleration = 2
# Configure and run
swarm.set_verbosity(False)
swarm.set_cost(flatten_cost(ackley))
swarm.set_parameters(swarm_size, personal_acceleration, global_acceleration, [num_func_evals])
swarm.define_variables(ranges, is_bounded)
solution = swarm.optimize()
print(solution)
# AIWPSO
print("AIWPSO")
# Parameters
swarm = particle_swarm_optimization.AIWPSO()
swarm_size = 20; personal_acceleration = 2; global_acceleration = 2
min_inertia = 0.3; max_inertia = 0.99
# Configure and run
swarm.set_verbosity(False)
swarm.set_cost(flatten_cost(ackley))
swarm.set_parameters(swarm_size, personal_acceleration, global_acceleration, min_inertia, max_inertia, [num_func_evals])
swarm.define_variables(ranges, is_bounded)
solution = swarm.optimize()
print(solution)