-
Notifications
You must be signed in to change notification settings - Fork 117
/
pool.cpp
96 lines (85 loc) · 3.46 KB
/
pool.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
// Copyright (C) 2007, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#include <cassert>
#include <iomanip>
#include "CoinPragma.hpp"
#include "CbcModel.hpp"
#include "OsiClpSolverInterface.hpp"
#include "CbcSolver.hpp"
#include "CoinTime.hpp"
//#############################################################################
/************************************************************************
This main program shows how to take advantage of the standalone cbc in your program,
while still making major modifications.
First it reads in an integer model from an mps file
Then it initializes the integer model with cbc defaults
Then it calls CbcMain1 passing all parameters apart from first but with callBack to modify stuff
Finally it prints solution
*/
int main(int argc, const char *argv[])
{
OsiClpSolverInterface solver1;
// Read in model using argv[1]
// and assert that it is a clean model
std::string mpsFileName;
#if defined(SAMPLEDIR)
mpsFileName = SAMPLEDIR "/p0033.mps";
#else
if (argc < 2) {
fprintf(stderr, "Do not know where to find sample MPS files.\n");
exit(1);
}
#endif
if (argc >= 2)
mpsFileName = argv[1];
int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
if (numMpsReadErrors != 0) {
printf("%d errors reading MPS file\n", numMpsReadErrors);
return numMpsReadErrors;
}
// Tell solver to return fast if presolve or initial solve infeasible
solver1.getModelPtr()->setMoreSpecialOptions(3);
// Pass to Cbc initialize defaults
CbcModel modelA(solver1);
CbcSolverUsefulData solverData;
CbcModel *model = &modelA;
CbcMain0(modelA,solverData);
modelA.setMaximumSavedSolutions(5);
/* Now go into code for standalone solver
Could copy arguments and add -quit at end to be safe
but this will do
*/
if (argc > 2) {
CbcMain1(argc - 1, argv + 1, modelA,solverData);
} else {
const char *argv2[] = { "driver4", "-solve", "-quit" };
CbcMain1(3, argv2, modelA,solverData);
}
// Solver was cloned so get current copy
OsiSolverInterface *solver = model->solver();
// Print solution if finished (could get from model->bestSolution() as well
if (model->bestSolution()) {
int numberColumns = solver->getNumCols();
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
// do solutions
for (int solutionNumber = 0; solutionNumber < modelA.numberSavedSolutions();
solutionNumber++) {
const double *solution = modelA.savedSolution(solutionNumber);
std::cout << "-------------------------------------- solution "
<< solutionNumber + 1 << " objective " << modelA.savedSolutionObjective(solutionNumber)
<< std::endl;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
double value = solution[iColumn];
if (fabs(value) > 1.0e-7 && solver1.isInteger(iColumn))
std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver1.getModelPtr()->columnName(iColumn)
<< resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
}
std::cout << "--------------------------------------" << std::endl;
}
std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
} else {
std::cout << " No solution!" << std::endl;
}
return 0;
}