forked from dwr-psandhu/jCbc
/
jCbc.cpp
2157 lines (1729 loc) · 60.7 KB
/
jCbc.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
#include <iostream>
#include <iomanip>
#include <sstream>
#include "pthread.h"
#include "sched.h"
#include "semaphore.h"
#include "omp.h"
#if defined(_WIN32) || defined(WIN32)
#include "windows.h"
#endif
#include <algorithm>
#include "CbcEventHandler.hpp"
#include "CoinPragma.hpp"
#include "CbcModel.hpp"
#include "CoinModel.hpp"
#include "OsiClpSolverInterface.hpp"
#include "OsiCbcSolverInterface.hpp"
#include "OsiSolverInterface.hpp"
#include "CbcStrategy.hpp"
#include "CglPreProcess.hpp"
#include "CoinTime.hpp"
#include "CbcHeuristicDiveCoefficient.hpp"
#include "CbcHeuristicDiveFractional.hpp"
#include "CbcHeuristicDiveGuided.hpp"
#include "CbcHeuristicDiveVectorLength.hpp"
#include "CbcHeuristicDivePseudoCost.hpp"
#include "CbcHeuristicDiveLineSearch.hpp"
#include "CbcHeuristic.hpp"
#include "CoinError.hpp"
#include "OsiCuts.hpp"
#include "CglCutGenerator.hpp"
#include "CglGomory.hpp"
#include "CglProbing.hpp"
#include "CglKnapsackCover.hpp"
#include "CglOddHole.hpp"
#include "CglMixedIntegerRounding.hpp"
#include "CglTwomir.hpp"
#include "ClpSimplex.hpp"
#include "ClpPresolve.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinBuild.hpp"
#include "CbcBranchDynamic.hpp"
#include "CbcBranchDecision.hpp"
#include "CbcBranchDefaultDecision.hpp"
#include "CbcCutGenerator.hpp"
#include "CbcHeuristicLocal.hpp"
#include "CglRedSplit.hpp"
#include "CglClique.hpp"
#include "CglFlowCover.hpp"
#include "CglMixedIntegerRounding2.hpp"
#include "CglSimpleRounding.hpp"
#include "CbcHeuristicDINS.hpp"
#include "CbcHeuristicDive.hpp"
#include "CbcHeuristicDiveLineSearch.hpp"
#include "CbcHeuristicDivePseudoCost.hpp"
#include "CbcHeuristicDW.hpp"
#include "CbcHeuristicGreedy.hpp"
#include "CbcHeuristicPivotAndFix.hpp"
#include "CbcHeuristicRandRound.hpp"
#include "CbcHeuristicRENS.hpp"
#include "CbcHeuristicVND.hpp"
#include "CglGMI.hpp"
#include "CglRedSplit2.hpp"
#include "CglResidualCapacity.hpp"
#include "CglZeroHalf.hpp"
#include "CbcTreeLocal.hpp"
#include "CbcCompare.hpp"
#include "CbcBranchActual.hpp"
#include "ClpSolve.hpp"
#include "CoinWarmStart.hpp"
#include "CbcHeuristicFPump.hpp"
#include "CbcHeuristicRINS.hpp"
#include "CbcSolver.hpp"
//=================================================================================================================
using namespace std;
int b;
class abort_exception: public exception
{
virtual const char* what() const throw()
{
return "Abort exception happened";
}
} a_e_1;
//==================================================================================================================
/* Meaning of whereFrom:
1 after initial solve by dualsimplex etc
2 after preprocessing
3 just before branchAndBound (so user can override)
4 just after branchAndBound (before postprocessing)
5 after postprocessing
*/
static int callBack(CbcModel * model, int whereFrom)
{
int returnCode=0;
switch (whereFrom) {
case 1:
if(b==1)
returnCode=1;
break;
case 2:
if(b==1)
returnCode=1;
break;
case 3:
if(b==1)
returnCode=1;
break;
case 4:
if(b==1)
returnCode=1;
break;
case 5:
if(b==1)
returnCode=1;
break;
default:
abort();
}
return returnCode;
}
#include "CbcEventHandler.hpp"
static int cancelAsap=0;
/*
0 - not yet in Cbc
1 - in Cbc with new signal handler
2 - ending Cbc
*/
static int statusOfCbc=0;
#include "CoinSignal.hpp"
static CoinSighandler_t saveSignal = static_cast<CoinSighandler_t> (0);
extern "C" {
static void
#if defined(_MSC_VER)
__cdecl
#endif // _MSC_VER
signal_handler(int /*whichSignal*/)
{
cancelAsap=3;
return;
}
}
class MyEventHandler3 : public CbcEventHandler {
public:
/**@name Overrides */
//@{
virtual CbcAction event(CbcEvent whichEvent);
//@}
/**@name Constructors, destructor etc*/
//@{
/** Default constructor. */
MyEventHandler3();
/// Constructor with pointer to model (redundant as setEventHandler does)
MyEventHandler3(CbcModel * model);
/** Destructor */
virtual ~MyEventHandler3();
/** The copy constructor. */
MyEventHandler3(const MyEventHandler3 & rhs);
/// Assignment
MyEventHandler3& operator=(const MyEventHandler3 & rhs);
/// Clone
virtual CbcEventHandler * clone() const ;
//@}
protected:
// data goes here
};
//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
MyEventHandler3::MyEventHandler3 ()
: CbcEventHandler()
{
}
//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs)
: CbcEventHandler(rhs)
{
}
// Constructor with pointer to model
MyEventHandler3::MyEventHandler3(CbcModel * model)
: CbcEventHandler(model)
{
}
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
MyEventHandler3::~MyEventHandler3 ()
{
}
//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
MyEventHandler3 &
MyEventHandler3::operator=(const MyEventHandler3& rhs)
{
if (this != &rhs) {
CbcEventHandler::operator=(rhs);
}
return *this;
}
//-------------------------------------------------------------------
// Clone
//-------------------------------------------------------------------
CbcEventHandler * MyEventHandler3::clone() const
{
return new MyEventHandler3(*this);
}
CbcEventHandler::CbcAction
MyEventHandler3::event(CbcEvent whichEvent)
{
if (b==1) {
return stop; // say finished
} else {
return noAction; // carry on
}
}
class MyEventHandler4 : public ClpEventHandler {
public:
/**@name Overrides */
//@{
virtual int event(Event whichEvent);
//@}
/**@name Constructors, destructor etc*/
//@{
/** Default constructor. */
MyEventHandler4();
/// Constructor with pointer to model (redundant as setEventHandler does)
MyEventHandler4(ClpSimplex * model);
/** Destructor */
virtual ~MyEventHandler4();
/** The copy constructor. */
MyEventHandler4(const MyEventHandler4 & rhs);
/// Assignment
MyEventHandler4& operator=(const MyEventHandler4 & rhs);
/// Clone
virtual ClpEventHandler * clone() const ;
//@}
protected:
// data goes here
};
//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
MyEventHandler4::MyEventHandler4 ()
: ClpEventHandler()
{
}
//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
MyEventHandler4::MyEventHandler4 (const MyEventHandler4 & rhs)
: ClpEventHandler(rhs)
{
}
// Constructor with pointer to model
MyEventHandler4::MyEventHandler4(ClpSimplex * model)
: ClpEventHandler(model)
{
}
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
MyEventHandler4::~MyEventHandler4 ()
{
}
//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
MyEventHandler4 &
MyEventHandler4::operator=(const MyEventHandler4& rhs)
{
if (this != &rhs) {
ClpEventHandler::operator=(rhs);
}
return *this;
}
//-------------------------------------------------------------------
// Clone
//-------------------------------------------------------------------
ClpEventHandler * MyEventHandler4::clone() const
{
return new MyEventHandler4(*this);
}
int
MyEventHandler4::event(Event whichEvent)
{
//if ( (cancelAsap&1)!=0 ) {
if ( b==1 ) {
//printf("Clp got cancel\n");
return 5;
} else {
return -1;
}
}
//=============================================================================================================
// CoinModel build objects
void addCol(CoinModel *build, double collb, double colub, double obj, const char *name, bool isInt){
build->addCol(0,NULL,NULL,collb,colub,obj,name, isInt);}
void addRow(CoinModel *build, int numberInRow, int index [], double values [], double rowlb, double rowup, const char *name){
build->addRow(numberInRow, (const int*) index, values, rowlb,rowup,name);
}
// Add rows at once to OsiClpSolverInterface
void addRows(OsiClpSolverInterface *solver, CoinModel *build){solver->loadFromCoinModel(*build);}
//set Integer variables in OsiClpSolverInterface
void setInteger(OsiClpSolverInterface *solver, int i){solver->setInteger(i);}
// Assign OsiClpSolverInterface to CbcModel
void assignSolver(CbcModel *model, OsiClpSolverInterface *solver){
OsiSolverInterface * solver1 = solver;
model->assignSolver(solver1);
delete[] solver1;
}
// Reading/Writing Lp/Mps with OsiClpSolverInterface
void readMps(OsiClpSolverInterface *solver, const char *name){solver->readMps(name,"");}
void readLp(OsiClpSolverInterface *solver, const char *name){
// try{
solver->setLogLevel(0);solver->readLp(name);
// } catch(CoinError e){
// e.print();
// }
}
void writeMps(OsiClpSolverInterface *solver, const char *name){solver->writeMps(name);}
void writeLp(OsiClpSolverInterface *solver, const char *name){solver->writeLp(name);}
// CbcModel functions
void branchAndBound(CbcModel * model){model->branchAndBound();}
void setLogLevel(CbcModel * model, int i){model->setLogLevel(i);}
//=========================================================================================================
//solvers
void solve(CbcModel *model, OsiClpSolverInterface *solver, int logLevel= 0){
CbcMain0(*model);
model->setLogLevel(logLevel);
const char * argv2[]={"driver3","-strong","5","-heuristicsOnOff","off","-presolve","off","-cutsOnOff","off","-primalS","-preprocess","off","-tune","100000000","-passc","1","-feas","off","-rins","off","-solve","-quit"};
CbcMain1(22,argv2,*model);
}
void solve_1(CbcModel *model, OsiClpSolverInterface *solver, int logLevel= 0){
// Set strategy - below is == CbcStrategyDefault()
OsiSolverInterface * solver2= solver;
CbcModel model1(*solver2);
/*model->assignSolver(solver2);
model->solver()->setIntParam(OsiNameDiscipline,1);
CbcStrategyDefault strategy(true,5,5);
model->setStrategy(strategy);
// Do complete search
model->setLogLevel(logLevel);
model->branchAndBound();*/
CbcStrategyDefault strategy;
model1.setStrategy(strategy);
model1.branchAndBound();
model->gutsOfCopy(model1, 2);
model->setProblemStatus(model1.status());
model->setSecondaryStatus(model1.secondaryStatus());
}
double round(double a,int n){
int temp = pow(10.,n);
double temp1 = a*temp;
double temp2 = temp1-floor(temp1);
if (temp2 <= 0.5)
return floor(temp1)/temp;
else
return ceil(temp1)/temp;
}
void cutoff(CbcModel *model, int n){
const CoinPackedMatrix *B = model->solver()->getMatrixByCol();
int nnz = B->getNumElements();
const double *el=B->getElements();
double *el_new = new double[nnz];
for (int i=0;i<nnz;i++){
el_new[i]=round(el[i],11);
}
CoinPackedMatrix *A=new CoinPackedMatrix(true,B->getMinorDim(),B->getMajorDim(),nnz,el_new,B->getIndices(),B->getVectorStarts(), B->getVectorLengths());
model->solver()->replaceMatrix(*A);
}
int solve_2(CbcModel *model, OsiClpSolverInterface *solver, int logLevel= 0){
solver->setHintParam(OsiDoDualInInitial,true,OsiHintTry);
//solver->setHintParam(OsiDoScale,true,OsiHintTry);
solver->setLogLevel(logLevel);
//int n = solver->getNumCols();
//cout << "-----------NumVars = " << n << " --------------\n";
//std::string * Names = new std::string[n];
//double time = CoinCpuTime();
OsiClpSolverInterface temp = OsiClpSolverInterface(*solver);
OsiSolverInterface *solver2 = solver;
CglPreProcess *process = new CglPreProcess;
process->messageHandler()->setLogLevel(logLevel);
solver2 = process->preProcess(*solver,false,2);
//model.assignSolver(solver2);
if(solver2)
cout << " ";
else{
//model most probably infeasible but double check!
OsiSolverInterface *temp1= &temp;
CbcModel model1(*temp1);
model1.branchAndBound();
if (model1.isProvenInfeasible()){
//model->gutsOfCopy(model1, 2);
model->setProblemStatus(0);
model->setSecondaryStatus(1);
cout << " \nmodel infeasible\n";
delete process;
return 0;
}
if (model1.isProvenOptimal()) {
*solver=OsiClpSolverInterface(temp);
const double * sol = model1.getColSolution();
double Obj = model1.getObjValue();
int nCols = model1.getNumCols();
model->setBestSolution(sol, nCols,Obj,true);
model->gutsOfCopy(model1, 2);
model->setProblemStatus(0);
model->setSecondaryStatus(0);
delete process;
//cout << "=====================" <<model->isProvenInfeasible() << " =======" << model->isProvenOptimal() <<endl;
return 1;
}
}
//solver2->initialSolve();
//solver2->resolve();
CbcModel model1(*solver2);
//model1.solver()->setIntParam(CbcOsiParam::DUALBOUND,1000000);
model1.solver()->setHintParam(OsiDoDualInInitial,true,OsiHintTry);
//model1.solver()->setHintParam(OsiDoScale,true,OsiHintTry);
model1.setLogLevel(logLevel);
model1.messageHandler()->setLogLevel(logLevel);
model1.solver()->messageHandler()->setLogLevel(logLevel);
model1.setMaximumNodes(model->getMaximumNodes());
model1.setThreadMode(model->getThreadMode());
model1.setNumberThreads(model->getNumberThreads());
model1.setIntegerTolerance(model->getIntegerTolerance());
//CbcMain0(model1);
//model1.solver()->initialSolve();
model1.initialSolve();
//================================================================================================================
/*
CglProbing generator1;
generator1.setUsingObjective(true);
generator1.setMaxPass(1);
generator1.setMaxPassRoot(5);
// Number of unsatisfied variables to look at
generator1.setMaxProbe(10);
generator1.setMaxProbeRoot(1000);
// How far to follow the consequences
generator1.setMaxLook(50);
generator1.setMaxLookRoot(500);
// Only look at rows with fewer than this number of elements
generator1.setMaxElements(200);
generator1.setRowCuts(3);
CglGomory generator2;
// try larger limit
generator2.setLimit(300);
CglKnapsackCover generator3;
CglRedSplit generator4;
// try larger limit
generator4.setLimit(200);
CglClique generator5;
generator5.setStarCliqueReport(false);
generator5.setRowCliqueReport(false);
CglMixedIntegerRounding2 mixedGen;
CglFlowCover flowGen;
CglGMI cut1;
CglMixedIntegerRounding2 cut2;
CglOddHole cut3;
CglSimpleRounding cut4;
CglResidualCapacity cut5;
CglTwomir cut6;
CglZeroHalf cut7;
// Add in generators
// Experiment with -1 and -99 etc
model1.addCutGenerator(&generator1,-1,"Probing");
model1.addCutGenerator(&generator2,-1,"Gomory");
model1.addCutGenerator(&generator3,-1,"Knapsack");
model1.addCutGenerator(&generator4,-1,"RedSplit");
model1.addCutGenerator(&generator5,-1,"Clique");
model1.addCutGenerator(&flowGen,-1,"FlowCover");
model1.addCutGenerator(&mixedGen,-1,"MixedIntegerRounding");
model1.addCutGenerator(&cut1,-1,"GMI");
model1.addCutGenerator(&cut2,-1,"MixedIntegerRounding2");
model1.addCutGenerator(&cut3,-1,"OddHole");
model1.addCutGenerator(&cut4,-1,"SimpleRounding");
model1.addCutGenerator(&cut5,-1,"ResidualCapacity");
model1.addCutGenerator(&cut6,-1,"Twomir");
model1.addCutGenerator(&cut7,-1,"ZeroHalf");
*/
// Uncommenting this should switch off all CBC messages
// model1.messagesPointer()->setDetailMessages(10,10000,NULL);
// Allow rounding heuristic
//CbcRounding heuristic1(model1);
//CbcHeuristicLocal heuristic2(model1);
//CbcHeuristicDiveLineSearch heuristic3(model1);
//CbcHeuristicDivePseudoCost heuristic4(model1);
//CbcHeuristicDiveVectorLength heuristic5(model1);
//CbcHeuristicDW heuristic6(model1);
//CbcHeuristicLocal heuristic8(model1);
//CbcHeuristicPivotAndFix heuristic9(model1);
//CbcHeuristicRENS heuristic10(model1);
//CbcHeuristicVND heuristic11(model1);
//model1.addHeuristic(&heuristic1);
//model1.addHeuristic(&heuristic2);
//model1.addHeuristic(&heuristic3);
//model1.addHeuristic(&heuristic4);
//model1.addHeuristic(&heuristic5);
//model1.addHeuristic(&heuristic6);
//model1.addHeuristic(&heuristic8);
//model1.addHeuristic(&heuristic9);
//model1.addHeuristic(&heuristic10);
//model1.addHeuristic(&heuristic11);
// Do initial solve to continuous
//model1.initialSolve();
// Could tune more
double objValue = model1.solver()->getObjSense()*model1.solver()->getObjValue();
double minimumDropA=CoinMin(1.0,fabs(objValue)*1.0e-3+1.0e-4);
double minimumDrop= fabs(objValue)*1.0e-4+1.0e-4;
//printf("min drop %g (A %g)\n",minimumDrop,minimumDropA);
model1.setMinimumDrop(minimumDrop);
model1.setMaximumCutPassesAtRoot(50); // use minimum drop
model1.setMaximumCutPasses(1000);
// Switch off strong branching if wanted
// model1.setNumberStrong(0);
// Do more strong branching if small
model1.setNumberStrong(8);
model1.setNumberBeforeTrust(5);
model1.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
model1.messageHandler()->setLogLevel(logLevel);
model1.solver()->messageHandler()->setLogLevel(logLevel);
// Default strategy will leave cut generators as they exist already
// so cutsOnlyAtRoot (1) ignored
// numberStrong (2) is 5 (default)
// numberBeforeTrust (3) is 5 (default is 0)
// printLevel (4) defaults (0)
//CbcStrategyDefault strategy(5);
// strategy.setupPreProcessing(2);
//model1.setStrategy(strategy);
model1.setLogLevel(logLevel);
model1.findCliques(true,10,20);
model1.setTypePresolve(2);
//model1.setSpecialOptions(4);
model1.setMoreSpecialOptions(16777216);
model1.setMoreSpecialOptions(32768);
model1.setMoreSpecialOptions(1024);
model1.setMoreSpecialOptions2(1);
model1.setMoreSpecialOptions2(128);
//model1.setMoreSpecialOptions2(8192);
//================================================================================================================
//CbcTreeLocal localTree(&model,model.solver()->getColSolution(),10,0,50,10000,2000);
//model.passInTreeHandler(localTree);
//CbcStrategyDefault strategy;
//model1.setStrategy(strategy);
model1.branchAndBound();
OsiSolverInterface * solver3;
process->postProcess(*model1.solver());
solver3 = solver;
model->gutsOfCopy(model1, 2);
model->setProblemStatus(model1.status());
model->setSecondaryStatus(model1.secondaryStatus());
//CbcModel *temp = model;
//temp = model;
//model1.assignSolver(solver3);
//model = model1.clone(true);
//model = model1;
//model1.gutsOfDestructor();
if (model1.isProvenOptimal()){
model->setProblemStatus(0);
model->setSecondaryStatus(0);
}
if(model1.isProvenInfeasible()){
model->setProblemStatus(0);
model->setSecondaryStatus(1);
}
delete process;
return 2;
}
int solve_3(CbcModel *model, OsiClpSolverInterface *solver, int logLevel= 0,double presolve_tolerance=1e-07){
model->setLogLevel(logLevel);
solver->setLogLevel(logLevel);
ClpSimplex *simplex = solver->getModelPtr();
ClpPresolve pinfo;
double time_2 = CoinCpuTime();
ClpSimplex * simplex2 = pinfo.presolvedModel(*simplex,presolve_tolerance,true,5,false,false);
double time_3 = CoinCpuTime() - time_2;
//cout << "Presolve= "<< time_3 << "\n";
int status = 0;
if(simplex2){
simplex2->setLogLevel(logLevel);
status = simplex2->status();
}
if(!simplex2 || status == 1 || status == 2){
model->setProblemStatus(0);
model->setSecondaryStatus(1);
cout << " \nmodel infeasible\n";
return 1;
}
OsiClpSolverInterface solver2(simplex2);
solver2.messageHandler()->setLogLevel(logLevel);
CbcModel model1(solver2);
model1.setLogLevel(logLevel);
model1.solver()->messageHandler()->setLogLevel(logLevel);
model1.setMaximumNodes(model->getMaximumNodes());
model1.setThreadMode(model->getThreadMode());
model1.setNumberThreads(model->getNumberThreads());
model1.setIntegerTolerance(model->getIntegerTolerance());
double time = CoinCpuTime();
//model1.setLogLevel(logLevel);
//CbcMain0(model1);
model1.setLogLevel(logLevel);
//const char * argv2[]={"driver3","-strong","0","-passC","1","-rins","off" ,"-feas","off","-tune","1000000","-solve","-quit"};
//CbcMain1(13,argv2,model1);
//CbcStrategyDefault strategy;
//model1.setStrategy(strategy);
model1.branchAndBound();
double time1 = CoinCpuTime() - time;
//cout << "Branch and bound time = "<< time1<< endl;
OsiClpSolverInterface * clpSolver = dynamic_cast<OsiClpSolverInterface *> (model1.solver());
assert (clpSolver);
clpSolver->setLogLevel(logLevel);
ClpSimplex * clp = clpSolver->getModelPtr();
clp->setLogLevel(logLevel);
*simplex2 = *clp;
double time4 = CoinCpuTime();
simplex2->checkSolution();
pinfo.postsolve(true);
//cout << "postsolve" << CoinCpuTime() - time4;
const int * original = pinfo.originalColumns();
double * lower2 = simplex2->columnLower();
double * upper2 = simplex2->columnUpper();
const char * info2 = simplex2->integerInformation();
double * lower = simplex->columnLower();
double * upper = simplex->columnUpper();
int i;
for (i=0;i<simplex2->numberColumns();i++) {
// if (info2[i]) {
int iSeq = original[i];
upper[iSeq]=upper2[i];
lower[iSeq]=lower2[i];
//
}
//simplex->dual();
model->gutsOfCopy(model1,2);
model->setProblemStatus(model1.status());
model->setSecondaryStatus(model1.secondaryStatus());
if (model1.solver()->isProvenOptimal()){
model->setProblemStatus(0);
model->setSecondaryStatus(0);
}
else if(model1.isProvenInfeasible()){
model->setProblemStatus(0);
model->setSecondaryStatus(1);
}
delete simplex2;
return 0;
}
//============================================================================================================================
// OsiClpSolverInterface functions
void initialSolve(OsiClpSolverInterface *solver){solver->initialSolve();}
void setLogLevel(OsiClpSolverInterface *solver, int i){solver->setLogLevel(i);}
int isInteger(OsiClpSolverInterface *solver, int i){return solver->isInteger(i); }
int isBinary(OsiClpSolverInterface *solver, int i){return solver->isBinary(i); }
// All these methods can also be done using OsiClpSolverInterface, with CbcModel it will probably take longer time.
const double * getSol(CbcModel *model){return model->solver()->getColSolution();}
int isInteger(CbcModel *model, int i){return model->isInteger(i); }
void addRows(CbcModel *model, CoinModel *build){
OsiClpSolverInterface *solver1;
solver1=new OsiClpSolverInterface;
OsiSolverInterface * solver = solver1;
solver->loadFromCoinModel(*build);
model->assignSolver(solver);
delete[] solver, solver1;
}
void readMps(CbcModel *model, const char *name){model->solver()->readMps(name,"");}
void readLp(CbcModel *model, const char *name){model->solver()->readLp(name);}
void writeMps(CbcModel *model, const char *name){model->solver()->writeMps(name);}
void writeLp(CbcModel *model, const char *name){model->solver()->writeLp(name);}
void setInteger(CbcModel *model, int i){model->solver()->setInteger(i);}
//Names
std::string getRowName(OsiClpSolverInterface *solver, int i){return solver->getRowName(i);}
std::string getColName(OsiClpSolverInterface *solver, int i){return solver->getColName(i);}
std::string getRowName(CbcModel *model, int i){return model->solver()->getRowName(i);}
std::string getColName(CbcModel *model, int i){return model->solver()->getColName(i);}
void setModelName(OsiClpSolverInterface *solver, std::string name){solver->setStrParam(OsiProbName,name);}
std::string getModelName(OsiClpSolverInterface *solver){
std::string temp;
solver->getStrParam(OsiProbName,temp);
return temp;
}
//problem status
int status(CbcModel *model){return model->status();}//returns 0 if finished (which includes the case
//when the algorithm is finished because it has been proved infeasible),
//1 if stopped by user, and 2 if difficulties arose.
int isProvenOptimal(CbcModel *model){return model->isProvenOptimal();}
int isProvenInfeasible(CbcModel *model){return model->isProvenInfeasible();}
int secondaryStatus(CbcModel *model){return model->secondaryStatus();}
/* cbc secondary status of problem
-1 unset (status_ will also be -1)
0 search completed with solution
1 linear relaxation not feasible (or worse than cutoff)
2 stopped on gap
3 stopped on nodes
4 stopped on time
5 stopped on user event
6 stopped on solutions
7 linear relaxation unbounded*/
//problem stats
int getNumRows(OsiClpSolverInterface *solver){return solver->getNumRows();}
int getNumCols(OsiClpSolverInterface *solver){return solver->getNumCols();}
int getNumRows(CbcModel *model){return model->getNumRows();}
int getNumCols(CbcModel *model){return model->getNumCols();}
int numberIntegers(CbcModel *model){return model->numberIntegers();}
//solution
const double * getColSolution(OsiClpSolverInterface *solver){return solver->getColSolution();}
const double * getRowPrice(OsiClpSolverInterface *solver){return solver->getRowPrice();}
const double * getRowActivity(OsiClpSolverInterface *solver){return solver->getRowActivity();}
const double * getReducedCost(OsiClpSolverInterface *solver){return solver->getReducedCost();}
const double * getColSolution(CbcModel *model){return model->bestSolution();}
const double * getRowPrice(CbcModel *model){return model->getRowPrice();}
const double * getRowActivity(CbcModel *model){return model->getRowActivity();}
const double * getReducedCost(CbcModel *model){return model->getReducedCost();}
const double getObjValue(CbcModel *model){return model->solver()->getObjValue();}
const double getObjValue(OsiClpSolverInterface *solver){return solver->getObjValue();}
//time
double getCoinCpuTime(){return CoinCpuTime();}
//CoinPackedMatrix
void loadProblem(OsiClpSolverInterface *solver, CoinPackedMatrix *byRow, double columnLower[], double columnUpper[],
double objective[], double rowLower[], double rowUpper[]){
solver->loadProblem(*byRow,columnLower, columnUpper, objective,rowLower, rowUpper);
}
CoinPackedMatrix * createMatrix( int numberColumns, int numberRows, int numberElements ,
double elementByRow[], int column[], int rowStart[]){
CoinPackedMatrix * M = new CoinPackedMatrix(false, numberColumns, numberRows,
numberElements ,elementByRow, (const int*)column, (const int*)rowStart, NULL);
return M;
}
void setRowName(OsiClpSolverInterface *solver, int i , const char *name){
solver->setRowName(i,name);
}
//tolerances
void setPrimalTolerance(CbcModel *model, double a){
model->solver()->setDblParam(OsiPrimalTolerance, a);
}
void setDualTolerance(CbcModel *model, double a){
model->solver()->setDblParam(OsiDualTolerance, a);
}
void setIntegerTolerance(CbcModel *model, double a){
model->setIntegerTolerance(a);
}
void test(std::pair<std::string, int> a){
cout << a.first << " + " << a.second;
}
void test1(std::vector<std::pair<std::string, int> > a){
cout << a.at(0).first << " + " << a.at(1).second;
}
int solve_whs(CbcModel *model, OsiClpSolverInterface *solver, std::string names[], int values[],int intvars, int logLevel= 0,double presolve_tolerance=1e-07){
// try{
//std::vector< std::pair< std::string, double > > A;
model->setLogLevel(logLevel);
solver->setLogLevel(logLevel);
ClpSimplex *simplex = solver->getModelPtr();
int numCols = solver->getNumCols();
ClpPresolve pinfo;
ClpSimplex * simplex2 = pinfo.presolvedModel(*simplex,presolve_tolerance,true,5,false,false);
int status = 0;
if(simplex2){
simplex2->initialSolve();
simplex2->setLogLevel(logLevel);
status = simplex2->status();
}
if(!simplex2 || status == 1 || status == 2){
model->setProblemStatus(0);
model->setSecondaryStatus(1);
cout << " \nmodel infeasible\n";
return 1;
}
bool whs = false;
const double * sol0 = NULL;
ClpSimplex *simplex_cpy = new ClpSimplex(*simplex2);
OsiClpSolverInterface solver1(simplex_cpy);
double * obj = simplex2->objective();
double obj_val = 0.0;
// int priority[solver1.getNumCols()];
// for (int i=0;i<solver1.getNumIntegers();i++)
// priority[i]=0;
if (intvars > 0){
const double * UB = solver1.getColUpper();
const double * LB = solver1.getColLower();
for (int i=0;i<solver1.getNumCols();i++){
solver1.setObjCoeff(i,0);
if (solver1.isInteger(i)){
for (int k=0;k<intvars;k++){
if (solver1.getColName(i) == names[k]){
if (UB[i]>LB[i]){
solver1.setColLower(i,values[k]);
solver1.setColUpper(i,values[k]);
//if (values[k]==1)
// priority[i]=-1;
//else
// priority[i]=1;
}
}
}
}
}
// CbcModel model2(solver1);
//model2.setIntParam(CbcModel::CbcMaxNumSol,1);
solver1.setLogLevel(0);
solver1.messageHandler()->setLogLevel(0);