1 | // Copyright (C) 2005, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | #if defined(_MSC_VER) |
---|
4 | // Turn off compiler warning about long names |
---|
5 | # pragma warning(disable:4786) |
---|
6 | #endif |
---|
7 | |
---|
8 | #include <cassert> |
---|
9 | #include <iomanip> |
---|
10 | |
---|
11 | |
---|
12 | // For Branch and bound |
---|
13 | #include "CbcModel.hpp" |
---|
14 | #include "CbcStrategy.hpp" |
---|
15 | #include "OsiClpSolverInterface.hpp" |
---|
16 | // Preprocessing |
---|
17 | #include "CglPreProcess.hpp" |
---|
18 | |
---|
19 | #include "CoinTime.hpp" |
---|
20 | |
---|
21 | //############################################################################# |
---|
22 | |
---|
23 | |
---|
24 | /************************************************************************ |
---|
25 | |
---|
26 | This main program reads in an integer model from an mps file. |
---|
27 | It then uses default strategy - just cuts at root node |
---|
28 | |
---|
29 | ************************************************************************/ |
---|
30 | |
---|
31 | int main (int argc, const char *argv[]) |
---|
32 | { |
---|
33 | |
---|
34 | OsiClpSolverInterface solver1; |
---|
35 | |
---|
36 | // Read in model using argv[1] |
---|
37 | // and assert that it is a clean model |
---|
38 | std::string mpsFileName; |
---|
39 | #if defined(SAMPLEDIR) |
---|
40 | mpsFileName = SAMPLEDIR "/p0033.mps"; |
---|
41 | #else |
---|
42 | if (argc < 2) { |
---|
43 | fprintf(stderr, "Do not know where to find sample MPS files.\n"); |
---|
44 | exit(1); |
---|
45 | } |
---|
46 | #endif |
---|
47 | if (argc>=2) mpsFileName = argv[1]; |
---|
48 | int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),""); |
---|
49 | assert(numMpsReadErrors==0); |
---|
50 | double time1 = CoinCpuTime(); |
---|
51 | |
---|
52 | /* Options are: |
---|
53 | preprocess to do preprocessing |
---|
54 | time in minutes |
---|
55 | if 2 parameters and numeric taken as time |
---|
56 | */ |
---|
57 | bool preProcess=false; |
---|
58 | double minutes=-1.0; |
---|
59 | int nGoodParam=0; |
---|
60 | for (int iParam=2; iParam<argc;iParam++) { |
---|
61 | if (!strcmp(argv[iParam],"preprocess")) { |
---|
62 | preProcess=true; |
---|
63 | nGoodParam++; |
---|
64 | } else if (!strcmp(argv[iParam],"time")) { |
---|
65 | if (iParam+1<argc&&isdigit(argv[iParam+1][0])) { |
---|
66 | minutes=atof(argv[iParam+1]); |
---|
67 | if (minutes>=0.0) { |
---|
68 | nGoodParam+=2; |
---|
69 | iParam++; // skip time |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | if (nGoodParam==0&&argc==3&&isdigit(argv[2][0])) { |
---|
75 | // If time is given then stop after that number of minutes |
---|
76 | minutes = atof(argv[2]); |
---|
77 | if (minutes>=0.0) |
---|
78 | nGoodParam=1; |
---|
79 | } |
---|
80 | if (nGoodParam!=argc-2&&argc>=2) { |
---|
81 | printf("Usage <file> [preprocess] [time <minutes>] or <file> <minutes>\n"); |
---|
82 | exit(1); |
---|
83 | } |
---|
84 | // See if we want preprocessing |
---|
85 | OsiSolverInterface * solver2=&solver1; |
---|
86 | CglPreProcess process; |
---|
87 | if (preProcess) { |
---|
88 | /* Do not try and produce equality cliques and |
---|
89 | do up to 5 passes */ |
---|
90 | solver2 = process.preProcess(solver1,false,5); |
---|
91 | if (!solver2) { |
---|
92 | printf("Pre-processing says infeasible\n"); |
---|
93 | exit(2); |
---|
94 | } |
---|
95 | solver2->resolve(); |
---|
96 | } |
---|
97 | CbcModel model(*solver2); |
---|
98 | // If time is given then stop after that number of minutes |
---|
99 | if (minutes>=0.0) { |
---|
100 | std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl; |
---|
101 | model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes); |
---|
102 | } |
---|
103 | // Set strategy - below is == CbcStrategyDefault() |
---|
104 | CbcStrategyDefault strategy(true,5,0); |
---|
105 | model.setStrategy(strategy); |
---|
106 | // Do complete search |
---|
107 | |
---|
108 | model.branchAndBound(); |
---|
109 | |
---|
110 | std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, " |
---|
111 | <<model.getNodeCount()<<" nodes with objective " |
---|
112 | <<model.getObjValue() |
---|
113 | <<(!model.status() ? " Finished" : " Not finished") |
---|
114 | <<std::endl; |
---|
115 | |
---|
116 | // Print solution if finished - we can't get names from Osi! |
---|
117 | |
---|
118 | if (model.getMinimizationObjValue()<1.0e50) { |
---|
119 | // post process |
---|
120 | OsiSolverInterface * solver; |
---|
121 | if (preProcess) { |
---|
122 | process.postProcess(*model.solver()); |
---|
123 | // Solution now back in solver1 |
---|
124 | solver = & solver1; |
---|
125 | } else { |
---|
126 | solver = model.solver(); |
---|
127 | } |
---|
128 | int numberColumns = solver->getNumCols(); |
---|
129 | |
---|
130 | const double * solution = solver->getColSolution(); |
---|
131 | |
---|
132 | int iColumn; |
---|
133 | std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14); |
---|
134 | |
---|
135 | std::cout<<"--------------------------------------"<<std::endl; |
---|
136 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
137 | double value=solution[iColumn]; |
---|
138 | if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) |
---|
139 | std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl; |
---|
140 | } |
---|
141 | std::cout<<"--------------------------------------"<<std::endl; |
---|
142 | |
---|
143 | std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific); |
---|
144 | } |
---|
145 | return 0; |
---|
146 | } |
---|