-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.cpp
More file actions
1382 lines (1250 loc) · 49.1 KB
/
Algorithms.cpp
File metadata and controls
1382 lines (1250 loc) · 49.1 KB
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
#include "Algorithms.hpp"
#include <string>
#include <ilcplex/ilocplex.h>
#include "BigInteger/BigIntegerLibrary.hh"
#include <vector>
ILOMIPINFOCALLBACK2(get_root_node_sol, IloNumVarArray*, var_array, IloNumArray*, sol_array) {
getEnv().out() << "--------------------------RUNNING MSE------------------" << endl;
try {
if (hasIncumbent()){ // if has feasible integer solution
getIncumbentValues(*sol_array, *var_array);
getEnv().out() << "--------------------------MSE SOLUTION------------------" << endl;
getEnv().out() << *sol_array << endl;
getEnv().out() << "--------------------------MSE SOLUTION------------------" << endl;
abort(); //terminate current cplex solver process
}
}
catch (...) {
throw;
}
}
void LexAlg::define_var() {
cplex = IloCplex(env);
model = IloModel(env);
var_array = IloNumVarArray(env);
range_array = IloRangeArray(env, 0);
obj_func = IloObjective(env);
cplex.extract(model);
}
void LexAlg::nonzero_index_mat_generate() {
// create the nonzero_coeff_index_mat
nonzero_coeff_index_mat = IloArray<IloIntArray>(env, var_array.getSize());
// the row index corresponds to variables
// Note that, in constr_matrix, the COLUMN index corresponds to variables
for (IloInt i = 0; i < nonzero_coeff_index_mat.getSize(); ++i) {
nonzero_coeff_index_mat[i] = IloIntArray(env, 0);
}
for (IloInt i = 0; i < constr_matrix.getSize(); ++i) {
for (IloInt var_ind = 0; var_ind < var_array.getSize(); ++var_ind) {
if (constr_matrix[i][var_ind] != 0) {
nonzero_coeff_index_mat[var_ind].add(i);
}
}
}
}
void LexAlg::random_graph_generator(IloInt num_of_nodes, IloInt prob, IloInt seed){
env.out() << "start generating random graph" << endl;
IloRandom rand_generator(env, seed);
IloInt rand_num;
var_array = IloNumVarArray(env, num_of_nodes, 0, 1, IloNumVar::Int);
//model.add(var_array);
for (IloInt i = 0; i < num_of_nodes; i++) {
for (IloInt j = i + 1; j < num_of_nodes; j++) {
rand_num = rand_generator.getInt(100);
if (rand_num < prob) {
range_array.add(var_array[i] + var_array[j] <= 1);
model.add(range_array[range_array.getSize() -1]);
}
}
}
obj_func = IloMaximize(env, IloSum(var_array));
model.add(obj_func);
}
void LexAlg::random_QCP_generator(IloInt _num_quad_cons, IloInt _num_linear_cons, IloInt _num_var, IloInt _rand_seed, IloInt _expected_sparsity) {
quad_constr_arr = IloArray<Quadratic_Constraint>(env, num_quadratic_cons);
IloRandom random_generator(env, _rand_seed);
if (binary_QCP) {
var_array = IloNumVarArray(env, _num_var, 0, 1, IloNumVar::Int);
}
else {
var_array = IloNumVarArray(env, _num_var, 0, 100, IloNumVar::Int);
}
quad_constr_arr = IloArray<Quadratic_Constraint>(env, _num_quad_cons);
constr_matrix = IloArray<IloNumArray>(env, _num_linear_cons);
// generate quadratic constraints
for (IloInt i = 0; i < _num_quad_cons; i++) {
Quadratic_Constraint single_quad_constr(env, _num_var);
for (IloInt j = 0; j < _num_var; j++) {
for (IloInt z = 0; z < _num_var; z++) {
if (random_generator.getInt(100) < _expected_sparsity) {
single_quad_constr.quad_coeff_mat[j][z] = 1; // random_generator.getInt(2); // bug the densiy further reduced by 1/2
}
else {
single_quad_constr.quad_coeff_mat[j][z] = 0;
}
}
single_quad_constr.linear_coeff[j] = random_generator.getInt(2);
}
if (!binary_QCP) {
// make quadratic matrix be diagonally-dominant
for (IloInt j = 0; j < _num_var; j++) {
for (IloInt z = 0; z < _num_var; z++) {
if (j != z)
single_quad_constr.quad_coeff_mat[j][j] += single_quad_constr.quad_coeff_mat[j][z];
}
}
}
single_quad_constr.ub = random_generator.getInt(1000);
quad_constr_arr[i] = single_quad_constr;
// add into the model
// build a single quadratic constraint
IloNumExprArray quad_constr(env, _num_var);
for (IloInt j = 0; j < _num_var; j++) {
quad_constr[j] = IloScalProd(single_quad_constr.quad_coeff_mat[j], var_array);
}
range_array.add(IloScalProd(quad_constr, var_array) + IloScalProd(single_quad_constr.linear_coeff, var_array) <= single_quad_constr.ub);
model.add(range_array[range_array.getSize() - 1]);
}
// generate linear constraints
constr_matrix_bound = IloNumArray(env, _num_linear_cons);
constr_matrix = IloArray<IloNumArray>(env, _num_linear_cons);
for (IloInt i = 0; i < _num_linear_cons; i++) {
IloNumArray single_linear_constr(env, _num_var);
IloNum rand_num;
for (IloInt j = 0; j < _num_var; j++) {
single_linear_constr[j] = random_generator.getInt(1); // bug here, this always give 0
}
constr_matrix[i] = single_linear_constr;
rand_num = random_generator.getInt(100);
constr_matrix_bound[i] = rand_num;
// add into the model
range_array.add(IloScalProd(single_linear_constr, var_array) <= rand_num);
model.add(range_array[range_array.getSize() - 1]);
}
// generate obj coeff array
ori_coeff_array = IloNumArray(env, _num_var);
for (IloInt i = 0; i < _num_var; i++) {
ori_coeff_array[i] = random_generator.getInt(100);
}
obj_func = IloMaximize(env, IloScalProd(ori_coeff_array, var_array));
model.add(obj_func);
}
void LexAlg::readproblem() {
if (generate_QCP) {
random_QCP_generator(num_quadratic_cons, num_linear_cons, num_var, rand_seed, sparsity_QCP);
obj_extra_const = 0;
cplex.extract(model);
new_obj_coeff = ori_coeff_array.copy();
sol_array = IloNumArray(env, var_array.getSize());
callback_sol_arr = IloNumArray(env, var_array.getSize());
sign_of_cols = IloIntArray(env, var_array.getSize());
for (IloInt i = 0; i < var_array.getSize(); i++) {
index_array.push_back(i);
sign_of_cols[i] = 1;
}
nonzero_index_mat_generate();
all_sol_array = IloNumArray(env, var_array.getSize());
return;
}
else {
if (generate_rand_graph) {
random_graph_generator(num_of_nodes, edge_prob, rand_seed);
obj_extra_const = 0;
env.out() << "random graph is generated" << endl;
cplex.extract(model);
}
else {
cplex.importModel(model, name, obj_func, var_array, range_array);
cplex.extract(model);
}
}
/*
***************TEST SECTION***************
*/
sol_array = IloNumArray(env, var_array.getSize());
var_change_ub = IloNumArray(env, var_array.getSize());
var_change_lb = IloNumArray(env, var_array.getSize());
var_change_indi = IloNumArray(env, var_array.getSize());
callback_sol_arr = IloNumArray(env, var_array.getSize());
for (IloInt i = 0; i < var_array.getSize(); i++) {
index_array.push_back(i);
}
//check if any variable has nonzero lower bound
for (IloInt i = 0; i < var_array.getSize(); ++i) {
if (var_array[i].getLB() != 0) {
var_change_indi[i] = 1;
var_change_lb[i] = var_array[i].getLB();
}
}
ori_coeff_array = IloNumArray(env, var_array.getSize());
new_obj_coeff = IloNumArray(env, var_array.getSize());
new_obj_coeff = ori_coeff_array.copy();
for (IloExpr::LinearIterator it = IloExpr(obj_func.getExpr()).getLinearIterator(); it.ok(); ++it) {
for (IloInt i = 0; i < var_array.getSize(); i++) {
if (var_array[i].getId() == it.getVar().getId()) {
ori_coeff_array[i] = it.getCoef();
break;
}
}
}
if (obj_func.getSense() == IloObjective::Maximize) {
//the model is maximization problem
for (IloInt i = 0; i < ori_coeff_array.getSize(); i++) {
if (ori_coeff_array[i] < 0) {
var_change_ub[i] = var_array[i].getUB();
var_change_indi[i] = -1;
var_change_lb[i] = 0; // note that, if we replace x_i by u_i - y_i, then y_i must have zero as its lower bound and hence we no longer need to store the lower bound
new_obj_coeff[i] = -ori_coeff_array[i];
}
else {
var_change_ub[i] = 0;
new_obj_coeff[i] = ori_coeff_array[i];
}
}
}
else {
//the model is minimization problem
for (IloInt i = 0; i < ori_coeff_array.getSize(); i++) {
if (ori_coeff_array[i] > 0) {
var_change_ub[i] = var_array[i].getUB();
var_change_indi[i] = -1;
var_change_lb[i] = 0; // note that, if we replace x_i by u_i - y_i, then y_i must have zero as its lower bound and hence we no longer need to store the lower bound
new_obj_coeff[i] = ori_coeff_array[i];
}
else {
var_change_ub[i] = 0;
new_obj_coeff[i] = -ori_coeff_array[i];
}
}
}
obj_extra_const = IloScalProd(new_obj_coeff, var_change_lb) -IloScalProd(new_obj_coeff, var_change_ub)+obj_func.getConst();
obj_func.setExpr( IloScalProd(new_obj_coeff, var_array) + obj_extra_const);
obj_func.setSense(IloObjective::Maximize);
}
void LexAlg::check_packing() {
/*
This function converts the invoking model to the following form
(P1)
max c1 x1 + ... + cn xn
s.t. A11 x1 + ... + A1n xn <= b1
A12 x1 + ... + A2n xn <= b2
...
Am1 x1 + ... + Amn xn <= bm
0 <= xi <= ui
This function checks if constraint matrix of given model_file is packing or not.
This function also initializes constr_matrix which store coefficients of each constraint.
*/
/***********************************
ALERT: Assume all variables are nonnegative
************************************/
try {
env.out() << "--------------start packing check----------------" << endl;
constr_matrix = IloArray<IloNumArray>(env, range_array.getSize());
constr_matrix_bound = IloNumArray(env, range_array.getSize());
for (IloInt i = 0; i < constr_matrix.getSize(); i++) {
constr_matrix[i] = IloNumArray(env, var_array.getSize());
}
IloBool finite_upper_bound_flag = IloFalse;
IloBool finite_lower_bound_flag = IloFalse;
IloBool packing_flag = IloTrue;
IloRange temp_range;
IloInt j = 0;
IloNum temp_bound;
// change bounds of all variables which have been replaced in readproblem()
for (IloInt k = 0; k < var_array.getSize(); ++k) {
if (var_change_indi[k] != 0) {
// k-th var x_k is replaced by y_k = u_k - x_k
// OR
// k-th var x_k is replaced by y_k = x_k - l_k
IloInt l_bound = var_array[k].getLB();
IloInt u_bound = var_array[k].getUB();
var_array[k].setUB(IloInfinity);
var_array[k].setLB(0);
var_array[k].setUB(u_bound - l_bound);
}
}
IloInt num_of_range = range_array.getSize();
for (IloInt i = 0; i < num_of_range; ++i) {
finite_upper_bound_flag = IloFalse;
finite_lower_bound_flag = IloFalse;
temp_range = range_array[i];
j = 0;
for (IloExpr::LinearIterator it = temp_range.getLinearIterator(); it.ok(); ++it) {
while (it.getVar().getId() != var_array[j].getId()) {
constr_matrix[i][j] = 0;
j = j + 1;
}
constr_matrix[i][j] = it.getCoef(); //Store the coefficients of the current constraint into constr_matrix
j = j + 1;
}
// Update the i-th constraint if we have changed variables in the readproblem()
for (IloInt k = 0; k < var_array.getSize(); ++k) {
if (var_change_indi[k] == -1) {
// k-th var x_k is replaced by y_k = u_k - x_k
// Change coefficients of current constraint
constr_matrix[i][k] = constr_matrix[i][k] * (-1); // a_k is replaced by -a_k
}
}
// The next part are used to update the bound of constraints and break equality into two inequalities
if (temp_range.getUB() < IloInfinity) {
finite_upper_bound_flag = IloTrue;
}
if (temp_range.getLB() > -IloInfinity) {
finite_lower_bound_flag = IloTrue;
}
/*
Exactly one of the following 3 if-blocks will be run in each iteration
1. b1 <= Ax <= b2
2. Ax <= b
3. b <= Ax
*/
if (finite_upper_bound_flag == IloTrue && finite_lower_bound_flag == IloTrue) {
// This case b1 <= Ax <= b2, then we need to split this constraint into two
temp_bound = temp_range.getLB() + IloScalProd(var_change_ub, constr_matrix[i]) - IloScalProd(var_change_lb, constr_matrix[i]); // new bound: b - SUM_{k: x_k is changed by u_k - y_k} a_k * u_k + SUM_{k: x_k is changed by l_k + y_k} a_k * l_k
IloNumArray temp_constr_row(env, var_array.getSize());
for (IloInt _temp_ind = 0; _temp_ind < var_array.getSize(); ++_temp_ind) {
temp_constr_row[_temp_ind] = -constr_matrix[i][_temp_ind];
}
range_array.add(IloScalProd(temp_constr_row, var_array) <= -temp_bound); // b <= Ax ----> - Ax <= -b
model.add(range_array[range_array.getSize() - 1]);
constr_matrix.add(temp_constr_row);
range_array[i].setLB(-IloInfinity);
constr_matrix_bound.add(-temp_bound);
// now change the Ax <= b2 part
temp_bound = temp_range.getUB() + IloScalProd(var_change_ub, constr_matrix[i]) - IloScalProd(var_change_lb, constr_matrix[i]); // new bound: b - SUM_{k: x_k is changed by u_k - y_k} a_k * u_k + SUM_{k: x_k is changed by l_k + y_k} a_k * l_k
range_array[i].setUB(temp_bound);
range_array[i].setExpr(IloScalProd(constr_matrix[i], var_array));
constr_matrix_bound[i] = temp_range.getUB();
}
if (finite_upper_bound_flag == IloTrue && finite_lower_bound_flag == IloFalse) {
// Constriant type: Ax <= b ---> Ax <= b - SUM_{k: x_k is changed by u_k - x_k} a_k * u_k
// This is the upper bound for current constraint after variable replacements
// Note that, constr_matrix[i][k] = - a_k ( it has been changed ) if x_k is replaced
temp_bound = temp_range.getUB() + IloScalProd(var_change_ub, constr_matrix[i]) - IloScalProd(var_change_lb, constr_matrix[i]); // new bound: b - SUM_{k: x_k is changed by u_k - y_k} a_k * u_k + SUM_{k: x_k is changed by l_k + y_k} a_k * l_k
range_array[i].setExpr(IloScalProd(constr_matrix[i], var_array));
range_array[i].setUB(temp_bound);
constr_matrix_bound[i] = temp_range.getUB();
}
if (finite_upper_bound_flag == IloFalse && finite_lower_bound_flag == IloTrue) {
// Constraint type: b <= Ax ----> b - SUM_{k: x_k is changed by u_k - x_k} a_k * u_k <= Ax
// This is the lower bound for current constraint after variable replacements
// Note that, constr_matrix[i][k] = - a_k ( it has been changed ) if x_k is replaced
// Then convert b' <= Ax ----> -Ax <= -b'
temp_bound = temp_range.getLB() + IloScalProd(var_change_ub, constr_matrix[i]) - IloScalProd(var_change_lb, constr_matrix[i]); // new bound: b - SUM_{k: x_k is changed by u_k - y_k} a_k * u_k + SUM_{k: x_k is changed by l_k + y_k} a_k * l_k
for (IloInt _temp_ind = 0; _temp_ind < var_array.getSize(); ++_temp_ind) {
constr_matrix[i][_temp_ind] = -constr_matrix[i][_temp_ind];
}
range_array[i].setExpr(IloScalProd(constr_matrix[i], var_array));
range_array[i].setLB(-IloInfinity);
range_array[i].setUB(-temp_bound);
constr_matrix_bound[i] = temp_range.getUB();
}
// if we have a constraint like b <= Ax <= c, then current model cannot be packing
if (finite_lower_bound_flag && finite_upper_bound_flag) { packing_flag = IloFalse; }
}
// Check if constraint matrix is packing (column by column)
sign_of_cols = IloIntArray(env, var_array.getSize());
IloBool positive_sign_flag = IloFalse;
IloBool negative_sign_flag = IloFalse;
if (packing_flag) {
for (IloInt k = 0; k < var_array.getSize(); ++k) {
positive_sign_flag = IloFalse;
negative_sign_flag = IloFalse;
for (IloInt i = 0; i < constr_matrix.getSize(); ++i) {
if (constr_matrix[i][k] > 0) { positive_sign_flag = IloTrue;
}
if (constr_matrix[i][k] < 0) { negative_sign_flag = IloTrue;
}
if (positive_sign_flag && negative_sign_flag) {
packing_flag = IloFalse;
}
}
if (positive_sign_flag) { sign_of_cols[k] = 1; }
if (negative_sign_flag) { sign_of_cols[k] = -1; }
if (positive_sign_flag == IloTrue && negative_sign_flag == IloTrue) { sign_of_cols[k] = 9; }
}
}
if (packing_flag) {
not_packing = IloFalse;
//string outputfilename = name;
//outputfilename += "_yes.lp";
//cplex.exportModel(outputfilename.c_str());
nonzero_index_mat_generate();
env.out() << "---------Current model satifies packing property------------" << endl;
return;
}
else {
not_packing = IloTrue;
//string outputfilename = name;
//outputfilename += "_not.lp";
//cplex.exportModel(outputfilename.c_str());
env.out() << "---------Current model does NOT satisfy packing property------------" << endl;
}
}
catch (...) {
cplex.exportModel("error_model.lp");
throw;
}
}
void LexAlg::convert_to_packing() {
/*
This function converts the invoking model to the packing type
Suppose the invoking model is the following
(P1)
max c1 x1 + ... + cn xn
s.t. A11 x1 + ... + A1n xn <= b1
A12 x1 + ... + A2n xn <= b2
...
Am1 x1 + ... + Amn xn <= bm
0 <= xi <= ui
Then the packing model is: (we introduce a new variable called s)
(P2)
max c1 x1 + ... + cn xn
s.t. (A11 + h1) x1 + ... + (A1n + hn) xn + s <= b1 + h0
(A12 + h1) x1 + ... + (A2n + hn) xn + s <= b2 + h0
...
(Am1 + h1) x1 + ... + (Amn + hn) xn + s <= bm + h0
h1 x1 + h2 x2 + ... + hn xn + s <= h0 (****)
0 <= xi <= ui
0 <= s
where
hj = max { 0, max_i ( -Aij ) } = max { 0, - (min_i Aij) }, the negative of minimum entry of j-th column if the minimum entry is negative
h0 = max { 0, max_i ( -bi ), OPTh } and OPTh is the optimal value of { max hx: Ax <= b, 0 <= x <= u }
Note that (P1) and (P2) are NOT equivalent. We should project the solution of (P2) on the hyperplane h1 x1 + h2 x2 + ... + hn xn + s = h0 to get a feasible solution to (P1)
*/
if (not_packing == IloFalse) { return; } // if the invoking model is the packing model, then we are done
// If the invoking model is not packing, then start converting
packing_convertor = IloTrue;
h_array = IloNumArray(env, var_array.getSize()); // To construct vector h, we need to find minimum entry of each column
// Note that the initial values of h_array are 0
h_0 = 0; // To construct h0, we need to find the minimum value of bound bi's
// Find the minimum value of each column and bound
for (IloInt ind_col = 0; ind_col < var_array.getSize(); ++ind_col) {
for (IloInt ind_row = 0; ind_row < constr_matrix.getSize(); ++ind_row) {
// If the constraint type is Ax <= b
if (range_array[ind_row].getUB() < IloInfinity && constr_matrix[ind_row][ind_col] < h_array[ind_col]) {
h_array[ind_col] = constr_matrix[ind_row][ind_col];
if (h_0 > range_array[ind_row].getUB()) { h_0 = range_array[ind_row].getUB(); }
}
// If the constraint type is Ax => b, we should consider -Ax <= -b
if (range_array[ind_row].getLB() > -IloInfinity && (-constr_matrix[ind_row][ind_col]) < h_array[ind_col]) {
h_array[ind_col] = -constr_matrix[ind_row][ind_col];
if (h_0 > (-range_array[ind_row].getLB())) { h_0 = -range_array[ind_row].getLB(); }
}
}
}
// h_array[j] = min{ 0, min_i A_{ij} }
// We now construct hj = max { 0, - (min_i Aij) } = - h_array[j]
for (IloInt _ind = 0; _ind < h_array.getSize(); ++_ind) {
h_array[_ind] = -h_array[_ind];
}
// h0 = min_i b_i and we need to let h0 = max { 0, max_i ( -bi ), OPTh } and OPTh is the optimal value of { max hx: Ax <= b, 0 <= x <= u }
h_0 = -h_0;
// Find OPT h
// Change the objective function from cx to hx
obj_func.setExpr(IloScalProd(var_array, h_array));
IloConversion temp_conver = IloConversion(env, var_array, ILOFLOAT);
model.add(temp_conver);
cplex.solve();
IloNum z_h = cplex.getObjValue(); // z_h = OPTh
h_0 = IloMax(h_0, IloCeil(z_h)); // h_0 = max { 0, max_i ( -bi ), OPTh }
model.remove(temp_conver); // Remove relaxation
temp_conver.end(); // Release memory
/******************
Start convert the model to (P2) form
*******************/
packing_var_array = IloNumVarArray(env, 0); // the array of all variables appearing in the packing model
packing_var_array.add(var_array); // packing_var_array = var_array + s
IloNumVar s(env, 0, h_0, ILOINT); // Create a new variable
packing_var_array.add(s); // The last variable of packing_var_array is s
packing_range_array = IloRangeArray(env, 0); // Store the new constraints appering when constructing packing model
IloNum temp_ub;
for (IloInt index_row = 0; index_row < constr_matrix.getSize(); index_row++) {
/*
Change constraints. There are mainly 3 types of constraints.
1. Ax <= b -----> (A.+ h) x + s <= b + h0
2. Ax => b -----> (-A. + h) x + s <= -b + h0
3. b1 <= Ax <= b2 ------> two constraints: (A.+ h) x + s <= b2 + h0 and (-A. + h) x + s <= -b1 + h0
*/
if (range_array[index_row].getUB() < IloInfinity) {
temp_ub = range_array[index_row].getUB();
for (IloInt index_col = 0; index_col < constr_matrix[index_row].getSize(); index_col++) {
constr_matrix[index_row][index_col] += h_array[index_col];
}
constr_matrix_bound[index_row] = temp_ub + h_0;
packing_range_array.add(IloScalProd(constr_matrix[index_row], var_array) + s <= constr_matrix_bound[index_row]);
model.add(packing_range_array[packing_range_array.getSize() - 1]);
//range_array[index_row].setExpr(IloScalProd(constr_matrix[index_row], var_array) + s <= constr_matrix_bound[index_row]);
}
if (range_array[index_row].getLB() > -IloInfinity) {
IloNumArray new_constr_row(env, var_array.getSize());
for (IloInt index_col = 0; index_col < constr_matrix[index_row].getSize(); index_col++) {
if (range_array[index_row].getUB() < IloInfinity) {
new_constr_row[index_col] = -constr_matrix[index_row][index_col] + 2 * h_array[index_col];
}
else {
constr_matrix[index_row][index_col] = -constr_matrix[index_row][index_col] + h_array[index_col];
}
}
if (range_array[index_row].getUB() < IloInfinity) {
constr_matrix.add(new_constr_row);
constr_matrix_bound.add(-range_array[index_row].getLB() + h_0);
packing_range_array.add(IloScalProd(new_constr_row, var_array) + s <= -range_array[index_row].getLB() + h_0);
}
else {
constr_matrix_bound[index_row] = -range_array[index_row].getLB() + h_0;
packing_range_array.add(IloScalProd(constr_matrix[index_row], var_array) + s <= -range_array[index_row].getLB() + h_0);
}
model.add(packing_range_array[packing_range_array.getSize() - 1]);
}
}
packing_range_array.add(IloScalProd(h_array, var_array) + s <= h_0); // Add the constraint hx + s <= h0
model.add(packing_range_array[packing_range_array.getSize() - 1]);
constr_matrix.add(h_array);
constr_matrix_bound.add(h_0);
// Set the objective function back to original
obj_func.setExpr(IloScalProd(new_obj_coeff, var_array) + obj_extra_const);
obj_func.setSense(IloObjective::Maximize);
not_packing = IloFalse; // Label current model as packing type
model.remove(range_array); // Remove the old constriants
for (IloInt k = 0; k < var_array.getSize(); ++k) {
sign_of_cols[k] = 1; // all entries in every column are non-negative
}
}
void LexAlg::packing_matrix() {
/*
If matrix is not packing, then convert it to a matrix of packing type
*/
/***********************************
ALERT: Assume all variables are nonnegative
************************************/
//check_packing();
if (not_packing) {
convert_to_packing();
nonzero_index_mat_generate();
}
}
void LexAlg::get_ori_sol() {
// Get the solution to the original model (the model specified in the input file)
// Before running this function, sol_array must have been assgined values
ori_sol_array = IloNumArray(env, sol_array.getSize());
for (IloInt _ind = 0; _ind < var_array.getSize(); ++_ind) {
if (var_change_indi[_ind] != 0) {
// current variable xi has been replaced by ui - yi
ori_sol_array[_ind] = var_change_ub[_ind] - sol_array[_ind];
}
else {
ori_sol_array[_ind] = sol_array[_ind];
}
}
}
void Bisection::solve() {
IloInt num_of_var = var_array.getSize();
IloNumArray ub_array(env, num_of_var);
IloNumArray lb_array(env, num_of_var);
IloRangeArray equality_array(env, 0);
for (IloInt i = 0; i < var_array.getSize(); i++) {
lb_array[i] = var_array[i].getLB();
}
for (IloInt i = 0; i < ub_array.getSize(); i++) {
ub_array[i] = var_array[i].getUB();
}
IloInt start_index = var_array.getSize() - 1;
IloBool loop_flag = IloTrue;
IloBool func_flag = IloTrue;
IloInt max_index;
IloInt loop_upper;
IloBool if_max_problem = IloTrue;
if (obj_func.getSense() == IloObjective::Maximize) {
if_max_problem = IloTrue;
}
else {
if_max_problem = IloFalse;
}
IloNumArray coef_bisection(env, var_array.getSize());
//coef_bisection[0] = 0;
obj_func.setLinearCoefs(var_array, coef_bisection);
try {
cplex.solve();
}
catch (IloException& ex) {
cerr << "Problem is infeasible" << endl;
return;
}
while (func_flag) {
env.out() << " LOOP START " << endl;
loop_flag = IloTrue;
while (loop_flag) {
//cplex.exportModel("temp1.lp");
for (IloInt i = start_index; i >= 0; i--) {
if (lb_array[index_array[i]] != ub_array[index_array[i]]) {
loop_flag = IloFalse;
max_index = i;
break;
}
}
start_index = max_index;
env.out() << "update index: " << max_index << endl;
if (loop_flag && start_index == 0) {
//two solutions are identical
loop_flag = IloFalse;
func_flag = IloFalse;
}
}
if (func_flag == IloFalse) {
break;
}
loop_upper = IloCeil((lb_array[index_array[max_index]] + ub_array[index_array[max_index]]) / 2);
if (if_max_problem) {
cplex.addCut(var_array[index_array[max_index]] >= loop_upper);
try {
cplex.solve();
cplex.clearCuts();
}
catch (IloException& ex) {
//do nothing
}
if (cplex.getStatus() == IloAlgorithm::Infeasible) {
//var_array[index_array[max_index]].setUB(loop_upper - 1);
equality_array.add(var_array[index_array[max_index]] <= loop_upper - 1);
model.add(equality_array[equality_array.getSize() - 1]);
ub_array[index_array[max_index]] = loop_upper - 1;
}
else {
//var_array[index_array[max_index]].setLB(loop_upper);
equality_array.add(var_array[index_array[max_index]] >= loop_upper);
model.add(equality_array[equality_array.getSize() - 1]);
lb_array[index_array[max_index]] = loop_upper;
}
}
else {
cplex.addCut(var_array[index_array[max_index]] <= loop_upper - 1);
try {
cplex.solve();
cplex.clearCuts();
}
catch (IloException& ex) {
//do nothing
}
if (cplex.getStatus() == IloAlgorithm::Infeasible) {
//var_array[index_array[max_index]].setLB(loop_upper);
equality_array.add(var_array[index_array[max_index]] >= loop_upper);
model.add(equality_array[equality_array.getSize()-1]);
lb_array[index_array[max_index]] = loop_upper;
}
else {
//var_array[index_array[max_index]].setUB(loop_upper - 1);
equality_array.add(var_array[index_array[max_index]] <= loop_upper - 1);
model.add(equality_array[equality_array.getSize() - 1]);
ub_array[index_array[max_index]] = loop_upper - 1;
}
}
}
env.out() << "OKAY" << endl;
cplex.solve();
cplex.getValues(var_array, sol_array);
model.remove(equality_array);
return;
}
void Greedy::solve() {
/*
For a given permutation PER,
Try the greedy method by maximize each variable in the order PER(n), PER(n-1), ..., PER(1)
Each iteration, we solve an IP problem
*/
try {
env.out() << "Start Greedy Solver" << endl;
IloNumArray cofarray(env, var_array.getSize());
IloInt numofvar = var_array.getSize();;
cofarray = IloNumArray(env, numofvar);
for (IloInt i = 0; i < numofvar; i++) {
cofarray[i] = 0;
}
cofarray[index_array[numofvar-1]] = 1;
obj_func.setLinearCoefs(var_array, cofarray);
cplex.solve();
IloNum objval;
objval = cplex.getObjValue();
for (IloInt i = numofvar - 2; i > -1; i--) {
cofarray[index_array[i]] = 1;
cofarray[index_array[i+1]] = 0;
obj_func.setLinearCoefs(var_array, cofarray);
cplex.addCut(var_array[index_array[i+1]] == objval);
cplex.solve();
objval = cplex.getObjValue();
env.out() << "current index: " << i << endl;
}
cplex.solve();
cplex.getValues(var_array, sol_array);
cplex.clearCuts();
}
catch (IloException& ex) {
cerr << "Error: " << ex << endl;
}
catch (...) {
cerr << "Error" << endl;
}
return;
}
void RoundingLP::block_model_solver(IloBool &if_optimal, IloRangeArray &block_constr) {
try {
//IloIntArray index_array(env, var_array.getSize());
while (true) {
// solve LP relaxation
cplex.solve();
if (cplex.getStatus() == IloAlgorithm::InfeasibleOrUnbounded || cplex.getStatus() == IloAlgorithm::Infeasible) {
env.out() << "infeasible" << endl;
if_optimal = IloFalse;
return;
}
else {
cplex.getValues(sol_array, var_array);
IloNumArray block_sol_array(env, size_of_block);
for (IloInt i = 0; i < size_of_block; ++i) {
block_sol_array[i] = sol_array[index_array[i + block_start_ind]];
}
IloBool all_are_int = IloTrue;
IloNum tol = IloPower(10, -5);
for (IloInt i = block_start_ind; i < block_end_ind; i++) {
if (sol_array[index_array[i]] - IloFloor(sol_array[index_array[i]]) > tol && IloCeil(sol_array[index_array[i]]) - sol_array[index_array[i]] > tol) {
all_are_int = IloFalse;
break;
}
}
if (all_are_int == IloTrue) {
env.out() << "block is optimal" << endl;
for (IloInt i = block_end_ind - 1; i >= block_start_ind; i--) {
block_constr.add(var_array[index_array[i]] == IloRound(sol_array[index_array[i]]));
model.add(block_constr[block_constr.getSize() - 1]);
}
if_optimal = IloTrue;
return;
}
else {
model.remove(conv_lexi_array);
conv_lexi_array.clear();
conv_lexi_order_generator(conv_lexi_array, pi, IloScalProd(coeff_array, sol_array));
model.add(conv_lexi_array);
}
}
}
}
catch (IloException& ex) {
cerr << "Error: " << ex << endl;
}
catch (...) {
cerr << "Error" << endl;
}
return;
}
void RoundingLP::RLP_preprocessor() {
mse_range_array_lb = IloRangeArray(env, 0);
mse_range_array_ub = IloRangeArray(env, 0);
mse_var_array = IloNumVarArray(env);
num_of_var = var_array.getSize();
pi = IloNumArray(env, max_size_of_block);
coeff_array = IloNumArray(env, num_of_var);
//block_constr_array = IloArray<IloRangeArray>(env, IloCeil((num_of_var) / 3) + 1);
block_constr_array = IloArray<IloRangeArray>(env, 0);
conv_lexi_array = IloRangeArray(env, 0);
if_min = IloFalse;
if (obj_func.getSense() == IloObjective::Minimize) {
if_min = IloTrue;
}
//initialize block_constr_array
/*
for (IloInt i = 0; i < block_constr_array.getSize(); i++) {
block_constr_array[i] = IloRangeArray(env, 0);
}
*/
//initial coefficent is 0
for (IloInt i = 0; i < num_of_var; i++) {
coeff_array[i] = 0;
}
/*
Let all variables with all-nonpositive-coefficents in constrains to attain their upperbound
*/
if (packing_convertor) {
// If the packing convertor function has been called, then there is an auxiliary variable s in the invoking model
all_sol_array = IloNumArray(env, packing_var_array.getSize());
}
else {
// If the packing convertor function has not been called, then the invoking model contains exactly same variables as the input model
all_sol_array = IloNumArray(env, var_array.getSize());
}
IloInt nonzero_row;
// if packing_convertor is true, then coefficent matrix cannot have any negative entries
if (not_packing == IloFalse) {
for (IloInt var_ind = 0; var_ind < var_array.getSize(); ++var_ind) {
if (sign_of_cols[var_ind] <= 0) {
// For each non-positive column, let the associated variables attain their upper bounds
all_sol_array[var_ind] = var_array[var_ind].getUB();
for (IloInt _ind_2 = 0; _ind_2 < nonzero_coeff_index_mat[var_ind].getSize(); ++_ind_2) {
// Then update the upper bound of each constraint
nonzero_row = nonzero_coeff_index_mat[var_ind][_ind_2];
constr_matrix_bound[nonzero_row] -= all_sol_array[var_ind] * constr_matrix[nonzero_row][var_ind];
}
cplex.addCut(var_array[var_ind] == var_array[var_ind].getUB());
//add extra cut to enforce the variable to be its maximum
}
}
}
env.out() << "Preprossor finished" << endl;
}
void RoundingLP::solve() {
try {
//env.out() << "Current permutation: " << index_array << endl;
// TODO: add the selection about whether use Greedy_solver() or not
if (not_packing == IloFalse) {
packing_greedy_solver();
if (packing_convertor){
if (run_MSE) {
env.out() << "RUN MSE" << endl;
proj_along(new_obj_coeff);
//env.out() << sol_array << endl;
getMSEsolution(MSE_relaxed_flag);
//env.out() << "after MSE" << endl;
//env.out() << sol_array << endl;
}
else {
proj();
}
}
return;
}
IloRangeArray lb_var_range_arr(env, 0);
if (callback_sol_is_int) {
//if the callback solution is an integral solution, then we check if we can increase any variables
for (IloInt i = 0; i < var_array.getSize(); ++i) {
//var_array[i].setLB(callback_sol_arr[i]);
lb_var_range_arr.add(var_array[i] >= callback_sol_arr[i]);
}
}
model.add(lb_var_range_arr);
IloBool block_opt_flag;
block_ind = 0;
block_end_ind = var_array.getSize();
size_of_block = 14;
IloNum _temp = 1;
while (_temp < block_cut_off && block_end_ind > size_of_block && size_of_block < max_size_of_block) {
size_of_block += 1;
_temp *= (var_array[index_array[block_end_ind - size_of_block]].getUB() + 1);
}
block_start_ind = IloMax(block_end_ind - size_of_block, 0);
block_constr_array.add(IloRangeArray(env, 0));
while (block_start_ind >= 0) {
env.out() << "current block start index: " << block_start_ind << endl;
//_temp = 1;
pi[0] = 1;
for (IloInt i = block_start_ind; i < block_end_ind - 1; i++) {
pi[i - block_start_ind + 1] = (var_array[index_array[i]].getUB() + 1)*pi[i - block_start_ind];
}
for (IloInt i = block_start_ind; i < block_end_ind; i++) {
coeff_array[index_array[i]] = pi[i - block_start_ind];
}
obj_func.setLinearCoefs(var_array, coeff_array);
block_opt_flag = IloTrue;
env.out() << "start block solver" << endl;
block_model_solver(block_opt_flag, block_constr_array[block_ind]);
env.out() << "block solver finished" << endl;
if (block_opt_flag == IloFalse) {
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
WARNING : THIS BLOCK METHOD IS NOT USED NOW
TO MAKE IT WORK, ONE NEED TO ADD THE FEATURE TO DETERMINE size_of_block WHEN GO TO PREVIOUS BLOCK
*/
env.out() << "cannot optimize block" << endl;
/*
Backwards tracking method:
Delete all cuts which have been added for this block.
Then go to previous block.
Cut previous optimal solution and resolve previous block.
*/
model.remove(block_constr_array[block_ind]);
block_constr_array[block_ind].clear();
if (block_ind == 0) {
env.out() << "problem is infeasible" << endl;
return;
}
//Go back to previous block
env.out() << "go to previous block" << endl;
block_ind = block_ind - 1;
for (IloInt i = block_start_ind; i < block_end_ind; i++) {
coeff_array[index_array[i]] = 0;
}
block_start_ind = block_end_ind;
block_end_ind = block_start_ind + size_of_block;
for (IloInt i = block_start_ind; i < block_end_ind; i++) {
coeff_array[index_array[i]] = pi[i - block_start_ind];
}
model.remove(block_constr_array[block_ind]);
block_constr_array[block_ind].clear();
/*
Backwards track new constraints
*/
model.remove(conv_lexi_array);
conv_lexi_array.clear();
conv_lexi_order_generator(conv_lexi_array, pi, IloScalProd(coeff_array, sol_array) - 1);
model.add(conv_lexi_array);
env.out() << "new model created" << endl;
}
else {
for (IloInt i = block_start_ind; i < block_end_ind; i++) {
coeff_array[index_array[i]] = 0;
}
block_end_ind = block_start_ind;
if (block_end_ind == 0) {
block_start_ind = -1;
}
else {
//Determine the size of next block
size_of_block = 0;
_temp = 1;
while (_temp < block_cut_off && block_end_ind > size_of_block && size_of_block < max_size_of_block) {
size_of_block += 1;
_temp *= (var_array[index_array[block_end_ind - size_of_block]].getUB() + 1);
}
block_start_ind = IloMax(block_end_ind - size_of_block, 0);
block_constr_array.add(IloRangeArray(env, 0));
block_ind += 1; //Go to next block
}
}
}
cplex.solve();
cplex.getValues(var_array, sol_array);