-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
341 lines (274 loc) · 15 KB
/
main.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""\
------------------------------------------------------------
USAGE: <PROGNAME> [-h] [-b BENCHMARK] [-s]
[--benchmark_params [BENCHMARK_PARAMS [BENCHMARK_PARAMS ...]]]
[--sat_files SAT_FILES] [--acceptors ACCEPTORS [ACCEPTORS ...]]
[--acceptor_probs ACCEPTOR_PROBS [ACCEPTOR_PROBS ...]]
[--max_mutation MAX_MUTATION] [--num_run NUM_RUN]
[--log_file LOG_FILE] [--dump_file DUMP_FILE] [-s]
OPTIONS:
-h, --help show this help message and exit
-b BENCHMARK, --benchmark BENCHMARK
BENCHMARK : the benchmark to be tested on (Benchmark
in {OneMax, Cliff, Jump, Sat, GapPath default: Sat}
--benchmark_params [BENCHMARK_PARAMS [BENCHMARK_PARAMS ...]]
benchmark problem parameters if one of [OneMax, Cliff,
Jump] is selected
--sat_files SAT_FILES
CNF files if SAT benchmark is selected
--acceptors ACCEPTORS [ACCEPTORS ...]
a list of acceptors chosen to be selected
--acceptor_probs ACCEPTOR_PROBS [ACCEPTOR_PROBS ...]
probabilities of given acceptors
--max_mutation MAX_MUTATION
maximum number of mutation
--num_run NUM_RUN number of run for each problem instance
--log_file LOG_FILE logging file
--dump_file DUMP_FILE
file that store the json.dumps() result
-s, --show show the statistic bar chart based on pervious data
------------------------------------------------------------\
"""
import os, re, sys, logging, json, argparse
from ttictoc import TicToc
from statistics import mean, median
import matplotlib.pyplot as plt
from math import ceil
import numpy as np
from HH import *
from Satlib import *
from Acceptor import *
from Benchmark import *
#==============================================================================
# Command line processing
def proc_cmd():
cli=argparse.ArgumentParser()
cli.add_argument("-b", "--benchmark",
help="BENCHMARK : the benchmark to be tested on (Benchmark in {OneMax, Cliff, Jump, Sat, GapPath default: Sat}")
cli.add_argument("--benchmark_params", nargs="*", help="benchmark problem parameters if one of [OneMax, Cliff, Jump] is selected")
cli.add_argument("--sat_files", default="./sat/", help="CNF files if SAT benchmark is selected")
cli.add_argument("--acceptors", nargs="+", default=["am", "oi"], help="a list of acceptors chosen to be selected")
cli.add_argument("--acceptor_probs", nargs="+", type=float, default=[1.0, 1.0], help="probabilities of given acceptors")
cli.add_argument("--max_mutation", type=int, default="200000", help="maximum number of mutation")
cli.add_argument("--num_run", type=int, default=100, help="number of run for each problem instance")
cli.add_argument("--log_file", default="hh.log", help="logging file")
cli.add_argument("--dump_file", default="hh_data.dump", help="file that store the json.dumps() result")
cli.add_argument("-s", "--show", action="store_true", help="show the statistic bar chart based on pervious data")
args=cli.parse_args()
return cli, args
def test_leadingone(args):
benchmark=Benchmark.benchmark_factory(args.benchmark, *args.benchmark_params)
hh_lo=HH(args.acceptors, args.acceptor_probs, benchmark, args.max_mutation) # 2/n
hh_lo.optimize()
hh_lo.stat()
# oneMax=OneMax(*args.benchmark_params)
# hh_one=HH(args.acceptors, args.acceptor_probs, oneMax, args.max_mutation) # 2/n
# hh_one.optimize()
# hh_one.stat()
# cliff=Cliff(probability=0.2, n=100, d=25)
# hh_cliff=HH([am, oi], [0.01, 0.99], cliff)
# hh_cliff.optimize()
# hh_cliff.stat()
#
# jump=Jump(probability=0.2, n=100, m=25)
# hh_jump=HH([am, oi], [1, 1], jump)
# hh_jump.optimize()
# hh_jump.stat()
def main():
# test_leadingone()
cli, args=proc_cmd()
logging.basicConfig(filename=args.log_file, level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.info(args)
if args.benchmark:
if args.benchmark.lower() in ["onemax", "cliff", "jump", "sat", "gappath"]:
if args.benchmark.lower()=='sat':
p, pattern=os.path.split(args.sat_files)
print(p, pattern)
if not (os.path.exists(p) and os.path.isdir(p)):
warning = ( "*** ERROR: directory for sat files (opt: --sat_files SAT_DIR default: ./sat)! ***\n"
" -- path (%s) not exists!\n"
" -- must be a existent directory"
) % (args.sat_files)
print(warning, file=sys.stderr)
cli.print_help()
sys.exit()
else:
test_sat(args)
else:
test_leadingone(args)
else:
warning = (
"*** ERROR: benchmark for Hyper-Heuristic (opt: --benchmark BENCHMARK default: Sat)! ***\n"
" -- value (%s) not recognised!\n"
" -- must be one of: OneMax / Cliff / Jump / Sat / GapPath "
) % (args.benchmark)
print(warning, file=sys.stderr)
cli.print_help()
sys.exit()
if args.dump_file and args.show:
draw_sat_stat_graph(args.dump_file)
def test_sat(args):
hh_sat=HH(args.acceptors, args.acceptor_probs, None, args.max_mutation) # 2/n
uf_li, (uf_mutation, uf_goal, uf_runtime, uf_mutation_to_max_goal), (uf_num, uf_global_ind, uf_mutation_li, uf_goal_li, uf_runtime_li)=\
opt_sat(args.sat_files, hh_sat, args.num_run)
dump_to_file(args.dump_file, uf_li=uf_li, uf_mutation=uf_mutation, uf_goal=uf_goal, uf_runtime=uf_runtime,
uf_mutation_to_max_goal=uf_mutation_to_max_goal)
dump_to_file(args.dump_file, uf_num=uf_num, uf_global=uf_global_ind,
uf_mutation_li=uf_mutation_li, uf_goal_li=uf_goal_li, uf_runtime_li=uf_runtime_li)
(average_mutation, average_goal, average_runtime), (median_mutation, median_goal, median_runtime)=\
calculate_stat(uf_mutation_li, uf_goal_li, uf_runtime_li)
dump_to_file(args.dump_file, average_mutation=average_mutation, average_goal=average_goal, average_runtime=average_runtime,
median_mutation=median_mutation, median_goal=median_goal, median_runtime=median_runtime)
def draw_sat_stat_graph(dump_file):
num_per_subp=4
uf_num, average_mutation, average_goal, median_mutation, median_goal=\
loads_from_file(dump_file, "uf_num", "average_mutation", "average_goal", "median_mutation", "median_goal")
draw_bar_charts(num_per_subp, uf_num, average_mutation, average_goal,
median_mutation, median_goal)
# uf_num, average_mutation, average_goal, average_runtime, median_mutation, median_goal, median_runtime= \
# loads_from_file(dump_file, "uf_num", "average_mutation", "average_goal", "median_mutation", "median_goal")
# draw_bar_charts(num_per_subp, uf_num, average_mutation, average_goal, average_runtime,
# median_mutation, median_goal, median_runtime)
def opt_sat(sat_files, hh_sat, num_run):
# uf_li=list(pathlib.Path(dir_name).glob("**/*.cnf"))
p, pattern=os.path.split(sat_files)
uf_li=[os.path.join(p, x) for x in os.listdir(p) if re.fullmatch(pattern, x)]
num_instance=len(uf_li)
uf_mutation=[[0]*num_run for _ in range(num_instance)]
uf_mutation_to_max_goal=[[0]*num_run for _ in range(num_instance)]
uf_goal=[[0]*num_run for _ in range(num_instance)]
uf_runtime=[[0]*num_run for _ in range(num_instance)]
uf_num=[[0]*num_instance for _ in range(2)]
uf_global_ind=[list() for _ in range(2)]
uf_mutation_li=[[list() for _ in range(num_instance)] for _ in range(2)]
uf_goal_li=[[list() for _ in range(num_instance)] for _ in range(2)]
uf_runtime_li=[[list() for _ in range(num_instance)] for _ in range(2)]
t=TicToc()
for instance_id in range(num_instance):
logging.info("======NEXT INSTANCE:{}======".format(instance_id))
sat=Sat(str(uf_li[instance_id]))
sat.print_inner_var()
for run_id in range(num_run):
logging.info("------NEXT RUN:{}------".format(run_id))
t.tic()
sat.reset_solution()
hh_sat.reset_benchmark(sat)
hh_sat.optimize()
hh_sat.stat()
t.toc()
uf_mutation[instance_id][run_id]=hh_sat.num_mutate
uf_mutation_to_max_goal[instance_id][run_id]=hh_sat.num_mutate_to_max_goal
uf_goal[instance_id][run_id]=hh_sat.max_goal
uf_runtime[instance_id][run_id]=t.elapsed
# find global optima within max_mutate
if hh_sat.max_goal==sat.num_cla:
uf_num[0][instance_id]+=1
uf_global_ind[0].append((instance_id, run_id))
uf_mutation_li[0][instance_id].append(hh_sat.num_mutate)
uf_goal_li[0][instance_id].append(hh_sat.max_goal)
uf_runtime_li[0][instance_id].append(t.elapsed)
# didn't find global optima within max_mutate
else:
uf_num[1][instance_id]+=1
uf_global_ind[1].append((instance_id, run_id))
uf_mutation_li[1][instance_id].append(hh_sat.num_mutate)
uf_goal_li[1][instance_id].append(hh_sat.max_goal)
uf_runtime_li[1][instance_id].append(t.elapsed)
return uf_li, (uf_mutation, uf_goal, uf_runtime, uf_mutation_to_max_goal), (uf_num, uf_global_ind, uf_mutation_li, uf_goal_li, uf_runtime_li)
def calculate_stat(uf_mutation_li, uf_goal_li, uf_runtime_li):
average_goal=[list() for _ in range(2)]
median_goal=[list() for _ in range(2)]
average_mutation=[list() for _ in range(2)]
median_mutation=[list() for _ in range(2)]
average_runtime=[list() for _ in range(2)]
median_runtime=[list() for _ in range(2)]
for instance_id in range(len(uf_mutation_li[0])):
for suc_id in range(2):
if uf_mutation_li[suc_id][instance_id]:
average_mutation[suc_id].append(mean(uf_mutation_li[suc_id][instance_id]))
median_mutation[suc_id].append(median(uf_mutation_li[suc_id][instance_id]))
else:
average_mutation[suc_id].append(0)
median_mutation[suc_id].append(0)
if uf_goal_li[suc_id][instance_id]:
average_goal[suc_id].append(mean(uf_goal_li[suc_id][instance_id]))
median_goal[suc_id].append(median(uf_goal_li[suc_id][instance_id]))
else:
average_goal[suc_id].append(0)
median_goal[suc_id].append(0)
if uf_runtime_li[suc_id][instance_id]:
average_runtime[suc_id].append(mean(uf_runtime_li[suc_id][instance_id]))
median_runtime[suc_id].append(median(uf_runtime_li[suc_id][instance_id]))
else:
average_runtime[suc_id].append(0)
median_runtime[suc_id].append(0)
return (average_mutation, average_goal, average_runtime), (median_mutation, median_goal, median_runtime)
# def draw_bar_charts(inst_per_subp, uf_num, average_mutation, average_goal, average_runtime,
# median_mutation, median_goal, median_runtime):
def draw_bar_charts(inst_per_subp, uf_num, average_mutation, average_goal,
median_mutation, median_goal):
width=0.35
fig=plt.figure()
fig.suptitle("Average goal/step for each instance")
fig, ax_lst=plt.subplots(ceil(len(uf_num[0])/inst_per_subp), 3)
for instance_subp_id in range(0, len(uf_num[0]), inst_per_subp):
x_ind=np.arange(instance_subp_id, instance_subp_id+inst_per_subp)
time_glo=ax_lst[int(instance_subp_id/inst_per_subp)][0]\
.bar(x_ind-width/2, uf_num[0][instance_subp_id:instance_subp_id+inst_per_subp], width,
label="Time for instances that achieved global optima".format())
time_loc=ax_lst[int(instance_subp_id/inst_per_subp)][0]\
.bar(x_ind+width/2, uf_num[1][instance_subp_id:instance_subp_id+inst_per_subp], width,
label="Time for instances that didn't achieved global optima".format())
autolabel(ax_lst[int(instance_subp_id/inst_per_subp)][0], time_glo, "left")
autolabel(ax_lst[int(instance_subp_id/inst_per_subp)][0], time_loc, "right")
for id in range(inst_per_subp):
instance_id=instance_subp_id+id
print("The times for instance {} that achieved global optima is {}, times that didn't achieved is {}"
.format(instance_id, uf_num[0][instance_id], uf_num[1][instance_id]))
if uf_num[0][instance_id]!=0:
print("The average mutation to achieve global optima {} is {}".
format(average_goal[0][instance_id], average_mutation[0][instance_id]))
if uf_num[1][instance_id]!=0:
print("The average maximum goal achieved within {} steps is {}".
format(average_mutation[1][instance_id], average_goal[1][instance_id]))
local_goal=ax_lst[int(instance_subp_id/inst_per_subp), 1].\
bar(x_ind, np.array(average_goal[1][instance_subp_id:instance_subp_id+inst_per_subp]), width, label="")
# autolabel(ax_lst[int(instance_subp_id/inst_per_subp), 1], local_goal, "left")
global_mutaiton=ax_lst[int(instance_subp_id/inst_per_subp), 2].\
bar(x_ind, np.array(average_mutation[0][instance_subp_id:instance_subp_id+inst_per_subp]), width, label="")
# autolabel(ax_lst[int(instance_subp_id/inst_per_subp), 2], global_mutaiton, "left")
fig.show()
return average_goal, average_mutation
def autolabel(ax, rects, xpos='center'):
"""
Attach a text label above each bar in *rects*, displaying its height.
*xpos* indicates which side to place the text w.r.t. the center of
the bar. It can be one of the following {'center', 'right', 'left'}.
"""
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0, 'right': 1, 'left': -1}
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(offset[xpos]*3, 3), # use 3 points offset
textcoords="offset points", # in both directions
ha=ha[xpos], va='bottom')
def dump_to_file(filename, **kwargs):
with open(filename, 'a') as f:
for k, v in kwargs.items():
f.write(k+'\n')
f.write(json.dumps(v)+'\n')
def loads_from_file(filename, *argv):
res=[None]*len(argv)
with open(filename, 'r') as f:
while True:
line=f.readline()
if not line:
break
if line[:-1].lower() in argv:
res[argv.index(line[:-1].lower())]=json.loads(f.readline())
return tuple(res)
if __name__ == '__main__':
main()