source: trunk/Bonmin/src/Algorithms/BonSubMipSolver.cpp @ 1410

Last change on this file since 1410 was 1410, checked in by pbonami, 11 years ago

Add MSVisualStudio dir

File size: 9.2 KB
Line 
1// (C) Copyright International Business Machines (IBM) 2006
2// All Rights Reserved.
3// This code is published under the Common Public License.
4//
5// Authors :
6// P. Bonami, International Business Machines
7//
8// Date :  12/07/2006
9
10
11// Code separated from BonOaDecBase to try to clarify OAs
12#include "BonSubMipSolver.hpp"
13#include "CbcModel.hpp"
14#include "CbcStrategy.hpp"
15#include "OsiAuxInfo.hpp"
16#include "OsiClpSolverInterface.hpp"
17
18#ifdef COIN_HAS_CPX
19#include "OsiCpxSolverInterface.hpp"
20#endif
21
22#include "BonRegisteredOptions.hpp"
23
24namespace Bonmin {
25  /** Constructor */
26  SubMipSolver::SubMipSolver(OsiSolverInterface * lp,
27      const CbcStrategy * strategy):
28      lp_(lp),
29      clp_(NULL),
30      cpx_(NULL),
31      cbc_(NULL),
32      lowBound_(-COIN_DBL_MAX),
33      optimal_(false),
34      integerSolution_(NULL),
35      strategy_(NULL)
36  {
37    clp_ = (lp_ == NULL)? NULL :
38        dynamic_cast<OsiClpSolverInterface *>(lp_);
39#ifdef COIN_HAS_CPX
40    cpx_ = (lp_ == NULL)? NULL :
41        dynamic_cast<OsiCpxSolverInterface *>(lp_);
42#endif
43    if (strategy) strategy_ = strategy->clone();
44  }
45  SubMipSolver::~SubMipSolver()
46  {
47    if (strategy_) delete strategy_;
48    if (integerSolution_) delete [] integerSolution_;
49    if (cbc_) delete cbc_;
50  }
51
52  /** Assign lp solver. */
53  void
54  SubMipSolver::setLpSolver(OsiSolverInterface * lp)
55  {
56    lp_ = lp;
57    clp_ = (lp_ == NULL) ? NULL :
58        dynamic_cast<OsiClpSolverInterface *>(lp_);
59#ifdef COIN_HAS_CPX
60    cpx_ = (lp_ == NULL) ? NULL :
61        dynamic_cast<OsiCpxSolverInterface *>(lp_);
62#endif
63    lowBound_ = -COIN_DBL_MAX;
64    optimal_ = false;
65    if (integerSolution_) {
66      delete [] integerSolution_;
67      integerSolution_ = NULL;
68    }
69  }
70
71
72 void 
73 SubMipSolver::find_good_sol(double cutoff, int loglevel, double max_time,
74                                    int max_node){
75
76     if(clp_){
77      if (!strategy_){
78        CbcStrategyDefault * strat = new CbcStrategyDefault(1,0,0, loglevel);
79        strat->setupPreProcessing();
80        strategy_ = strat;
81      }
82      OsiBabSolver empty;
83      if (cbc_) delete cbc_;
84      cbc_ = new CbcModel(*clp_);
85      cbc_->solver()->setAuxiliaryInfo(&empty);
86
87      //Change Cbc messages prefixes
88      strcpy(cbc_->messagesPointer()->source_,"OaCbc");
89
90      cbc_->setLogLevel(loglevel);
91      cbc_->solver()->messageHandler()->setLogLevel(0);
92      clp_->resolve();
93      cbc_->setStrategy(*strategy_);
94      cbc_->setLogLevel(loglevel);
95      cbc_->solver()->messageHandler()->setLogLevel(0);
96      cbc_->setMaximumNodes(max_node);
97      cbc_->setMaximumSeconds(max_time);
98      cbc_->setMaximumSolutions(1);
99      cbc_->setCutoff(cutoff);
100
101      cbc_->solver()->writeMpsNative("FP.mps", NULL, NULL, 1);
102     
103      cbc_->branchAndBound();
104      lowBound_ = cbc_->getBestPossibleObjValue();
105
106      if (cbc_->isProvenOptimal() || cbc_->isProvenInfeasible())
107        optimal_ = true;
108      else optimal_ = false;
109
110      if (cbc_->getSolutionCount()) {
111        if (!integerSolution_)
112          integerSolution_ = new double[lp_->getNumCols()];
113        CoinCopyN(cbc_->bestSolution(), lp_->getNumCols(), integerSolution_);
114      }
115      else if (integerSolution_) {
116        delete [] integerSolution_;
117        integerSolution_ = NULL;
118      }
119      nodeCount_ = cbc_->getNodeCount();
120      iterationCount_ = cbc_->getIterationCount();
121
122     }
123     else if (cpx_){
124#ifndef COIN_HAS_CPX
125        throw CoinError("Unsuported solver, for local searches you should use clp or cplex",
126            "performLocalSearch",
127            "OaDecompositionBase::SubMipSolver");
128#else
129        CPXENVptr env = cpx_->getEnvironmentPtr();
130        CPXLPptr cpxlp = cpx_->getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ALL);
131        CPXsetintparam(env, CPX_PARAM_NODELIM, max_node);
132        CPXsetdblparam(env, CPX_PARAM_TILIM, max_time);
133        CPXsetdblparam(env, CPX_PARAM_CUTUP, cutoff);
134
135        CPXsetintparam(env,CPX_PARAM_INTSOLLIM, 10000);
136        CPXsetintparam(env,CPX_PARAM_NODELIM, 100000);
137        CPXsetdblparam(env,CPX_PARAM_TILIM, max_time);
138
139        cpx_->branchAndBound();
140
141        int status = CPXgetbestobjval(env, cpxlp, &lowBound_);
142     
143        int stat = CPXgetstat( env, cpxlp);
144        optimal_ |= (stat == CPXMIP_INFEASIBLE); 
145        nodeCount_ = CPXgetnodecnt(env , cpxlp);
146        iterationCount_ = CPXgetmipitcnt(env , cpxlp);
147
148        CPXsetintparam(env, CPX_PARAM_INTSOLLIM, 1);
149        CPXsetintparam(env,CPX_PARAM_NODELIM, 1000);
150        while(stat == CPXMIP_NODE_LIM_INFEAS){
151           cpx_->branchAndBound();
152           int stat = CPXgetstat( env, cpxlp);
153           optimal_ |= (stat == CPXMIP_INFEASIBLE);
154           nodeCount_ = CPXgetnodecnt(env , cpxlp);
155           iterationCount_ = CPXgetmipitcnt(env , cpxlp);
156        }
157     
158        if (status)
159          throw CoinError("Error in getting some CPLEX information",
160                          "OaDecompositionBase::SubMipSolver",
161                          "performLocalSearch");
162#endif
163    }
164  }
165
166  void
167  SubMipSolver::optimize(double cutoff, int loglevel, double maxTime,
168      int maxNodes)
169  {
170    if (clp_) {
171      if (!strategy_){
172        CbcStrategyDefault * strat = new CbcStrategyDefault(1,0,0, loglevel);
173        strat->setupPreProcessing();
174        strategy_ = strat;
175      }
176
177      OsiBabSolver empty;
178      if (cbc_) delete cbc_;
179      cbc_ = new CbcModel(*clp_);
180      cbc_->solver()->setAuxiliaryInfo(&empty);
181
182      //Change Cbc messages prefixes
183      strcpy(cbc_->messagesPointer()->source_,"OaCbc");
184
185      cbc_->setLogLevel(loglevel);
186      cbc_->solver()->messageHandler()->setLogLevel(0);
187      clp_->resolve();
188      cbc_->setStrategy(*strategy_);
189      cbc_->setLogLevel(loglevel);
190      cbc_->solver()->messageHandler()->setLogLevel(0);
191      cbc_->setMaximumNodes(maxNodes);
192      cbc_->setMaximumSeconds(maxTime);
193      cbc_->setCutoff(cutoff);
194
195      cbc_->solver()->writeMpsNative("FP.mps", NULL, NULL, 1);
196      cbc_->branchAndBound();
197      lowBound_ = cbc_->getBestPossibleObjValue();
198
199      if (cbc_->isProvenOptimal() || cbc_->isProvenInfeasible())
200        optimal_ = true;
201      else optimal_ = false;
202
203      if (cbc_->getSolutionCount()) {
204        if (!integerSolution_)
205          integerSolution_ = new double[lp_->getNumCols()];
206        CoinCopyN(cbc_->bestSolution(), lp_->getNumCols(), integerSolution_);
207      }
208      else if (integerSolution_) {
209        delete [] integerSolution_;
210        integerSolution_ = NULL;
211      }
212      nodeCount_ = cbc_->getNodeCount();
213      iterationCount_ = cbc_->getIterationCount();
214    }
215    else {
216      lp_->messageHandler()->setLogLevel(loglevel);
217#ifdef COIN_HAS_CPX
218      if (cpx_) {
219        CPXENVptr env = cpx_->getEnvironmentPtr();
220        CPXsetintparam(env, CPX_PARAM_NODELIM, maxNodes);
221        CPXsetdblparam(env, CPX_PARAM_TILIM, maxTime);
222        CPXsetdblparam(env, CPX_PARAM_CUTUP, cutoff);
223        //CpxModel = cpx_;
224      }
225      else
226#endif
227     {
228        throw CoinError("Unsuported solver, for local searches you should use clp or cplex",
229            "performLocalSearch",
230            "OaDecompositionBase::SubMipSolver");
231    }
232
233    lp_->branchAndBound();
234
235    optimal_ = lp_->isProvenOptimal();
236#ifdef COIN_HAS_CPX
237    if (cpx_) {
238      //CpxModel = NULL;
239      CPXENVptr env = cpx_->getEnvironmentPtr();
240      CPXLPptr cpxlp = cpx_->getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ALL);
241
242      int status = CPXgetbestobjval(env, cpxlp, &lowBound_);
243     
244      int stat = CPXgetstat( env, cpxlp);
245      optimal_ |= (stat == CPXMIP_INFEASIBLE); 
246       nodeCount_ = CPXgetnodecnt(env , cpxlp);
247      iterationCount_ = CPXgetmipitcnt(env , cpxlp);
248      if (status)
249        throw CoinError("Error in getting some CPLEX information","OaDecompositionBase::SubMipSolver","performLocalSearch");
250    }
251#endif
252
253      if (lp_->getFractionalIndices().size() == 0) {
254        if (!integerSolution_)
255          integerSolution_ = new double[lp_->getNumCols()];
256        CoinCopyN(lp_->getColSolution(), lp_->getNumCols() , integerSolution_);
257      }
258      else if (integerSolution_) {
259        delete [] integerSolution_;
260        integerSolution_ = NULL;
261      }
262    }
263  }
264
265   /** Assign a strategy. */
266   void 
267   SubMipSolver::setStrategy(CbcStrategy * strategy)
268   {
269     if (strategy_) delete strategy_;
270     strategy_ = strategy->clone();
271   }
272
273  /** Register options.*/
274  void
275  SubMipSolver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions)
276  {
277    roptions->SetRegisteringCategory("Options for MILP solver", RegisteredOptions::BonminCategory);
278    roptions->AddStringOption3("milp_solver",
279        "Choose the subsolver to solve MILP sub-problems in OA decompositions.",
280        "Cbc_D",
281        "Cbc_D","Coin Branch and Cut with its default",
282        "Cbc_Par", "Coin Branch and Cut with passed parameters",
283        "Cplex","Ilog Cplex",
284        " To use Cplex, a valid license is required and you should have compiled OsiCpx in COIN-OR  (see Osi documentation).");
285    roptions->setOptionExtraInfo("milp_solver",5);
286
287    roptions->AddBoundedIntegerOption("milp_log_level",
288        "specify MILP solver log level.",
289        0,3,0,
290        "Set the level of output of the MILP subsolver in OA : "
291        "0 - none, 1 - minimal, 2 - normal low, 3 - normal high"
292                                     );
293    roptions->setOptionExtraInfo("milp_log_level",5);
294
295
296  }
297}/* Ends Bonmin namespace.*/
Note: See TracBrowser for help on using the repository browser.