-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (207 loc) · 8.88 KB
/
main.py
File metadata and controls
223 lines (207 loc) · 8.88 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
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
from classic_ant_main import ant
from tree_ant_main import tree_ant
from PSO import objective_function, ParticleSwarmOptimization
from firework_algo import firework
import argparse
import numpy as np
import sys
compil_flag_list = ["O2", "O3", "Ofast"]
simd_list = ["avx", "avx2", "avx512"]
output_value = "points"
DefaultIterMax = 10
DefaultMethod = "ACO"
Defaultnumberpopulation = 100
Defaultnfirework = 5
Defaultalpha = 0.8
Defaultrho = 0.25
DefaultQ = 1
Defaultc1 = 1.5
Defaultc2 = 1.5
Defaultw = 0.8
Defaultn1 = 256
Defaultn2 = 256
Defaultn3 = 256
Defaultnthread = 32
Defaulttimeout = 30
Defaulta = 0.04
Defaultb = 0.8
Defaultts = 50
Defaultamp = 60
Defaultngs = 5
def cmdLineParsing():
parser = argparse.ArgumentParser()
parser.add_argument(
"--i", help="maximum nb of iterations , default : 10", default=DefaultIterMax, type=int)
parser.add_argument(
"--n1", help="problem size along first axis , default : 256", default=Defaultn1, type=int)
parser.add_argument(
"--n2", help="problem size along first axis , default : 256", default=Defaultn2, type=int)
parser.add_argument(
"--n3", help="problem size along first axis , default : 256", default=Defaultn3, type=int)
parser.add_argument(
"--nt", help="maximum number of thread , default : 32", default=Defaultnthread, type=int)
parser.add_argument(
"--m",
help="population based method (ACO, Tree_ACO, PSO,firework) default:ACO",
default=DefaultMethod,
)
try:
index_method = sys.argv.index("--m") + 1
bool = sys.argv[index_method] == "firework"
except ValueError:
bool = False
if bool:
parser.add_argument(
"--n", help="number of particles/ants/fireworks on each iteration , default : 100 for population, 5 for firework", default=Defaultnfirework, type=int)
else:
parser.add_argument(
"--n", help="number of particles/ants/fireworks on each iteration , default : 100 for population, 5 for firework", default=Defaultnumberpopulation, type=int)
parser.add_argument(
"--rho", help="evaporation_rate for ant colonies , default : 0.25", default=Defaultalpha, type=np.float64)
parser.add_argument(
"--alpha", help="value of exponent for ant colonies , default : 0.8", default=Defaultrho, type=np.float64)
parser.add_argument(
"--q", help="quantity if pheromons deposed for ant colonies , default : 1", default=DefaultQ, type=np.float64)
parser.add_argument(
"--c1", help="particle speed for PSO , default : 0.5", default=Defaultc1, type=np.float64)
parser.add_argument(
"--c2", help="group speed for PSO , default : 0.5", default=Defaultc2, type=np.float64)
parser.add_argument(
"--w", help="particle inertia for PSO , default : 0.8", default=Defaultw, type=np.float64)
parser.add_argument(
"--t", help="duration before timeout , default : 30", default=Defaulttimeout, type=int)
parser.add_argument(
"--a", help="minimum rate sparks/max_number_sparks , default : 0.04", default=Defaulta, type=np.float64)
parser.add_argument(
"--b", help="maximum rate sparks/max_number_sparks , default : 0.8", default=Defaultb, type=np.float64)
parser.add_argument(
"--mns", help="mns :max number of sparks generated by a firework , default : 50", default=Defaultts, type=int)
parser.add_argument(
"--amp", help="maximum explosion amplitude , default : 60", default=Defaultamp, type=np.float64)
parser.add_argument(
"--ngs", help="ngs : total number of sparks generated by gaussian distribution , default : 5", default=Defaultngs, type=int)
args = parser.parse_args()
if args.i <= 0:
sys.exit("Error: maximum nb of iterations must be an integer greater than 0!")
if args.m not in ("ACO", "Tree_ACO", "PSO", "firework"):
sys.exit(
"Error: local method must be in: [ACO, Tree_ACO, PSO, firework]!")
return args.i, args.n1, args.n2, args.n3, args.m, args.n, args.nt, args.alpha, args.rho, args.q, args.c1, args.c2, args.w, args.t, args.a, args.b, args.mns, args.amp, args.ngs
def ask_for(question, default_value):
res = input(question)
if res == "":
return default_value
else:
return int(res)
i, n1, n2, n3, method, n, n_threads_max, alpha, rho, Q, c1, c2, w, timeout, a, b, max_number_sparks, amplitude, gauss_sparks = cmdLineParsing()
if not len(sys.argv) > 1:
type_algo = int(
input(
"Which algorithm would you want to execute ? classic ant colony (0) or tree ant colony (1) or particle swarm (2)? "
)
)
while type_algo not in [0, 1, 2]:
print("please enter a valid algorithm")
type_algo = int(
input(
"Which algorithm would you want to execute ? classic ant colony (0) or tree ant colony (1) or particle swarm (2)? "
)
)
if type_algo == 0 or type_algo == 1:
nb_iter = ask_for(
"number of iterations (10 by default) : ", DefaultIterMax)
nb_ants = ask_for(
"number of ants per iteration (100 by default) : ", Defaultnumberpopulation
)
evaporation_rate = input(
"evaporation_rate (0.8 by default) : ") or Defaultalpha
n1_size = ask_for(
"size of the problem x (default size 256) : ", Defaultn1)
n2_size = ask_for(
"size of the problem y (default size 256) : ", Defaultn2)
n3_size = ask_for(
"size of the problem z (default size 256) : ", Defaultn3)
n_threads_max = ask_for(
"maximum number of threads (32 by default): ", 32)
if type_algo == 0:
print("executing classic ant colony optimization...")
problem = [n1_size, n2_size, n3_size]
ant(nb_ants, nb_iter, problem, rho, evaporation_rate, Q, timeout)
if type_algo == 1:
print("executing ant colony optimization on a tree...")
tree_ant(compil_flag_list, simd_list, n_threads_max, n1_size,
n2_size, n3_size, evaporation_rate, nb_iter, nb_ants, timeout)
if type_algo == 2:
max_iterations = ask_for("number of iterations (10 by default) : ", 10)
num_particles = ask_for(
"number of particles per iteration (100 by default) : ", 100
)
c1 = input("particle speed (1.5 by default) : ") or 1.5
c2 = input("group_speed (1.5 by default) : ") or 1.5
w = input("inertia (0.5 by default) : ") or Defaultw
problem_1 = ask_for(
"size of the problem x (default size 256) : ", Defaultn1)
problem_2 = ask_for(
"size of the problem y (default size 256) : ", Defaultn2)
problem_3 = ask_for(
"size of the problem z (default size 256) : ", Defaultn3)
n_threads_max = ask_for(
"maximum number of threads (32 by default) : ", 32)
timeout = ask_for("duration before timeout (30 by default) : ", 30)
print("executing particle swarm optimization...")
bounds = [
(0, 2),
(0, 2),
(1, n_threads_max),
(16, problem_1),
(1, problem_2),
(1, problem_3),
]
optimizer = ParticleSwarmOptimization(
objective_function,
bounds,
num_particles,
max_iterations,
c1,
c2,
w,
[problem_1, problem_2, problem_3],
timeout,
)
solution = optimizer.optimize()
solution_parameters = []
solution_parameters.append(compil_flag_list[solution[0][0]])
solution_parameters.append(simd_list[solution[0][1]])
solution_parameters.append(solution[0][2])
solution_parameters.append(solution[0][3])
solution_parameters.append(solution[0][4])
print("Solution: ", solution_parameters)
print("Fitness value: ", solution[1])
if len(sys.argv) > 1:
if method == "ACO":
problem = [n1, n2, n3]
ant(n, i, problem, rho, alpha, Q, timeout)
if method == "Tree_ACO":
tree_ant(
compil_flag_list, simd_list, n_threads_max, n1, n2, n3, alpha, i, n, timeout
)
if method == "PSO":
bounds = [(0, 2), (0, 2), (1, n_threads_max),
(16, n1), (1, n2), (1, n3)]
optimizer = ParticleSwarmOptimization(
objective_function, bounds, n, i, c1, c2, w, [n1, n2, n3], timeout,
)
solution = optimizer.optimize()
solution_parameters = [
compil_flag_list[solution[0][0]],
simd_list[solution[0][1]],
solution[0][2],
solution[0][3],
solution[0][4],
]
print("Solution: ", solution_parameters)
print("Fitness value: ", solution[1])
if method == "firework":
res = firework(n, a, b, "euclide", max_number_sparks,
gauss_sparks, amplitude, [n1,n2,n3],timeout)
print(res)