-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
239 lines (202 loc) · 7.03 KB
/
tasks.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
from pathlib import Path
from invoke import task, Failure
import optlis
from optlis import static, dynamic
ILS_MAX_EVALUATIONS = 0 # auto: |V| * 10_000 evaluations
CPLEX_TIME_LIMIT = 14_000 # 4 hours
@task
def check(c):
"""Checks all the required dependencies."""
print("Checking gcc...", end="")
has_gcc = c.run("which gcc", warn=True, hide=True)
print("OK" if has_gcc else "not found")
# @task
# def build(c):
# """Builds the c library with the gcc compiler."""
# build_dir, lib_dir = Path("./build"), Path("./lib")
# build_dir.mkdir(exist_ok=True)
# lib_dir.mkdir(exist_ok=True)
# print("Building c library with gcc...", end="")
# try:
# c.run(
# "gcc -c -fPIC optlis/solvers/localsearch.c -o "
# f"{build_dir / 'localsearch.o'}"
# )
# c.run(
# f"gcc -shared -Wl,-soname,localsearch.so -o "
# f"{lib_dir / 'localsearch.so'} {build_dir / 'localsearch.o'}"
# )
# except Failure as ex:
# print(ex)
# else:
# print("Done")
@task(
help={
"export_dir": "Directory to export instances.",
"seed": "Sets the seed for the random number generator (default 0).",
}
)
def export_benchmark(c, export_dir, seed=0):
"""Exports the instance benchmark to disk."""
export_to = Path(export_dir)
# Exports static instances
export_to_static = export_to / "static"
export_to_static.mkdir(parents=True, exist_ok=True)
optlis.static.instance_benchmark.generate_benchmark(export_to_static, seed)
# Exports dynamic instances
export_to_dynamic = export_to / "dynamic"
export_to_dynamic.mkdir(parents=True, exist_ok=True)
optlis.dynamic.instance_benchmark.generate_benchmark(export_to_dynamic, seed)
@task(
help={
"solver": "Chooses the optimization method (cplex or ils)",
"inst_dir": "Directory where instances are located",
"dynamic": "Sets the problem type to dynamic",
"relaxation": "Sets the relaxation threshold (in range [0, 1] default 0)",
"repeat": "Sets the number of repetitions to perform (ils only, default 35)",
"parallel": "Sets the number of parallel processes (ils only, default 4)",
"tt-off": "Disables travel times",
"log_dir": "Directory to export execution logs",
"sol_dir": "Directory to export solutions",
}
)
def bulk_solve(
c,
solver,
inst_dir,
dynamic=False,
relaxation=0.0,
repeat=35,
parallel=4,
tt_off=False,
log_dir=None,
sol_dir=None,
):
"""Solves all instances located in the 'inst-dir' directory."""
if solver.lower() == "ils":
if dynamic:
_bulk_solve_dynamic_ils(
inst_dir, ILS_MAX_EVALUATIONS, repeat, parallel, log_dir
)
else:
_bulk_solve_static_ils(
inst_dir,
relaxation,
ILS_MAX_EVALUATIONS,
repeat,
parallel,
tt_off,
log_dir,
)
elif solver.lower() == "cplex":
if dynamic:
_bulk_solve_dynamic_cplex(inst_dir, CPLEX_TIME_LIMIT, log_dir, sol_dir)
else:
_bulk_solve_static_cplex(
inst_dir, relaxation, CPLEX_TIME_LIMIT, tt_off, log_dir, sol_dir
)
else:
raise ValueError(f"'{solver}' is not a valid option, use 'cplex' or 'ils'")
def _bulk_solve_static_ils(
inst_dir, relaxation, stop, repeat, parallel, tt_off, log_dir
):
inst_paths = sorted(Path(inst_dir).glob("hx-*.dat"))
for i, path in enumerate(inst_paths, start=1):
print(f"Solving instance {path} ({i} of {len(inst_paths)})...")
instance = static.problem_data.load_instance(path, not tt_off)
if log_dir:
log_path = Path(log_dir) / f"{path.stem}.log"
else:
log_path = None
results = static.models.ils.optimize(
instance=instance,
runs=repeat,
parallel=parallel,
perturbation_strength=_get_irace_static_config(tt_off, relaxation),
relaxation_threshold=relaxation,
evaluations=stop,
log_path=log_path,
)
def _bulk_solve_dynamic_ils(inst_dir, stop, repeat, parallel, log_dir):
inst_paths = sorted(Path(inst_dir).glob("hx-*.dat"))
for i, path in enumerate(inst_paths, start=1):
print(f"Solving instance {path} ({i} of {len(inst_paths)})...")
instance = dynamic.problem_data.load_instance(path)
if log_dir:
log_path = Path(log_dir) / f"{path.stem}.log"
else:
log_path = None
results = dynamic.models.ils.optimize(
instance=instance,
runs=repeat,
parallel=parallel,
perturbation_strength=0.5,
evaluations=stop,
log_path=log_path,
)
def _bulk_solve_static_cplex(
inst_dir, relaxation, time_limit, tt_off, log_dir, sol_dir
):
if tt_off:
model = static.models.milp
else:
model = static.models.milp.model_2
inst_paths = sorted(Path(inst_dir).glob("hx-*.dat"))
for i, path in enumerate(inst_paths, start=1):
print(f"Solving instance {path} ({i} of {len(inst_paths)})...")
instance = static.problem_data.load_instance(path, not tt_off)
if sol_dir:
sol_path = Path(sol_dir) / f"{path.stem}.sol"
else:
sol_path = None
if log_dir:
log_path = Path(log_dir) / f"{path.stem}.log"
else:
log_path = None
results = static.models.milp.optimize(
instance=instance,
make_model=model,
relaxation_threshold=relaxation,
time_limit=time_limit,
log_path=log_path,
sol_path=sol_path,
)
print("")
def _bulk_solve_dynamic_cplex(inst_dir, time_limit, log_dir, sol_dir):
inst_paths = sorted(Path(inst_dir).glob("hx-*.dat"))
for i, path in enumerate(inst_paths, start=1):
print(f"Solving instance {path} ({i} of {len(inst_paths)})...")
instance = dynamic.problem_data.load_instance(path)
if sol_dir:
sol_path = Path(sol_dir) / f"{path.stem}.sol"
else:
sol_path = None
if log_dir:
log_path = Path(log_dir) / f"{path.stem}.log"
else:
log_path = None
results = dynamic.models.milp.optimize(
instance=instance,
time_limit=time_limit,
log_path=log_path,
sol_path=sol_path,
)
print("")
def _get_irace_static_config(tt_off, relaxation):
"""These values were separately generated by the irace package and are hardcoded
here for the purpuse of repeatability of results.
"""
if tt_off:
if relaxation == 0:
return 0.5
elif relaxation == 0.5:
return 0.56
else:
return 0.24
else:
if relaxation == 0:
return 0.61
elif relaxation == 0.5:
return 0.19
else:
return 0.86