forked from artefactory/learning-heterogeneous-preferences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_synthetic_experiments.py
280 lines (245 loc) · 9.08 KB
/
run_synthetic_experiments.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
import argparse
import json
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tqdm
from python.synthetic_data import SyntheticDataGenerator
from python.models import ClusterUTA, UTA
from python.heuristics import PLSHeuristic
def evaluation_routine(
base_dir,
run_id,
n_clusters,
n_criteria,
n_linear_pieces,
data_error,
learning_set_size,
test_set_size,
time_limit,
):
results_dir = os.path.join(base_dir, f"results/{run_id}")
os.makedirs(results_dir, exist_ok=True)
with open(os.path.join(results_dir, "params.json"), "w") as f:
json.dump(
{
"n_clusters": n_clusters,
"n_criteria": n_criteria,
"n_linera_pieces": n_linear_pieces,
"error": data_error,
"learning_set_size": learning_set_size,
},
f,
)
# Draw data
datagen = SyntheticDataGenerator(
n_clusters=n_clusters,
n_criteria=n_criteria,
mix_decisions=True,
gap=0.0,
noise=data_error,
)
X, Y, data_metadata = datagen.generate_data(
test_set_size + learning_set_size, return_clusters=True, return_utilities=True
)
df_X = pd.DataFrame(X, columns=[f"x_{i}" for i in range(n_criteria)])
df_Y = pd.DataFrame(Y, columns=[f"y_{i}" for i in range(n_criteria)])
df_ux = pd.DataFrame(
data_metadata["utilities_x"], columns=[f"ux_{i}" for i in range(n_clusters)]
)
df_uy = pd.DataFrame(
data_metadata["utilities_y"], columns=[f"uy_{i}" for i in range(n_clusters)]
)
# Save data generation
df_data = pd.concat([df_X, df_Y, df_ux, df_uy], axis=1)
df_data["cluster"] = data_metadata["clusters"]
df_data.to_csv(os.path.join(results_dir, "data.csv"), index=False)
weights = np.stack([data_metadata[f"weights_{i}"] for i in range(n_clusters)])
np.save(os.path.join(results_dir, f"data_weights.npy"), weights)
mweights = np.stack(
[data_metadata[f"marginal_weights_{i}"] for i in range(n_clusters)]
)
np.save(os.path.join(results_dir, f"marginal_data_weights.npy"), mweights)
X_train, X_test = X[:learning_set_size], X[learning_set_size:]
Y_train, Y_test = Y[:learning_set_size], Y[learning_set_size:]
# Compute the model
model = ClusterUTA(n_clusters=n_clusters, n_pieces=n_linear_pieces)
t_start = time.time()
hist = model.fit(X_train, Y_train, time_limit=time_limit)
uta_train_time = time.time() - t_start
# Compute and save results
U_train = model.predict_utility(X_train)
df_u_train_x = pd.DataFrame(
U_train, columns=[f"u_x_train_{i}" for i in range(n_clusters)]
)
U_test = model.predict_utility(X_test)
df_u_test_x = pd.DataFrame(
U_test, columns=[f"u_x_test_{i}" for i in range(n_clusters)]
)
U_train = model.predict_utility(Y_train)
df_u_train_y = pd.DataFrame(
U_train, columns=[f"u_y_train_{i}" for i in range(n_clusters)]
)
U_test = model.predict_utility(Y_test)
df_u_test_y = pd.DataFrame(
U_test, columns=[f"u_y_test_{i}" for i in range(n_clusters)]
)
df_u_train_x.to_csv(
os.path.join(results_dir, f"milo_u_x_train.csv"), index=False
)
df_u_test_x.to_csv(os.path.join(results_dir, f"milo_u_x_test.csv"), index=False)
df_u_train_y.to_csv(
os.path.join(results_dir, f"milo_u_y_train.csv"), index=False
)
df_u_test_y.to_csv(os.path.join(results_dir, f"milo_u_y_test.csv"), index=False)
model.save_model(results_dir)
np.save(os.path.join(results_dir, f"milo_fit_time.npy"), np.array(uta_train_time))
with open(os.path.join(results_dir, "milo_status.txt"), "w") as f:
f.write(f"{model.status}\n")
model = PLSHeuristic(
models_class=UTA, n_clusters=n_clusters, n_init=20
)
t_start = time.time()
hist = model.fit(X_train, Y_train)
heuristic_train_time = time.time() - t_start
U_train = model.predict_utility(X_train)
df_u_train_x = pd.DataFrame(
U_train, columns=[f"u_x_train_{i}" for i in range(n_clusters)]
)
U_test = model.predict_utility(X_test)
df_u_test_x = pd.DataFrame(U_test, columns=[f"u_x_test_{i}" for i in range(n_clusters)])
U_train = model.predict_utility(Y_train)
df_u_train_y = pd.DataFrame(
U_train, columns=[f"u_y_train_{i}" for i in range(n_clusters)]
)
U_test = model.predict_utility(Y_test)
df_u_test_y = pd.DataFrame(U_test, columns=[f"u_y_test_{i}" for i in range(n_clusters)])
df_u_train_x.to_csv(os.path.join(results_dir, "heuristic_u_x_train.csv"), index=False)
df_u_test_x.to_csv(os.path.join(results_dir, "heuristic_u_x_test.csv"), index=False)
df_u_train_y.to_csv(os.path.join(results_dir, "heuristic_u_y_train.csv"), index=False)
df_u_test_y.to_csv(os.path.join(results_dir, "heuristic_u_y_test.csv"), index=False)
np.save(os.path.join(results_dir, f"heuristic_fit_time.npy"), np.array(heuristic_train_time))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("save_dir", type=str, help="Directory to save results.")
parser.add_argument(
"-r",
"--repetitions",
default=1,
type=int,
help="Number of experiments for each combination of parameters.",
)
parser.add_argument(
"-to", "--timeout", default=1800, type=int, help="TimeOut for the solver."
)
parser.add_argument(
"-tss",
"--test_set_size",
default=2**12,
type=int,
help="Number of samples in the testing set.",
)
parser.add_argument(
"-cl",
"--n_clusters",
type=int,
nargs="+",
default=2,
help="Number of clusters considered in data generation and modeling - can be int or list.",
)
parser.add_argument(
"-cr",
"--n_criteria",
type=int,
nargs="+",
default=6,
help="Number of criteria for the data - can be int or list",
)
parser.add_argument(
"-p",
"--n_pieces",
type=int,
nargs="+",
default=5,
help="Number of pieces for the UTA models - can be int or list.",
)
parser.add_argument(
"-lss",
"--learning_set_size",
type=int,
nargs="+",
default=2**10,
help="Learning set size - can be int or list.",
)
parser.add_argument(
"-e",
"--error",
type=int,
nargs="+",
default=0,
help="Error percentage - can be int or list.",
)
args = parser.parse_args()
base_dir = args.save_dir
repetitions = args.repetitions
timeout = args.timeout
test_set_size = args.test_set_size
n_clusters = args.n_clusters
if isinstance(n_clusters, int):
n_clusters = [n_clusters]
if not isinstance(n_clusters, list):
raise ValueError(
f"n_clusters should be int or list of int and is: {type(n_clusters)}"
)
n_criteria = args.n_criteria
if isinstance(n_criteria, int):
n_criteria = [n_criteria]
if not isinstance(n_criteria, list):
raise ValueError(
f"n_criteria should be int or list of int and is: {type(n_criteria)}"
)
n_pieces = args.n_pieces
if isinstance(n_pieces, int):
n_pieces = [n_pieces]
if not isinstance(n_pieces, list):
raise ValueError(
f"n_pieces should be int or list of int and is: {type(n_pieces)}"
)
train_set_size = args.learning_set_size
if isinstance(train_set_size, int):
train_set_size = [train_set_size]
if not isinstance(train_set_size, list):
raise ValueError(
f"train_set_size should be int or list of int and is: {type(train_set_size)}"
)
error = args.error
if isinstance(error, int):
error = [error]
if not isinstance(error, list):
raise ValueError(f"error should be int or list of int and is: {type(error)}")
for rep in range(repetitions):
print(f"Currently at rep: {rep}")
for err in error:
for n_cl in n_clusters:
for n_cr in n_criteria:
for n_p in n_pieces:
for lss in train_set_size:
run_id = f"{n_cl}_{n_cr}_{err}_{lss}_{n_p}_{rep}"
if os.path.exists(
os.path.join(base_dir, f"results/{run_id}")
):
print(f"Skipping {run_id}")
else:
evaluation_routine(
base_dir=base_dir,
run_id=run_id,
n_clusters=n_cl,
n_criteria=n_cr,
n_linear_pieces=n_p,
data_error=err / 100,
learning_set_size=lss,
test_set_size=test_set_size,
time_limit=timeout,
)