Repository navigation
Expand file tree
/
Copy pathgenerate_scenario.py
More file actions
135 lines (109 loc) · 4.83 KB
/
Copy pathgenerate_scenario.py
File metadata and controls
135 lines (109 loc) · 4.83 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
"""
generate_scenario.py -- reads performance data and features from a csv files and generates a simple ASlib scenario
Assumption: features with -512 (missing value) indicate a timeout during feature computation
@author: Marius Lindauer
"""
import os
import sys
import argparse
import arff
import yaml
import copy
def generate_scenario(runtime_fn, features_fn, cutoff):
""" generates an ASlib scenario"""
description = {"scenario_id": "UNKNOWN",
"performance_measures": ["runtime"],
"maximize": [False],
"performance_type": ["runtime"],
"algorithm_cutoff_time": cutoff,
"algorithm_cutoff_memory": "?",
"features_cutoff_time": "?",
"features_cutoff_memory": "?",
"algorithms_deterministic": "?", #TODO
"algorithms_stochastic": "",
"metainfo_algorithms": {},
"features_deterministic": "?", #TODO
"features_stochastic": "",
"number_of_feature_steps": 1,
"feature_steps":{"ALL":{"provides":"?"}}, #TODO
"default_steps": ["ALL"]}
#default algorithm info
algo_info = {"configuration": "",
"deterministic": "true"}
algos = []
with open(runtime_fn, "r") as fp:
algos = fp.readline().replace("\n","").split(",")[1:]
algos = map(lambda x: x.strip("'").replace(" ","_"), algos)
algo_infos = {}
for algo in algos:
algo_infos[algo] = copy.deepcopy(algo_info)
description["metainfo_algorithms"] = copy.deepcopy(algo_infos)
attributes = [ ["instance_id", "STRING"],
["repetition", "NUMERIC"],
["algorithm", "STRING"],
["runtime", "NUMERIC"],
["runstatus", ["ok" , "timeout" , "memout" , "not_applicable" , "crash" , "other"]]
]
data = []
for line in fp:
line = line.replace("\n","").split(",")
inst = line[0]
for algo, perf in zip(algos, line[1:]):
status = "ok" if float(perf) < cutoff else "timeout"
data.append([inst, "1", algo, perf, status])
run_data = {"attributes": attributes,
"data": data,
"relation" : "ALGORITHM_RUNS"
}
with open("algorithm_runs.arff", "w") as fp:
arff.dump(run_data, fp)
instances = []
status = {}
with open(features_fn, "r") as fp:
feats = fp.readline().replace("\n", "").split(",")[1:]
description["features_deterministic"] = copy.deepcopy(feats)
description["feature_steps"]["ALL"]["provides"] = feats
attributes = [ ["instance_id", "STRING"],
["repetition", "NUMERIC"]]
data = []
for f in feats:
attributes.append([f, "NUMERIC"])
for line in fp:
line = line.replace("\n","").split(",")
inst = line[0]
feats = line[1:]
if sum(map(float, feats)) == -512*len(feats):
status[inst] = "timeout"
feats = ["?"]*len(feats)
else:
status[inst] = "ok"
d = [inst,1]
d.extend(feats)
data.append(d)
fv_data = {"attributes": attributes,
"data": data,
"relation" : "FEATURES"
}
with open("feature_values.arff", "w") as fp:
arff.dump(fv_data, fp)
fs_data = [[inst, "1", stat] for inst,stat in status.iteritems()]
fs_attributes = [
["instance_id", "STRING"],
["repetition", "NUMERIC"],
["ALL", ["ok" , "timeout" , "memout" , "not_applicable" , "crash" , "other"]]
]
fs_data = {"attributes": fs_attributes,
"data": fs_data,
"relation" : "FEATURES_RUNSTATUS"
}
with open("feature_runstatus.arff", "w") as fp:
arff.dump(fs_data, fp)
with open("description.txt", "w") as fp:
yaml.dump(description, fp, default_flow_style=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--runtime", required=True, help="csv file with runtimes (rows instances, cols algorithms)")
parser.add_argument("--features", required=True, help="csv file with instance features (rows instances, cols features)")
parser.add_argument("--cutoff", required=True, type=float, help="runtime cutoff")
args_ = parser.parse_args()
generate_scenario(args_.runtime, args_.features, args_.cutoff)