1 //-----------------------------------------------------
2 // Simple example usage of the cut generation library.
3 //
4 // This sample program adds general disjunction cuts from
5 // Belotti et al. and resolves the relaxation problem to see
6 // whether the bound is improved.
7 //
8 // usage:
9 //   solve_with_gd1 mpsFileName
10 // example:
11 //   solve_with_mir ../../Data/Sample/p0033
12 //-----------------------------------------------------
13 
14 // STDLIB headers
15 #include <cassert>
16 #include <iostream>
17 #include <string>
18 #include <cassert>
19 #include <sstream>
20 // CoinUtils headers
21 #include <CoinError.hpp>
22 #include <CoinWarmStartBasis.hpp>
23 // OSI headers
24 #include <OsiCuts.hpp>
25 // OSICONIC header
26 #include <OsiConicSolverInterface.hpp>
27 #include <OsiConicCuts.hpp>
28 // COLA headers
29 #include <ColaModel.hpp>
30 
31 // get IPM solver
32 #if defined(__OSI_MOSEK__)
33   // use mosek as IPM solver
34   #include <OsiMosekSolverInterface.hpp>
35   //#define IPM_SOLVER OsiMosekSolverInterface
36   typedef OsiMosekSolverInterface IPM_SOLVER;
37 #elif defined(__OSI_CPLEX__)
38   // use cplex as IPM solver
39   #include <OsiCplexSolverInterface.hpp>
40   typedef OsiCplexSolverInterface IPM_SOLVER;
41 #elif defined(__OSI_IPOPT__)
42   // use ipopt
43   #include <OsiIpoptSolverInterface.hpp>
44   typedef OsiIpoptSolverInterface IPM_SOLVER;
45 #endif
46 
47 // CGL headers
48 // #include "CglKnapsackCover.hpp"
49 // #include "CglSimpleRounding.hpp"
50 // #include "CglGMI.hpp"
51 // #include "CglGomory.hpp"
52 // #include "CglMixedIntegerRounding.hpp"
53 // #include "CglMixedIntegerRounding2.hpp"
54 // Conic CGL headers
55 #include "CglConicGD1.hpp"
56 
57 using std::cerr;
58 using std::cout;
59 using std::endl;
60 using std::string;
61 
main(int argc,const char * argv[])62 int main(int argc, const char *argv[]) {
63   // If no parms specified then use these
64   string mpsFileName = argv[1];
65   string baseName = mpsFileName.substr(mpsFileName.rfind("/")+1,
66         mpsFileName.rfind(".")-mpsFileName.rfind("/")-1);
67   try {
68     // Instantiate a specific solver interface
69     //OsiConicSolverInterface * si = new ColaModel();
70     OsiConicSolverInterface * si = new IPM_SOLVER();
71     // Read file describing problem
72     si->readMps(mpsFileName.c_str(),"mps");
73     // Solve continuous problem
74     si->initialSolve();
75     // Save the orig socp relaxation value for
76     // comparisons later
77     double origSocpObj = si->getObjValue();
78     // Instantiate cut generator
79     CglConicGD1 cg(si);
80     bool equalObj;
81     CoinRelFltEq eq(0.0001);
82     int num_cut = 0;
83     double obj;
84     //---------------------------------------------------
85     // Keep applying cuts until no more cuts are generated
86     //---------------------------------------------------
87     do {
88       // Get current solution value
89       obj = si->getObjValue();
90       std::cout << "Obj value is " << obj << std::endl;
91       // Generate and apply cuts
92       //OsiConicSolverInterface * nsi = cg.generateAndAddBestCut(*si);
93       OsiConicSolverInterface * nsi = cg.generateAndAddCuts(*si);
94       delete si;
95       si = nsi;
96       std::stringstream msg_stream;
97       //msg_stream << baseName << "dc" << cg.getNumCutsAdded();
98       msg_stream << baseName;
99       si->writeMps(msg_stream.str().c_str());
100       msg_stream.str();
101       si->resolve();
102       equalObj = eq(si->getObjValue(), obj);
103       break;
104     } while (!equalObj);
105     // double const * sol = si->getColSolution();
106     // Print total number of cuts applied,
107     // and total improvement in the SOCP objective value
108     cout <<endl <<endl;
109     cout << "----------------------------------------------------------"
110          <<endl;
111     cout << "Cut generation phase completed:" <<endl;
112     cout << "   " << cg.getNumCutsAdded() << " many cuts added." << endl;
113     cout << "   changing the SOCP objective value from " << origSocpObj
114          << " to " << si->getObjValue() <<endl;
115     cout << "----------------------------------------------------------"
116          <<endl;
117     cout <<endl <<endl;
118     // write mps file
119     si->writeMps("after_cuts", "mps", 0.0);
120   delete si;
121   }
122   catch (CoinError e) {
123     cout << e.className() << "::" << e.methodName() << " - " << e.message() << endl;
124   }
125   return 0;
126 }
127