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

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

Remove debug output

File size: 9.5 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      CbcStrategyDefault * strat_default = NULL;
78      if (!strategy_){
79        strat_default = new CbcStrategyDefault(1,5,5, loglevel);
80        strat_default->setupPreProcessing();
81        strategy_ = strat_default;
82      }
83      OsiBabSolver empty;
84      if (cbc_) delete cbc_;
85      cbc_ = new CbcModel(*clp_);
86      cbc_->solver()->setAuxiliaryInfo(&empty);
87
88      //Change Cbc messages prefixes
89      strcpy(cbc_->messagesPointer()->source_,"OaCbc");
90
91      cbc_->setLogLevel(loglevel);
92      cbc_->solver()->messageHandler()->setLogLevel(0);
93      clp_->resolve();
94      cbc_->setStrategy(*strategy_);
95      cbc_->setLogLevel(loglevel);
96      cbc_->solver()->messageHandler()->setLogLevel(0);
97      cbc_->setMaximumNodes(max_node);
98      cbc_->setMaximumSeconds(max_time);
99      cbc_->setMaximumSolutions(1);
100      cbc_->setCutoff(cutoff);
101
102      //cbc_->solver()->writeMpsNative("FP.mps", NULL, NULL, 1);
103     
104      cbc_->branchAndBound();
105      lowBound_ = cbc_->getBestPossibleObjValue();
106
107      if (cbc_->isProvenOptimal() || cbc_->isProvenInfeasible())
108        optimal_ = true;
109      else optimal_ = false;
110
111      if (cbc_->getSolutionCount()) {
112        if (!integerSolution_)
113          integerSolution_ = new double[lp_->getNumCols()];
114        CoinCopyN(cbc_->bestSolution(), lp_->getNumCols(), integerSolution_);
115      }
116      else if (integerSolution_) {
117        delete [] integerSolution_;
118        integerSolution_ = NULL;
119      }
120      nodeCount_ = cbc_->getNodeCount();
121      iterationCount_ = cbc_->getIterationCount();
122
123      if(strat_default != NULL){
124        delete strat_default;
125        strategy_ = NULL;
126      }
127     }
128     else if (cpx_){
129#ifndef COIN_HAS_CPX
130        throw CoinError("Unsuported solver, for local searches you should use clp or cplex",
131            "performLocalSearch",
132            "OaDecompositionBase::SubMipSolver");
133#else
134        CPXENVptr env = cpx_->getEnvironmentPtr();
135        CPXLPptr cpxlp = cpx_->getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ALL);
136        CPXsetintparam(env, CPX_PARAM_NODELIM, max_node);
137        CPXsetdblparam(env, CPX_PARAM_TILIM, max_time);
138        CPXsetdblparam(env, CPX_PARAM_CUTUP, cutoff);
139
140        CPXsetintparam(env,CPX_PARAM_INTSOLLIM, 10000);
141        CPXsetintparam(env,CPX_PARAM_NODELIM, 100000);
142        CPXsetdblparam(env,CPX_PARAM_TILIM, max_time);
143
144        cpx_->branchAndBound();
145
146        int status = CPXgetbestobjval(env, cpxlp, &lowBound_);
147     
148        int stat = CPXgetstat( env, cpxlp);
149        optimal_ |= (stat == CPXMIP_INFEASIBLE); 
150        nodeCount_ = CPXgetnodecnt(env , cpxlp);
151        iterationCount_ = CPXgetmipitcnt(env , cpxlp);
152
153        CPXsetintparam(env, CPX_PARAM_INTSOLLIM, 1);
154        CPXsetintparam(env,CPX_PARAM_NODELIM, 1000);
155        while(stat == CPXMIP_NODE_LIM_INFEAS){
156           cpx_->branchAndBound();
157           int stat = CPXgetstat( env, cpxlp);
158           optimal_ |= (stat == CPXMIP_INFEASIBLE);
159           nodeCount_ = CPXgetnodecnt(env , cpxlp);
160           iterationCount_ = CPXgetmipitcnt(env , cpxlp);
161        }
162     
163        if (status)
164          throw CoinError("Error in getting some CPLEX information",
165                          "OaDecompositionBase::SubMipSolver",
166                          "performLocalSearch");
167#endif
168    }
169  }
170
171  void
172  SubMipSolver::optimize(double cutoff, int loglevel, double maxTime,
173      int maxNodes)
174  {
175    if (clp_) {
176      CbcStrategyDefault * strat_default = NULL;
177      if (!strategy_){
178        strat_default = new CbcStrategyDefault(1,5,5, loglevel);
179        strat_default->setupPreProcessing();
180        strategy_ = strat_default;
181      }
182
183      OsiBabSolver empty;
184      if (cbc_) delete cbc_;
185      cbc_ = new CbcModel(*clp_);
186      cbc_->solver()->setAuxiliaryInfo(&empty);
187
188      //Change Cbc messages prefixes
189      strcpy(cbc_->messagesPointer()->source_,"OaCbc");
190
191      cbc_->setLogLevel(loglevel);
192      cbc_->solver()->messageHandler()->setLogLevel(0);
193      clp_->resolve();
194      cbc_->setStrategy(*strategy_);
195      cbc_->setLogLevel(loglevel);
196      cbc_->solver()->messageHandler()->setLogLevel(0);
197      cbc_->setMaximumNodes(maxNodes);
198      cbc_->setMaximumSeconds(maxTime);
199      cbc_->setCutoff(cutoff);
200
201      //cbc_->solver()->writeMpsNative("FP.mps", NULL, NULL, 1);
202      cbc_->branchAndBound();
203      lowBound_ = cbc_->getBestPossibleObjValue();
204
205      if (cbc_->isProvenOptimal() || cbc_->isProvenInfeasible())
206        optimal_ = true;
207      else optimal_ = false;
208
209      if (cbc_->getSolutionCount()) {
210        if (!integerSolution_)
211          integerSolution_ = new double[lp_->getNumCols()];
212        CoinCopyN(cbc_->bestSolution(), lp_->getNumCols(), integerSolution_);
213      }
214      else if (integerSolution_) {
215        delete [] integerSolution_;
216        integerSolution_ = NULL;
217      }
218      nodeCount_ = cbc_->getNodeCount();
219      iterationCount_ = cbc_->getIterationCount();
220      if(strat_default != NULL){
221        delete strat_default;
222        strategy_ = NULL;
223      }
224    }
225    else {
226      lp_->messageHandler()->setLogLevel(loglevel);
227#ifdef COIN_HAS_CPX
228      if (cpx_) {
229        CPXENVptr env = cpx_->getEnvironmentPtr();
230        CPXsetintparam(env, CPX_PARAM_NODELIM, maxNodes);
231        CPXsetdblparam(env, CPX_PARAM_TILIM, maxTime);
232        CPXsetdblparam(env, CPX_PARAM_CUTUP, cutoff);
233        //CpxModel = cpx_;
234      }
235      else
236#endif
237     {
238        throw CoinError("Unsuported solver, for local searches you should use clp or cplex",
239            "performLocalSearch",
240            "OaDecompositionBase::SubMipSolver");
241    }
242
243    lp_->branchAndBound();
244
245    optimal_ = lp_->isProvenOptimal();
246#ifdef COIN_HAS_CPX
247    if (cpx_) {
248      //CpxModel = NULL;
249      CPXENVptr env = cpx_->getEnvironmentPtr();
250      CPXLPptr cpxlp = cpx_->getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ALL);
251
252      int status = CPXgetbestobjval(env, cpxlp, &lowBound_);
253     
254      int stat = CPXgetstat( env, cpxlp);
255      optimal_ |= (stat == CPXMIP_INFEASIBLE); 
256       nodeCount_ = CPXgetnodecnt(env , cpxlp);
257      iterationCount_ = CPXgetmipitcnt(env , cpxlp);
258      if (status)
259        throw CoinError("Error in getting some CPLEX information","OaDecompositionBase::SubMipSolver","performLocalSearch");
260    }
261#endif
262
263      if (lp_->getFractionalIndices().size() == 0) {
264        if (!integerSolution_)
265          integerSolution_ = new double[lp_->getNumCols()];
266        CoinCopyN(lp_->getColSolution(), lp_->getNumCols() , integerSolution_);
267      }
268      else if (integerSolution_) {
269        delete [] integerSolution_;
270        integerSolution_ = NULL;
271      }
272    }
273  }
274
275   /** Assign a strategy. */
276   void 
277   SubMipSolver::setStrategy(CbcStrategy * strategy)
278   {
279     if (strategy_) delete strategy_;
280     strategy_ = strategy->clone();
281   }
282
283  /** Register options.*/
284  void
285  SubMipSolver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions)
286  {
287    roptions->SetRegisteringCategory("Options for MILP solver", RegisteredOptions::BonminCategory);
288    roptions->AddStringOption3("milp_solver",
289        "Choose the subsolver to solve MILP sub-problems in OA decompositions.",
290        "Cbc_D",
291        "Cbc_D","Coin Branch and Cut with its default",
292        "Cbc_Par", "Coin Branch and Cut with passed parameters",
293        "Cplex","Ilog Cplex",
294        " To use Cplex, a valid license is required and you should have compiled OsiCpx in COIN-OR  (see Osi documentation).");
295    roptions->setOptionExtraInfo("milp_solver",5);
296
297    roptions->AddBoundedIntegerOption("milp_log_level",
298        "specify MILP solver log level.",
299        0,3,0,
300        "Set the level of output of the MILP subsolver in OA : "
301        "0 - none, 1 - minimal, 2 - normal low, 3 - normal high"
302                                     );
303    roptions->setOptionExtraInfo("milp_log_level",5);
304
305
306  }
307}/* Ends Bonmin namespace.*/
Note: See TracBrowser for help on using the repository browser.