-
Notifications
You must be signed in to change notification settings - Fork 0
/
localbranching.py
1024 lines (806 loc) · 40.1 KB
/
localbranching.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
import pyscipopt
from pyscipopt import Model
import numpy as np
import sys
from memory_profiler import profile
from models_rl import SimplePolicy
import torch
from utility import imitation_accuracy
import pathlib
import gzip
import pickle
from event import PrimalBoundChangeEventHandler
class LocalBranching:
def __init__(self, MIP_model, MIP_sol_bar, k=20, node_time_limit=30, total_time_limit=3600, is_symmetric=True):
self.MIP_model = MIP_model
self.MIP_sol_best = self.copy_solution( self.MIP_model, MIP_sol_bar)
self.MIP_obj_best = self.MIP_model.getSolObjVal(self.MIP_sol_best)
self.MIP_obj_init = self.MIP_obj_best # initial obj before adapting k
self.MIP_sol_bar = self.copy_solution( self.MIP_model, MIP_sol_bar)
self.subMIP_sol_best = self.copy_solution(self.MIP_model, MIP_sol_bar)
self.MIP_obj_bar = self.MIP_obj_best
self.n_vars = self.MIP_model.getNVars()
self.n_binvars = self.MIP_model.getNBinVars()
self.default_node_time_limit = node_time_limit
self.default_initial_node_time_limit = node_time_limit
self.primal_no_improvement_account = 0
self.total_time_limit = total_time_limit
self.total_time_available = self.total_time_limit
self.total_time_expired = 0
self.div_max = 3
self.default_k = k
self.eps = eps = .0000001
self.t_node = self.default_node_time_limit
self.k = k
self.first = False
self.diversify = False
self.div = 0
self.is_symmetric = is_symmetric
# if not self.is_symmetric:
# self.default_k = self.default_k / 2
self.reset_k_at_2nditeration = False
self.rightbranch_index = 0
self.actions = {'reset': 0, 'unchange':1, 'increase': 2, 'decrease':3, 'free':4}
self.k_stepsize = 1/2
self.t_stepsize = 3
self.alpha = 0.01
self.primal_objs = []
self.primal_times = []
self.primal_times.append(self.total_time_limit - self.total_time_available)
self.primal_objs.append(self.MIP_obj_best)
self.primalbound_handler = PrimalBoundChangeEventHandler()
self.MIP_model.includeEventhdlr(self.primalbound_handler, 'primal_bound_update_handler',
'store every new primal bound and its time stamp')
def create_subMIP(self):
# self.subMIP_model, subMIP_vars, success = self.MIP_model.createCopy(problemName='subMIPmodel', origcopy=False)
#
# # create a primal solution for the copy MIP by copying the solution of original MIP
# self.subMIP_sol_bar = self.subMIP_model.createSol()
# self.n_vars = self.MIP_model.getNVars()
# MIP_vars = self.MIP_model.getVars()
#
# for j in range(self.n_vars):
# val = self.MIP_model.getSolVal(self.MIP_sol_bar, MIP_vars[j])
# self.subMIP_model.setSolVal(self.subMIP_sol_bar, subMIP_vars[j], val)
self.subMIP_model = self.MIP_model
self.subMIP_model.resetParams()
self.subMIP_sol_bar = self.MIP_sol_bar
# feasible = self.subMIP_model.checkSol(solution=self.subMIP_sol_bar)
# if feasible:
# self.subMIP_model.addSol(subMIP_sol_bar, False)
# print("the incumbent solution of subMIP for local branching is added to subMIP")
# else:
# print("Error: the incumbent solution of subMIP for local branching is not feasible!")
if not self.first == True:
self.subMIP_ub = self.subMIP_model.getSolObjVal(self.subMIP_sol_bar)
else:
self.subMIP_ub = self.subMIP_model.infinity()
if self.subMIP_ub >=0:
self.subMIP_model.setObjlimit(0.999 * self.subMIP_ub)
else:
self.subMIP_model.setObjlimit(1.001 * self.subMIP_ub)
self.primalbound_handler.primal_times = []
self.primalbound_handler.primal_bounds = []
# print("Initial obj before LB: {}".format(self.subMIP_obj_bar))
def copy_solution(self, model, solution):
"""create a copy of solution for MIP_model"""
solution_copy = model.createSol()
MIP_vars = model.getVars()
self.n_vars = model.getNVars()
for j in range(self.n_vars):
val = model.getSolVal(solution, MIP_vars[j])
model.setSolVal(solution_copy, MIP_vars[j], val)
return solution_copy
def copy_solution_subMIP_to_MIP(self, subMIP_sol, MIP_sol):
"""copy a solution of subMIP to MIP"""
subMIP_vars = self.subMIP_model.getVars()
MIP_vars = self.MIP_model.getVars()
for j in range(self.n_vars):
val = self.subMIP_model.getSolVal(subMIP_sol, subMIP_vars[j])
self.MIP_model.setSolVal(MIP_sol, MIP_vars[j], val)
def left_branch(self, t_node, is_symmetric=True):
self.create_subMIP()
if is_symmetric:
self.add_LBconstraint()
else:
self.add_LBconstraintAsym()
self.subMIP_model.setParam('limits/time', t_node)
self.subMIP_model.setParam("display/verblevel", 0)
# for strong diversify(first==True), abort as soon as finding first feasible solution.
if self.first:
self.subMIP_model.setParam('limits/solutions', 1)
self.subMIP_model.setSeparating(pyscipopt.SCIP_PARAMSETTING.FAST)
self.subMIP_model.setPresolve (pyscipopt.SCIP_PARAMSETTING.FAST)
self.subMIP_model.optimize()
def step_localbranch(self, k_action, t_action, lb_bits):
self.k = self.update_k(k_action, self.k_stepsize)
self.t_node = self.update_t(t_action, self.t_stepsize)
# reset k_stand and k at the 2nd iteration if reset option is enable
if (lb_bits == 2) and self.reset_k_at_2nditeration:
self.default_k = 20
if not self.is_symmetric:
self.default_k = 10
self.k = self.default_k
self.diversify = False
self.first = False
t_node = np.minimum(self.t_node, self.total_time_available)
self.left_branch(t_node, is_symmetric=self.is_symmetric) # execute 1 iteration of lb
# node_time_limit = self.node_time_limit
self.primal_no_improvement_account += 1
t_leftbranch = self.subMIP_model.getSolvingTime()
self.total_time_available -= t_leftbranch
subMIP_status = self.subMIP_model.getStatus()
# update best obj of original MIP before printing
# subMIP_obj_best = self.subMIP_model.getObjVal()
# if subMIP_obj_best < self.MIP_obj_best:
# self.MIP_obj_best = subMIP_obj_best
div_pre = self.div
k_pre = self.k
MIP_obj_best_pre = self.MIP_obj_best
state = np.zeros((7, ))
n_sols_subMIP = self.subMIP_model.getNSols()
subMIP_obj_best = None
if n_sols_subMIP > 0:
subMIP_sol_best = self.subMIP_model.getBestSol()
subMIP_obj_best = self.subMIP_model.getSolObjVal(subMIP_sol_best)
if subMIP_obj_best < self.MIP_obj_best:
primal_bounds = self.primalbound_handler.primal_bounds
primal_times = self.primalbound_handler.primal_times
self.primal_no_improvement_account = 0
for i in range(len(primal_times)):
primal_times[i] += self.total_time_expired
self.primal_objs.extend(primal_bounds)
self.primal_times.extend(primal_times)
# case 1
if subMIP_status == "optimal" or subMIP_status == "bestsollimit":
subMIP_sol_best = self.subMIP_model.getBestSol()
self.copy_solution_subMIP_to_MIP(subMIP_sol_best, self.subMIP_sol_best)
subMIP_obj_best = self.subMIP_model.getSolObjVal(subMIP_sol_best)
# assert subMIP_obj_best < self.subMIP_ub, "SubMIP is optimal and improved solution of subMIP is expected! But no improved solution found!"
self.subMIP_model.freeTransform()
# add the reversed right branch constraint to MIP_model
if self.is_symmetric == True:
self.rightbranch_reverse(k=self.k)
else:
self.rightbranch_reverse_asym(k=self.k)
# update best MIP_sol_bar
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_bar)
self.MIP_obj_bar = subMIP_obj_best
# update MIP_sol_best and best obj of original MIP
if subMIP_obj_best < self.MIP_obj_best:
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_best)
self.MIP_obj_best = subMIP_obj_best
self.diversify = False
self.first = False
state[0:5] = [1, 0, 0, 0, 0]
# self.k = self.k_standard
# case 2
elif subMIP_status == "infeasible" or subMIP_status == "inforunbd":
self.subMIP_model.freeTransform()
# add the reversed right branch constraint to MIP_model
if self.is_symmetric == True:
self.rightbranch_reverse(k=self.k)
else:
self.rightbranch_reverse_asym(k=self.k)
state[0:5] = [0, 1, 0, 0, 0]
if self.diversify:
self.div += 1
# node_time_limit = self.subMIP_model.infinity()
self.first = True
state[4] = 1 # set state[first]=1 when sol not improved for successive 2 iterations
# self.k += np.ceil(self.k_standard / 2)
self.diversify = True
elif subMIP_status == "timelimit" or subMIP_status == "sollimit":
n_sols = self.subMIP_model.getNSols()
subMIP_sol_best = self.subMIP_model.getBestSol()
self.copy_solution_subMIP_to_MIP(subMIP_sol_best, self.subMIP_sol_best)
subMIP_obj_best = self.subMIP_model.getSolObjVal(subMIP_sol_best)
# case 3
if n_sols > 0 and subMIP_obj_best < self.subMIP_ub:
self.subMIP_model.freeTransform()
if not self.first:
# add the reversed right branch constraint to exclude MIP_sol_bar
if self.is_symmetric == True:
self.rightbranch_reverse(k=0.0)
else:
self.rightbranch_reverse_asym(k=0.0)
# to do: refine best solution
# assert subMIP_obj_best < self.subMIP_ub, "SubMIP has feasible solutions and an improved solution of subMIP is expected! But no improved solution found!"
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_bar)
self.MIP_obj_bar = subMIP_obj_best
# update best obj of original MIP
if subMIP_obj_best < self.MIP_obj_best:
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_best)
self.MIP_obj_best = subMIP_obj_best
self.diversify = False
self.first = False
# self.k = self.k_standard
state[0:5] = [0, 0, 1, 0, 0]
# case 4
else:
self.subMIP_model.freeTransform()
state[0:5] = [0, 0, 0, 1, 0]
if self.diversify:
# to do: add the reversed right branch constraint to exclude MIP_sol_bar
if self.is_symmetric == True:
self.rightbranch_reverse(k=0.0)
else:
self.rightbranch_reverse_asym(k=0.0)
self.div += 1
# node_time_limit = self.subMIP_model.infinity()
# self.k += np.ceil(self.k_standard / 2)
self.first = True
state[4] = 1
# else:
# self.k -= np.ceil(self.k_standard / 2)
self.diversify = True
self.subMIP_model.delCons(self.constraint_LB)
self.subMIP_model.releasePyCons(self.constraint_LB)
del self.constraint_LB
if self.primal_no_improvement_account > 0 and self.primal_no_improvement_account % 5 == 0:
self.default_node_time_limit *= self.t_stepsize
if self.default_node_time_limit > self.default_initial_node_time_limit and self.primal_no_improvement_account == 0:
self.default_node_time_limit /= self.t_stepsize
print('LB round: {:.0f}'.format(lb_bits),
'Solving time: {:.4f}'.format(self.total_time_limit - self.total_time_available),
'Best Obj: {:.4f}'.format(self.MIP_obj_best),
# 'Obj_subMIP: {:.4f}'.format(str(subMIP_obj_best)),
'n_sols_subMIP: {:.0f}'.format(n_sols_subMIP),
'K: {:.0f}'.format(k_pre),
'self.div: {:.0f}'.format(div_pre),
'LB Status: {}'.format(subMIP_status)
)
# avoid negative time reward
if t_leftbranch > t_node:
t_leftbranch = t_node
# calculate rewards reward = alpha * reward_t + (1-alpha * reward_obj)
obj_norm = np.abs(MIP_obj_best_pre - self.MIP_obj_best)/ np.maximum(np.abs(MIP_obj_best_pre), np.abs(self.MIP_obj_best))
t_norm = 1 - t_leftbranch / t_node
state[5:7] = [t_norm, obj_norm]
# # calculate rewards reward = alpha * reward_t + (1-alpha * reward_obj)
# reward_obj = obj_norm
# if obj_norm > 0:
# if t_leftbranch >= t_node:
# reward_t = -1
# else:
# reward_t = t_norm
# else:
# reward_t = 0
# reward = self.alpha * reward_t + (1-self.alpha)*reward_obj
# calculate reward: reward = obj_improve_bit * t_leftbranch_bit
# # reward 1
# obj_best_local = self.MIP_obj_best # reward option 1: use the best obj after running that lb iteration
# obj_improve = np.abs(self.MIP_obj_init - obj_best_local) / np.abs(self.MIP_obj_init)
# reward = obj_improve * t_leftbranch
# # reward 2
# obj_best_local = MIP_obj_best_pre # reward option 2: use the best obj before running that lb iteration
# obj_improve = np.abs(self.MIP_obj_init - obj_best_local) / np.abs(self.MIP_obj_init)
# reward = obj_improve * t_leftbranch
# reward 3
obj_improve_local = np.abs(MIP_obj_best_pre - self.MIP_obj_best) / np.abs(self.MIP_obj_init)
reward = obj_improve_local * self.total_time_available
done = (self.total_time_available <= 0) or (self.k >= self.n_binvars)
info = None
self.total_time_expired += t_leftbranch
return state, reward, done, info
def solve_rightbranch(self):
"""
solve the MIP of right branch with the time available.
:return:
"""
self.MIP_model.addSol(self.MIP_sol_best)
self.primalbound_handler.primal_bounds = []
self.primalbound_handler.primal_times = []
if self.total_time_available > 0:
self.MIP_model.setObjlimit(self.MIP_obj_best - self.eps)
self.MIP_model.setParam('limits/time', self.total_time_available)
self.MIP_model.optimize()
best_obj = self.MIP_model.getObjVal()
if best_obj < self.MIP_obj_best:
self.MIP_obj_best = best_obj
if self.subMIP_model.getNSols() > 0:
subMIP_sol_best = self.subMIP_model.getBestSol()
subMIP_obj_best = self.subMIP_model.getSolObjVal(subMIP_sol_best)
primal_bounds = self.primalbound_handler.primal_bounds
primal_times = self.primalbound_handler.primal_times
for i in range(len(primal_times)):
primal_times[i] += self.total_time_expired
self.primal_objs.extend(primal_bounds)
self.primal_times.extend(primal_times)
self.total_time_available -= self.MIP_model.getSolvingTime()
self.total_time_expired += self.MIP_model.getSolvingTime()
def policy_vanilla(self, state):
lb_status = state[0:4].argmax()
if lb_status == 0: #[1, 0, 0, 0]
k_action = self.actions['reset']
t_action = self.actions['reset']
elif lb_status == 1: # state[0:4] == [0, 1, 0, 0]:
if state[4] == 0:
k_action = self.actions['increase']
t_action = self.actions['reset']
elif state[4] == 1:
k_action = self.actions['increase']
t_action = self.actions['free']
elif lb_status == 2: # state[0:4] == [0, 0, 1, 0]:
k_action = self.actions['reset']
t_action = self.actions['reset']
elif lb_status == 3:# state[0:4] == [0, 0, 0, 1]:
if state[4] == 0:
k_action = self.actions['decrease']
t_action = self.actions['reset']
elif state[4] == 1:
k_action = self.actions['increase']
t_action = self.actions['free']
return k_action, t_action
def update_k(self, action, k_stepsize):
switcher = {
self.actions['reset']: self.default_k,
self.actions['unchange']: self.k,
self.actions['decrease']: np.ceil(self.k - k_stepsize * self.k),
self.actions['increase']: np.ceil(self.k + k_stepsize * self.k)
}
return switcher.get(action, 'Error: Invilid k action!')
def update_t(self, action, t_stepsize):
switcher = {
self.actions['reset']: self.default_node_time_limit,
self.actions['unchange']: self.t_node,
self.actions['decrease']: self.t_node - t_stepsize * self.t_node,
self.actions['increase']: self.t_node + t_stepsize * self.t_node,
self.actions['free']: self.MIP_model.infinity()
}
return switcher.get(action, 'Error: Invilid k action!')
def mdp_localbranch(self, is_symmetric=True, reset_k_at_2nditeration=False, policy=None, optimizer=None, criterion=None, device=None, samples_dir=None):
# self.total_time_limit = total_time_limit
self.total_time_available = self.total_time_limit
self.first = False
self.diversify = False
self.t_node = self.default_node_time_limit
self.div = 0
self.is_symmetric = is_symmetric
self.reset_k_at_2nditeration = reset_k_at_2nditeration
lb_bits = 0
t_list = []
obj_list = []
lb_bits_list = []
lb_bits_list.append(lb_bits)
t_list.append(self.total_time_limit - self.total_time_available)
obj_list.append(self.MIP_obj_best)
accu_instance = 0
loss_instance = 0
k_action = self.actions['unchange']
t_action = self.actions['unchange']
done = (self.total_time_available <= 0) or (self.k >= self.n_binvars)
while not done: # and self.div < self.div_max
lb_bits += 1
# execute one iteration of LB and get the state and rewards
state, rewards, done, _ = self.step_localbranch(k_action=k_action, t_action=t_action, lb_bits=lb_bits)
# k_vanilla, t_action = self.policy_vanilla(state)
# k_action = k_vanilla
if policy is not None:
# state_torch = torch.FloatTensor(state).view(1, -1)
# k_vanilla_torch = torch.LongTensor(np.array(k_vanilla).reshape(-1))
# if device is not None:
# state_torch.to(device)
# k_vanilla_torch.to(device)
# k_pred = policy(state_torch)
# loss = criterion(k_pred, k_vanilla_torch)
# accu = imitation_accuracy(k_pred, k_vanilla_torch)
# # for online learning, update policy
# if optimizer is not None:
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# loss_instance += loss.item()
# accu_instance += accu.item()
# k_action = k_pred.argmax(1, keepdim=False).item()
k_action = policy.select_action(state)
else:
k_vanilla, t_action = self.policy_vanilla(state)
k_action = k_vanilla
# data_sample = [state, k_vanilla]
#
# filename = f'{samples_dir}imitation_{self.MIP_model.getProbName()}_{lb_bits}.pkl'
#
# with gzip.open(filename, 'wb') as f:
# pickle.dump(data_sample, f)
lb_bits_list.append(lb_bits)
t_list.append(self.total_time_limit - self.total_time_available)
obj_list.append(self.MIP_obj_best)
print(
'K_final: {:.0f}'.format(self.k),
'div_final: {:.0f}'.format(self.div)
)
self.solve_rightbranch()
t_list.append(self.total_time_limit - self.total_time_available)
obj_list.append(self.MIP_obj_best)
status = self.MIP_model.getStatus()
# if status == "optimal" or status == "bestsollimit":
# self.MIP_obj_best = self.MIP_model.getObjVal()
elapsed_time = self.total_time_limit - self.total_time_available
lb_bits_list = np.array(lb_bits_list).reshape(-1)
times_list = np.array(t_list).reshape(-1)
objs_list = np.array(obj_list).reshape(-1)
del self.subMIP_sol_best
del self.MIP_sol_bar
del self.MIP_sol_best
loss_instance = loss_instance / lb_bits
accu_instance = accu_instance / lb_bits
return status, self.MIP_obj_best, elapsed_time, lb_bits_list, times_list, objs_list, loss_instance, accu_instance
def search_localbranch(self, is_symmetric=True, reset_k_at_2nditeration=False):
# self.total_time_limit = total_time_limit
self.total_time_available = self.total_time_limit
self.first = False
self.diversify = False
node_time_limit = self.default_node_time_limit
self.div = 0
self.is_symmetric = is_symmetric
lb_bits = 0
t_list = []
obj_list = []
lb_bits_list = []
lb_bits_list.append(lb_bits)
t_list.append(self.total_time_limit - self.total_time_available)
obj_list.append(self.MIP_obj_best)
while self.total_time_available > 0 and self.k < self.n_binvars: # and self.div < self.div_max
lb_bits += 1
# reset k_stand and k at the 2nd iteration if reset option is enable
if lb_bits == 2 and reset_k_at_2nditeration == True:
self.default_k = 20
if not self.is_symmetric:
self.default_k = 10
self.k = self.default_k
self.diversify = False
self.first = False
node_time_limit = np.minimum(node_time_limit ,self.total_time_available)
self.left_branch(node_time_limit, is_symmetric=self.is_symmetric) # execute 1 iteration of lb
node_time_limit = self.default_node_time_limit
self.total_time_available -= self.subMIP_model.getSolvingTime()
subMIP_status = self.subMIP_model.getStatus()
# update best obj of original MIP before printing
# subMIP_obj_best = self.subMIP_model.getObjVal()
# if subMIP_obj_best < self.MIP_obj_best:
# self.MIP_obj_best = subMIP_obj_best
div_pre = self.div
k_pre = self.k
# case 1
if subMIP_status == "optimal" or subMIP_status == "bestsollimit":
subMIP_sol_best = self.subMIP_model.getBestSol()
self.copy_solution_subMIP_to_MIP(subMIP_sol_best, self.subMIP_sol_best)
subMIP_obj_best = self.subMIP_model.getSolObjVal(subMIP_sol_best)
assert subMIP_obj_best < self.subMIP_ub, "SubMIP is optimal and improved solution of subMIP is expected! But no improved solution found!"
self.subMIP_model.freeTransform()
# add the reversed right branch constraint to MIP_model
if self.is_symmetric == True:
self.rightbranch_reverse(k=self.k)
else:
self.rightbranch_reverse_asym(k=self.k)
# update best obj of original MIP
if subMIP_obj_best < self.MIP_obj_best:
self.MIP_obj_best = subMIP_obj_best
# update best MIP_sol_bar
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_bar)
self.MIP_obj_bar = subMIP_obj_best
# update MIP_sol_best
if subMIP_obj_best < self.MIP_obj_best:
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_best)
self.MIP_obj_best = subMIP_obj_best
self.diversify = False
self.first = False
self.k = self.default_k
# case 2
elif subMIP_status == "infeasible" or subMIP_status == "inforunbd":
self.subMIP_model.freeTransform()
# add the reversed right branch constraint to MIP_model
if self.is_symmetric == True:
self.rightbranch_reverse(k=self.k)
else:
self.rightbranch_reverse_asym(k=self.k)
if self.diversify:
self.div += 1
node_time_limit = self.subMIP_model.infinity()
self.first =True
self.k += np.ceil(self.default_k / 2)
self.diversify = True
elif subMIP_status == "timelimit" or subMIP_status == "sollimit":
n_sols = self.subMIP_model.getNSols()
subMIP_sol_best = self.subMIP_model.getBestSol()
self.copy_solution_subMIP_to_MIP(subMIP_sol_best, self.subMIP_sol_best)
subMIP_obj_best = self.subMIP_model.getSolObjVal(subMIP_sol_best)
# case 3
if n_sols >0 and subMIP_obj_best < self.subMIP_ub:
self.subMIP_model.freeTransform()
if not self.first:
# add the reversed right branch constraint to exclude MIP_sol_bar
if self.is_symmetric == True:
self.rightbranch_reverse(k=0.0)
else:
self.rightbranch_reverse_asym(k=0.0)
# to do: refine best solution
# assert subMIP_obj_best < self.subMIP_ub, "SubMIP has feasible solutions and an improved solution of subMIP is expected! But no improved solution found!"
# update best obj of original MIP
if subMIP_obj_best < self.MIP_obj_best:
self.MIP_obj_best = subMIP_obj_best
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_bar)
self.MIP_obj_bar = subMIP_obj_best
if subMIP_obj_best < self.MIP_obj_best:
self.copy_solution_subMIP_to_MIP(self.subMIP_sol_best, self.MIP_sol_best)
self.MIP_obj_best = subMIP_obj_best
self.diversify = False
self.first = False
self.k = self.default_k
# case 4
else:
self.subMIP_model.freeTransform()
if self.diversify:
# to do: add the reversed right branch constraint to exclude MIP_sol_bar
if self.is_symmetric == True:
self.rightbranch_reverse(k=0.0)
else:
self.rightbranch_reverse_asym(k=0.0)
self.div += 1
node_time_limit = self.subMIP_model.infinity()
self.k += np.ceil(self.default_k / 2)
self.first = True
else:
self.k -= np.ceil(self.default_k / 2)
self.diversify = True
print('LB round: {:.0f}'.format(lb_bits),
'Solving time: {:.4f}'.format(self.total_time_limit - self.total_time_available),
'Best Obj: {:.4f}'.format(self.MIP_obj_best),
# 'Best Obj subMIP: {:.4f}'.format(self.subMIP_model.getObjVal()),
'K: {:.0f}'.format(k_pre),
'self.div: {:.0f}'.format(div_pre),
'LB Status: {}'.format(subMIP_status)
)
lb_bits_list.append(lb_bits)
t_list.append(self.total_time_limit - self.total_time_available)
obj_list.append(self.MIP_obj_best)
self.subMIP_model.delCons(self.constraint_LB)
self.subMIP_model.releasePyCons(self.constraint_LB)
del self.constraint_LB
# self.subMIP_model.freeSol(self.subMIP_sol_bar)
# self.subMIP_model.freeProb()
# del self.subMIP_sol_bar
# del self.subMIP_model
print(
'K_final: {:.0f}'.format(self.k),
'div_final: {:.0f}'.format(self.div)
)
self.MIP_model.setObjlimit(self.MIP_obj_best - self.eps)
self.MIP_model.addSol(self.MIP_sol_best)
if self.total_time_available > 0:
self.MIP_model.setParam('limits/time', self.total_time_available)
self.MIP_model.optimize()
status = self.MIP_model.getStatus()
if status == "optimal" or status == "bestsollimit":
self.MIP_obj_best = self.MIP_model.getObjVal()
self.total_time_available -= self.MIP_model.getSolvingTime()
elapsed_time = self.total_time_limit - self.total_time_available
t_list.append(self.total_time_limit - self.total_time_available)
obj_list.append(self.MIP_obj_best)
lb_bits = np.array(lb_bits_list).reshape(-1)
times = np.array(t_list).reshape(-1)
objs = np.array(obj_list).reshape(-1)
del lb_bits_list
del t_list
del obj_list
del self.subMIP_sol_best
del self.MIP_sol_bar
del self.MIP_sol_best
return status, self.MIP_obj_best, elapsed_time, lb_bits, times, objs
def rightbranch_reverse(self, k):
"""
add the reversed right branch constraint to MIP_model
:param k:
:return:
"""
vars = self.MIP_model.getVars()
n_binvars = self.MIP_model.getNBinVars()
rhs = self.MIP_model.infinity()
lhs = k + 1
cons_vars = np.empty(n_binvars, dtype=np.object)
cons_vals = np.empty(n_binvars)
# compute coefficient for reversed LB constraint
for i in range(0, n_binvars):
val = self.MIP_model.getSolVal(self.MIP_sol_bar, vars[i])
assert self.MIP_model.isFeasIntegral(val), "Error: Solution passed to LB is not integral!"
if self.MIP_model.isFeasEQ(val, 1.0):
cons_vals[i] = -1.0
lhs -=1.0
rhs -=1.0
else:
cons_vals[i] = 1.0
cons_vars[i] = vars[i]
assert cons_vars[i].vtype() == "BINARY", "Error: local branching constraint uses a non-binary variable!"
# create right local branch constraints
constraint_rightbranch = self.MIP_model.createConsBasicLinear(self.MIP_model.getProbName() + '_rightbranching_'+ str(self.rightbranch_index), n_binvars,
cons_vars, cons_vals, lhs, rhs)
self.MIP_model.addPyCons(constraint_rightbranch)
self.MIP_model.releasePyCons(constraint_rightbranch)
del constraint_rightbranch
del vars
del cons_vars
del cons_vals
def rightbranch_reverse_asym(self, k):
"""
add the reversed right branch constraint to MIP_model
:param k:
:return:
"""
vars = self.MIP_model.getVars()
n_binvars = self.MIP_model.getNBinVars()
rhs = self.MIP_model.infinity()
lhs = k + 1
cons_vars = np.empty(n_binvars, dtype=np.object)
cons_vals = np.empty(n_binvars)
# compute coefficient for reversed LB constraint
for i in range(0, n_binvars):
val = self.MIP_model.getSolVal(self.MIP_sol_bar, vars[i])
assert self.MIP_model.isFeasIntegral(val), "Error: Solution passed to LB is not integral!"
if self.MIP_model.isFeasEQ(val, 1.0):
cons_vals[i] = -1.0
lhs -=1.0
rhs -=1.0
else:
cons_vals[i] = 0.0
cons_vars[i] = vars[i]
assert cons_vars[i].vtype() == "BINARY", "Error: local branching constraint uses a non-binary variable!"
# create right local branch constraints
constraint_rightbranch = self.MIP_model.createConsBasicLinear(self.MIP_model.getProbName() + '_rightbranching_'+ str(self.rightbranch_index), n_binvars,
cons_vars, cons_vals, lhs, rhs)
self.MIP_model.addPyCons(constraint_rightbranch)
self.MIP_model.releasePyCons(constraint_rightbranch)
del constraint_rightbranch
del vars
del cons_vars
del cons_vals
def add_LBconstraint(self):
"""symmetric local branching constraint over all binary variables"""
vars = self.subMIP_model.getVars()
n_binvars = self.subMIP_model.getNBinVars()
lhs = 0
rhs = self.k
cons_vars = np.empty(n_binvars, dtype=np.object)
cons_vals = np.empty(n_binvars)
# compute coefficients for LB constraint
for i in range(0, n_binvars):
val = self.subMIP_model.getSolVal(self.subMIP_sol_bar, vars[i])
assert self.subMIP_model.isFeasIntegral(val), "Error: Solution passed to LB is not integral!"
if self.subMIP_model.isFeasEQ(val, 1.0):
cons_vals[i] = -1.0
lhs -= 1.0
rhs -= 1.0
else:
cons_vals[i] = 1.0
cons_vars[i] = vars[i]
assert cons_vars[i].vtype() == "BINARY", "Error: local branching constraint uses a non-binary variable!"
# create and add LB constraint to mip_model
self.constraint_LB = self.subMIP_model.createConsBasicLinear(self.subMIP_model.getProbName() + "_localbranching", n_binvars,
cons_vars, cons_vals, lhs, rhs)
self.subMIP_model.addPyCons(self.constraint_LB)
# self.subMIP_model.releasePyCons(self.constraint_LB)
del vars
del cons_vars
del cons_vals
# for j in range(0, n_binvars): # release cons_vars variables after creating a constraint
# self.subMIP_model.releaseVar(cons_vars[j])
def add_LBconstraintAsym(self):
"""symmetric local branching constraint over all binary variables"""
vars = self.subMIP_model.getVars()
n_binvars = self.subMIP_model.getNBinVars()
lhs = 0
rhs = self.k
cons_vars = np.empty(n_binvars, dtype=np.object)
cons_vals = np.empty(n_binvars)
# compute coefficients for LB constraint
for i in range(0, n_binvars):
val = self.subMIP_model.getSolVal(self.subMIP_sol_bar, vars[i])
assert self.subMIP_model.isFeasIntegral(val), "Error: Solution passed to LB is not integral!"
if self.subMIP_model.isFeasEQ(val, 1.0):
cons_vals[i] = -1.0
lhs -= 1.0
rhs -= 1.0
else:
cons_vals[i] = 0.0
cons_vars[i] = vars[i]
assert cons_vars[i].vtype() == "BINARY", "Error: local branching constraint uses a non-binary variable!"
# create and add LB constraint to mip_model
self.constraint_LB = self.subMIP_model.createConsBasicLinear(self.subMIP_model.getProbName() + "_localbranching", n_binvars,
cons_vars, cons_vals, lhs, rhs)
self.subMIP_model.addPyCons(self.constraint_LB)
# self.subMIP_model.releasePyCons(self.constraint_LB)
del vars
del cons_vars
del cons_vals
# for j in range(0, n_binvars): # release cons_vars variables after creating a constraint
# self.subMIP_model.releaseVar(cons_vars[j])
def addLBConstraint(mip_model, mip_sol, neighborhoodsize):
"""symmetric local branching constraint over all binary variables"""
vars = mip_model.getVars()
n_binvars = mip_model.getNBinVars()
lhs = 0
rhs = neighborhoodsize
cons_vars = np.empty(n_binvars, dtype=np.object)
cons_vals = np.empty(n_binvars)
# compute coefficients for LB constraint
for i in range(0, n_binvars):
val = mip_model.getSolVal(mip_sol, vars[i])
assert mip_model.isFeasIntegral(val), "Error: Solution passed to LB is not integral!"
if mip_model.isFeasEQ(val, 1.0):
cons_vals[i] = -1.0
lhs -= 1.0
rhs -= 1.0
else:
cons_vals[i] = 1.0
cons_vars[i] = vars[i]
assert cons_vars[i].vtype() == "BINARY", "Error: local branching constraint uses a non-binary variable!"
# create and add LB constraint to mip_model
constraint_LB = mip_model.createConsBasicLinear(mip_model.getProbName()+"_localbranching", n_binvars, cons_vars, cons_vals, lhs, rhs)
mip_model.addPyCons(constraint_LB)
# mip_model.releasePyCons(constraint_LB)
# for j in range(0, n_binvars): # release cons_vars variables after creating a constraint
# mip_model.releaseVar(cons_vars[j])
del vars
del cons_vars
del cons_vals
return mip_model, constraint_LB
def addLBConstraintAsymmetric(mip_model, mip_sol, neighborhoodsize):
"""asymmetric local branching variables over the support of binary variables"""
vars = mip_model.getVars()
n_binvars = mip_model.getNBinVars()
lhs = 0
rhs = neighborhoodsize
cons_vars = np.empty(n_binvars, dtype=np.object)
cons_vals = np.empty(n_binvars)
# compute coefficients for LB constraint
for i in range(0, n_binvars):
val = mip_model.getSolVal(mip_sol, vars[i])
assert mip_model.isFeasIntegral(val), "Error: Solution passed to LB is not integral!"
if mip_model.isFeasEQ(val, 1.0):
cons_vals[i] = -1.0
lhs -= 1.0
rhs -= 1.0
else:
cons_vals[i] = 0.0
cons_vars[i] = vars[i]
assert cons_vars[i].vtype() == "BINARY", "Error: local branching constraint uses a non-binary variable!"
# create and add LB constraint to mip_model
constraint_LB = mip_model.createConsBasicLinear(mip_model.getProbName()+"_localbranching", n_binvars, cons_vars, cons_vals, lhs, rhs)
mip_model.addPyCons(constraint_LB)
# mip_model.releasePyCons(constraint_LB)
# for j in range(0, n_binvars): # release cons_vars variables after creating a constraint
# mip_model.releaseVar(cons_vars[j])
del vars
del cons_vars
del cons_vals
return mip_model, constraint_LB
def addLBConstraintAsymJustslackvars(mip_model, mip_sol, neighborhoodsize, indexlist_slackvars):
"""asymmetric local branching constraints over the support of slack variables"""
vars = mip_model.getVars()
n_slackvars = len(indexlist_slackvars)
lhs = 0
rhs = neighborhoodsize
cons_vars = np.empty(n_slackvars, dtype=np.object)
cons_vals = np.empty(n_slackvars)
# compute coefficients for LB constraint