BonEcpCuts.cpp
Go to the documentation of this file.
1 // (C) Copyright International Business Machines (IBM) 2006, 2007
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // P. Bonami, International Business Machines
7 //
8 // Date : 12/20/2006
9 //#define ECP_DEBUG
10 #include "BonEcpCuts.hpp"
11 #include "BonSolverHelp.hpp"
12 #include "BonBabInfos.hpp"
13 #include "BonCbc.hpp"
14 namespace Bonmin
15 {
16 
17 
19  OaDecompositionBase(b,false, false)
20  {
21  assignLpInterface(NULL);
22  b.options()->GetIntegerValue("ecp_max_rounds", numRounds_,b.prefix());
23  b.options()->GetNumericValue("ecp_abs_tol", abs_violation_tol_,b.prefix());
24  b.options()->GetNumericValue("ecp_rel_tol", rel_violation_tol_,b.prefix());
25  b.options()->GetNumericValue("ecp_probability_factor", beta_,b.prefix());
26  }
27 
28  double
29  EcpCuts::doEcpRounds(OsiSolverInterface &si,
30  bool leaveSiUnchanged,
31  double* violation)
32  {
33  OsiSolverInterface * saveLp = lp_;
34  lp_ = &si;
35  OsiCuts cs;
36  bool saveLeaveSi = leaveSiUnchanged_;
37  leaveSiUnchanged_ = leaveSiUnchanged;
38  generateCuts(si, cs);
39  lp_ = saveLp;
40  leaveSiUnchanged_ = saveLeaveSi;
41  if (violation) *violation = violation_;
42  return objValue_;
43  }
44 
45  void
46  EcpCuts::generateCuts(const OsiSolverInterface &si,
47  OsiCuts & cs,
48  const CglTreeInfo info) const
49  {
50  if (beta_ >=0) {
51  BabInfo * babInfo = dynamic_cast<BabInfo *> (si.getAuxiliaryInfo());
52  assert(babInfo);
53  assert(babInfo->babPtr());
54  const CbcNode * node = babInfo->babPtr()->model().currentNode();
55  int level = (node == NULL) ? 0 : babInfo->babPtr()->model().currentNode()->depth();
56  double rand = CoinDrand48();
57  double score = pow(2.,-level)*beta_;
58  //printf("depth %i, score %g , rand %g\n", level, score, rand);
59  if (score <= rand)
60  return;
61  }
62  double orig_violation = nlp_->getNonLinearitiesViolation(
63  si.getColSolution(), si.getObjValue());
64 //#define ECP_DEBUG
65 #ifdef ECP_DEBUG
66  std::cout<<"Initial Constraint violation: "<<orig_violation<<std::endl;
67  std::cout<<"Initial objectvie value"<<si.getObjValue()<<std::endl;
68 #endif
69  if (orig_violation <= abs_violation_tol_)
70  return;
71 #ifdef ECP_DEBUG
72  std::cout<<"Generating ECP cuts"<<std::endl;
73 #endif
74  solverManip * lpManip = NULL;
75  bool infeasible = false;
76  violation_ = orig_violation;
77  for (int i = 0 ; i < numRounds_ ; i++) {
79  violation_ > rel_violation_tol_*orig_violation) {
80  int numberCuts = - cs.sizeRowCuts();
81  const double * toCut = parameter().addOnlyViolated_ ?
82  si.getColSolution():NULL;
83  const OsiSolverInterface &localSi = (lpManip == NULL) ?
84  si : *(lpManip->si());
85  nlp_->getOuterApproximation(cs, localSi.getColSolution(), 1, toCut, parameter().global_);
86  numberCuts += cs.sizeRowCuts();
87  if (numberCuts > 0 && i + 1 < numRounds_ ) {
88  if (lpManip==NULL) {
89  assert(lp_ == NULL);
90  if (lp_ == NULL)
91  lpManip = new solverManip(si);
92  else
93  lpManip = new solverManip(lp_, true,true,
94  false,false);
95  }
96  installCuts(*lpManip->si(), cs,numberCuts);
97 #ifdef ECP_DEBUG
98  std::cerr<<"Installed "<<numberCuts<<"cuts in lp"<<std::endl;
99 #endif
100  lpManip->si()->resolve();
101 #ifdef ECP_DEBUG
102  std::cerr<<"New objective "<<lpManip->si()->getObjValue()<<std::endl;
103 #endif
104  if (lpManip->si()->isProvenPrimalInfeasible()) {
105  infeasible = true;
106 #ifdef ECP_DEBUG
107  std::cout<<"Stopping Ecp generation because problem became infeasible"<<std::endl;
108 #endif
109  break;
110  }
111  violation_ = nlp_->getNonLinearitiesViolation(
112  lpManip->si()->getColSolution(),
113  lpManip->si()->getObjValue());
114 #ifdef ECP_DEBUG
115  std::cout<<"Constraint violation: "<<violation_<<std::endl;
116 #endif
117  }
118  else break;
119  }
120  else break;
121  }
122  if (!infeasible) {
123  if (lpManip != NULL) {
124  lpManip->si()->resolve();
125  if (lpManip->si()->isProvenPrimalInfeasible())
126  objValue_ = COIN_DBL_MAX;
127  else
128  objValue_ = lpManip->si()->getObjValue();
129  }
130  }
131  else objValue_ = COIN_DBL_MAX;
132  if (lpManip) {
133  if (lp_ != NULL && lpManip != NULL) {
134  lpManip->restore();
135  }
136 
137  delete lpManip;
138  }
139 #ifdef ECP_DEBUG
140  std::cout<<"End ecp cut generation"<<std::endl;
141 #endif
142  return;
143  }
144 
145  void
147  {
148  roptions->SetRegisteringCategory("ECP cuts generation", RegisteredOptions::BonminCategory);
149  roptions->AddLowerBoundedIntegerOption("filmint_ecp_cuts",
150  "Specify the frequency (in terms of nodes) at which some a la filmint ecp cuts are generated.",
151  0,0,
152  "A frequency of 0 amounts to to never solve the NLP relaxation.");
153  roptions->setOptionExtraInfo("filmint_ecp_cuts",3);
154  roptions->AddLowerBoundedIntegerOption
155  ("ecp_max_rounds",
156  "Set the maximal number of rounds of ECP cuts.",
157  0,5,
158  "");
159  roptions->setOptionExtraInfo("ecp_max_rounds",3);
160  roptions->AddLowerBoundedNumberOption
161  ("ecp_abs_tol",
162  "Set the absolute termination tolerance for ECP rounds.",
163  0,false,1e-6,
164  "");
165  roptions->setOptionExtraInfo("ecp_abs_tol",3);
166  roptions->AddLowerBoundedNumberOption
167  ("ecp_rel_tol",
168  "Set the relative termination tolerance for ECP rounds.",
169  0,false,0.,
170  "");
171  roptions->setOptionExtraInfo("ecp_rel_tol",3);
172  roptions->AddNumberOption
173  ("ecp_probability_factor",
174  "Factor appearing in formula for skipping ECP cuts.",
175  10.,
176  "Choosing -1 disables the skipping.");
177  roptions->setOptionExtraInfo("ecp_probability_factor",3);
178  }
179 } // end namespace bonmin.
double violation_
Record NLP infeasibility at final point of Ecp.
Definition: BonEcpCuts.hpp:86
Small class to manipulatee various things in an OsiSolverInterface and restore them.
void getOuterApproximation(OsiCuts &cs, int getObj, const double *x2, bool global)
Get the outer approximation constraints at the current optimal point.
void fint fint fint real fint real real real real real real real real real fint real fint fint fint real fint fint fint fint * info
double abs_violation_tol_
absolute tolerance for NLP constraint violation to stop ECP rounds
Definition: BonEcpCuts.hpp:90
virtual void generateCuts(const OsiSolverInterface &si, OsiCuts &cs, const CglTreeInfo info=CglTreeInfo()) const
Standard cut generation methods.
Definition: BonEcpCuts.cpp:46
bool addOnlyViolated_
Add only violated OA inequalities.
static void registerOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register ecp cuts options.
Definition: BonEcpCuts.cpp:146
OsiSolverInterface * lp_
A linear solver.
double objValue_
Record obj value at final point of Ecp.
Definition: BonEcpCuts.hpp:84
virtual double getObjValue() const
Get objective function value (can&#39;t use default)
void assignLpInterface(OsiSolverInterface *si)
Assign an OsiTMINLPInterface.
void fint fint fint real fint real real real real real real real real real * e
A class to have all elements necessary to setup a branch-and-bound.
EcpCuts(BabSetupBase &b)
Definition: BonEcpCuts.cpp:18
void installCuts(OsiSolverInterface &si, const OsiCuts &cs, int numberCuts)
Install cuts in solver.
double rel_violation_tol_
relative tolerance for NLP constraint violation to stop ECP rounds
Definition: BonEcpCuts.hpp:92
double doEcpRounds(OsiSolverInterface &si, bool leaveSiUnchanged, double *violation=NULL)
Definition: BonEcpCuts.cpp:29
int numRounds_
maximum number of iterations of generation.
Definition: BonEcpCuts.hpp:88
Bab * babPtr()
Pointer to the branch-and-bound algorithm (to access CbcModel).
Definition: BonBabInfos.hpp:44
static const int infeasible
Definition: Couenne.cpp:68
bool leaveSiUnchanged_
Wether or not we should remove cuts at the end of the procedure.
double beta_
Factor for probability for skipping cuts.
Definition: BonEcpCuts.hpp:94
OsiSolverInterface * si()
Get pointer to solver interface.
Ipopt::SmartPtr< Ipopt::OptionsList > options()
Acces list of Options.
Base class for OA algorithms.
OsiTMINLPInterface * nlp_
Pointer to nlp interface.
return b
Definition: OSdtoa.cpp:1719
const char * prefix() const
Get prefix to use for options.
const CbcModel & model() const
Get cbc model used to solve.
Definition: BonCbc.hpp:87
Bonmin class for passing info between components of branch-and-cuts.
Definition: BonBabInfos.hpp:19