-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblem-bsfCode.cpp
More file actions
1914 lines (1688 loc) · 55.9 KB
/
Problem-bsfCode.cpp
File metadata and controls
1914 lines (1688 loc) · 55.9 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
/*==============================================================================
Project: LiFe
Theme: Apex Method
Module: Problem-bsfCode.cpp (Implementation of Problem Code)
Prefix: PC
Author: Leonid B. Sokolinsky
This source code has been produced with using BSF-skeleton
==============================================================================*/
// PP_STATE_START
// PP_STATE_FIND_INITIAL_APPROXIMATION
// PP_STATE_FIND_INTERIOR_POINT
// PP_STATE_DETERMINE_DIRECTION
// PP_STATE_MOVING_ALONG_SURFACE
// PP_STATE_LANDING
#include "Problem-Data.h" // Problem Types
#include "Problem-Forwards.h" // Problem Function Forwards
#include "Problem-bsfParameters.h" // BSF-skeleton parameters
#include "BSF-SkeletonVariables.h" // Skeleton Variables
using namespace std;
void PC_bsf_SetInitParameter(PT_bsf_parameter_T* parameter) {
for (int j = 0; j < PD_n; j++) // Generating initial approximation
parameter->x[j] = PD_u[j];
}
void PC_bsf_Init(bool* success) {
PD_state = PP_STATE_START;
PD_problemName = PP_PROBLEM_NAME;
if (PP_MODE_BLOCK_HCV_VARIABLE && PP_MODE_USE_LCV_VARIABLE) {
cout << "Modes PP_MODE_BLOCK_HCV_VARIABLE & PP_MODE_USE_LCV_VARIABLE are incompatible!\n";
*success = false;
return;
}
*success = LoadMatrixFormat();
if (*success == false)
return;
//
//
//
PD_MTX_File_so = PP_PATH;
PD_MTX_File_so += PP_MTX_PREFIX;
PD_MTX_File_so += PD_problemName;
PD_MTX_File_so += PP_MTX_POSTFIX_SO;
PD_firstLcvI = INT_MAX;
PD_firstZcvI = INT_MAX;
for (int j1 = 0; j1 < PD_n; j1++) {
if (fabs(PD_c[PD_objI[j1]]) / fabs(PD_c[PD_objI[0]]) <= PP_LOW_COST_PERCENTILE) {
PD_firstLcvI = j1;
for (int j2 = j1; j2 < PD_n; j2++)
if (PD_c[PD_objI[j2]] == 0) {
PD_firstZcvI = j2;
break;
}
break;
}
}
MakeObjVector(PD_c, PD_objVector);
UnitObjVector(PD_e_c);
}
void PC_bsf_SetListSize(int* listSize) {
*listSize = PP_MM;
}
void PC_bsf_SetMapListElem(PT_bsf_mapElem_T* elem, int i) {
elem->a = PD_A[i];
elem->b = &(PD_b[i]);
}
// 0. Pseudo-pojection
void PC_bsf_MapF(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T* reduceElem, int* success // 1 - reduceElem was produced successfully; 0 - otherwise
) {
int exitCode;
Vector_ProjectOnHalfspace(BSF_sv_parameter.x, mapElem->a, *mapElem->b, reduceElem->projection, &exitCode);
switch (exitCode) {
case PP_EXITCODE_DEGENERATE_INEQUALITY:
for (int j = 0; j < PD_n; j++)
reduceElem->projection[j] = 0;
reduceElem->nonZeroCounter = 0;
reduceElem->pointIn = true;
break;
case PP_EXITCODE_POINT_BELONGS_TO_HALFSPACE:
for (int j = 0; j < PD_n; j++)
reduceElem->projection[j] = 0;
reduceElem->nonZeroCounter = 0;
reduceElem->pointIn = true;
break;
case PP_EXITCODE_SUCCESSFUL_PROJECTING:
reduceElem->nonZeroCounter = 1;
reduceElem->pointIn = false;
break;
default:
cout << "Vector_ProjectOnHalfspace: Undefined exit code!" << endl;
break;
}
}
// 1. CheckIn
void PC_bsf_MapF_1(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_1* reduceElem, int* success) {
if (mapElem->a[PP_ADD_FLAG] == 0)
reduceElem->pointIn = PointInHalfspace(BSF_sv_parameter.x, mapElem->a, *mapElem->b);
else
reduceElem->pointIn = true;
}
void PC_bsf_MapF_2(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_2* reduceElem, int* success) {
// not used
}
void PC_bsf_MapF_3(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_3* reduceElem, int* success) {
// not used
}
// 0. Pseudo-pojection
void PC_bsf_ReduceF(PT_bsf_reduceElem_T* x, PT_bsf_reduceElem_T* y, PT_bsf_reduceElem_T* z) { // z = x + y
Vector_Addition(x->projection, y->projection, z->projection);
z->nonZeroCounter = x->nonZeroCounter + y->nonZeroCounter;
z->pointIn = x->pointIn && y->pointIn;
}
// 1. CheckIn
void PC_bsf_ReduceF_1(PT_bsf_reduceElem_T_1* x, PT_bsf_reduceElem_T_1* y, PT_bsf_reduceElem_T_1* z) {
z->pointIn = x->pointIn && y->pointIn;
}
void PC_bsf_ReduceF_2(PT_bsf_reduceElem_T_2* x, PT_bsf_reduceElem_T_2* y, PT_bsf_reduceElem_T_2* z) {
// not used
}
void PC_bsf_ReduceF_3(PT_bsf_reduceElem_T_3* x, PT_bsf_reduceElem_T_3* y, PT_bsf_reduceElem_T_3* z) {
// not used
}
//0. Start
void PC_bsf_ProcessResults(
PT_bsf_reduceElem_T* reduceResult,
int reduceCounter, // Number of successfully produced Elements of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
#ifdef PP_MAX_ITER_COUNT
if (BSF_sv_iterCounter > PP_MAX_ITER_COUNT) {
cout << "-------------> PC_bsf_ProcessResults: Acceptable maximum number of iterations is exceeded: PP_MAX_ITER_COUNT = "
<< PP_MAX_ITER_COUNT << endl;
*exit = true;
return;
};
#endif // PP_MAX_ITER_COUNT
PD_pointIn = reduceResult->pointIn;
if (PD_pointIn)
return;
static PT_vector_T relaxationVector;
for (int j = 0; j < PD_n; j++)
relaxationVector[j] = reduceResult->projection[j] / (double)(reduceResult->nonZeroCounter);
Vector_PlusEquals(parameter->x, relaxationVector);
}
// 1. Movement on Polytope ========================================================
void PC_bsf_ProcessResults_1(
PT_bsf_reduceElem_T_1* reduceResult,
int reduceCounter, // Number of successfully produced Elements of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
#ifdef PP_MAX_ITER_COUNT
if (BSF_sv_iterCounter > PP_MAX_ITER_COUNT) {
cout << "-------------> Acceptable maximum number of iterations is exceeded: PP_MAX_ITER_COUNT = " << PP_MAX_ITER_COUNT << endl;
*exit = true;
return;
}
#endif // PP_MAX_ITER_COUNT
PD_pointIn = reduceResult->pointIn;
}
void PC_bsf_ProcessResults_2(
PT_bsf_reduceElem_T_2* reduceResult,
int reduceCounter, // Number of successfully produced Elements of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
// not used
}
void PC_bsf_ProcessResults_3(
PT_bsf_reduceElem_T_3* reduceResult,
int reduceCounter, // Number of successfully produced Elements of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
// not used
}
void PC_bsf_JobDispatcher(
PT_bsf_parameter_T* parameter, // Current Approximation
int* job,
bool* exit,
double t
) {
static PT_vector_T shiftBasePoint;
static double* ptr_unitVectorToSurface;
static int landingNo;
const char* x0_File = PD_MTX_File_x0.c_str();
//
bool goOn, repeat;
static int detDirSwitch;
static int lcvI;
static double c_lcvI;
static double new_c_lcv;
static int max_lcvI;
static double max_objF_lcv;
static double max_new_c_lcv;
static double objF_lcv;
#define BEGIN_LCV_UTILIZATION 0
#define POSITIVE_LCV 1
#define NEGATIVE_LCV 2
#define POSITIVE_ZCV 3
#define NEGATIVE_ZCV 4
#define END_LCV_UTILIZATION 5
switch (PD_state) {
case PP_STATE_START://-------------------------- Start -----------------------------
if (PointInPolytope_s(PD_x0)) {
Vector_Copy(PD_x0, PD_apexPoint);
ApexPoint(PD_x0, PD_apexPoint);
#ifdef PP_DEBUG
cout << "Apex point:\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << PD_apexPoint[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n)
cout << " ...";
cout << "\tF(x) = " << setw(PP_SETW) << ObjF(PD_apexPoint);
cout << endl;
cout << "--------- Finding initial_approximation ------------\n";
#endif
// Preparations for finding initial approximation
Vector_Copy(PD_apexPoint, parameter->x);
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_FIND_INITIAL_APPROXIMATION;
break;
}
// Preparations for finding interior point
Vector_Copy(PD_x0, parameter->x);
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_FIND_INTERIOR_POINT;
#ifdef PP_DEBUG
cout << "--------- Finding interior point ------------\n";
#endif
break;
case PP_STATE_FIND_INTERIOR_POINT://------------------------- Finding interior point -----------------------------
if (!PD_pointIn) {
return;
}
ApexPoint(parameter->x, PD_apexPoint);
#ifdef PP_DEBUG
cout << "Apex point:\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << PD_apexPoint[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n)
cout << " ...";
cout << "\tF(x) = " << setw(PP_SETW) << ObjF(PD_apexPoint);
cout << endl;
cout << "--------- Finding initial_approximation ------------\n";
#endif
// Preparations for finding initial approximation
Vector_Copy(PD_apexPoint, parameter->x);
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_FIND_INITIAL_APPROXIMATION;
break;
case PP_STATE_FIND_INITIAL_APPROXIMATION://-------------------------- Finding initial approximationt -----------------------------
if (!PD_pointIn) {
return;
}
/*debug2**
SavePoint(parameter->x, x0_File, t);
cout << "==================> F(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
*exit = true;
return;
/*end debug*/
#ifdef PP_DEBUG
cout << "Iter # " << BSF_sv_iterCounter << ". Elapsed time: " << round(t) << endl;
cout << "u0 =\t\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif
Vector_Copy(parameter->x, PD_u);
PD_objF_u = ObjF(PD_u);
/*debug3**
*exit = true;
return;
/*end debug*/
// Preparations for determining direction
Vector_PlusEquals(parameter->x, PD_objVector);
assert(!PointInPolytope_s(parameter->x));
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_DETERMINE_DIRECTION;
PD_numDetDir = 0;
/*debug00*/
#ifdef PP_DEBUG
cout << "--------- Determine Direction ------------\n";
#endif
/*end debug*/
break;
case PP_STATE_DETERMINE_DIRECTION://------------------------- Determine Direction -----------------------------
if (!PD_pointIn)
return;
if (!PP_MODE_USE_LCV_VARIABLE) {
/*debug00*/
#ifdef PP_DEBUG
cout << "w =\t\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif
/*end debug*/
}
if (PP_MODE_USE_LCV_VARIABLE) {
if (PD_firstLcvI == INT_MAX) {
cout << "Error: The PP_MODE_USE_LCV_VARIABLE=true, but there are no low cost variables!\n";
*exit = true;
return;
}
switch (detDirSwitch) {
case BEGIN_LCV_UTILIZATION:
max_lcvI = 0;
max_objF_lcv = -INFINITY;
/*debug00*/
#ifdef PP_DEBUG
cout << "w =\t\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif
/*end debug*/
PD_objF_w = ObjF(parameter->x);
lcvI = PD_firstLcvI;
c_lcvI = PD_c[PD_objI[lcvI]];
if (PD_firstLcvI < (PD_firstZcvI == INT_MAX ? PD_n : PD_firstZcvI)) {
PD_c[PD_objI[lcvI]] = fabs(PD_c[PD_objI[lcvI]]) + fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
detDirSwitch = POSITIVE_LCV;
}
else {
PD_c[PD_objI[lcvI]] = fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
detDirSwitch = POSITIVE_ZCV;
}
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
return;
case POSITIVE_LCV:
/*debug01**
#ifdef PP_DEBUG
if (lcvI == PD_firstLcvI)
cout << "---------------- Nonzero low cost variables with '+' ----------------\n";
cout << "#" << lcvI << "|" << PD_objI[lcvI] << ":\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif // PP_DEBUG
/*end debug*/
objF_lcv = ObjF(parameter->x);
if (objF_lcv > PD_objF_w + PP_EPS_ZERO_DIR
&& objF_lcv > PD_objF_u + PP_EPS_ZERO_DIR
&& objF_lcv > max_objF_lcv)
{
max_objF_lcv = objF_lcv;
max_lcvI = lcvI;
max_new_c_lcv = new_c_lcv;
}
lcvI++;
c_lcvI = PD_c[PD_objI[lcvI]];
if (lcvI < (PD_firstZcvI == INT_MAX ? PD_n : PD_firstZcvI)) {
PD_c[PD_objI[lcvI]] = fabs(PD_c[PD_objI[lcvI]]) + fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
return;
}
lcvI = PD_firstLcvI;
c_lcvI = PD_c[PD_objI[lcvI]];
PD_c[PD_objI[lcvI]] = -(fabs(PD_c[PD_objI[lcvI]]) + fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2));
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
detDirSwitch = NEGATIVE_LCV;
return;
case NEGATIVE_LCV:
/*debug01*
#ifdef PP_DEBUG
if (lcvI == PD_firstLcvI)
cout << "---------------- Nonzero low cost variables with '-' ----------------\n";
cout << "#" << lcvI << "|" << PD_objI[lcvI] << ":\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif // PP_DEBUG
/*end debug*/
objF_lcv = ObjF(parameter->x);
if (objF_lcv > PD_objF_w + PP_EPS_ZERO_DIR
&& objF_lcv > PD_objF_u + PP_EPS_ZERO_DIR
&& objF_lcv > max_objF_lcv)
{
max_objF_lcv = objF_lcv;
max_lcvI = lcvI;
max_new_c_lcv = new_c_lcv;
}
lcvI++;
c_lcvI = PD_c[PD_objI[lcvI]];
if (lcvI < (PD_firstZcvI == INT_MAX ? PD_n : PD_firstZcvI)) {
PD_c[PD_objI[lcvI]] = -(fabs(PD_c[PD_objI[lcvI]]) + fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2));
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
return;
}
if (PD_firstZcvI != INT_MAX) {
lcvI = PD_firstZcvI;
c_lcvI = PD_c[PD_objI[lcvI]];
PD_c[PD_objI[lcvI]] = fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
detDirSwitch = POSITIVE_ZCV;
return;
}
else
{
if (max_lcvI > 0) {
c_lcvI = PD_c[PD_objI[max_lcvI]];
PD_c[PD_objI[max_lcvI]] = max_new_c_lcv;
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[max_lcvI]] = c_lcvI;
}
else
MakeObjVector(PD_c, PD_objVector);
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
detDirSwitch = END_LCV_UTILIZATION;
return;
}
case POSITIVE_ZCV:
/*debug01**
#ifdef PP_DEBUG
if (lcvI == PD_firstZcvI)
cout << "---------------- Zero cost variables with '+' ----------------\n";
cout << "#" << lcvI << "|" << PD_objI[lcvI] << ":\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif // PP_DEBUG
/*end debug*/
objF_lcv = ObjF(parameter->x);
if (objF_lcv > PD_objF_w + PP_EPS_ZERO_DIR
&& objF_lcv > PD_objF_u + PP_EPS_ZERO_DIR
&& objF_lcv > max_objF_lcv)
{
max_objF_lcv = objF_lcv;
max_lcvI = lcvI;
max_new_c_lcv = new_c_lcv;
}
lcvI++;
if (lcvI < PD_n) {
c_lcvI = PD_c[PD_objI[lcvI]];
PD_c[PD_objI[lcvI]] = fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
if (lcvI < PD_firstZcvI)
PD_c[PD_objI[lcvI]] += c_lcvI;
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
return;
}
lcvI = PD_firstZcvI;
c_lcvI = PD_c[PD_objI[lcvI]];
PD_c[PD_objI[lcvI]] = -fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
detDirSwitch = NEGATIVE_ZCV;
return;
case NEGATIVE_ZCV:
/*debug01**
#ifdef PP_DEBUG
if (lcvI == PD_firstZcvI)
cout << "---------------- Zero cost variables with '-' ----------------\n";
cout << "#" << lcvI << "|" << PD_objI[lcvI] << ":\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
#endif // PP_DEBUG
/*end debug*/
objF_lcv = ObjF(parameter->x);
if (objF_lcv > PD_objF_w + PP_EPS_ZERO_DIR
&& objF_lcv > PD_objF_u + PP_EPS_ZERO_DIR
&& objF_lcv > max_objF_lcv)
{
max_objF_lcv = objF_lcv;
max_lcvI = lcvI;
max_new_c_lcv = new_c_lcv;
}
lcvI++;
if (lcvI < PD_n) {
c_lcvI = PD_c[PD_objI[lcvI]];
PD_c[PD_objI[lcvI]] = -fabs(PD_c[PD_objI[PD_firstLcvI - 1]] / 2);
new_c_lcv = PD_c[PD_objI[lcvI]];
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[lcvI]] = c_lcvI;
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
return;
}
if (max_lcvI > 0) {
c_lcvI = PD_c[PD_objI[max_lcvI]];
PD_c[PD_objI[max_lcvI]] = max_new_c_lcv;
MakeObjVector(PD_c, PD_objVector);
PD_c[PD_objI[max_lcvI]] = c_lcvI;
}
else
MakeObjVector(PD_c, PD_objVector);
Vector_Copy(PD_u, parameter->x);
Vector_PlusEquals(parameter->x, PD_objVector);
detDirSwitch = END_LCV_UTILIZATION;
return;
case END_LCV_UTILIZATION:
/*debug02*/
#ifdef PP_DEBUG
if (max_lcvI > 0) {
cout << "Optimal POSITIVE_LCV is found with native c = "
<< (PD_c[PD_objI[max_lcvI]] == 0 ? 0 : PD_c[PD_objI[max_lcvI]])
<< " and new c = " << max_new_c_lcv << " : #" << max_lcvI << "|"
<< PD_objI[max_lcvI] << ":\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x) << endl;
}
#endif // PP_DEBUG
/*end debug*/
/*debug00*/
#ifdef PP_DEBUG
if (max_lcvI > 0) {
cout << "w =\t\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x);
cout << endl;
}
#endif
/*end debug*/
/*debug8*/
if (fabs(ObjF(parameter->x) - PP_EXACT_OBJ_VALUE) <= PP_EPS_OBJ) {
Vector_Copy(parameter->x, PD_u);
PD_objF_u = ObjF(parameter->x);
*exit = true;
return;
}
/*end debug*/
if (max_lcvI == 0)
cout << "Optimal POSITIVE_LCV not found!\n";
MakeObjVector(PD_c, PD_objVector);
break;
default:
cout << "PC_bsf_JobDispatcher:switch (detDirSwitch): Undefined Switch!" << endl;
*exit = true;
return;
}
}
detDirSwitch = BEGIN_LCV_UTILIZATION;
DetermineDirection(parameter, exit, &repeat);
if (*exit)
break;
if (repeat) {
// Preparations for determining direction
Vector_Copy(PD_u, parameter->x);
PD_objF_u = ObjF(PD_u);
Vector_PlusEquals(parameter->x, PD_objVector);
PD_numDetDir = 0;
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_DETERMINE_DIRECTION;
#ifdef PP_DEBUG
cout << "--------- Determine Direction ------------\n";
#endif
break;
}
/*debug*/
//SavePoint(PD_u, x0_File, t);
/*end debug*/
// Preparations for motion
PD_shiftLength = PP_START_SHIFT_LENGTH;
PD_numShiftsSameLength = 0;
Shift(PD_u, PD_direction, PD_shiftLength, parameter->x);
/*debug00*/
#ifdef PP_DEBUG
cout << "--------- Moving along surface ------------\n";
cout << "Sift = " << setw(PP_SETW) << PD_shiftLength << "\tt = ";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << parameter->x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << ObjF(parameter->x);
cout << endl;
#endif
/*end debug*/
*job = PP_JOB_CHECK;
ptr_unitVectorToSurface = PD_direction;
PD_state = PP_STATE_MOVING_ALONG_SURFACE;
break;
case PP_STATE_MOVING_ALONG_SURFACE://-------------------------- Moving along surface -----------------------------
MovingOnSurface(ptr_unitVectorToSurface, PD_u, parameter->x, &goOn);
if (goOn)
return;
// Preparations for landing
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_LANDING;
/*debug00*/
#ifdef PP_DEBUG
cout << "--------- Landing ------------\n";
#endif //
/*end debug*/
break;
case PP_STATE_LANDING://-------------------------- Landing -----------------------------
if (!PD_pointIn)
return;
Vector_Copy(parameter->x, PD_u);
PD_objF_u = ObjF(PD_u);
//WriteTrace(PD_u);
/*debug8*/
if (fabs(PD_objF_u - PP_EXACT_OBJ_VALUE) <= PP_EPS_OBJ) {
*exit = true;
return;
}
/*end debug*/
#ifdef PP_DEBUG
if (landingNo % PP_BSF_TRACE_COUNT == 0) {
cout << "Landing# " << landingNo << ". Elapsed time: " << round(t) << endl;
cout << "u =\t\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << PD_u[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(t) = " << setw(PP_SETW) << PD_objF_u;
cout << endl;
//SavePoint(PD_u, x0_File, t);
}
landingNo++;
#endif
// Preparations for determining direction
Vector_PlusEquals(parameter->x, PD_objVector);
/*debug*/if (PointInPolytope_s(parameter->x)) {
cout << "Apex point in polytope!\n";
cout << "Maybe, you should increase PP_OBJECTIVE_VECTOR_LENGTH.\n";
*exit = true;
return;
}/*end debug*/
*job = PP_JOB_PSEUDOPOJECTION;
PD_state = PP_STATE_DETERMINE_DIRECTION;
PD_numDetDir = 0;
/*debug00*/
#ifdef PP_DEBUG
cout << "--------- Determining direction ------------\n";
#endif
/*end debug*/
break;
default://------------------------------------- default -----------------------------------
cout << "PC_bsf_JobDispatcher: Undefined state!" << endl;
*exit = true;
break;
}
}
void PC_bsf_ParametersOutput(PT_bsf_parameter_T parameter) {
cout << "=================================================== BSF Target ====================================================" << endl;
cout << "Problem name: " << PD_problemName << endl;
cout << "Number of Workers: " << BSF_sv_numOfWorkers << endl;
#ifdef PP_BSF_OMP
#ifdef PP_BSF_NUM_THREADS
cout << "Number of Threads: " << PP_BSF_NUM_THREADS << endl;
#else
cout << "Number of Threads: " << omp_get_num_procs() << endl;
#endif
#else
cout << "OpenMP is turned off!" << endl;
#endif
#ifdef PP_BSF_FRAGMENTED_MAP_LIST
cout << "Map List is Fragmented." << endl;
#else
cout << "Map List is not Fragmented." << endl;
#endif
cout << "Before conversion: m =\t" << PP_M << "\tn = " << PP_N << endl;
cout << "After conversion: m =\t" << PD_m << "\tn = " << PD_n << endl;
cout << "Eps Zero Compare:\t" << PP_EPS_ZERO_COMPARE << endl;
cout << "Eps Min Dir Length:\t" << PP_EPS_DIR_LENGTH << endl;
cout << "Eps Objective:\t\t" << PP_EPS_OBJ << endl;
cout << "Eps Shift:\t\t" << PP_EPS_SHIFT << endl;
cout << "Eps Zero Direction:\t" << PP_EPS_ZERO_DIR << endl;
cout << "Exact Obj Value:\t" << PP_EXACT_OBJ_VALUE << endl;
cout << "Eta to Apex:\t\t" << PP_ETA_TO_APEX << endl;
cout << "Low Cost Percentile:\t" << PP_LOW_COST_PERCENTILE << endl;
cout << "Gap Max:\t\t" << PP_GAP << endl;
cout << "Obj Vector Length:\t" << PP_OBJECTIVE_VECTOR_LENGTH << endl;
cout << "Start Shift Lengt:\t" << PP_START_SHIFT_LENGTH << endl;
cout << "Blocking obj var:\t" << (PP_MODE_BLOCK_HCV_VARIABLE ? "true" : "false") << endl;
cout << "Use low cost vars:\t" << (PP_MODE_USE_LCV_VARIABLE ? "true" : "false") << endl;
cout << "--------------- Statisics ---------------\n";
cout << "Number of high-cost variables:\t" << (PD_firstLcvI == INT_MAX ? PD_n : PD_firstLcvI) << endl;
cout << "Number of low-cost variables:\t" << (PD_firstLcvI == INT_MAX ? 0 : (PD_firstZcvI == INT_MAX ? PD_n : PD_firstZcvI) - PD_firstLcvI) << endl;
cout << "Number of zero-cost variables:\t" << (PD_firstZcvI == INT_MAX ? 0 : PD_n - PD_firstZcvI) << endl;
cout << "Problem Scale:\t\t\t" << ProblemScale() << endl;
cout << "--------------- Data ---------------\n";
#ifdef PP_MATRIX_OUTPUT
cout << "------- Matrix PD_A & Column PD_b -------" << endl;
for (int i = 0; i < PD_m; i++) {
cout << i << ")";
for (int j = 0; j < PD_n; j++)
cout << setw(PP_SETW) << PD_A[i][j];
cout << "\t<=" << setw(PP_SETW) << PD_b[i] << endl;
}
#endif // PP_MATRIX_OUTPUT
cout << "Obj Function:\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << PD_c[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n)
cout << " ...";
cout << endl;
cout << "x0 =\t\t";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++) cout << setw(PP_SETW) << PD_x0[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n)
cout << " ...";
cout << "\tF(x) = " << setw(PP_SETW) << ObjF(PD_x0);
cout << endl;
cout << "-------------------------------------------" << endl;
}
void PC_bsf_CopyParameter(PT_bsf_parameter_T parameterIn, PT_bsf_parameter_T* parameterOutP) {
for (int j = 0; j < PD_n; j++)
parameterOutP->x[j] = parameterIn.x[j];
if (parameterIn.m > 0) {
PD_A[parameterIn.m][PP_ADD_FLAG] = 1;
PD_A[parameterIn.m][PD_objI[parameterIn.indexToBlock]] = -parameterIn.sign;
PD_b[parameterIn.m] = -parameterIn.sign * parameterIn.b;
PD_A[parameterIn.m + 1][PP_ADD_FLAG] = 1;
PD_A[parameterIn.m + 1][PD_objI[parameterIn.indexToBlock]] = parameterIn.sign;
PD_b[parameterIn.m + 1] = parameterIn.sign * parameterIn.b;
}
}
// 0. Start
void PC_bsf_IterOutput(PT_bsf_reduceElem_T* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int nextJob) {
cout << "# " << BSF_sv_iterCounter << "\tTime " << round(elapsedTime);
cout << "\tx = ";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++) cout << setw(PP_SETW) << parameter.x[PD_objI[j]];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << "\tF(x)= " << setw(PP_SETW) << ObjF(parameter.x) << endl;
}
// 1. Movement on Polytope
void PC_bsf_IterOutput_1(PT_bsf_reduceElem_T_1* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int nextJob)
{
/*cout << "------------------ 1. Movement on Polytope. Iter # " << BSF_sv_iterCounter << " ------------------" << endl;
cout << "Elapsed time: " << round(elapsedTime) << endl;
cout << "PD_u:";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << PD_u[j];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << endl;
cout << "PD_direction:";
for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PD_n); j++)
cout << setw(PP_SETW) << PD_direction[j];
if (PP_OUTPUT_LIMIT < PD_n) cout << " ...";
cout << endl;
cout << "Sift Length = " << PD_shiftLength << endl;/**/
};
// 2.
void PC_bsf_IterOutput_2(PT_bsf_reduceElem_T_2* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int nextJob)
{
// not used
}
void PC_bsf_IterOutput_3(PT_bsf_reduceElem_T_3* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int nextJob)
{
// not used
}
// 0. Start
void PC_bsf_ProblemOutput(PT_bsf_reduceElem_T* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter, double t) {
ProblemOutput(t);
}
// 1. Movement on Polytope
void PC_bsf_ProblemOutput_1(PT_bsf_reduceElem_T_1* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter, double t) {
ProblemOutput(t);
}
void PC_bsf_ProblemOutput_2(PT_bsf_reduceElem_T_2* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter, double t) {
// not used
}
void PC_bsf_ProblemOutput_3(PT_bsf_reduceElem_T_3* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter, double t) {
// not used
}
//----------------------- Assigning Values to BSF-skeleton Variables (Do not modify!) -----------------------
void PC_bsfAssignAddressOffset(int value) { BSF_sv_addressOffset = value; };
void PC_bsfAssignIterCounter(int value) { BSF_sv_iterCounter = value; };
void PC_bsfAssignJobCase(int value) { BSF_sv_jobCase = value; };
void PC_bsfAssignMpiMaster(int value) { BSF_sv_mpiMaster = value; };
void PC_bsfAssignMpiRank(int value) { BSF_sv_mpiRank = value; };
void PC_bsfAssignNumberInSublist(int value) { BSF_sv_numberInSublist = value; };
void PC_bsfAssignNumOfWorkers(int value) { BSF_sv_numOfWorkers = value; };
void PC_bsfAssignParameter(PT_bsf_parameter_T parameter) { PC_bsf_CopyParameter(parameter, &BSF_sv_parameter); }
void PC_bsfAssignSublistLength(int value) { BSF_sv_sublistLength = value; };
//---------------------------------- Problem functions -------------------------
inline PT_float_T Vector_DotProduct(PT_vector_T x, PT_vector_T y) {
PT_float_T sum = 0;
for (int j = 0; j < PD_n; j++)
sum += x[j] * y[j];
return sum;
}
inline PT_float_T Vector_Norm(PT_vector_T x) {
return sqrt(Vector_NormSquare(x));
}
inline PT_float_T Vector_NormSquare(PT_vector_T x) {
PT_float_T sum = 0;
for (int j = 0; j < PD_n; j++) {
sum += x[j] * x[j];
}
return sum;
}
inline bool PointInHalfspace // If the point belongs to the Halfspace with prescigion of PD_Gap
(PT_vector_T point, PT_vector_T a, PT_float_T b) {
return Vector_DotProduct(a, point) <= b + PP_GAP;
}
inline bool PointInHalfspace_s // If the point belongs to the Halfspace with prescigion of PP_EPS_ZERO_COMPARE
(PT_vector_T point, PT_vector_T a, PT_float_T b) {
return Vector_DotProduct(a, point) <= b + PP_EPS_ZERO_COMPARE;
}
inline bool PointInPolytope_s(PT_vector_T x) { // If the point belongs to the polytope with prescigion of PP_EPS_ZERO_COMPARE
for (int i = 0; i < PD_m; i++) {
if (PD_A[i][PP_ADD_FLAG] == 1)
continue;
if (!PointInHalfspace_s(x, PD_A[i], PD_b[i]))
return false;
}
return true;
}
inline void Shift(PT_vector_T basePoint, PT_vector_T direction, PT_float_T siftLength, PT_vector_T endPoint) {
for (int j = 0; j < PD_n; j++)
endPoint[j] = basePoint[j] + direction[j] * siftLength;
}
// Calculating unit vector of direction from startPoint to endPoint
inline bool GetDirection(PT_vector_T startPoint, PT_vector_T endPoint, PT_vector_T unitVector) {
for (int j = 0; j < PD_n; j++) {
unitVector[j] = endPoint[j] - startPoint[j];
}
double normOfUnitVector = Vector_Norm(unitVector);
if (normOfUnitVector < PP_EPS_DIR_LENGTH)
return false;
for (int j = 0; j < PD_n; j++) {
unitVector[j] /= normOfUnitVector;
}
return true;
}
inline void Vector_Copy(PT_vector_T fromPoint, PT_vector_T toPoint) { // toPoint = fromPoint
for (int j = 0; j < PD_n; j++)
toPoint[j] = fromPoint[j];
}
inline void Vector_PlusEquals(PT_vector_T equalVector, PT_vector_T plusVector) { // equalVector += plusVector
for (int j = 0; j < PD_n; j++)
equalVector[j] += plusVector[j];
}
inline void Vector_MinusEquals(PT_vector_T equalPoint, PT_vector_T minusVector) { // equalPoint += minusVector
for (int j = 0; j < PD_n; j++)
equalPoint[j] -= minusVector[j];
}
inline void Vector_Addition(PT_vector_T x, PT_vector_T y, PT_vector_T z) { // z = x + y
for (int j = 0; j < PD_n; j++)
z[j] = x[j] + y[j];
}
inline void Vector_Subtraction(PT_vector_T x, PT_vector_T y, PT_vector_T z) { // z = x - y
for (int j = 0; j < PD_n; j++)
z[j] = x[j] - y[j];
}
inline void Vector_MultiplyByNumber(PT_vector_T x, double r, PT_vector_T y) { // y = r*x
for (int j = 0; j < PD_n; j++)
y[j] = x[j] * r;
}
inline void Vector_MultiplyEquals(PT_vector_T x, double r) { // x = r*x
for (int j = 0; j < PD_n; j++)
x[j] *= r;