forked from jfrchicanog/boxMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_boxes.cpp
1885 lines (1622 loc) · 53 KB
/
main_boxes.cpp
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
//Algorithm which calculates the Pareto front of an MOCO (multiobjective combinatorial optimization) problem
//
//Author: Miguel Angel Dominguez Rios
//Date of last modification : 09/12/2019
#include <list>
#include <vector>
#include <ilcplex/cplex.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <set>
#include "boxes2.h"
//GLOBAL VARIABLES
int box_id = 0;
int boxes_destroyed = 0;
int p = 0; //Dimension of the objective space
struct Container {
Box *VBox;
int position;
};
class Compare_value {
public:
bool operator()(Box *x, Box *y) const {
return x->value > y->value;
}
};
void get_name_string(string *name) {
//Take the name of an archive, ommiting the path
//Example: Input ./KP/data/hello.lp Output hello.lp
std::size_t pos;
pos = name->find("/");
while (pos != -1) {
*name = name->substr(pos + 1);
pos = name->find("/");
}
}
void error_input() {
printf("\nTo execute BOXES write ./BOXES (arg1) (arg2) (arg3) (arg4) (arg5) (arg6) (arg7) (arg8) (arg9) (arg10) (arg11)");
printf("\n(arg1) is the objective costs file");
printf("\n(arg2) is the .lp file");
printf("\n(arg3) is the maximum total execution time in seconds (0 for unlimited time)");
printf("\n(arg4) is the maximum size of the Pareto front (0 for unlimited size)");
printf("\n(arg5) is the type of partition. Type 1(full), 2(p-partition)");
printf("\n(arg6) is the parameterization model. Type 1(chalmet), 2(tchebycheff) or 3(benson)");
printf("\n(arg7) is the box value. Type 11(volume), 12(scaled), 13(reduced), 14(reduced_scaled)");
printf("\n(arg8) is the number of set of boxes used. Type an integer positive number, or (inf) or (alternate).");
printf("\n(arg9) is the filtering proccess. Type 1(RE).");
printf("\n(arg10) is the value of CPX_PARAM_PARALLEL. Type -1, 0 or 1");
printf("\n(arg11) is the value of CPX_PARAM_THREADS. Type 0 or a(positive integer)\n");
exit(EXIT_FAILURE);
}
void error_open_file(const char *name) {
printf("\nImpossible to open file %s\n", name);
exit(EXIT_FAILURE);
}
void Input_control(int argc, char **argv, Input *input) {
//Controlling the input parameters. In case of error, exit.
FILE *fp;
if (argc != 12) { error_input(); }
//Data archive obj_file
const char *arch1 = argv[1];
fp = fopen(arch1, "r");
if (fp == NULL)
error_open_file(arch1);
else {
fclose(fp);
string a1 = argv[1];
input->path_obj_file = argv[1];
get_name_string(&a1);
input->obj_file = a1;
}
//Data archive model_file
const char *arch2 = argv[2];
fp = fopen(arch2, "r");
if (fp == NULL)
error_open_file(arch2);
else {
fclose(fp);
string a2 = argv[2];
input->path_lp_model = argv[2];
get_name_string(&a2);
input->lp_model = a2;
}
//Maximum execution time
std::string time = argv[3];
double tmax = atof(argv[3]);
if ((time != "0") && (time != "0.0")) {
if (tmax <= 0.0)
error_input();
}
input->tmax = tmax;
//Maximum size of Pareto front
std::string pointlimit = argv[4];
int maxsize = atoi(argv[4]);
if (pointlimit != "0") {
if (maxsize <= 0)
error_input();
}
input->pointlimit = maxsize;
//Partition
std::string type = argv[5];
if ((type == "1") || (type == "full")) input->partition = "full";
else if ((type == "2") || (type == "p-partition")) input->partition = "p-partition";
else error_input();
//Problem model
std::string model = argv[6];
if ((model == "1") || (model == "chalmet")) input->parameterization_model = "chalmet";
else if ((model == "2") || (model == "tchebycheff")) input->parameterization_model = "tchebycheff";
else if ((model == "3") || (model == "benson")) input->parameterization_model = "benson";
else error_input();
//Box value
std::string box = argv[7];
if ((box == "11") || (box == "volume")) input->box_value = "volume";
else if ((box == "12") || (box == "scaled")) input->box_value = "scaled";
else if ((box == "13") || (box == "reduced")) input->box_value = "reduced";
else if ((box == "14") || (box == "reduced_scaled")) input->box_value = "reduced_scaled";
else error_input();
//Set of boxes
std::string set_b = argv[8];
if (set_b == "inf") input->set_of_boxes = "inf";
else if (set_b == "alternate") input->set_of_boxes = "alternate";
else {
input->set_of_boxes = argv[8];
int set_b = atoi(argv[8]);
if (set_b < 1) error_input();
}
//Filtering process
std::string filtering = argv[9];
if ((filtering == "1") || (filtering == "RE")) input->filtering = "RE";
else { error_input(); }
//CPX_PARAM_PARALLEL
std::string sparallel = argv[10];
if ((sparallel != "-1") && (sparallel != "0") && (sparallel != "1")) error_input();
input->cpx_param_parallel = atoi(argv[10]);
//CPX_PARAM_THREADS
std::string sthreads = argv[11];
if (sthreads != "0") {
int x = atoi(argv[11]);
if ((x <= 0) || (x > 32)) error_input();
}
input->cpx_param_threads = atoi(argv[11]);
//Incompatibilities
if ((input->partition == "p-partition") && (input->filtering == "RA")) error_input();
}
void error_CPLEX_status(int code) {
printf("\nCPLEX error status = %d", code);
exit(EXIT_FAILURE);
}
void Create_CPLEX_object_and_set_CPLEX_parameters(MOILP *P, string name, int parallel, int threads) {
CPXENVptr *env = new(CPXENVptr); //Create CPLEX environment
CPXLPptr *lp = new(CPXLPptr); //Create associated problem to CPLEX environment
int status = 0;
const char *namec = name.c_str();
*env = CPXopenCPLEX(&status);
*lp = CPXcreateprob(*env, &status, namec);
status = CPXsetintparam(*env, CPX_PARAM_PARALLELMODE, parallel); //-1 CPX_PARALLEL_OPPORTUNISTIC; 0 (default) CPX_PARALLEL_AUTO 1 CPX_PARALLEL_DETERMINISTIC
status = CPXsetintparam(*env, CPX_PARAM_THREADS, threads); //0 (default, CPLEX decides) ; 1 (sequential, single Thread) ; N (uses up to N threads)
status = CPXsetdblparam(*env, CPX_PARAM_EPGAP, 0.0); //Default 1e-04. Value between 0 and 1. Sets a relative tolerance on the gap between the best integer objective and the objective of the best node remaining
status = CPXsetdblparam(*env, CPX_PARAM_EPAGAP, 0.0); //Default 1e-06
status = CPXsetdblparam(*env, CPX_PARAM_EPINT, 0.0); //Default 1e-05. CPLEX tolerance for integer variables
P->env = env;
P->lp = lp;
}
int compare_vectors(vector<double> *v1, vector<double> *v2, int p) {
//Compare two p-dimensional vectors v1 and v2
//Return 1 if v1 <= v2
//Return 0 otherwise
for (int index = 0; index < p; index++) {
if (v1->at(index) > v2->at(index)) {
return 0;
}
}
return 1;
}
void Set_MOILP_structure(MOILP *P, Input *input) {
//Read .lp CPLEX model and objectives archive. If the problem is MAX, convert it into MIN
FILE *fp;
int i, j, n, m, status, original_sense, pointlimit;
double max_time, bound;
string name;
list<solution> *PF = new(list<solution>);
//Read CPLEX problem
status = CPXreadcopyprob(*P->env, *P->lp, input->path_lp_model, NULL);
if (status != 0) error_CPLEX_status(status);
fp = fopen(input->path_obj_file, "r");
if (fp == NULL) error_open_file(input->path_obj_file);
fscanf(fp, "%d", &p); //Read number of objectives
fscanf(fp, "%d", &n); //Read number of variables
fscanf(fp, "%d", &m); //Read number of constraints
//Creating the objective costs matrix
double **F = (double **)malloc(p * sizeof(double *));
for (i = 0; i < p; i++) {
F[i] = (double *)malloc(n * sizeof(double));
for (j = 0; j < n; j++) {
fscanf(fp, "%lf", &F[i][j]); //Read costs for every objective
}
}
fclose(fp);
//MAX problems are transformed into MIN problems
original_sense = CPXgetobjsen(*P->env, *P->lp);
if (original_sense == CPX_MAX) {
for (i = 0; i < p; i++) {
for (j = 0; j < n; j++) {
F[i][j] = -F[i][j];
}
}
CPXchgobjsen(*P->env, *P->lp, CPX_MIN);
}
//Max time and point limit
max_time = input->tmax;
if (input->tmax == 0) max_time = MAX_DOUBLE;
pointlimit = input->pointlimit;
if (input->pointlimit == 0) pointlimit = MAX_INTEGER;
//Bound for Nadir and Ideal point
bound = (original_sense == CPX_MIN) ? -CPX_INFBOUND : CPX_INFBOUND;
P->Ideal.resize(p);
P->BoundforNadir.resize(p);
for (i = 0; i < p; i++) {
P->Ideal.at(i) = bound;
P->BoundforNadir.at(i) = -bound;
}
name = input->lp_model;
name.resize(name.length() - 3); //Eliminate ".lp" in the name
//Initialize MOILP structure
//P->env, P->lp, P->Ideal, P->BoundforNadir were previously assigned
P->PF = PF;
P->F = F;
P->dimension = p;
P->n_var = n;
P->n_const = m;
P->n_iterations = 0;
P->hypervolume = 0.0;
P->spread = -1.0;
P->max_time = max_time;
P->pointlimit = pointlimit;
P->original_problem_type = original_sense; // CPXgetobjsen(*P->env, *P->lp);
P->name = name;
}
void Solve_z(MOILP *P, int *stat, double *z) {
//Solve a MIP problem, returning the stat and objective value
CPXmipopt(*P->env, *P->lp);
CPXgetobjval(*P->env, *P->lp, z);
*stat = CPXgetstat(*P->env, *P->lp);
}
bool Calculate_problem_bounds(MOILP *P, int *indices, double *scaling) {
//Calculate Ideal point and a bound for Nadir point of the problem. We always consider MIN problems.
//In vector scaling we save the range (bound_of_nadir[i] - ideal[i]) for every component
TIEMPO t;
t.init();
int i, stat;
double obj, t_ref;
double tmax = P->max_time;
int cur_objsense = CPXgetobjsen(*P->env, *P->lp); //CPX_MIN 1 //CPX_MAX -1
printf("\nCalculating bounds...");
//Ideal point
for (i = 0; i < p; i++) {
t.acum(); t_ref = tmax - t.value();
if (t_ref < 0) t_ref = 0.0;
CPXsetdblparam(*P->env, CPX_PARAM_TILIM, t_ref);
CPXchgobj(*P->env, *P->lp, P->n_var, indices, P->F[i]);
Solve_z(P, &stat, &obj);
P->Ideal.at(i) = obj;
if (stat != CPXMIP_OPTIMAL) {
printf("\nNON OPTIMAL SOLUTION IS FOUND");
P->Count.total_time = P->max_time;
return false;
}
}
//Upper bound for Nadir point
CPXchgobjsen(*P->env, *P->lp, -cur_objsense);
for (i = 0; i < p; i++) {
t.acum(); t_ref = tmax - t.value();
if (t_ref < 0) t_ref = 0.0;
CPXsetdblparam(*P->env, CPX_PARAM_TILIM, t_ref);
CPXchgobj(*P->env, *P->lp, P->n_var, indices, P->F[i]);
Solve_z(P, &stat, &obj);
P->BoundforNadir.at(i) = obj + 1;
if (stat != CPXMIP_OPTIMAL) {
printf("\nNON OPTIMAL SOLUTION IS FOUND");
P->Count.total_time = P->max_time;
return false;
}
}
//Restore
CPXchgobjsen(*P->env, *P->lp, cur_objsense);
//Calculating ranges for every component
for (i = 0; i < P->dimension; i++) {
scaling[i] = P->BoundforNadir.at(i) - P->Ideal.at(i);
}
t.acum();
printf("DONE after %lf seconds", t.value());
return true;
}
Box *Create_box(point *LB, point *UB) {
//Given the opposite extreme points for the box, we create the corresponding structure (except its value)
Box *B = new(Box);
//Create a pointer to a point with dimension dim
point *lb = new(point); lb->resize(p);
point *ub = new(point); ub->resize(p);
for (int i = 0; i < p; i++) {
lb->at(i) = LB->at(i);
ub->at(i) = UB->at(i);
}
//Creating the Box
B->box_id = box_id++;
B->lb = lb;
B->ub = ub;
return B;
}
void Destroy_box(Box *B) {
//Destroy box and free allocated memory
boxes_destroyed++;
free(B->lb);
free(B->ub);
free(B);
}
double get_volume(point *lb, point *ub) {
//Get the volume of the box
double volume = 1.0;
for (int i = 0; i < p; i++) {
volume = volume * (ub->at(i) - lb->at(i));
}
return volume;
}
double get_scaled_volume(point *lb, point *ub, double *scaling) {
//Get scaled volume of the box
double volume = 1.0;
for (int i = 0; i < p; i++) {
volume = volume * ((ub->at(i) - lb->at(i)) / scaling[i]);
}
return volume;
}
void get_value(Box *B, string value, double *scaling) {
//Get the value of the box
if ((value == "volume") || (value == "reduced")) B->value = get_volume(B->lb, B->ub);
else if ((value == "scaled") || (value == "reduced_scaled")) B->value = get_scaled_volume(B->lb, B->ub, scaling);
}
Box *CREATE_BOX_AND_VALUE(point *LB, point *UB, point *z, Input *input, double *scaling, int coordinate) {
string X = input->box_value;
Box *B = Create_box(LB, UB);
if (B != NULL) {
get_value(B, X, scaling);
if (input->partition == "full") {
if (X == "reduced") B->value -= get_volume(LB, z);
else if (X == "reduced_scaled") B->value -= get_scaled_volume(LB, z, scaling);
}
}
return B;
}
void CREATE_INITIAL_BOX(Box **B0, point *LB, point *UB, Input *input, double *scaling) {
//The initial box is never empty, because it is suppossed that there is at least one non-dominated point
//Create the box
*B0 = Create_box(LB, UB);
//Assign the value of the box
get_value(*B0, input->box_value, scaling); //Assign the value of the box
//Initial box is considered as the first sibling. In fact, it has no parent, but we need this
(*B0)->pos_sibling = 0;
}
void INSERT_INTO_LIST_2(MOILP *P, vector<multiset<Box *, Compare_value>> *L, Box *B, int position) {
//We insert in L[position] the new box. If that element of the vector does not exist, we create it
//Every element of vector L is a tree, ordered by its value
TIEMPO t;
t.init();
if (position == int(L->size())) { //Create new tree
multiset<Box *, Compare_value> new_tree;
L->push_back(new_tree);
}
L->at(position).insert(B);
t.acum();
P->Count.TAddList += t.value();
}
void INSERT_INTO_LIST_1(MOILP *P, vector<multiset<Box *, Compare_value>> *L, Box *B, int *RemainingBoxesToExplore) {
//We insert the new box in the corresponding cell of L, according to its sibling's position
TIEMPO t;
t.init();
L->at(B->pos_sibling).insert(B);
(*RemainingBoxesToExplore)++;
t.acum();
P->Count.TAddList += t.value();
}
void Create_chalmet_model(MOILP *P, int *indice_col) {
//Given the original model, create Chalmet et al model
// MIN (f1 + .... + fp)
// s.t. x in X
// fi(x) <= ki for i = 1,...,p
//
int i, j, matbeg = 0;
double *new_c = (double *)malloc(P->n_var * sizeof(double));
//The sense of the new constraints are <= because we have a MIN problem
const char *sense = "L";
for (i = 0; i < P->n_var; i++) {
new_c[i] = 0;
for (j = 0; j < P->dimension; j++) {
new_c[i] += P->F[j][i];
}
}
CPXchgobj(*P->env, *P->lp, P->n_var, indice_col, new_c); //min sum(fi)
for (i = 0; i < P->dimension; i++) {
CPXaddrows(*P->env, *P->lp, 0, 1, P->n_var, 0, sense, &matbeg, indice_col, P->F[i], NULL, NULL); //new constraint
}
free(new_c);
}
void Create_tchebycheff_model(MOILP *P, int *indice_col) {
//Given the original model, create the Tchebycheff model
// MIN ((f1 + .... + fp) - alfa)
// s.t. x in X
// fi(x) <= ki - alfa for i = 1,...,p
// alfa >= 0
//
int i, j, matbeg = 0;
double *new_c = (double *)malloc((P->n_var + 1) * sizeof(double));
//The sense of the new constraints are <=, since we have a MIN problem
const char *sense = "L";
for (i = 0; i < P->n_var; i++) {
new_c[i] = 0;
for (j = 0; j < p; j++) {
new_c[i] += P->F[j][i];
}
}
int index_alfa = P->n_var;
double coef_alfa = -1.0;
CPXaddcols(*P->env, *P->lp, 1, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
CPXchgobj(*P->env, *P->lp, P->n_var, indice_col, new_c); //min (eps*sum(fi))
CPXchgobj(*P->env, *P->lp, 1, &index_alfa, &coef_alfa); //min (eps*sum(fi) - alfa)
int numrows = CPXgetnumrows(*P->env, *P->lp);
for (i = 0; i < p; i++) {
string rowname = "c" + to_string(P->n_const + i + 1);
char *rownamechar = new char[rowname.length() + 1];
strcpy(rownamechar, rowname.c_str());
CPXaddrows(*P->env, *P->lp, 0, 1, P->n_var, 0, sense, &matbeg, indice_col, P->F[i], NULL, &rownamechar); //new constraint
numrows++;
CPXchgcoef(*P->env, *P->lp, numrows - 1, P->n_var, 1.0); //fi(x)+alfa <= 0
free(rownamechar);
}
free(new_c);
}
void Create_benson_model(MOILP *P) {
//Given the original MIN model, create the Benson model
// MAX (l1 + l2 + ... + lp)
// s.t. x in X
// fi(x) + li = ui for i = 1,...,p
// li >= 0 for i = 1,...,p
//
int i, status, matbeg = 0;
int *indice_col = (int *)malloc((P->n_var + p) * sizeof(int));
double *new_c = (double *)malloc((P->n_var + p) * sizeof(double));
for (i = 0; i < P->n_var; i++) {
indice_col[i] = i;
new_c[i] = 0.0;
}
for (i = P->n_var; i < P->n_var + p; i++) {
indice_col[i] = i;
new_c[i] = 1.0;
}
//The sense of the new constraints are "="
const char *sense = "E";
status = CPXaddcols(*P->env, *P->lp, p, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); //Add new variables li
status = CPXchgobjsen(*P->env, *P->lp, CPX_MAX);
status = CPXchgobj(*P->env, *P->lp, P->n_var + p, indice_col, new_c); //MAX (l1 + l2 +... +lp)
int numrows = CPXgetnumrows(*P->env, *P->lp);
for (i = 0; i < p; i++) {
status = CPXaddrows(*P->env, *P->lp, 0, 1, P->n_var, 0, sense, &matbeg, indice_col, P->F[i], NULL, NULL); //new constraint
numrows++;
status = CPXchgcoef(*P->env, *P->lp, numrows - 1, P->n_var + i, 1.0); //fi(x)+ li = 0
}
free(new_c);
}
void CREATE_MODEL(MOILP *P, int *indices, Input *input) {
if (input->parameterization_model == "chalmet") {
Create_chalmet_model(P, indices);
}
else if (input->parameterization_model == "tchebycheff") {
Create_tchebycheff_model(P, indices);
}
else if (input->parameterization_model == "benson") {
Create_benson_model(P);
}
}
Box *SELECT_NEXT_BOX_2(MOILP *P, vector<multiset<Box *, Compare_value>> *L) {
//The next box to select is in the top of L[0] (maximum value in the current list of boxes)
TIEMPO tt;
tt.init();
multiset<Box *>::iterator it = L->at(0).begin();
tt.acum(); P->Count.TSearching_next_box += tt.value();
return *it;
}
Box *SELECT_NEXT_BOX_1(MOILP *P, vector<multiset<Box *, Compare_value>> *L, int *counter) {
//The next box to select varies in every component of L. We use a counter
TIEMPO tt;
tt.init();
int index;
index = *counter % p;
while (L->at(index).size() == 0) { //In case of L(index) been empty
(*counter)++;
index = *counter % p;
}
multiset<Box *>::iterator it = L->at(index).begin();
(*counter)++;
tt.acum(); P->Count.TSearching_next_box += tt.value();
return *it;
}
bool check_time_and_pointlimit(TIEMPO *t, double maxtime, size_t size_PF, int pointlimit) {
//Return false if time limit or the desired number of solutions are reached. Return true, otherwise
t->acum();
return ((t->value() < maxtime) && (size_PF < pointlimit));
}
void set_time_for_solver(MOILP *P, TIEMPO *t) {
//Set the maximum time available for CPLEX
t->acum();
double tt = P->max_time - t->value();
if (tt < 0) tt = 0;
CPXsetdblparam(*P->env, CPX_PARAM_TILIM, tt);
}
void Solve(MOILP *P, int *stat, double *x, double *obj) {
//Solve a MIP problem, returning the stat, solution, objective value and execution time
TIEMPO t;
t.init();
CPXmipopt(*P->env, *P->lp);
CPXgetobjval(*P->env, *P->lp, obj);
*stat = CPXgetx(*P->env, *P->lp, x, 0, P->n_var - 1);
*stat = CPXgetstat(*P->env, *P->lp);
P->n_iterations++;
t.acum();
P->Count.TSolver += t.value();
}
void Set_constraint_extreme_box(MOILP *P, point *LB, point *UB) {
//Set the rhs values for the new constraints (fi(x) <= ki) in the current iteration
//ki = Ni - 1, being Ni the bound of the local nadir point (MIN problems)
point *N = UB;
int *indices = (int *)malloc(p * sizeof(int));
double *values = (double *)malloc(p * sizeof(double));
for (int i = 0; i < p; i++) {
indices[i] = P->n_const + i;
values[i] = N->at(i) - 0.5; //Because of rounding errors when using CPXPARAMEPINT = 0 , we substract a small value 0<alfa<1
}
CPXchgrhs(*P->env, *P->lp, p, indices, values); //s.t. fi <= ki, i = 1,...,p
free(indices); free(values);
}
void Calculate_Image_of_x(double **F, double *x, int n, point *z, int p) {
//Given a vector-solution x, return the point z = (f1(x), ... , fp(x))
int i, j;
for (i = 0; i < p; i++) {
z->at(i) = 0.0;
for (j = 0; j < n; j++) {
z->at(i) += F[i][j] * x[j];
}
}
}
void Add_new_PF_point(double *x, int dim_x, point *z, std::list<solution> *PF, double time) {
//Insert a new efficient solution, its Pareto Front point (x,z), and the time when the solution was obtained
point *xx = new(point); xx->resize(dim_x);
point *zz = new(point); zz->resize(p);
solution *A = new(solution);
for (int i = 0; i < dim_x; i++) xx->at(i) = x[i];
for (int i = 0; i < z->size(); i++) zz->at(i) = z->at(i);
A->x = xx;
A->z = zz;
A->time_point = time;
PF->push_back(*A);
}
void Find_upper_bounds_that_contains_z_2(MOILP *P, vector<list<Container>> *Bj, list<Container> *R, vector<multiset<Box *, Compare_value>> *L, point *z) {
//In case of option "alternate RE", R will be a subset of L, which contains all boxes with upper bound dominating the point z.
//Bj , j=1,..,p are the set of points that have 1 component equals z (weakly dominates)
TIEMPO t;
t.init();
int i, j;
multiset<Box *>::iterator k;
int coincidence_axis;
int number_of_coincidences;
bool dominates;
for (i = 0; i < L->size(); i++) { //For every tree in vector L
k = L->at(i).begin();
while (k != L->at(i).end()) {
coincidence_axis = -1;
number_of_coincidences = 0;
dominates = true;
for (j = 0; j < p; j++) {
if (z->at(j) >(*k)->ub->at(j)) {
dominates = false;
j = p;
}
else if (z->at(j) == (*k)->ub->at(j)) {
coincidence_axis = j;
number_of_coincidences++;
}
}
if (dominates) {
if (coincidence_axis == -1) { //Strictly dominates
Container c;
c.VBox = *k; c.position = i;
R->push_back(c);
k = L->at(i).erase(k);
}
else if (number_of_coincidences == 1) {
Container c;
c.VBox = *k; c.position = i;
Bj->at(coincidence_axis).push_back(c);
k = L->at(i).erase(k);
}
else {
++k;
}
}
else {
++k;
}
}
}
t.acum();
P->Count.TSearching_reachable += t.value();
}
void Find_upper_bounds_that_strictly_contains_z_2(MOILP *P, list<Container> *R, vector<multiset<Box *, Compare_value>> *L, point *z) {
//In case of option "alternate RA", R will be a subset of L, which contains all boxes with upper bound dominating the point z.
TIEMPO t;
t.init();
int i, j;
multiset<Box *>::iterator k;
int coincidence_axis;
bool dominates;
for (i = 0; i < L->size(); i++) { //For every tree in vector L
k = L->at(i).begin();
while (k != L->at(i).end()) {
coincidence_axis = -1;
dominates = true;
for (j = 0; j < p; j++) {
if (z->at(j) >(*k)->ub->at(j)) {
dominates = false;
j = p;
}
else if (z->at(j) == (*k)->ub->at(j)) {
coincidence_axis = j;
}
}
if (dominates) {
if (coincidence_axis == -1) { //Strictly dominates
Container c;
c.VBox = *k; c.position = i;
R->push_back(c);
k = L->at(i).erase(k);
}
else {
++k;
}
}
else {
++k;
}
}
}
t.acum();
P->Count.TSearching_reachable += t.value();
}
void Find_upper_bounds_that_contains_z_1(MOILP *P, vector<list<Box *>> *Bj, list<Box *> *R, vector<multiset<Box *, Compare_value>> *L, point *z, int *RemainingBoxesToExplore) {
//In case of option of several lists and "RE", R will be a subset of L, which contains all boxes with upper bound dominating the point z.
//Bj , j=1,..,p are the set of points that have 1 component equals z (weakly dominates)
TIEMPO t;
t.init();
int i, j;
multiset<Box *>::iterator k;
int coincidence_axis;
int number_of_coincidences;
bool dominates;
for (i = 0; i < L->size(); i++) { //For every tree in vector L
k = L->at(i).begin();
while (k != L->at(i).end()) {
coincidence_axis = -1;
number_of_coincidences = 0;
dominates = true;
for (j = 0; j < p; j++) {
if (z->at(j) >(*k)->ub->at(j)) {
dominates = false;
j = p;
}
else if (z->at(j) == (*k)->ub->at(j)) {
coincidence_axis = j;
number_of_coincidences++;
}
}
if (dominates) {
if (coincidence_axis == -1) { //Strictly dominates
R->push_back(*k);
k = L->at(i).erase(k);
(*RemainingBoxesToExplore)--;
}
else if (number_of_coincidences == 1) {
Bj->at(coincidence_axis).push_back(*k);
k = L->at(i).erase(k);
(*RemainingBoxesToExplore)--;
}
else {
++k;
}
}
else {
++k;
}
}
}
t.acum();
P->Count.TSearching_reachable += t.value();
}
void Find_upper_bounds_that_strictly_contains_z_1(MOILP *P, list<Box *> *R, vector<multiset<Box *, Compare_value>> *L, point *z, int *RemainingBoxesToExplore) {
//In case of option of several lists and "RA", R will be a subset of L, which contains all boxes with upper bound dominating the point z.
TIEMPO t;
t.init();
int i, j;
multiset<Box *>::iterator k;
int coincidence_axis;
bool dominates;
for (i = 0; i < L->size(); i++) { //For every tree in vector L
k = L->at(i).begin();
while (k != L->at(i).end()) {
coincidence_axis = -1;
dominates = true;
for (j = 0; j < p; j++) {
if (z->at(j) >(*k)->ub->at(j)) {
dominates = false;
j = p;
}
else if (z->at(j) == (*k)->ub->at(j)) {
coincidence_axis = j;
}
}
if (dominates) {
if (coincidence_axis == -1) { //Strictly dominates
R->push_back(*k);
k = L->at(i).erase(k);
(*RemainingBoxesToExplore)--;
}
else {
++k;
}
}
else {
++k;
}
}
}
t.acum();
P->Count.TSearching_reachable += t.value();
}
bool Calculate_point_t(point *t, Box *B, point *zz) {
//When the point zz is outside box B, we project all the coordenates outside the box to a point to the box-frontier. Return that "new" point, t
//t is the projection of zz to the box B
bool is_inside = true;
int i, p = int(zz->size());
for (i = 0; i < p; i++) {
if (zz->at(i) < B->lb->at(i)) {
t->at(i) = B->lb->at(i);
is_inside = false;
}
else {
t->at(i) = zz->at(i);
}
}
return (is_inside);
}
void create_u_j_bounds_p_partition(point **ib, point **ub, Box *B, point *z, point *tz, int coordinate) {
//Creating the new bounds for the coordinate-sibling using p-partition
for (int i = 0; i < p; i++) {
if (i < coordinate) {
(*ib)->at(i) = B->lb->at(i); (*ub)->at(i) = B->ub->at(i);
}
else if (i == coordinate) {
(*ib)->at(i) = B->lb->at(i); (*ub)->at(i) = tz->at(i);
}
else {
(*ib)->at(i) = tz->at(i); (*ub)->at(i) = B->ub->at(i);
}
}
if (z->at(coordinate) < (*ib)->at(coordinate)) {
*ub = NULL;
return;
}
}
void create_u_j_bounds_full(MOILP *P, point **ib, point **ub, Box *B, point *z, int coordinate) {
//Creating the new bounds for the coordinate-sibling using full partition
for (int i = 0; i < p; i++) {
(*ib)->at(i) = P->Ideal.at(i);
if (i == coordinate) {
(*ub)->at(i) = z->at(i);
}
else {
(*ub)->at(i) = B->ub->at(i);
}
if ((*ib)->at(i) >= (*ub)->at(i)) {
*ub = NULL;
return;
}
}
}
Box *PARTITION_BOX_full(MOILP *P, Box *it, point *z, int coordinate, int p, Input *input, double *scaling) {
//Create new box partitioning (it) respect to z, according the coordinate "coordinate"
//We use full method
TIEMPO t;
t.init();
Box *u_j = new(Box);
point *ib = new(point), *ub = new(point);
ib->resize(p); ub->resize(p);
create_u_j_bounds_full(P, &ib, &ub, it, z, coordinate);
if (ub != NULL) {
u_j = CREATE_BOX_AND_VALUE(ib, ub, z, input, scaling, coordinate);
}
else {
t.acum();
P->Count.TSplit += t.value();
return (NULL);
}
u_j->pos_sibling = coordinate;
t.acum();
P->Count.TSplit += t.value();
return u_j;
}
Box *PARTITION_BOX_partition(MOILP *P, Box *it, point *z, point *tz, bool *is_inside, int coordinate, Input *input, double *scaling) {
//Create new box partitioning (it) respect to tz (projection of z to it), according the coordinate "coordinate".
//We use p-partition method
TIEMPO t;
t.init();
Box *u_j = new(Box);
point *ib = new(point), *ub = new(point);
ib->resize(p); ub->resize(p);
create_u_j_bounds_p_partition(&ib, &ub, it, z, tz, coordinate);
if (ub != NULL) {
u_j = CREATE_BOX_AND_VALUE(ib, ub, z, input, scaling, coordinate);
if ((coordinate == p - 1) && (is_inside)) {
if (input->box_value == "reduced") {
double vol_dominated_part = get_volume(ib, z);
u_j->value -= vol_dominated_part;
}
else if (input->box_value == "reduced_scaled") {
double vol_dominated_part = get_scaled_volume(ib, tz, scaling);
u_j->value -= vol_dominated_part;
}
}
}
else {
t.acum();
P->Count.TSplit += t.value();
return (NULL);
}
u_j->pos_sibling = coordinate;
t.acum();
P->Count.TSplit += t.value();
return u_j;
}
void update_to_min_lb(point *LB, point *lb2) {
//Point LB = min (LB, lb2)
int i;
int p = int(LB->size());
for (i = 0; i < p; i++) {
if (lb2->at(i) < LB->at(i)) {
LB->at(i) = lb2->at(i);
}
}
}