-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_cars_experiments.py
185 lines (162 loc) · 5.99 KB
/
run_cars_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
import argparse
import logging
import os
import numpy as np
import time
from sklearn.model_selection import GroupShuffleSplit
from python.real_data import load_cars_preferences_pairs
from python.models import ClusterUTA, UTA
from python.heuristics import PLSHeuristic
def run_xp(
base_dir,
run_id,
timeout,
seed,
test_size=0.46,
train_sizes=[100, 150, 200, 400, 600, 1000, 2500, 10000],
clusters=[2, 3, 4, 5],
epsilon=0.05,
n_pieces=5,
):
results_dir = os.path.join(base_dir, f"results/{run_id}")
if os.path.exists(results_dir):
logging.warning(f"{results_dir} already exists, if results already exist, they will be overwritten.")
os.makedirs(results_dir, exist_ok=True)
X, Y, choice_ids = load_cars_preferences_pairs()
gss = GroupShuffleSplit(n_splits=1, train_size=1-test_size, random_state=seed)
for i, (train_index, test_index) in enumerate(gss.split(X, Y, choice_ids)):
X_train = X[train_index]
X_test = X[test_index]
Y_train = Y[train_index]
Y_test = Y[test_index]
choice_ids_train = choice_ids[train_index]
choice_ids_test = choice_ids[test_index]
np.save(os.path.join(results_dir, "X_train.npy"), X_train)
np.save(os.path.join(results_dir, "Y_train.npy"), Y_train)
np.save(os.path.join(results_dir, "X_test.npy"), X_test)
np.save(os.path.join(results_dir, "Y_test.npy"), Y_test)
np.save(os.path.join(results_dir, "ids_train.npy"), choice_ids_train)
np.save(os.path.join(results_dir, "ids_test.npy"), choice_ids_test)
for ds in train_sizes:
for cluster in clusters:
t_start = time.time()
milo_model = ClusterUTA(n_clusters=cluster, n_pieces=n_pieces, epsilon=epsilon)
hist = milo_model.fit(
X_train[:ds],
Y_train[:ds],
cluster_grouping=choice_ids_train[:ds],
time_limit=timeout,
n_threads=12,
)
t_end = time.time()
np.save(os.path.join(results_dir, f"milo_{cluster}_clusters_{ds}.npy"), milo_model.coeffs)
np.save(os.path.join(results_dir, f"{cluster}_clusters_{ds}_milo_fit_time.npy"), np.array(t_end - t_start))
np.save(
os.path.join(results_dir, f"{cluster}_clusters_{ds}_milo_fit_status.npy"), np.array(milo_model.status)
)
heuristic = PLSHeuristic(
models_class=UTA, n_clusters=cluster, n_init=4, max_iter_by_init=20
)
t_start = time.time()
hist = heuristic.fit(X_train, Y_train)
t_end = time.time()
np.save(os.path.join(results_dir, f"heuristic_{cluster}_clusters_{ds}.npy"), np.stack([md.coeffs for md in heuristic.models]))
np.save(os.path.join(results_dir, f"{cluster}_clusters_{ds}_heuristic_fit_time.npy"), np.array(t_end - t_start))
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=0.46,
type=float,
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(
"-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",
"--epsilon",
type=float,
default=0.05,
help="Magin of utility between preferences.",
)
args = parser.parse_args()
base_dir = args.save_dir
repetitions = args.repetitions
timeout = args.timeout
test_set_size = args.test_set_size
epsilon = args.epsilon
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_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)}"
)
for seed in np.random.randint(low=0, high=666, size=(repetitions,)):
for n_p in n_pieces:
for lss in train_set_size:
run_id = f"{lss}_{n_p}_{seed}"
if os.path.exists(
os.path.join(base_dir, f"results/{run_id}")
):
print(f"Skipping {run_id}")
else:
run_xp(
base_dir=base_dir,
run_id=run_id,
timeout=timeout,
seed=seed,
test_size=test_set_size,
train_sizes=train_set_size,
clusters=n_clusters,
epsilon=epsilon,
n_pieces=n_p,
)