-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
627 lines (504 loc) · 22.2 KB
/
utilities.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
import datetime
import numpy as np
import scipy.sparse as sp
import pyscipopt as scip
import pickle
import gzip
def log(str, logfile=None):
str = f'[{datetime.datetime.now()}] {str}'
print(str)
if logfile is not None:
with open(logfile, mode='a') as f:
print(str, file=f)
def init_scip_params(model, seed=0, heuristics=True, presolving=True, separating=True, conflict=True):
seed = seed % 2147483648 # SCIP seed range
# set up randomization
model.setBoolParam('randomization/permutevars', True)
model.setIntParam('randomization/permutationseed', seed)
model.setIntParam('randomization/randomseedshift', seed)
# separation only at root node
model.setIntParam('separating/maxrounds', 0)
# no restart
model.setIntParam('presolving/maxrestarts', 0)
# if asked, disable presolving
if not presolving:
model.setIntParam('presolving/maxrounds', 0)
model.setIntParam('presolving/maxrestarts', 0)
# if asked, disable separating (cuts)
if not separating:
model.setIntParam('separating/maxroundsroot', 0)
# if asked, disable conflict analysis (more cuts)
if not conflict:
model.setBoolParam('conflict/enable', False)
# if asked, disable primal heuristics
if not heuristics:
model.setHeuristics(scip.SCIP_PARAMSETTING.OFF)
def extract_state(model, buffer=None):
"""
Compute a bipartite graph representation of the solver. In this
representation, the variables and constraints of the MILP are the
left- and right-hand side nodes, and an edge links two nodes iff the
variable is involved in the constraint. Both the nodes and edges carry
features.
Parameters
----------
model : pyscipopt.scip.Model
The current model.
buffer : dict
A buffer to avoid re-extracting redundant information from the solver
each time.
Returns
-------
variable_features : dictionary of type {'names': list, 'values': np.ndarray}
The features associated with the variable nodes in the bipartite graph.
edge_features : dictionary of type ('names': list, 'indices': np.ndarray, 'values': np.ndarray}
The features associated with the edges in the bipartite graph.
This is given as a sparse matrix in COO format.
constraint_features : dictionary of type {'names': list, 'values': np.ndarray}
The features associated with the constraint nodes in the bipartite graph.
"""
if buffer is None or model.getNNodes() == 1:
buffer = {}
# update state from buffer if any
s = model.getState(buffer['scip_state'] if 'scip_state' in buffer else None)
if 'state' in buffer:
obj_norm = buffer['state']['obj_norm']
else:
obj_norm = np.linalg.norm(s['col']['coefs'])
obj_norm = 1 if obj_norm <= 0 else obj_norm
row_norms = s['row']['norms']
row_norms[row_norms == 0] = 1
# Column features
n_cols = len(s['col']['types'])
if 'state' in buffer:
col_feats = buffer['state']['col_feats']
else:
col_feats = {}
col_feats['type'] = np.zeros((n_cols, 4)) # BINARY INTEGER IMPLINT CONTINUOUS
col_feats['type'][np.arange(n_cols), s['col']['types']] = 1
col_feats['coef_normalized'] = s['col']['coefs'].reshape(-1, 1) / obj_norm
col_feats['has_lb'] = ~np.isnan(s['col']['lbs']).reshape(-1, 1)
col_feats['has_ub'] = ~np.isnan(s['col']['ubs']).reshape(-1, 1)
col_feats['sol_is_at_lb'] = s['col']['sol_is_at_lb'].reshape(-1, 1)
col_feats['sol_is_at_ub'] = s['col']['sol_is_at_ub'].reshape(-1, 1)
col_feats['sol_frac'] = s['col']['solfracs'].reshape(-1, 1)
col_feats['sol_frac'][s['col']['types'] == 3] = 0 # continuous have no fractionality
col_feats['basis_status'] = np.zeros((n_cols, 4)) # LOWER BASIC UPPER ZERO
col_feats['basis_status'][np.arange(n_cols), s['col']['basestats']] = 1
col_feats['reduced_cost'] = s['col']['redcosts'].reshape(-1, 1) / obj_norm
col_feats['age'] = s['col']['ages'].reshape(-1, 1) / (s['stats']['nlps'] + 5)
col_feats['sol_val'] = s['col']['solvals'].reshape(-1, 1)
col_feats['inc_val'] = s['col']['incvals'].reshape(-1, 1)
col_feats['avg_inc_val'] = s['col']['avgincvals'].reshape(-1, 1)
col_feat_names = [[k, ] if v.shape[1] == 1 else [f'{k}_{i}' for i in range(v.shape[1])] for k, v in col_feats.items()]
col_feat_names = [n for names in col_feat_names for n in names]
col_feat_vals = np.concatenate(list(col_feats.values()), axis=-1)
variable_features = {
'names': col_feat_names,
'values': col_feat_vals,}
# Row features
if 'state' in buffer:
row_feats = buffer['state']['row_feats']
has_lhs = buffer['state']['has_lhs']
has_rhs = buffer['state']['has_rhs']
else:
row_feats = {}
has_lhs = np.nonzero(~np.isnan(s['row']['lhss']))[0]
has_rhs = np.nonzero(~np.isnan(s['row']['rhss']))[0]
row_feats['obj_cosine_similarity'] = np.concatenate((
-s['row']['objcossims'][has_lhs],
+s['row']['objcossims'][has_rhs])).reshape(-1, 1)
row_feats['bias'] = np.concatenate((
-(s['row']['lhss'] / row_norms)[has_lhs],
+(s['row']['rhss'] / row_norms)[has_rhs])).reshape(-1, 1)
row_feats['is_tight'] = np.concatenate((
s['row']['is_at_lhs'][has_lhs],
s['row']['is_at_rhs'][has_rhs])).reshape(-1, 1)
row_feats['age'] = np.concatenate((
s['row']['ages'][has_lhs],
s['row']['ages'][has_rhs])).reshape(-1, 1) / (s['stats']['nlps'] + 5)
tmp = s['row']['dualsols'] / (row_norms * obj_norm)
row_feats['dualsol_val_normalized'] = np.concatenate((
-tmp[has_lhs],
+tmp[has_rhs])).reshape(-1, 1)
row_feat_names = [[k, ] if v.shape[1] == 1 else [f'{k}_{i}' for i in range(v.shape[1])] for k, v in row_feats.items()]
row_feat_names = [n for names in row_feat_names for n in names]
row_feat_vals = np.concatenate(list(row_feats.values()), axis=-1)
constraint_features = {
'names': row_feat_names,
'values': row_feat_vals,}
# Edge features
if 'state' in buffer:
edge_row_idxs = buffer['state']['edge_row_idxs']
edge_col_idxs = buffer['state']['edge_col_idxs']
edge_feats = buffer['state']['edge_feats']
else:
coef_matrix = sp.csr_matrix(
(s['nzrcoef']['vals'] / row_norms[s['nzrcoef']['rowidxs']],
(s['nzrcoef']['rowidxs'], s['nzrcoef']['colidxs'])),
shape=(len(s['row']['nnzrs']), len(s['col']['types'])))
coef_matrix = sp.vstack((
-coef_matrix[has_lhs, :],
coef_matrix[has_rhs, :])).tocoo(copy=False)
edge_row_idxs, edge_col_idxs = coef_matrix.row, coef_matrix.col
edge_feats = {}
edge_feats['coef_normalized'] = coef_matrix.data.reshape(-1, 1)
edge_feat_names = [[k, ] if v.shape[1] == 1 else [f'{k}_{i}' for i in range(v.shape[1])] for k, v in edge_feats.items()]
edge_feat_names = [n for names in edge_feat_names for n in names]
edge_feat_indices = np.vstack([edge_row_idxs, edge_col_idxs])
edge_feat_vals = np.concatenate(list(edge_feats.values()), axis=-1)
edge_features = {
'names': edge_feat_names,
'indices': edge_feat_indices,
'values': edge_feat_vals,}
if 'state' not in buffer:
buffer['state'] = {
'obj_norm': obj_norm,
'col_feats': col_feats,
'row_feats': row_feats,
'has_lhs': has_lhs,
'has_rhs': has_rhs,
'edge_row_idxs': edge_row_idxs,
'edge_col_idxs': edge_col_idxs,
'edge_feats': edge_feats,
}
return constraint_features, edge_features, variable_features
def extract_state_fast(model, buffer=None):
"""
fast version (only compute column features) of above function
"""
if buffer is None or model.getNNodes() == 1:
buffer = {}
# update state from buffer if any
s = model.getStateFast(buffer['scip_state'] if 'scip_state' in buffer else None)
if 'state' in buffer:
obj_norm = buffer['state']['obj_norm']
else:
obj_norm = np.linalg.norm(s['col']['coefs'])
obj_norm = 1 if obj_norm <= 0 else obj_norm
# Column features
n_cols = len(s['col']['types'])
if 'state' in buffer:
col_feats = buffer['state']['col_feats']
else:
col_feats = {}
col_feats['type'] = np.zeros((n_cols, 4)) # BINARY INTEGER IMPLINT CONTINUOUS
col_feats['type'][np.arange(n_cols), s['col']['types']] = 1
col_feats['coef_normalized'] = s['col']['coefs'].reshape(-1, 1) / obj_norm
col_feats['has_lb'] = ~np.isnan(s['col']['lbs']).reshape(-1, 1)
col_feats['has_ub'] = ~np.isnan(s['col']['ubs']).reshape(-1, 1)
col_feats['sol_is_at_lb'] = s['col']['sol_is_at_lb'].reshape(-1, 1)
col_feats['sol_is_at_ub'] = s['col']['sol_is_at_ub'].reshape(-1, 1)
col_feats['sol_frac'] = s['col']['solfracs'].reshape(-1, 1)
col_feats['sol_frac'][s['col']['types'] == 3] = 0 # continuous have no fractionality
col_feats['basis_status'] = np.zeros((n_cols, 4)) # LOWER BASIC UPPER ZERO
col_feats['basis_status'][np.arange(n_cols), s['col']['basestats']] = 1
col_feats['reduced_cost'] = s['col']['redcosts'].reshape(-1, 1) / obj_norm
col_feats['age'] = s['col']['ages'].reshape(-1, 1) / (s['stats']['nlps'] + 5)
col_feats['sol_val'] = s['col']['solvals'].reshape(-1, 1)
col_feats['inc_val'] = s['col']['incvals'].reshape(-1, 1)
col_feats['avg_inc_val'] = s['col']['avgincvals'].reshape(-1, 1)
col_feat_names = [[k, ] if v.shape[1] == 1 else [f'{k}_{i}' for i in range(v.shape[1])] for k, v in col_feats.items()]
col_feat_names = [n for names in col_feat_names for n in names]
col_feat_vals = np.concatenate(list(col_feats.values()), axis=-1)
variable_features = {
'names': col_feat_names,
'values': col_feat_vals,}
if 'state' not in buffer:
buffer['state'] = {
'obj_norm': obj_norm,
'col_feats': col_feats,
}
return variable_features
def valid_seed(seed):
"""Check whether seed is a valid random seed or not."""
seed = int(seed)
if seed < 0 or seed > 2**32 - 1:
raise argparse.ArgumentTypeError(
"seed must be any integer between 0 and 2**32 - 1 inclusive")
return seed
def compute_extended_variable_features(state, candidates):
"""
Utility to extract variable features only from a bipartite state representation.
Parameters
----------
state : dict
A bipartite state representation.
candidates: list of ints
List of candidate variables for which to compute features (given as indexes).
Returns
-------
variable_states : np.array
The resulting variable states.
"""
constraint_features, edge_features, variable_features = state
constraint_features = constraint_features['values']
edge_indices = edge_features['indices']
edge_features = edge_features['values']
variable_features = variable_features['values']
cand_states = np.zeros((
len(candidates),
variable_features.shape[1] + 3*(edge_features.shape[1] + constraint_features.shape[1]),
))
# re-order edges according to variable index
edge_ordering = edge_indices[1].argsort()
edge_indices = edge_indices[:, edge_ordering]
edge_features = edge_features[edge_ordering]
# gather (ordered) neighbourhood features
nbr_feats = np.concatenate([
edge_features,
constraint_features[edge_indices[0]]
], axis=1)
# split neighborhood features by variable, along with the corresponding variable
var_cuts = np.diff(edge_indices[1]).nonzero()[0]+1
nbr_feats = np.split(nbr_feats, var_cuts)
nbr_vars = np.split(edge_indices[1], var_cuts)
assert all([all(vs[0] == vs) for vs in nbr_vars])
nbr_vars = [vs[0] for vs in nbr_vars]
# process candidate variable neighborhoods only
for var, nbr_id, cand_id in zip(*np.intersect1d(nbr_vars, candidates, return_indices=True)):
cand_states[cand_id, :] = np.concatenate([
variable_features[var, :],
nbr_feats[nbr_id].min(axis=0),
nbr_feats[nbr_id].mean(axis=0),
nbr_feats[nbr_id].max(axis=0)])
cand_states[np.isnan(cand_states)] = 0
return cand_states
def extract_khalil_variable_features(model, candidates, root_buffer, fast=False):
"""
Extract features following Khalil et al. (2016) Learning to Branch in Mixed Integer Programming.
Parameters
----------
model : pyscipopt.scip.Model
The current model.
candidates : list of pyscipopt.scip.Variable's
A list of variables for which to compute the variable features.
root_buffer : dict
A buffer to avoid re-extracting redundant root node information (None to deactivate buffering).
Returns
-------
variable_features : 2D np.ndarray
The features associated with the candidate variables.
"""
# update state from state_buffer if any
scip_state = model.getKhalilStateFast(root_buffer, candidates) if fast else model.getKhalilState(root_buffer, candidates)
variable_feature_names = sorted(scip_state)
variable_features = np.stack([scip_state[feature_name] for feature_name in variable_feature_names], axis=1)
return variable_features
def preprocess_variable_features(features, interaction_augmentation, normalization):
"""
Features preprocessing following Khalil et al. (2016) Learning to Branch in Mixed Integer Programming.
Parameters
----------
features : 2D np.ndarray
The candidate variable features to preprocess.
interaction_augmentation : bool
Whether to augment features with 2-degree interactions (useful for linear models such as SVMs).
normalization : bool
Wether to normalize features in [0, 1] (i.e., query-based normalization).
Returns
-------
variable_features : 2D np.ndarray
The preprocessed variable features.
"""
# 2-degree polynomial feature augmentation
if interaction_augmentation:
interactions = (
np.expand_dims(features, axis=-1) * \
np.expand_dims(features, axis=-2)
).reshape((features.shape[0], -1))
features = np.concatenate([features, interactions], axis=1)
# query-based normalization in [0, 1]
if normalization:
features -= features.min(axis=0, keepdims=True)
max_val = features.max(axis=0, keepdims=True)
max_val[max_val == 0] = 1
features /= max_val
return features
def load_flat_samples(filename, feat_type, label_type, augment_feats, normalize_feats):
with gzip.open(filename, 'rb') as file:
sample = pickle.load(file)
state, khalil_state, best_cand, cands, cand_scores = sample['data']
cands = np.array(cands)
cand_scores = np.array(cand_scores)
cand_states = []
if feat_type in ('all', 'gcnn_agg'):
cand_states.append(compute_extended_variable_features(state, cands))
if feat_type in ('all', 'khalil'):
cand_states.append(khalil_state)
cand_states = np.concatenate(cand_states, axis=1)
best_cand_idx = np.where(cands == best_cand)[0][0]
# feature preprocessing
cand_states = preprocess_variable_features(cand_states, interaction_augmentation=augment_feats, normalization=normalize_feats)
if label_type == 'scores':
cand_labels = cand_scores
elif label_type == 'ranks':
cand_labels = np.empty(len(cand_scores), dtype=int)
cand_labels[cand_scores.argsort()] = np.arange(len(cand_scores))
elif label_type == 'bipartite_ranks':
# scores quantile discretization as in
# Khalil et al. (2016) Learning to Branch in Mixed Integer Programming
cand_labels = np.empty(len(cand_scores), dtype=int)
cand_labels[cand_scores >= 0.8 * cand_scores.max()] = 1
cand_labels[cand_scores < 0.8 * cand_scores.max()] = 0
else:
raise ValueError(f"Invalid label type: '{label_type}'")
return cand_states, cand_labels, best_cand_idx
def load_flat_samples_modified(filename, feat_type, label_type, augment_feats, normalize_feats):
"""
Modifies the `load_flat_samples` to adapt to the new structure in samples.
"""
with gzip.open(filename, 'rb') as file:
sample = pickle.load(file)
# root data
if sample['type'] == "root":
state, khalil_state, cands, best_cand, cand_scores = sample['root_state'] # best_cand is relative to cands (in practical_l2b/02_generate_dataset.py)
best_cand_idx = best_cand
else:
# data for gcnn
obss, best_cand, obss_feats, _ = sample['obss']
v, gcnn_c_feats, gcnn_e = obss
gcnn_v_feats = v[:, :19] # gcnn features
state = {'values':gcnn_c_feats}, gcnn_e, {'values':gcnn_v_feats}
sample_cand_scores = obss_feats['scores']
cands = np.where(sample_cand_scores != -1)[0]
cand_scores = sample_cand_scores[cands]
khalil_state = v[:,19:-1][cands]
best_cand_idx = np.where(cands == best_cand)[0][0]
cands = np.array(cands)
cand_scores = np.array(cand_scores)
cand_states = []
if feat_type in ('all', 'gcnn_agg'):
cand_states.append(compute_extended_variable_features(state, cands))
if feat_type in ('all', 'khalil'):
cand_states.append(khalil_state)
cand_states = np.concatenate(cand_states, axis=1)
# best_cand_idx = np.where(cands == best_cand)[0][0]
# feature preprocessing
cand_states = preprocess_variable_features(cand_states, interaction_augmentation=augment_feats, normalization=normalize_feats)
if label_type == 'scores':
cand_labels = cand_scores
elif label_type == 'ranks':
cand_labels = np.empty(len(cand_scores), dtype=int)
cand_labels[cand_scores.argsort()] = np.arange(len(cand_scores))
elif label_type == 'bipartite_ranks':
# scores quantile discretization as in
# Khalil et al. (2016) Learning to Branch in Mixed Integer Programming
cand_labels = np.empty(len(cand_scores), dtype=int)
cand_labels[cand_scores >= 0.8 * cand_scores.max()] = 1
cand_labels[cand_scores < 0.8 * cand_scores.max()] = 0
else:
raise ValueError(f"Invalid label type: '{label_type}'")
return cand_states, cand_labels, best_cand_idx
def _preprocess(state, mode='min-max-1', bias=True):
"""
Implements preprocessing of `state`.
Parameters
----------
state : np.array
2D array of features. rows are variables and columns are features.
Return
------
(np.array) : same shape as state but with transformed variables
"""
if mode == "min-max-1":
return preprocess_variable_features(state, interaction_augmentation=False, normalization=True)
elif mode == "min-max-2":
state -= state.min(axis=0, keepdims=True)
max_val = state.max(axis=0, keepdims=True)
max_val[max_val == 0] = 1
state = 2 * state/max_val - 1
if bias:
state[:,-1] = 1 # bias
return state
def _loss_fn(logits, labels, weights):
"""
Cross-entropy loss
"""
loss = torch.nn.CrossEntropyLoss(reduction='none')(logits, labels)
return torch.sum(loss * weights)
def _compute_root_loss(separation_type, model, var_feats, root_n_vs, root_cands, root_n_cands, batch_size, root_cands_separation=False):
"""
Computes losses due to auxiliary task imposed on root GCNN.
Parameters
----------
separation_type : str
Type of separation to compute at root node's variable features
model : model.BaseModel
A base model, which may contain some model.PreNormLayer layers.
var_feats : torch.tensor
(2D) variable features at the root node
root_n_vs : torch.tensor
(1D) number of variables per sample
root_cands : torch.tensor
(1D) candidates variables (strong branching) at the root node
root_n_cands : torch.tensor
(1D) number of root candidate variables per sample
batch_size : int
number of samples
root_cands_separation : bool
True if separation is to be computed only between candidate variables at the root node. Useful for larger problems like Capacitated Facility Location.
Return
------
(np.float): loss value
"""
if root_cands_separation:
# compute separation loss only for candidates at root
n_vs = root_n_cands
var_feats = model.pad_features(var_feats[root_cands], root_n_cands)
else:
n_vs = root_n_vs
var_feats = model.pad_features(var_feats, root_n_vs)
n_pairs = n_vs ** 2
A = torch.matmul(var_feats, var_feats.transpose(2,1)) # dot products
mask = torch.zeros_like(A)
for i,nv in enumerate(n_vs):
mask[i, nv:, :] = 1.0
mask[i, :, nv:] = 1.0
mask[i, torch.arange(nv), torch.arange(nv)] = 1.0
mask = mask.type(torch.bool)
if separation_type == "MHE":
D = torch.sqrt(2 * (1 - A) + 1e-3) ** -1 - 1/2
elif separation_type == "ED":
D = 4 - 2 * (1 - A)
else:
raise ValueError(f"Unknown signal for auxiliary task: {signal_type}")
D[mask] = 0.0
root_loss = 0.5 * D.sum(axis=[1,2])/n_pairs
root_loss = torch.mean(root_loss)
return root_loss
def _distillation_loss(logits, teacher_scores, labels, weights, T, alpha):
"""
Implements distillation loss.
"""
p = F.log_softmax(logits/T, dim=-1)
q = F.softmax(teacher_scores/T, dim=-1)
l_kl = F.kl_div(p, q, reduction="none") * (T**2)
l_kl = torch.sum(torch.sum(l_kl, dim=-1) * weights)
l_ce = torch.nn.CrossEntropyLoss(reduction='none')(logits, labels)
l_ce = torch.sum(l_ce * weights)
return l_kl * alpha + l_ce * (1. - alpha)
def _get_model_type(model_name):
"""
Returns the name of the model to which `model_name` belongs
Parameters
----------
model_name : str
name of the model
Return
------
(str) : name of the folder to which this model belongs
"""
if "concat" in model_name:
if "-pre" in model_name:
return "concat-pre"
return "concat"
if "hybridsvm-film" in model_name:
return "hybridsvm-film"
if "hybridsvm" in model_name:
return "hybridsvm"
if "film" in model_name:
if "-pre" in model_name:
return "film-pre"
return "film"
raise ValueError(f"Unknown model_name:{model_name}")