-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_solver_commands.py
More file actions
170 lines (141 loc) · 6.26 KB
/
setup_solver_commands.py
File metadata and controls
170 lines (141 loc) · 6.26 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
import argparse
import itertools
import logging
import os
import random
import sys
import numpy as np
import pandas as pd
import config
import data_loader
from data_loader import get_best_result
from solve.helper import export_configurations
parser = argparse.ArgumentParser()
parser.add_argument('problem')
parser.add_argument('-s', '--solver', nargs='+', choices=['all'] + list(config.SOLVER.keys()),
default=['sicstus'])
parser.add_argument('-t', '--timeout', type=int, default=config.TIMEOUT)
parser.add_argument('-lb', '--lower_boundaries', nargs='+', default=[None, 1.0, 0.98, 0.95, 0.9, 0.8, 0.5])
parser.add_argument('-ub', '--upper_boundaries', nargs='+', default=[None, 1.0, 1.02, 1.05, 1.1, 1.2, 1.5])
parser.add_argument('-n', '--instances', type=int, default=10)
parser.add_argument('--cluster', action='store_true', default=False,
help='Store commands in instance format for cluster execution (abel)')
parser.add_argument('-v', "--verbose", help="increase output verbosity",
action="store_true")
parser.add_argument('--instance-filter', default=None)
parser.add_argument('--select-best', action="store_true", default=False,
help='Select instances with the best estimations, otherwise random')
parser.add_argument('--predictions', nargs='+', default=None)
args = parser.parse_args()
solvers = config.SOLVER.values() if 'all' in args.solver else [config.SOLVER[key] for key in args.solver]
try:
problem = next(p for p in config.PROBLEMS if args.problem == p.name)
except StopIteration:
logging.error('Problem %s does not exist in problem list' % args.problem.name)
sys.exit(1)
result_filename = 'run_motivation_{}.sh'.format(problem.name)
if args.predictions:
pred_cache = 'logs/{}_median_estimations.p'.format(problem.name)
if not os.path.isfile(pred_cache):
filter_fn = lambda df: pd.DataFrame(df.loc[(df.estimator == 'networka') & (df.loss == 'shiftedmse') & (df.adjustment == 0.1)])
preds = data_loader.load_predict_log(args.predictions, problem.name, filter_fn)
preds = preds.groupby(['problem', 'estimator', 'adjustment', 'loss', 'outputs', 'dzn'], as_index=False).median()
preds.to_pickle(pred_cache)
else:
preds = pd.read_pickle(pred_cache)
configurations = []
mzn_path = os.path.join(problem.basedir, problem.name, problem.mzn)
avail_dzns = problem.get_dzns()
if args.instance_filter:
feasible_instances = open(args.instance_filter).read().splitlines()
dzns = [d for d in avail_dzns if d[1] in feasible_instances]
if 0 < args.instances < len(dzns):
dzns = random.sample(dzns, args.instances)
elif args.predictions:
preds = preds[((preds['underest'] <= preds['optimum']) & (preds['optimum'] <= preds['overest']))]
dzns = random.sample([d for d in avail_dzns if d[0] in preds.dzn.tolist()], args.instances)
elif 0 < args.instances < len(avail_dzns):
dzns = random.sample(avail_dzns, args.instances)
else:
dzns = avail_dzns
dzns.sort(key=lambda x: x[1])
if args.predictions:
dzn_names = [dzn_name for dzn_name, _ in dzns]
preds = preds[preds.dzn.isin(dzn_names)]
for i, (dzn_name, dzn_path) in enumerate(dzns):
try:
best_result = get_best_result(dzn_path)
except ValueError as e:
logging.warning('No boundary found: %s: %s: %s' % (problem.name, dzn_name, e))
continue
boundaries = []
if args.predictions and not args.select_best:
for lb, ub in preds[preds.dzn == dzn_name][['underest', 'overest']].values:
if np.isnan(lb):
lb = None
else:
lb = int(np.floor(lb))
if np.isnan(ub):
ub = None
else:
ub = int(np.ceil(ub))
boundaries.append((lb, ub, lb, ub))
else:
for lb, ub in itertools.product(args.lower_boundaries, args.upper_boundaries):
if lb == 'None':
lb = None
if ub == 'None':
ub = None
if lb:
lbv = np.floor(lb * best_result).astype(int)
else:
lbv = None
if ub:
ubv = np.ceil(ub * best_result).astype(int)
else:
ubv = None
if (lbv, ubv) in boundaries:
continue
boundaries.append((lb, ub, lbv, ubv))
for lb, ub, lbv, ubv in boundaries:
for s in solvers:
ds = '({},{})'.format('%.2f' % lb if lb is not None else 'None',
'%.2f' % ub if ub is not None else 'None')
conf = config.ExecConfig(solver=s, problem=problem, mzn_path=mzn_path, dzn=dzn_name,
dzn_path=dzn_path, lower_bound_value=lbv, upper_bound_value=ubv,
lower_bound=lb, upper_bound=ub,
timeout=args.timeout, dataset=ds)
configurations.append(conf)
if args.cluster:
with open(result_filename, 'w') as writer:
for conf in configurations:
if conf.has_lower_bound:
if conf.solver.name == 'sunnycp':
bound_param = '-l'
else:
bound_param = '-lb'
lbarg = '{} {:d}'.format(bound_param, conf.lower_bound_value)
else:
lbarg = ''
if conf.has_upper_bound:
if conf.solver.name == 'sunnycp':
bound_param = '-u'
else:
bound_param = '-ub'
ubarg = '{} {:d}'.format(bound_param, conf.upper_bound_value)
else:
ubarg = ''
script_line = '{solver};{mzn};{dzn};{lbarg};{ubarg};{timeout:d};{addargs};{comment}'.format(
solver=conf.solver.name,
mzn=conf.mzn_path,
dzn=conf.dzn_path,
lbarg=lbarg,
ubarg=ubarg,
timeout=conf.timeout,
addargs='-var {}'.format(conf.problem.objective_var),
comment=conf.dataset)
print(script_line, file=writer)
else:
csv_filename = 'motivation_{}.csv'.format(problem.name)
# run_configurations(configurations, csv_filename)
export_configurations(configurations, csv_filename, result_filename)