-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDspCInterface.cpp
859 lines (743 loc) · 25.9 KB
/
DspCInterface.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
/*
* DspCInterface.cpp
*
* Created on: Oct 27, 2014
* Author: kibaekkim
*/
// #define DSP_DEBUG
#include <cstdlib>
#include <cstdio>
#include "DspApiEnv.h"
#include "DspCInterface.h"
#include "Utility/DspMacros.h"
#include "Model/DecTssModel.h"
#include "Model/DecBlkModel.h"
#include "Solver/Deterministic/DeDriver.h"
#include "Solver/DualDecomp/DdDriverSerial.h"
#include "Solver/DantzigWolfe/DwSolverSerial.h"
#ifdef DSP_HAS_SCIP
#include "Solver/Benders/BdDriverSerial.h"
#endif /* DSP_HAS_SCIP */
#ifdef DSP_HAS_MPI
#ifdef DSP_HAS_SCIP
#include "Solver/Benders/BdDriverMpi.h"
#endif /* DSP_HAS_SCIP */
#include "Solver/DualDecomp/DdDriverMpi.h"
#include "Solver/DantzigWolfe/DwSolverMpi.h"
#endif /* DSP_HAS_MPI */
/* using __cplusplus */
#ifdef __cplusplus
extern "C" {
#endif
/** create API environment */
DspApiEnv * createEnv(void)
{
return new DspApiEnv;
}
/** free API environment */
void freeEnv(DspApiEnv * &env)
{
FREE_PTR(env);
}
/** free new model */
void freeModel(DspApiEnv * env)
{
DSP_API_CHECK_ENV();
FREE_PTR(env->model_);
}
/** free solver */
void freeSolver(DspApiEnv * env)
{
DSP_API_CHECK_ENV();
FREE_PTR(env->solver_);
}
/** If current model is stochastic, return the model as a TssModel object. If no model exists, create one. */
TssModel * getTssModel(DspApiEnv * env)
{
if (env->model_ == NULL)
{
env->model_ = new DecTssModel;
}
if (env->model_->isStochastic())
{
TssModel * tss;
try
{
tss = dynamic_cast<TssModel *>(env->model_);
}
catch (const std::bad_cast& e)
{
printf("Error: Model claims to be stochastic when it is not");
return NULL;
}
return tss;
}
else
{
printf("Error: Attempted to access feature only supported by stochastic models with a general decomposition model\n");
return NULL;
}
}
/** get model pointer */
DecModel * getModelPtr(DspApiEnv * env)
{
DSP_API_CHECK_ENV(NULL);
return env->model_;
}
/** set number of scenarios */
void setNumberOfScenarios(
DspApiEnv * env, /**< pointer to API object */
int nscen /**< number of scenarios */)
{
getTssModel(env)->setNumberOfScenarios(nscen);
}
/** set dimensions */
void setDimensions(
DspApiEnv * env, /**< pointer to API object */
const int ncols1, /**< number of first-stage columns */
const int nrows1, /**< number of first-stage rows */
const int ncols2, /**< number of second-stage columns */
const int nrows2 /**< number of second-stage rows */)
{
getTssModel(env)->setDimensions(ncols1, nrows1, ncols2, nrows2);
}
/** read smps files */
int readSmps(DspApiEnv * env, const char * smps)
{
return getTssModel(env)->readSmps(smps);
}
/** read dro files */
int readDro(DspApiEnv * env, const char * dro)
{
return getTssModel(env)->readDro(dro);
}
/** read quad files */
int readQuad(DspApiEnv * env, const char * smps, const char * quad)
{
return getTssModel(env)->readQuad(smps, quad);
}
/** load first-stage problem */
void loadFirstStage(
DspApiEnv * env, /**< pointer to API object */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const double * rlbd, /**< row lower bounds */
const double * rubd /**< row upper bounds */)
{
getTssModel(env)->loadFirstStage(start, index, value, clbd, cubd, ctype, obj, rlbd, rubd);
}
/** load first-stage problem with quadratic objective */
void loadQuadraticFirstStage(
DspApiEnv * env, /**< pointer to API object */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const int * qrowindex,/**< start index for first-stage quadratic objective */
const int * qcolindex,/**< quadratic objective column indices */
const double * qvalue,/**< quadratic objective elements */
const CoinBigIndex qnum, /**< number of quadratic elements */
const double * rlbd, /**< row lower bounds */
const double * rubd /**< row upper bounds */)
{
getTssModel(env)->loadFirstStage(start, index, value, clbd, cubd, ctype, obj, qrowindex, qcolindex, qvalue, qnum, rlbd, rubd);
}
/** load first-stage problem with quadratic objective and constraints*/
void loadQCQPFirstStage(
DspApiEnv * env, /**< pointer to API object */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const int * qobjrowindex, /**< quadratic objective row indices */
const int * qobjcolindex, /**< quadratic objective column indices */
const double * qobjvalue, /**< quadratic objective constraint elements value */
const CoinBigIndex qobjnum, /**< number of quadratic terms in the objective */
const double * rlbd, /**< row lower bounds */
const double * rubd, /**< row upper bounds */
const int nqrows, /**< number of quadratic rows */
const int * linnzcnt, /**< number of nonzero coefficients in the linear part of each constraint */
const int * quadnzcnt, /**< number of nonzero coefficients in the quadratic part of each constraint */
const double * rhs, /**< constraint rhs of each constraint */
const int * sense, /**< constraint sense of each constraint */
const int * linstart, /**< number of nonzero coefficients in the linear part of each constraint */
const int * linind, /**< indices for the linear part */
const double * linval, /**< nonzero coefficient of the linear part */
const int * quadstart, /**< number of nonzero coefficients in the quadratic part of each constraint */
const int * quadrow, /**< indices for the quadratic part */
const int * quadcol, /**< indices for the quadratic part */
const double * quadval /**< nonzero coefficient of the quadratic part */)
{
getTssModel(env)->loadFirstStage(start, index, value, clbd, cubd, ctype, obj, qobjrowindex, qobjcolindex, qobjvalue, qobjnum, rlbd, rubd, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval);
}
/** load second-stage problem */
void loadSecondStage(
DspApiEnv * env, /**< pointer to API object */
const int s, /**< scenario index */
const double prob, /**< probability */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const double * rlbd, /**< row lower bounds */
const double * rubd /**< row upper bounds */)
{
getTssModel(env)->loadSecondStage(s, prob, start, index, value, clbd, cubd, ctype, obj, rlbd, rubd);
}
/** load second-stage problem */
void loadQuadraticSecondStage(
DspApiEnv * env, /**< pointer to API object */
const int s, /**< scenario index */
const double prob, /**< probability */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const int * qrowindex,/**< start index for first-stage quadratic objective */
const int * qcolindex,/**< quadratic objective column indices */
const double * qvalue,/**< quadratic objective elements */
const CoinBigIndex qnum, /**< number of quadratic elements */
const double * rlbd, /**< row lower bounds */
const double * rubd /**< row upper bounds */)
{
getTssModel(env)->loadSecondStage(s, prob, start, index, value, clbd, cubd, ctype, obj, qrowindex, qcolindex, qvalue, qnum, rlbd, rubd);
}
/** load second-stage problem */
void loadQCQPSecondStage(
DspApiEnv * env, /**< pointer to API object */
const int s, /**< scenario index */
const double prob, /**< probability */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const int * qobjrowindex, /**< quadratic objective row indices */
const int * qobjcolindex, /**< quadratic objective column indices */
const double * qobjvalue, /**< quadratic objective constraint elements value */
const CoinBigIndex qobjnum, /**< number of quadratic terms in the objective */
const double * rlbd, /**< row lower bounds */
const double * rubd, /**< row upper bounds */
const int nqrows, /**< number of quadratic rows */
const int * linnzcnt, /**< number of nonzero coefficients in the linear part of each constraint */
const int * quadnzcnt, /**< number of nonzero coefficients in the quadratic part of each constraint */
const double * rhs, /**< constraint rhs of each constraint */
const int * sense, /**< constraint sense of each constraint */
const int * linstart, /**< number of nonzero coefficients in the linear part of each constraint */
const int * linind, /**< indices for the linear part */
const double * linval, /**< nonzero coefficient of the linear part */
const int * quadstart, /**< number of nonzero coefficients in the quadratic part of each constraint */
const int * quadrow, /**< indices for the quadratic part */
const int * quadcol, /**< indices for the quadratic part */
const double * quadval /**< nonzero coefficient of the quadratic part */)
{
getTssModel(env)->loadSecondStage(s, prob, start, index, value, clbd, cubd, ctype, obj, qobjrowindex, qobjcolindex, qobjvalue, qobjnum, rlbd, rubd, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval);
}
/**
* The following function allows to read the model by blocks. This
* is particularly useful for distributed memory computing.
*/
/** load block problems */
void loadBlockProblem(
DspApiEnv * env, /**< pointer to API object */
int id, /**< block ID */
int ncols, /**< number of columns */
int nrows, /**< number of rows */
int numels, /**< number of elements in the matrix */
const CoinBigIndex * start, /**< start index for each row */
const int * index, /**< column indices */
const double * value, /**< constraint elements */
const double * clbd, /**< column lower bounds */
const double * cubd, /**< column upper bounds */
const char * ctype, /**< column types */
const double * obj, /**< objective coefficients */
const double * rlbd, /**< row lower bounds */
const double * rubd /**< row upper bounds */) {
DSPdebugMessage("\n##############################################################\n"
"## The beginning of loadBlockProblem ##\n"
"##############################################################\n");
DSPdebugMessage("id = %d\n", id);
DSPdebugMessage("ncols = %d\n", ncols);
DSPdebugMessage("nrows = %d\n", nrows);
DSPdebugMessage("numels = %d\n", numels);
DSPdebugMessage("start:\n");
DSPdebug(DspMessage::printArray(nrows+1, start));
DSPdebugMessage("index:\n");
DSPdebug(DspMessage::printArray(numels, index));
DSPdebugMessage("value:\n");
DSPdebug(DspMessage::printArray(numels, value));
DSPdebugMessage("clbd:\n");
DSPdebug(DspMessage::printArray(ncols, clbd));
DSPdebugMessage("cubd:\n");
DSPdebug(DspMessage::printArray(ncols, cubd));
DSPdebugMessage("obj:\n");
DSPdebug(DspMessage::printArray(ncols, obj));
DSPdebugMessage("rlbd:\n");
DSPdebug(DspMessage::printArray(nrows, rlbd));
DSPdebugMessage("rubd:\n");
DSPdebug(DspMessage::printArray(nrows, rubd));
DSPdebugMessage("\n##############################################################\n"
"## The end of loadBlockProblem ##\n"
"##############################################################\n\n\n\n");
if (env->model_ == NULL)
env->model_ = new DecBlkModel;
BlkModel* blk = dynamic_cast<DecBlkModel*>(env->model_)->blkPtr();
if (blk == NULL)
fprintf(stderr, "Block decomposition model is not loaded.\n");
else {
blk->addBlock(id,
new DetBlock(start, index, value, numels,
ncols, nrows, clbd, cubd, ctype, obj, rlbd, rubd));
}
}
/** update block structure information */
void updateBlocks(
DspApiEnv * env /**< pointer to API object */) {
if (env->model_ == NULL)
fprintf(stderr, "Block decomposition model is not loaded.\n");
BlkModel* blk = dynamic_cast<DecBlkModel*>(env->model_)->blkPtr();
if (blk == NULL)
fprintf(stderr, "Block decomposition model is not loaded.\n");
else
blk->updateBlocks();
}
/** set initial solutions
* This function can be called multiple times for multiple initial solutions. */
void setSolution(
DspApiEnv * env, /**< pointer to API object */
int size, /**< size of solution array */
double * solution /**< solution to set */)
{
getTssModel(env)->setSolution(size, solution);
}
/** solve deterministic equivalent model */
void solveDe(DspApiEnv * env)
{
BGN_TRY_CATCH
DSP_API_CHECK_MODEL();
freeSolver(env);
env->solver_ = new DeDriver(env->model_, env->par_, env->message_);
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<DeDriver*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
END_TRY_CATCH(;)
}
/** solve dual decomposition */
void solveDd(DspApiEnv * env)
{
BGN_TRY_CATCH
DSP_API_CHECK_MODEL();
freeSolver(env);
env->solver_ = new DdDriverSerial(env->model_, env->par_, env->message_);
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<DdDriverSerial*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
END_TRY_CATCH(;)
}
/** solve Dantzig-Wolfe decomposition (with branch-and-bound) */
void solveDw(DspApiEnv * env)
{
BGN_TRY_CATCH
DSP_API_CHECK_MODEL();
freeSolver(env);
env->solver_ = new DwSolverSerial(env->model_, env->par_, env->message_);
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<DwSolverSerial*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
END_TRY_CATCH(;)
}
/** solve serial Benders decomposition */
void solveBd(DspApiEnv * env)
{
BGN_TRY_CATCH
#ifdef DSP_HAS_SCIP
DSP_API_CHECK_MODEL();
freeSolver(env);
if (!env->model_->isStochastic())
{
printf("Error: Benders decomposition is currently only supported by stochastic models\n");
return;
}
DecTssModel* dec = new DecTssModel(*getTssModel(env));
BdDriverSerial * bd = new BdDriverSerial(dec, env->par_, env->message_);
env->solver_ = bd;
DSPdebugMessage("Created a serial Benders object\n");
int nauxvars = env->par_->getIntParam("BD/NUM_CUTS_PER_ITER");
if (nauxvars > 0)
{
double * obj_aux = NULL;
double * clbd_aux = NULL;
double * cubd_aux = NULL;
/** set auxiliary variables */
obj_aux = new double [nauxvars];
clbd_aux = new double [nauxvars];
cubd_aux = new double [nauxvars];
CoinFillN(obj_aux, nauxvars, 1.0);
CoinFillN(clbd_aux, nauxvars, -COIN_DBL_MAX);
CoinFillN(cubd_aux, nauxvars, +COIN_DBL_MAX);
/** set auxiliary variables */
bd->setAuxVarData(nauxvars, obj_aux, clbd_aux, cubd_aux);
/** free memory */
FREE_ARRAY_PTR(obj_aux);
FREE_ARRAY_PTR(clbd_aux);
FREE_ARRAY_PTR(cubd_aux);
}
DSPdebugMessage("Set auxiliary variable data\n");
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<BdDriverSerial*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
#else
printf("Benders decomposition has been disabled because SCIP was not available.\n");
#endif
END_TRY_CATCH(;)
}
#ifdef DSP_HAS_MPI
/** solve parallel dual decomposition */
void solveDdMpi(DspApiEnv * env, MPI_Comm comm)
{
BGN_TRY_CATCH
int comm_size;
MPI_Comm_size(comm, &comm_size);
if (comm_size == 1) {
solveDd(env);
return;
}
DSP_API_CHECK_MODEL();
freeSolver(env);
env->solver_ = new DdDriverMpi(env->model_, env->par_, env->message_, comm);
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<DdDriverMpi*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
END_TRY_CATCH(;)
}
/** solve parallel Dantzig-Wolfe decomposition with branch-and-bound */
void solveDwMpi(DspApiEnv * env, MPI_Comm comm)
{
BGN_TRY_CATCH
int comm_size;
int comm_rank;
MPI_Comm_size(comm, &comm_size);
MPI_Comm_rank(comm, &comm_rank);
if (comm_size == 1) {
solveDw(env);
return;
}
DSP_API_CHECK_MODEL();
freeSolver(env);
env->solver_ = new DwSolverMpi(env->model_, env->par_, env->message_, comm);
/** Check whether or not the subproblems are distributed. */
int is_distributed;
if (env->model_->isStochastic())
{
is_distributed = env->model_->isDistributed() ? 1 : 0;
}
else
{
/**
* NOTE: This is really a temporary fix.
* We assume that the master process has no subproblem when the subproblems are distributed.
*/
if (comm_rank == 0)
is_distributed = env->model_->getNumSubproblems() == 0 ? 1 : 0;
MPI_Bcast(&is_distributed, 1, MPI_INT, 0, comm);
}
if (is_distributed == 1)
{
if (comm_rank == 0)
cout << "The distributed computing is not supported for Dantzig-Wolfe decomposition.\n"
"Please create all the scenario subproblems at each process.\n"
<< endl;
return;
}
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<DwSolverMpi*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
END_TRY_CATCH(;)
}
/** solve parallel Benders decomposition */
void solveBdMpi(
DspApiEnv * env, MPI_Comm comm)
{
BGN_TRY_CATCH
int comm_size;
MPI_Comm_size(comm, &comm_size);
if (comm_size == 1) {
solveBd(env);
return;
}
#ifdef DSP_HAS_SCIP
DSP_API_CHECK_MODEL();
freeSolver(env);
if (!env->model_->isStochastic())
{
printf("Error: Benders decomposition is currently only supported by stochastic models\n");
return;
}
//DSPdebugMessage("Creating a MPI Benders object (comm %d)\n", comm);
BdDriverMpi * bd = new BdDriverMpi(new DecTssModel(*getTssModel(env)), env->par_, env->message_, comm); //this line cause segment fault
//BdDriverMpi * bd = new BdDriverMpi(env->model_, env->par_, env->message_, comm);
env->solver_ = bd;
double * obj_aux = NULL;
double * clbd_aux = NULL;
double * cubd_aux = NULL;
int nauxvars = env->par_->getIntParam("BD/NUM_CUTS_PER_ITER");
if (nauxvars > 0)
{
/** set auxiliary variables */
obj_aux = new double [nauxvars];
clbd_aux = new double [nauxvars];
cubd_aux = new double [nauxvars];
CoinFillN(obj_aux, nauxvars, 1.0);
CoinFillN(clbd_aux, nauxvars, -COIN_DBL_MAX);
CoinFillN(cubd_aux, nauxvars, +COIN_DBL_MAX);
bd->setAuxVarData(nauxvars, obj_aux, clbd_aux, cubd_aux);
FREE_ARRAY_PTR(obj_aux);
FREE_ARRAY_PTR(clbd_aux);
FREE_ARRAY_PTR(cubd_aux);
}
DSP_RTN_CHECK_THROW(env->solver_->init());
DSP_RTN_CHECK_THROW(dynamic_cast<BdDriverMpi*>(env->solver_)->run());
DSP_RTN_CHECK_THROW(env->solver_->finalize());
#else
printf("Benders decomposition has been disabled because SCIP was not available.\n");
#endif
END_TRY_CATCH(;)
}
#endif
/** read parameter file */
void readParamFile(DspApiEnv * env, const char * param_file)
{
DSP_API_CHECK_ENV();
env->par_->readParamFile(param_file);
}
void setWassersteinAmbiguitySet(DspApiEnv *env, double lp_norm, double eps)
{
DSP_API_CHECK_ENV();
BGN_TRY_CATCH
DSP_API_CHECK_MODEL();
if (env->model_->isStochastic() == false)
throw "Distributionally robust is available for stochastic programs only.\n";
if (env->model_->isDistributed())
{
char msg[] = "The distributed computing of Wasserstein ambiguity set is not supported.\n"
"Please create all the scenario subproblems at each process.\n";
throw msg;
}
DSP_RTN_CHECK_THROW(getTssModel(env)->setWassersteinAmbiguitySet(lp_norm, eps));
END_TRY_CATCH(;)
}
/** set boolean parameter */
void setBoolParam(DspApiEnv * env, const char * name, bool value)
{
DSP_API_CHECK_ENV();
string strname(name);
env->par_->setBoolParam(name, value);
}
/** set integer parameter */
void setIntParam(DspApiEnv * env, const char * name, int value)
{
DSP_API_CHECK_ENV();
string strname(name);
env->par_->setIntParam(strname, value);
}
/** set double parameter */
void setDblParam(DspApiEnv * env, const char * name, double value)
{
DSP_API_CHECK_ENV();
string strname(name);
env->par_->setDblParam(strname, value);
}
/** set string parameter */
void setStrParam(DspApiEnv * env, const char * name, const char * value)
{
DSP_API_CHECK_ENV();
string strname(name);
string strvalue(value);
env->par_->setStrParam(strname, strvalue);
}
/** set boolean pointer parameter */
void setBoolPtrParam(DspApiEnv * env, const char * name, int size, bool * value)
{
DSP_API_CHECK_ENV();
string strname(name);
env->par_->setBoolPtrParamSize(strname, size);
for (int i = 0; i < size; ++i)
env->par_->setBoolPtrParam(strname, i, value[i]);
}
/** set integer pointer parameter */
void setIntPtrParam(DspApiEnv * env, const char * name, int size, int * value)
{
DSP_API_CHECK_ENV();
string strname(name);
env->par_->setIntPtrParamSize(strname, size);
for (int i = 0; i < size; ++i)
env->par_->setIntPtrParam(strname, i, value[i]);
}
/**
* Get functions
*/
/** get number of rows */
int getNumRows(DspApiEnv * env, int stage)
{
DSP_API_CHECK_MODEL(-1);
return getTssModel(env)->getNumRows(stage);
}
/** get number of columns */
int getNumCols(DspApiEnv * env, int stage)
{
DSP_API_CHECK_MODEL(-1);
return getTssModel(env)->getNumCols(stage);
}
/** get number of integer variables */
int getNumIntegers(DspApiEnv * env, int stage)
{
DSP_API_CHECK_MODEL(-1);
return getTssModel(env)->getNumIntegers(stage);
}
/** get number of scenarios */
int getNumScenarios(DspApiEnv * env)
{
DSP_API_CHECK_MODEL(-1);
return getTssModel(env)->getNumScenarios();
}
/** get number of coupling rows */
int getNumCouplingRows(DspApiEnv * env)
{
DSP_API_CHECK_MODEL(-1);
return getModelPtr(env)->getNumCouplingRows();
}
/** get total number of rows */
int getTotalNumRows(DspApiEnv * env)
{
DSP_API_CHECK_MODEL(-1);
return getModelPtr(env)->getFullModelNumRows();
}
/** get number of quadratic rows */
int getNumQRows(DspApiEnv * env, int s)
{
DSP_API_CHECK_MODEL(-1);
if (s < 0)
return getTssModel(env)->getNumCoreQRows();
else
return getTssModel(env)->getNumScenQRows(s);
}
/** get total number of columns */
int getTotalNumCols(DspApiEnv * env)
{
DSP_API_CHECK_MODEL(-1);
return getModelPtr(env)->getFullModelNumCols();
}
/** get number of subproblems */
int getNumSubproblems(DspApiEnv * env)
{
DSP_API_CHECK_MODEL(-1);
return getModelPtr(env)->getNumSubproblems();
}
/** get total cpu time */
double getCpuTime(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0.0);
return env->solver_->getCpuTime();
}
/** get solution time */
double getWallTime(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0.);
return env->solver_->getWallTime();
}
/** get solution status */
int getStatus(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0);
return env->solver_->getStatus();
}
/** get objective value */
double getPrimalBound(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0.0);
return env->solver_->getBestPrimalObjective();
}
/** get objective value */
double getDualBound(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0.0);
return env->solver_->getBestDualObjective();
}
/** get solution */
void getPrimalSolution(DspApiEnv * env, int num, double * solution)
{
DSP_API_CHECK_SOLVER();
CoinCopyN(env->solver_->getBestPrimalSolution(), num, solution);
}
/** get dual solution */
void getDualSolution(DspApiEnv * env, int num, double * solution)
{
DSP_API_CHECK_SOLVER();
CoinCopyN(env->solver_->getBestDualSolution(), num, solution);
}
/** get number of iterations */
int getNumIterations(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0);
return env->solver_->getNumIterations();
}
/** get number of nodes */
int getNumNodes(DspApiEnv * env)
{
DSP_API_CHECK_SOLVER(0);
return env->solver_->getNumNodes();
}
/**
* Misc
*/
void writeMps(DspApiEnv * env, const char * name)
{
DSP_API_CHECK_MODEL();
freeSolver(env);
env->solver_ = new DeDriver(env->model_, env->par_, env->message_);
env->solver_->init();
dynamic_cast<DeDriver*>(env->solver_)->writeExtMps(name);
env->solver_->finalize();
}
/** print model */
void printModel(DspApiEnv * env)
{
DSP_API_CHECK_MODEL();
env->model_->__printData();
}
int getVersionMajor(DspApiEnv *env)
{
return env->getVersionMajor();
}
int getVersionMinor(DspApiEnv *env)
{
return env->getVersionMinor();
}
int getVersionPatch(DspApiEnv *env)
{
return env->getVersionPatch();
}
#undef DSP_API_CHECK_MODEL
#undef DSP_API_CHECK_ENV
#ifdef __cplusplus
}
#endif