-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDRAFT.py
2070 lines (1617 loc) · 97.6 KB
/
DRAFT.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from scipy.stats import binom
import math
class DRAFT:
# HELPER FUNCTIONS
# Functions to calculate probabilities for the bagging model
def proba(self, k, N):
return binom.pmf(k, N, 1 / N)
def proba_inf(self, k, N):
calc = 0
for i in range(k):
calc += self.proba(i, N)
return calc
# Function to parse the branches and build the list of possible intervals for numerical attributes
def compute_numerical_attrs_intervals(self, trees_branches):
# Parse all the branches to determine split levels for numerical attributes
# and convert their value to integers modelling intervals between them
num_indices = [f[0] for f in self.numerical_attrs]
for i in range(len(self.numerical_attrs)):
self.numerical_attrs[i] = self.numerical_attrs[i] + [[]] + [[]] # adds lists of split values and intervals
# Retrieve all split values
for all_branches_t in trees_branches: # for each tree
for a_branch in all_branches_t: # iterate over its branches
for a_split in a_branch[0]: # iterate over the splits along the branch
feature_val = abs(a_split[0])-1
threshold_val = a_split[1]
if feature_val in num_indices: # numerical feature
idfeature = num_indices.index(feature_val)
if not(threshold_val in self.numerical_attrs[idfeature][3]):
self.numerical_attrs[idfeature][3].append(threshold_val)
# Sort them ($\mathcal{A}_i$)
for i in range(len(self.numerical_attrs)):
self.numerical_attrs[i][3].sort()
# Concatenate them with bounds ($\mathcal{I}_i$) to get all the possible intervals
for i in range(len(self.numerical_attrs)):
self.numerical_attrs[i][4] = [self.numerical_attrs[i][1]] + self.numerical_attrs[i][3] + [self.numerical_attrs[i][2]]
def parse_forest(self, clf, verbosity=False):
"""
Parses a given Random Forest learnt using the scikit-learn library and returns the different
values needed to build our reconstruction models.
Arguments
---------
clf: instance of sklearn.ensemble.RandomForestClassifier
The random forest whose structure will be leveraged to reconstruct its training set
verbosity: bool, optional (default True)
whether to print information about the search progress or not
Returns
-------
T, M, N, C, Z, max_max_depth, trees_branches where:
T: list of instances of sklearn.tree.DecisionTreeClassifier
The different trees within the provided forest
M: Number of attributes within the forest's training set
N: Number of examples within the forest's training set
C: Number of different classes within the forest's training set
Z: List of the different classes within the forest's training set (according to the first tree) -> only used for the models without bagging
max_max_depth: Maximum depth reached by any tree within the forest
trees_branches: list of list
for each tree, stores for each branch the list of associated splits along with the corresponding leaf's cardinalities
maxcards: Maximum per-class cardinalities among all trees -> only used for the models with bagging (without bagging, all these cardinalities should be equal accross all trees)
"""
import numpy as np
import sklearn
from copy import deepcopy
## Parse the forest
# Trees of the forest
T = clf.estimators_
# Nombre de features du jeu de données étudiées
M = T[0].n_features_in_
def retrieve_branches(number_nodes, children_left_list, children_right_list, nodes_features_list, nodes_value_list, nodes_thresholds ):
"""Retrieve decision tree branches"""
# Calculate if a node is a leaf
is_leaves_list = [(False if cl != cr else True) for cl, cr in zip(children_left_list, children_right_list)]
# Store the branches paths
paths = []
for i in range(number_nodes):
if is_leaves_list[i]:
# Search leaf node in previous paths
end_node = [path[-1] for path in paths]
# If it is a leave node yield the path
if i in end_node:
output = paths.pop(np.argwhere(i == np.array(end_node))[0][0])
output = output[:-1]
yield (output, list(nodes_value_list[i][0]))
else:
# Origin and end nodes
origin, end_l, end_r = i, children_left_list[i], children_right_list[i]
# Iterate over previous paths to add nodes
for index, path in enumerate(paths):
if origin == path[-1]:
path[-1] = [-nodes_features_list[origin], nodes_thresholds[origin]]
paths[index] = path + [end_l]
path[-1] = [nodes_features_list[origin], nodes_thresholds[origin]]
paths.append(path + [end_r])
# Initialize path in first iteration
if i == 0:
paths.append([[-nodes_features_list[i], nodes_thresholds[i]], children_left[i] ])
paths.append([[nodes_features_list[i], nodes_thresholds[i]], children_right[i] ])
max_max_depth = 0
trees_branches = []
first_tree = True
maxcards = []
# iterate over the trees of the forest
for tree in T:
t = tree.tree_
# Depending on sklearn version different parsing must be done here
sklearn_version = str(sklearn.__version__).split(".")
if int(sklearn_version[0]) <= 1 and int(sklearn_version[1]) <= 3:
nodes_value = t.value # For all nodes in the tree, list of their value (support for both classes)
else:
total_examples = t.weighted_n_node_samples
nodes_value = deepcopy(t.value) # For all nodes in the tree, list of their value (relative support for both classes)
for i in range(len(nodes_value)): # For each node
#print(total_examples[i], nodes_value[i])
for j in range(len(nodes_value[i][0])):
nodes_value[i][0][j] = np.round(nodes_value[i][0][j] * total_examples[i], decimals=0)
#print(total_examples[i], nodes_value[i], '\n')
assert(sum(nodes_value[i][0]) == total_examples[i]) # just make sure there were no rounding error
cards = list(nodes_value[0][0]) # at the root
if first_tree: # Init constants (only do it once)
# Nombre de classes (lisible par exemple ici en regardant la taille du tableau des cardinalités à la racine de l'arbre 0)
C = len(cards)
# Nombre d'individus (calculé ici en sommant les cardinalités par classes à la racine de l'arbre 0)
N = 0
for c in range(C):
N += int(cards[c])
# Classes des exemples
Z = np.zeros((N, C), dtype=int)
deb = 0
for c in range(C): # for each class
for i in range(deb, deb + int(cards[c])):
Z[i, c] = 1
deb += int(cards[c])
maxcards = [ 0 for c in range(C) ]
first_tree = False
for c in range(C):
if cards[c] > maxcards[c]:
maxcards[c] = cards[c]
max_depth = t.max_depth
if max_depth > max_max_depth:
max_max_depth = max_depth
n_nodes = t.node_count
children_left = t.children_left # For all nodes in the tree, list of their left children (or -1 for leaves)
children_right = t.children_right # For all nodes in the tree, list of their right children (or -1 for leaves)
nodes_features = deepcopy(t.feature) # For all nodes in the tree, list of their used feature (or -2 for leaves)
# Note that we need to make deep copies of the lists for which we modify elements
# Not sure we need deepcopy of threshold, but doing it to be safe
nodes_thresholds = deepcopy( t.threshold )
nodes_features += 1
all_branches = list(retrieve_branches(n_nodes, children_left, children_right, nodes_features, nodes_value, nodes_thresholds ))
trees_branches.append(all_branches)
if verbosity:
print("RF parsing done!")
return T, M, N, C, Z, max_max_depth, trees_branches, maxcards
# MAIN FUNCTIONS
def __init__(self, random_forest, one_hot_encoded_groups=[], ordinal_attributes=[], numerical_attributes=[]):
"""
Constructor.
Attributes
---------
random_forest: instance of sklearn.ensemble.RandomForestClassifier
The random forest whose structure will be leveraged to reconstruct its training set
one_hot_encoded_groups: list, optional
list of lists, where each sub-list contains the IDs of a group of attributes corresponding to a one-hot encoding of the same original feature.
not mandatory but if provided, can improve the performed reconstruction and may speed up the process
"""
from sklearn.ensemble import RandomForestClassifier
from copy import deepcopy
if not isinstance(random_forest, RandomForestClassifier):
raise TypeError("Expected a RandomForestClassifier but provided random_forest is of type " + str(type(random_forest)))
self.clf = random_forest
self.ohe_groups = one_hot_encoded_groups
self.ordinal_attrs = ordinal_attributes
self.numerical_attrs = deepcopy(numerical_attributes) # Since we will modify it to include split values
def fit(self, bagging=False, method='cp-sat', timeout=60, verbosity=True, n_jobs=-1, seed=0):
"""
Reconstructs a dataset compatible with the knowledge provided by random_forest.
In other terms, fits the data to the given model.
Arguments
---------
bagging: bool, optional (default False)
whether bootstrap sampling was used to train the base learners
the reconstruction model will be constructed accordingly
method: str in {'cp-sat', 'milp'}, optional (default 'cp-sat')
the type of formulation that will be used to perform the reconstruction
Note that `cp-sat` requires the OR-Tools Python library
and `milp` the GurobiPy one (see the Installation section of our README).
timeout: int, optional (default 60)
maximum cpu time (in seconds) to be used by the search
if the solver is not able to return a solution within the given time frame, it will be indicated in the returned dictionary
verbosity: bool, optional (default True)
whether to print information about the search progress or not
n_jobs: int in {-1, positives}, optional (default -1)
maximum number of threads to be used by the solver to parallelize search
if -1, use all available threads
seed: int, optional (default 0)
random number generator seed
used to fix the behaviour of the solvers
Returns
-------
output: dictionary containing:
-> 'max_max_depth': maximum depth found when parsing the trees within the forest.
-> 'status': the solve status returned by the solver.
- When method=`cp-sat` (OR-Tools solver), it can be 'UNKNOWN', 'MODEL_INVALID', 'FEASIBLE', 'INFEASIBLE', or 'OPTIMAL'.
- When method=`milp` (Gurobi solver), it can be "LOADED", "OPTIMAL", "INFEASIBLE", "INF_OR_UNBD", "UNBOUNDED", "CUTOFF", "ITERATION_LIMIT", "NODE_LIMIT", "TIME_LIMIT", "SOLUTION_LIMIT", "INTERRUPTED", "NUMERIC", "SUBOPTIMAL", "INPROGRESS", "USER_OBJ_LIMIT", or "WORK_LIMIT".
-> 'duration': duration
-> 'reconstructed_data': array of shape = [n_samples, n_attributes] encoding the reconstructed dataset.
Note that if the status is not OPTIMAL or FEASIBLE the reconstruction should not be used.
"""
if not method in ['cp-sat', 'milp']:
raise ValueError("Supported methods are either 'cp-sat' or 'milp', got: " + method)
if method == 'cp-sat':
# Whether to use the alternative formulation (described in alt_cp_model_bag.tex)
use_alt = 0
# Whether to use the new maximum likelihood objective
# (the old one minimizes the absolute difference to the cumulative distribution of
# probability that a sample is used at least b times, for every tree)
use_mleobj = 1
# Cannot use MLE objective with alternative formulation
assert( use_mleobj + use_alt <= 1 )
# Whether to use constraints that are not necessarily valid, but valid with high probability (measured by epsilon specified within that function)
useprobctr = 0
if n_jobs == -1:
n_jobs = 0 # value for OR-Tools
if not bagging:
self.perform_reconstruction_v1_CP_SAT(n_threads=n_jobs, time_out=timeout, verbosity=verbosity, seed=seed)
else:
if use_alt:
self.perform_reconstruction_v2_CP_SAT_alt(n_threads=n_jobs, time_out=timeout, verbosity=verbosity, seed=seed, useprobctr=useprobctr)
else:
self.perform_reconstruction_v2_CP_SAT(n_threads=n_jobs, time_out=timeout, verbosity=verbosity, seed=seed, use_mleobj=use_mleobj, useprobctr=useprobctr )
elif not bagging and method == 'milp':
if len(self.ordinal_attrs) > 0 or len(self.numerical_attrs) > 0:
raise AttributeError("Currently numerical and ordinal attributes are not supported with MILP formulations.")
if n_jobs == -1:
n_jobs = 0 # value for Gurobi
self.perform_reconstruction_v1_MILP(n_threads=n_jobs, time_out=timeout, verbosity=int(verbosity), seed=seed)
else:
raise AttributeError("Currently bagging is not supported with MILP formulations.")
if hasattr(self, 'result_dict'):
return self.result_dict
else:
raise RuntimeError('Something went wrong and the reconstruction could not be performed. Please report this issue to the developers.')
def perform_reconstruction_v1_CP_SAT(self, n_threads=0, time_out=60, verbosity=1, seed=0):
"""
Constructs and solves the CP based dataset reconstruction model (without the use of bagging to train the target random forest) using the OR-Tools CP-SAT solver.
Arguments
---------
n_threads: int >= 0, optional (default 0)
maximum number of threads to be used by the solver to parallelize search
if 0, use all available threads
time_out: int, optional (default 60)
maximum cpu time (in seconds) to be used by the search
if the solver is not able to return a solution within the given time frame, it will be indicated in the returned dictionary
verbosity: int, optional (default 1)
whether to print information (1) about the search progress or not (0)
seed: int, optional (default 0)
random number generator seed
used to fix the behaviour of the solver
Returns
-------
output: dictionary containing:
-> 'max_max_depth': maximum depth found when parsing the trees within the forest.
-> 'status': the solve status returned by the solver. It can be 'UNKNOWN', 'MODEL_INVALID', 'FEASIBLE', 'INFEASIBLE', or 'OPTIMAL'.
-> 'duration': duration
-> 'reconstructed_data': array of shape = [n_samples, n_attributes] encoding the reconstructed dataset.
Note that if the status is not OPTIMAL or FEASIBLE the reconstruction should not be used.
"""
from ortools.sat.python import cp_model
import numpy as np # useful
import time # time measurements
clf = self.clf
one_hot_encoded_groups = self.ohe_groups
start = time.time()
T, M, N, C, Z, max_max_depth, trees_branches, _ = self.parse_forest(clf, verbosity=verbosity)
### Create the CP model
## Variables
model = cp_model.CpModel()
# Reconstruction variables
ord_indices = [f[0] for f in self.ordinal_attrs] # Ordinal attributes => Integer variables
num_indices = [f[0] for f in self.numerical_attrs] # Numerical attributes => Integer variables modelling intervals between two splits
self.compute_numerical_attrs_intervals(trees_branches) # Compute the sorted list of all split values for each numerical attribute
x_vars = [[None] * M for k in range(N)]
for i in range(M):
if i in ord_indices:
idx = ord_indices.index(i)
for k in range(N):
x_vars[k][i] = model.NewIntVar(self.ordinal_attrs[idx][1], self.ordinal_attrs[idx][2], 'x_%d_%d' % (k, i) )
elif i in num_indices:
idx = num_indices.index(i)
#print("Num attr ", i, " : ", self.numerical_attrs[idx][4])
for k in range(N):
x_vars[k][i] = model.NewIntVar(0, len(self.numerical_attrs[idx][3]), 'x_%d_%d' % (k, i) )
else:
for k in range(N):
x_vars[k][i] = model.NewBoolVar('x_%d_%d' %(k,i))
# Contraintes
# one-hot encoding
for k in range(N):
for w in range(len(one_hot_encoded_groups)): # for each group of binary attributes one-hot encoding the same attribute
model.Add(cp_model.LinearExpr.Sum([x_vars[k][i] for i in one_hot_encoded_groups[w]]) == 1)
for all_branches_t in trees_branches: # for each tree
examples_capts = [[] for k in range(N)] # for each example we will ensure it is captured by exactly one branch
for a_branch_nb, a_branch in enumerate(all_branches_t): # iterate over its branches
branch_vars_c = [[] for c in range(C)] # for each class, list of examples of this class indicating whether they go to this branch
for k in range(N):
k_label_one_hot = Z[k]
for c in range(C):
if a_branch[1][c] > 0 and k_label_one_hot[c] == 1: # example is of the correct class and the branch contains examples from it
local_capt_var = model.NewBoolVar('branch_capt_%d_%d_%d' %(c,a_branch_nb,k))
branch_vars_c[c].append(local_capt_var)
examples_capts[k].append(local_capt_var)
for a_split in a_branch[0]:
feature_val = a_split[0]
feature_id = abs(feature_val)-1
threshold_val = a_split[1]
if feature_id in num_indices:
idx = num_indices.index(feature_id)
threshold_val = self.numerical_attrs[idx][3].index(threshold_val) # replace the actual split value with its index (interval nb)
if feature_val > 0:
model.Add( x_vars[k][feature_id] >= int( math.floor( threshold_val ) ) + 1 ).OnlyEnforceIf( local_capt_var )
elif feature_val < 0:
model.Add(x_vars[k][feature_id] <= int( math.floor( threshold_val ) ) ).OnlyEnforceIf( local_capt_var)
else:
raise ValueError("Feat 0 shouldn't be used here (1-indexed now)")
for c in range(C):
model.Add(cp_model.LinearExpr.Sum(branch_vars_c[c]) == int(a_branch[1][c])) # enforces the branch per-class cardinality
for k in range(N):
if len(examples_capts[k]) > 0:
model.Add(cp_model.LinearExpr.Sum(examples_capts[k]) == 1) # each example captured by exactly one branch
# Ordre lexicographique au sein des classes
'''deb = 0
for c in range(C): # for each class
for k in range(deb, deb + int(cards[c])-1):
model.Add(sum(2 ** (i) * x_vars[k][i] for i in range(M)) <= sum(2 ** (i) * x_vars[k+1][i] for i in range(M)))
deb += int(cards[c])'''
if verbosity:
print("Model creation done!")
# Résolution
solver = cp_model.CpSolver()
# Sets a time limit of XX seconds.
solver.parameters.log_search_progress = verbosity
solver.parameters.max_time_in_seconds = time_out
solver.parameters.num_workers = n_threads
solver.parameters.random_seed = seed
status = solver.Solve(model)
end = time.time()
duration = end - start
# Récupération statut/valeurs
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
x_sol = [[solver.Value(x_vars[k][i]) for i in range(M)] for k in range(N)]
# Retrieve actual numerical attributes' values from their assigned interval ID
for k in range(N):
for i in range(len(self.numerical_attrs)):
attr_id = self.numerical_attrs[i][0]
interval_id = x_sol[k][attr_id]
reconstructed_value = (self.numerical_attrs[i][4][interval_id]+self.numerical_attrs[i][4][interval_id+1])/2
x_sol[k][attr_id]=reconstructed_value
else:
if status == cp_model.INFEASIBLE or status == cp_model.MODEL_INVALID:
raise RuntimeError('Infeasible model: the reconstruction problem has no solution. Please make sure the provided one-hot encoding constraints are correct. Else, report this issue to the developers.')
else:
x_sol = np.random.randint(2,size = (N,M)) # Note that this is just to avoid raising an error but if the status is not OPTIMAL or FEASIBLE the reconstruction should not be used
solve_status = {0: 'UNKNOWN',
1: 'MODEL_INVALID',
2: 'FEASIBLE',
3: 'INFEASIBLE',
4:'OPTIMAL'}[status]
self.result_dict = {'max_max_depth':max_max_depth, 'status':solve_status, 'duration': duration, 'reconstructed_data':x_sol}
def perform_reconstruction_v1_MILP(self, n_threads=0, time_out=60, verbosity=1, seed=0):
"""
Constructs and solves the MILP based dataset reconstruction model (without the use of bagging to train the target random forest) using the Gurobi MILP solver through its Python wrapper.
Arguments
---------
n_threads: int in {-1, positives}, optional (default -1)
maximum number of threads to be used by the solver to parallelize search
if -1, use all available threads
time_out: int, optional (default 60)
maximum cpu time (in seconds) to be used by the search
if the solver is not able to return a solution within the given time frame, it will be indicated in the returned dictionary
verbosity: int, optional (default 1)
whether to print information (1) about the search progress or not (0)
seed: int, optional (default 0)
random number generator seed
used to fix the behaviour of the solvers
Returns
-------
output: dictionary containing:
-> 'max_max_depth': maximum depth found when parsing the trees within the forest.
-> 'status': the solve status returned by the solver. It can be "LOADED", "OPTIMAL", "INFEASIBLE", "INF_OR_UNBD", "UNBOUNDED", "CUTOFF", "ITERATION_LIMIT", "NODE_LIMIT", "TIME_LIMIT", "SOLUTION_LIMIT", "INTERRUPTED", "NUMERIC", "SUBOPTIMAL", "INPROGRESS", "USER_OBJ_LIMIT", or "WORK_LIMIT".
-> 'duration': duration
-> 'reconstructed_data': array of shape = [n_samples, n_attributes] encoding the reconstructed dataset.
Note that if the status is not OPTIMAL or FEASIBLE the reconstruction should not be used.
"""
import gurobipy as gp # solver
import numpy as np # useful
from gurobipy import GRB, quicksum # solver
import time # time measurements
import sklearn
clf = self.clf
one_hot_encoded_groups = self.ohe_groups
start = time.time()
### Create the MILP model
m = gp.Model("reconstruction_model")
m.setParam('LogToConsole', verbosity) # 0 or 1
m.setParam('TimeLimit', time_out) # in seconds
m.setParam('Threads', n_threads) # 0 = all threads available
m.setParam('Seed', seed) # for reproducibility
## Parse the forest
# Trees of the forest
T = clf.estimators_
# Depths attained in each tree
D = []
# Nodes of each tree
V = []
# Internal nodes of each tree
V_I = []
# List of the internal nodes of each trees for each depth
V_Id = []
# List of the internal nodes of each trees for each feature
V_If = []
# List of the leafs of each tree
V_L = []
# List of Left and Right children of each tree
L = []
R = []
# List for each tree, for each node, le nombre d'éléments de chaque classe qui passe par le noeud
Nb_c = []
# Nombre de features du jeu de données étudiées
M = T[0].n_features_in_
max_max_depth = 0
# iterate over the trees of the forest
from copy import deepcopy
for tree in T:
t = tree.tree_
max_depth = t.max_depth
if max_depth > max_max_depth:
max_max_depth = max_depth
tamp_leaves = []
tamp_inside = []
tamp_nd = [[]] * max_depth
tamp_nf = [[]] * M
n_nodes = t.node_count
children_left = t.children_left
children_right = t.children_right
feature = t.feature
threshold = t.threshold
node_depth = np.zeros(shape=n_nodes, dtype=np.int64)
is_leaves = np.zeros(shape=n_nodes, dtype=bool)
stack = [(0, 0)] # start with the root node id (0) and its depth (0)
while len(stack) > 0:
# `pop` ensures each node is only visited once
node_id, depth = stack.pop()
node_depth[node_id] = depth
# If the left and right child of a node is not the same we have a split
# node
is_split_node = children_left[node_id] != children_right[node_id]
# If a split node, append left and right children and depth to `stack`
# so we can loop through them
if is_split_node:
stack.append((children_left[node_id], depth + 1))
stack.append((children_right[node_id], depth + 1))
else:
is_leaves[node_id] = True
for i in range(n_nodes):
if is_leaves[i]:
tamp_leaves.append(i)
else:
tamp_inside.append(i)
tamp_nd[node_depth[i]] = tamp_nd[node_depth[i]] + [i]
tamp_nf[feature[i]] = tamp_nf[feature[i]] + [i]
V_I.append(tamp_inside)
V_Id.append(tamp_nd)
V_If.append(tamp_nf)
V_L.append(tamp_leaves)
D.append([d for d in range(max_depth)])
V.append([v for v in range(n_nodes)])
L.append(t.children_left)
R.append(t.children_right)
# Depending on sklearn version different parsing must be done here
sklearn_version = str(sklearn.__version__).split(".")
if int(sklearn_version[0]) <= 1 and int(sklearn_version[1]) <= 3:
nodes_value = t.value # For all nodes in the tree, list of their value (support for both classes)
else:
total_examples = t.weighted_n_node_samples
nodes_value = deepcopy(t.value) # For all nodes in the tree, list of their value (relative support for both classes)
for i in range(len(nodes_value)): # For each node
#print(total_examples[i], nodes_value[i])
for j in range(len(nodes_value[i][0])):
nodes_value[i][0][j] = np.round(nodes_value[i][0][j] * total_examples[i], decimals=0)
#print(total_examples[i], nodes_value[i], '\n')
assert(sum(nodes_value[i][0]) == total_examples[i]) # just make sure there were no rounding error
Nb_c.append(nodes_value.tolist())
# Nombre de classes (lisible par exemple ici en regardant la taille du tableau des cardinalités à la racine de l'arbre 0)
C = len(Nb_c[0][0][0])
# Nombre d'individus (calculé ici en sommant les cardinalités par classes à la racine de l'arbre 0)
N = 0
for c in range(C):
N += int(Nb_c[0][0][0][c])
# Nombre d'exemples par classe
distrib_classes = Nb_c[0][0][0]
# Classes des exemples
Z = np.zeros((C, N), dtype=int)
deb = 0
for c in range(C): # for each class
for i in range(deb, deb + int(distrib_classes[c])):
Z[c, i] = 1
deb += int(distrib_classes[c])
# Tableau qui pour chaque classe C nous donne la liste des individus de la classe C
Z_bis = []
for c in range(C):
list_tampon = []
for k in range(N):
if Z[c, k]:
list_tampon.append(k)
Z_bis.append(list_tampon)
if verbosity > 0:
print("RF parsing done!")
## Variables
# Variables lambda
indices_lambda = [(t,d,k) for t in range(len(T)) for d in D[t] for k in range(N)]
lam = m.addVars(indices_lambda, vtype = GRB.BINARY, name = "lambda")
# Variables de flot y
indices_y = [(t,v,k) for t in range(len(T)) for v in V[t] for k in range(N)]
y = m.addVars(indices_y, lb = 0, ub = 1, vtype = GRB.CONTINUOUS, name = "y")
# Variables x de reconstitution
x = m.addMVar((N,M), vtype = GRB.BINARY, name = "x")
#x = m.addMVar((N,M), lb = 0, ub = 1, vtype = GRB.CONTINUOUS, name = "x")
## Contraintes
for t in range(len(T)):
# Contraintes modélisant le flot des exemples
m.addConstrs((1 <= y[t, 0, k] for k in range(N)), name='c1')
#m.addConstrs((y[t, 0, k] <= 1 for k in range(N)), name='c2') # déjà dans l'ub
m.addConstrs((y[t, v, k] - y[t, L[t][v], k] - y[t, R[t][v], k] == 0 for v in V_I[t] for k in range(N)), name='c3')
m.addConstrs((quicksum(y[t, L[t][v], k] for v in V_Id[t][d]) <= lam[t, d, k] for d in D[t] for k in range(N)),
name='c4')
m.addConstrs((quicksum(y[t, R[t][v], k] for v in V_Id[t][d]) <= (1 - lam[t, d, k]) for d in D[t] for k in range(N)),
name='c5')
# Contraintes liant ces flots aux valeurs des attributs des exemples
m.addConstrs((x[k, i] <= 1 - y[t, L[t][v], k] for k in range(N) for i in range(M) for v in V_If[t][i]), name='c6')
m.addConstrs((y[t, R[t][v], k] <= x[k, i] for k in range(N) for i in range(M) for v in V_If[t][i] ), name='c7')
# Contraintes liant ces flots à la structure des arbres
m.addConstrs((Nb_c[t][v][0][c] <= quicksum(y[t, v, k] * Z[c, k] for k in range(N)) for v in V[t] for c in range(C)),
name='c8')
m.addConstrs((quicksum(y[t, v, k] * Z[c, k] for k in range(N)) <= Nb_c[t][v][0][c] for v in V[t] for c in range(C)),
name='c9')
#m.addConstrs((quicksum(y[t, v, k] * Z[c, k] for k in range(N)) == Nb_c[t][v][0][c] for v in V[t] for c in range(C)), name='c8')
# Ordre lexicographique au sein des classes
for c in range(C):
n_bis = len(Z_bis[c])
m.addConstrs((quicksum(2 ** (i) * x[Z_bis[c][k], i] for i in range(M)) <= quicksum(
2 ** (i) * x[Z_bis[c][k + 1], i] for i in range(M)) for k in range(n_bis - 1)), name='c10')
#contraintes particulières au dataset liées au one-hot encoding
m.addConstrs((quicksum(x[k, i] for i in one_hot_encoded_groups[w]) == 1 for k in range(N) for w in range(len(one_hot_encoded_groups))),
name='c11')
#Fonction objectif Maximiser ou Minimiser le nombre de 1
#m.setObjective(quicksum(x[k, i] for i in range(M) for k in range(N)), GRB.MINIMIZE) #Modify MINIMIZE in MAXIMIZE if we want to maximize
if verbosity > 0:
print("Model creation done!")
m.optimize()
end = time.time()
duration = end - start
'''
m.computeIIS()
m.write("my_iis.ilp")
'''
# see https://www.gurobi.com/documentation/9.5/refman/optimization_status_codes.html#sec:StatusCodes
statuses = {1: "LOADED", 2: "OPTIMAL", 3: "INFEASIBLE", 4: "INF_OR_UNBD", 5: "UNBOUNDED", 6: "CUTOFF", 7: "ITERATION_LIMIT", 8: "NODE_LIMIT", 9: "TIME_LIMIT", 10: "SOLUTION_LIMIT", 11: "INTERRUPTED", 12: "NUMERIC", 13: "SUBOPTIMAL", 14: "INPROGRESS", 15: "USER_OBJ_LIMIT", 16: "WORK_LIMIT"}
solve_status = statuses[m.status]
if solve_status == "INFEASIBLE":
raise RuntimeError('Infeasible model: the reconstruction problem has no solution. Please report this issue to the developers.')
if verbosity > 0:
print("Solve status: ", solve_status)
##Récupération des solutions
x_sol = x.getAttr(GRB.Attr.X)
x_sol = x_sol.tolist()
self.result_dict = {'max_max_depth':max_max_depth, 'status':solve_status, 'duration': duration, 'reconstructed_data':x_sol}
def perform_reconstruction_v2_CP_SAT(self, n_threads=0, time_out=60, verbosity=1, seed=0, use_mleobj=1, useprobctr = 0 ):
"""
Constructs and solves the CP based dataset reconstruction model (with the use of bagging to train the target random forest) using the OR-Tools CP-SAT solver.
Arguments
---------
n_threads: int >= 0, optional (default 0)
maximum number of threads to be used by the solver to parallelize search
if 0, use all available threads
time_out: int, optional (default 60)
maximum cpu time (in seconds) to be used by the search
if the solver is not able to return a solution within the given time frame, it will be indicated in the returned dictionary
verbosity: int, optional (default 1)
whether to print information (1) about the search progress or not (0)
seed: int, optional (default 0)
random number generator seed
used to fix the behaviour of the solvers
(it is not recommended to play with the two arguments below)
use_mleobj: int, optional (default 1)
Whether to use the new maximum likelihood objective (1)
or another one minimizing the absolute difference to the cumulative distribution of
probability that a sample is used at least b times, for every tree.
useprobctr: int, optional (default 0)
Whether to use constraints that are not necessarily valid, but valid with high probability (measured by epsilon specified within that function) (1) or not (0).
Returns
-------
output: dictionary containing:
-> 'max_max_depth': maximum depth found when parsing the trees within the forest.
-> 'status': the solve status returned by the solver. It can be 'UNKNOWN', 'MODEL_INVALID', 'FEASIBLE', 'INFEASIBLE', or 'OPTIMAL'.
-> 'duration': duration
-> 'reconstructed_data': array of shape = [n_samples, n_attributes] encoding the reconstructed dataset.
Note that if the status is not OPTIMAL or FEASIBLE the reconstruction should not be used.
"""
import ortools
from ortools.sat.python import cp_model
import numpy as np # useful
import time # time measurements
# This is the maximum number of times a sample can appear in a tree (note it will go from 0 to maxbval-1)
maxbval = 8
clf = self.clf
one_hot_encoded_groups = self.ohe_groups
start = time.time()
### Create the CP model
## Parse the forest
T, M, N, C, Z, max_max_depth, trees_branches, maxcards = self.parse_forest(clf, verbosity=verbosity)
# Defines the probabilities that an item will appear b times
P = []
Pexact = [0 for i in range(maxbval)]
for i in range(maxbval):
#P.append( 1 - self.proba_inf(i + 1, N) )
P.append(1 - self.proba_inf(i , N))
for i in range(maxbval):
if i < maxbval - 1:
Pexact[i] = P[i] - P[i+1]
else:
Pexact[i] = P[i]
if verbosity:
print("Probabilities of an item appearing at least b times:")
print(P)
print(sum(P))
print("Probabilities of an item appearing at EXACTLY b times:")
print(Pexact)
print(sum(Pexact))
ntrees = len( trees_branches )
## Variables
model = cp_model.CpModel()
# Reconstruction variables
# x[k][i] : Variables that represent what is sample k (each of its features i)
ord_indices = [f[0] for f in self.ordinal_attrs] # Ordinal attributes => Integer variables
num_indices = [f[0] for f in self.numerical_attrs] # Numerical attributes => Integer variables modelling intervals between two splits
self.compute_numerical_attrs_intervals(trees_branches) # Compute the sorted list of all split values for each numerical attribute
x_vars = [[None] * M for k in range(N)]
for i in range(M):
if i in ord_indices:
idx = ord_indices.index(i)
for k in range(N):
x_vars[k][i] = model.NewIntVar(self.ordinal_attrs[idx][1], self.ordinal_attrs[idx][2], 'x_%d_%d' % (k, i) )
elif i in num_indices:
idx = num_indices.index(i)
#print("Num attr ", i, " : ", self.numerical_attrs[idx][4])
for k in range(N):
x_vars[k][i] = model.NewIntVar(0, len(self.numerical_attrs[idx][3]), 'x_%d_%d' % (k, i) )
else:
for k in range(N):
x_vars[k][i] = model.NewBoolVar('x_%d_%d' %(k,i))
# y_vars[k][c][t][v]: Variables that represent the number of times sample k is used as class c
# in leaf/branch v of tree t
y_vars = [[[[] for t in range(ntrees)] for c in range(C)] for k in range(N)]
# w_vars[k][t][v]: Variables that represent if sample k is used in leaf v of tree t
w_vars = [[[] for t in range(ntrees) ] for k in range(N)]
# z_vars[k][c]: Variables that represent if sample k is assigned class c
z_vars = [[model.NewBoolVar('z_%d_%d'%(k,c)) for c in range(C) ] for k in range(N) ]
fixedzidx = 0
for c in range(C):
mincz = math.floor( maxcards[c] / maxbval)
for offset in range(mincz):
model.Add( z_vars[fixedzidx+offset][c] == 1)
fixedzidx += mincz
# eta_vars[k][t]: Variables that count how many times sample k is used in tree t
eta_vars = [[ model.NewIntVar(0,maxbval, 'eta_%d_%d'%(k,t)) for t in range(ntrees)] for k in range(N) ]
# q_vars[k][t][b] Variables that represent if sample k appears b times in tree t (needed for objective function
q_vars = [[[model.NewBoolVar('q_%d_%d_%d' %( t, k, b )) for b in range(maxbval) ] for t in range(ntrees) ] for k in range(N) ]
if use_mleobj == 0:
# obj_vars[t][b]: Variables that will capture the difference between sum_{k} q_{tkb} - N * p_b, for fixed t and b
obj_vars = [ [ model.NewIntVar(-N,N, 'obj_%d_%d' % (t,b) ) for b in range(maxbval) ] for t in range(ntrees) ]
# abs_obj_vars[t][b]: Variables that will capture the absolute value of obj_vars for fixed t and b
abs_obj_vars = [ [ model.NewIntVar(0,N, 'absobj_%d_%d' % (t,b) ) for b in range(maxbval) ] for t in range(ntrees) ]
objfun = []
if use_mleobj:
objfuncoeff = []
for t in range(ntrees):
for b in range(maxbval):
if use_mleobj == 1:
for k in range(N):
objfuncoeff.append( int( 10 * math.log(Pexact[b]) ) )
objfun.append( q_vars[k][t][b] )
else:
# Set obj value abs constraints
model.AddAbsEquality( abs_obj_vars[t][b], obj_vars[t][b] )
#Set relationship between obj_vars and q_vars
model.Add( cp_model.LinearExpr.Sum( [q_vars[k][t][bp] for k in range(N) for bp in range(b,maxbval) ] ) - int( N * P[b] ) == obj_vars[t][b] )
objfun.append( abs_obj_vars[t][b] )
if use_mleobj == 1:
model.Maximize( cp_model.LinearExpr.WeightedSum( objfun, objfuncoeff ) )
else:
model.Minimize( cp_model.LinearExpr.Sum(objfun) )
# Contraints
# one-hot encoding
for k in range(N):
for w in range(
len(one_hot_encoded_groups)): # for each group of binary attributes one-hot encoding the same attribute
model.Add(cp_model.LinearExpr.Sum([x_vars[k][i] for i in one_hot_encoded_groups[w]]) == 1)
# Enforces that every sample must be in at most one class
model.Add( cp_model.LinearExpr.Sum( z_vars[k] ) == 1)
for t in range(ntrees):
# Enforces relationship between counting variable eta and binary variables q
ortools_version = str(ortools.__version__).split(".")
if int(ortools_version[0]) <= 9 and int(ortools_version[1]) <= 8:
model.AddMapDomain( eta_vars[k][t], q_vars[k][t], offset=0 )
else:
model.add_map_domain( eta_vars[k][t], q_vars[k][t], offset=0 )
nleaves = []
for tid, all_branches_t in enumerate(trees_branches): # for each tree
etayvars = [[] for k in
range(N)] # for each example we will ensure it is captured by exactly one branch
nleaves.append( len(all_branches_t) )
for a_branch_nb, a_branch in enumerate(all_branches_t): # iterate over its branches
# Variables that will be involved in making sure number of samples in each leaf/branch is consistent
branch_vars_c = [[] for c in range(C)]
for k in range(N):
w_vars[k][tid].append( model.NewBoolVar('w_%d_%d_%d'%(k,tid,a_branch_nb)) )
# The loop right after is used to construct the constraints that if k is used in a given tree at a given leaf/branch
# Then the x's must respect all the splits within the corresponding branch
for a_split in a_branch[0]:
feature_val = a_split[0]
feature_id = abs(feature_val)-1
threshold_val = a_split[1]
if feature_id in num_indices:
idx = num_indices.index(feature_id)
threshold_val = self.numerical_attrs[idx][3].index(threshold_val) # replace the actual split value with its index (interval nb)
if feature_val > 0:
model.Add( x_vars[k][feature_id] >= int( math.floor( threshold_val ) ) + 1 ).OnlyEnforceIf( w_vars[k][tid][a_branch_nb] )
elif feature_val < 0:
model.Add(x_vars[k][feature_id] <= int( math.floor( threshold_val ) ) ).OnlyEnforceIf( w_vars[k][tid][a_branch_nb])
else:
raise ValueError("Feat 0 shouldn't be used here (1-indexed now)")
for c in range(C):
# Variable y represents how many times sample k is used in tree tid, node a_branch_nb,
# being that k is classified as class c
y_vars[k][c][tid].append( model.NewIntVar(0, maxbval, 'y_%d_%d_%d_%d' % (tid, a_branch_nb, k, c)) )
etayvars[k].append(y_vars[k][c][tid][a_branch_nb])
branch_vars_c[c].append(y_vars[k][c][tid][a_branch_nb])
# Constraints that enforce consistency between w and y variables
model.Add( y_vars[k][c][tid][a_branch_nb] == 0 ).OnlyEnforceIf( w_vars[k][tid][a_branch_nb].Not() )
# Constraints that enforce consistency between y and z variables
model.Add( y_vars[k][c][tid][a_branch_nb] == 0 ).OnlyEnforceIf( z_vars[k][c].Not() )
for c in range(C):
model.Add(cp_model.LinearExpr.Sum(branch_vars_c[c]) == int(
a_branch[1][c])) # enforces the branch per-class cardinality
for k in range(N):
model.Add(
cp_model.LinearExpr.Sum(etayvars[k]) == eta_vars[k][tid] ) # eta (Number of samples in a tree) is consistent with y
## ADDS probabilistic constraints, i.e. constraints that are not necessarily valid, but hold with high probability
## High here means <= eps
if useprobctr:
eps = 0.005
for b in range(2,maxbval):
# prob gets probability that a sample appears at least b times
prob = P[b]
cnt = 0
while prob > eps:
prob = prob*P[b]
cnt += 1
# This means that with prob >= 1-eps, cannot have more than cnt many q's having value at least b
print("Probabilistic constraint: At most %d samples appear %d or more times in a tree ( probability of this being true is %g) " %(cnt,b,1.0-prob) )
model.Add( cp_model.LinearExpr.Sum( [ q_vars[k][t][bp] for k in range(N) for bp in range(b,maxbval) ] ) <= cnt )
# Ordre lexicographique au sein des classes
'''deb = 0
for c in range(C): # for each class
for k in range(deb, deb + int(cards[c])-1):
model.Add(sum(2 ** (i) * x_vars[k][i] for i in range(M)) <= sum(2 ** (i) * x_vars[k+1][i] for i in range(M)))
deb += int(cards[c])'''
if verbosity:
print("Model creation done!")
# Some of this assumes that there are only two classes, so we will check this
singlerun = 1
if singlerun:
nfreezperrun = N
else:
assert( C == 2 )
# Let the solver decide on nfreezperrun number of z's at a time
nfreezperrun = 5
nruns = math.ceil( (N - fixedzidx) / nfreezperrun )
if nruns == 0 or singlerun == 1:
nruns = 1