-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
268 lines (192 loc) · 7.47 KB
/
data_loader.py
File metadata and controls
268 lines (192 loc) · 7.47 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
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
import glob
import json
import logging
import multiprocessing
import os
import pickle
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
import config
import dzn_eval
logger = logging.getLogger('combination-learning')
def flatten(lst):
flattened = []
if not isinstance(lst, (list, tuple, set)):
return [lst]
for sublist in lst:
if isinstance(sublist, (list, tuple, set)):
flattened.extend(flatten(sublist))
elif isinstance(sublist, dict):
flattened.extend(flatten(sorted(sublist.items(), key=lambda x: x[0])))
else:
flattened.append(sublist)
return flattened
def dzn_input_string(dznfile, delim='#'):
#dzn = pymzn.dzn_eval(dznfile)
dzn = dzn_eval.dzn_eval(dznfile)
sorted_values = []
for (_, v) in sorted(dzn.items(), key=lambda x: x[0]):
sorted_values.extend(flatten(v))
if delim:
sorted_values.append(delim)
if delim:
del sorted_values[-1]
return sorted_values
def join_result_csvs(csv_dir, keyword):
sub_dfs = []
for f in glob.glob(os.path.join(csv_dir, keyword)):
try:
pdf = pd.read_csv(f, sep=';', na_values='None', dtype={'Failed': bool})
if len(pdf) == 0:
print(f, 'is empty')
continue
if 'HasBound' not in pdf.columns:
pdf['HasBound'] = False
pdf['ObjBound'] = np.nan
sub_dfs.append(pdf)
except Exception as e:
print('File %s: %s' % (f, e))
if len(sub_dfs) > 1:
df = pd.concat(sub_dfs, axis=0)
else:
df = sub_dfs[0]
df['Failed'] = df['Failed'].astype('bool')
df.loc[~df.Complete, 'Duration'] = df[~df.Complete]['Duration'].astype('int')
return df
def get_baseline(problem=None, include_failed=False):
csv_dir = config.OUTPUT_DIR
baseline = join_result_csvs(csv_dir, 'baseline_*.csv')
# This is mostly for debug purposes, but might help for actual evaluations
# We limit the considered solvers and problems
if problem:
problem_names = [problem.name]
else:
problem_names = [p.name for p in config.PROBLEMS]
#rel_solvers = ['chuffed', 'cbc', 'gecode']
#baseline = baseline[(baseline.Problem.isin(problem_names) & (baseline.Solver.isin(rel_solvers)))]
baseline = baseline[(baseline.Problem.isin(problem_names))]
if not include_failed:
baseline = baseline[~baseline.Failed]
return baseline
def generate_benchmark_sample(sample_size=20, max_duration=180):
fn = lambda x: x.iloc[np.random.choice(range(0, len(x)), min(sample_size, len(x)), replace=False)]
df = get_baseline()
df = df[['Problem', 'DZN', 'Duration']].groupby(['Problem', 'DZN'], as_index=False).max()
df = df[df.Duration < max_duration][['Problem', 'DZN']].drop_duplicates()
df = df.groupby('Problem', as_index=False).apply(fn)
return df
def missing_optima(problem_set=config.PROBLEMS):
"""
Lists all problems and their instances where the optimum result has not been found.
For each problem instance the previous best result (from the baseline results) is given in column `Best`.
:return: pd.DataFrame with columns ['Problem', 'DZN', 'Best']
"""
missing_optima = []
baseline = get_baseline()
for problem in problem_set:
for dzn, dzn_path in problem.get_dzns():
opt_path = dzn_path + '.opt'
if not os.path.isfile(opt_path):
if problem.minmax == 'min':
prev_best = baseline[(baseline.Problem == problem.name) & (baseline.DZN == dzn)]['Objective'].min()
else:
prev_best = baseline[(baseline.Problem == problem.name) & (baseline.DZN == dzn)]['Objective'].max()
missing_optima.append((problem.name, dzn, prev_best))
return pd.DataFrame(missing_optima, columns=['Problem', 'DZN', 'Best'])
def missing_results(problem_set=config.PROBLEMS):
"""
Returns a DataFrame with columns ['Problem', 'DZN'], listing all problems and their instances without any result.
Considers opt result files and the existing baseline results.
"""
missing_results = missing_optima(problem_set)
return missing_results[missing_results.Best.isnull()][['Problem', 'DZN']]
def get_best_results(problem_set=config.PROBLEMS, dzn_filter=None):
rows = []
for problem in problem_set:
for dzn, dzn_path in problem.get_dzns(dzn_filter=dzn_filter):
opt, stat = get_best_result(dzn_path, include_status=True)
rows.append((problem.name, problem.mzn, dzn, opt, stat))
return pd.DataFrame(rows, columns=['problem', 'mzn', 'dzn', 'objective', 'status'])
def get_best_result(dzn_path, optimum_only=False, include_status=False):
opt_path = dzn_path + '.opt'
best_path = dzn_path + '.best'
if os.path.isfile(opt_path):
raw_optimum = open(opt_path, 'r').read()
status = 'COMPLETE'
elif not optimum_only and os.path.exists(best_path):
raw_optimum = open(best_path, 'r').read()
status = 'SOLFOUND'
else:
logging.warning('No result for %s' % dzn_path)
raw_optimum = 0
status = 'UNKNOWN'
if include_status:
return int(raw_optimum), status
else:
return int(raw_optimum)
def split_train_test_instances(problem, output_file, nb_datasets):
dzns = [dzn for dzn, path in problem.get_dzns()]
datasets = []
kf = KFold(n_splits=nb_datasets, shuffle=True)
for train, test in kf.split(dzns):
ds = {
'train': [dzns[x] for x in train],
'test': [dzns[x] for x in test]
}
datasets.append(ds)
pickle.dump(datasets, open(output_file, 'wb'), protocol=pickle.HIGHEST_PROTOCOL)
return datasets
def load_stats_log(files, problem_filter=None):
if isinstance(files, str):
files = [files]
files2 = []
for f in files:
files2.extend(glob.glob(f))
partial_dfs = []
for f in files2:
if open(f, 'r').read(1) == "{":
# JSON-based format
records = []
for x in open(f, 'r'):
if problem_filter and '"{}"'.format(problem_filter) not in x:
break
records.append(json.loads(x))
partial_dfs.append(pd.DataFrame.from_records(records))
else:
# CSV format
try:
partial_dfs.append(pd.read_csv(f))
except:
print("Failed to read file: ", f)
return pd.concat(partial_dfs)
def load_predict_log(files, problem_filter=None, filter_fn=None):
if isinstance(files, str):
files = [files]
files2 = []
for f in files:
files2.extend(glob.glob(f))
dfs = []
with multiprocessing.Pool() as p:
for df in p.imap(load_predict_log_file, files2):
if problem_filter:
df = df[df.problem == problem_filter]
if filter_fn:
df = filter_fn(df)
dfs.append(df)
return pd.concat(dfs)
def load_predict_log_file(filepath):
if open(filepath, 'r').read(1) == "{":
records = []
for x in open(filepath, 'r'):
try:
rec = pd.read_json(x.strip())
except:
rec = pd.Series(json.loads(x.strip()))
records.append(rec)
if len(records) == 0:
return None
df = pd.concat(records)
else:
df = pd.read_csv(filepath)
return df