Debian Codegraph @

Bonmin/src/Algorithms/OaGenerators/BonOACutGenerator2.cpp

raw
1 // (C) Copyright Carnegie Mellon University 2005, 2006
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // P. Bonami, Carnegie Mellon University
7 //
8 // Date : 05/26/2005
9
10 #include "BonOACutGenerator2.hpp"
11 #include "BonminConfig.h"
12
13 #include "OsiClpSolverInterface.hpp"
14
15 #include "CbcModel.hpp"
16 #include "BonCbcLpStrategy.hpp"
17 #ifdef COIN_HAS_CPX
18 #include "OsiCpxSolverInterface.hpp"
19 #endif
20 #include "OsiAuxInfo.hpp"
21 #include "BonSolverHelp.hpp"
22
23 #include <climits>
24
25 namespace Bonmin
26 {
27 static const char * txt_id = "OA decomposition";
28
29
30 /// Constructor with basic setup
31 OACutGenerator2::OACutGenerator2(BabSetupBase & b):
32 OaDecompositionBase(b, true, false)
33 {
34 std::string bonmin="bonmin.";
35 std::string prefix = (b.prefix() == bonmin) ? "" : b.prefix();
36 prefix += "oa_decomposition.";
37 subMip_ = new SubMipSolver(b, prefix);
38 double oaTime;
39 b.options()->GetNumericValue("time_limit",oaTime,prefix);
40 parameter().maxLocalSearch_ = INT_MAX;
41 b.options()->GetIntegerValue("solution_limit", parameter().maxSols_,prefix);
42 parameter().maxLocalSearchTime_ =
43 std::min(b.getDoubleParameter(BabSetupBase::MaxTime), oaTime);
44 if(parameter().maxSols_ > b.getIntParameter(BabSetupBase::MaxSolutions))
45 parameter().maxSols_ = b.getIntParameter(BabSetupBase::MaxSolutions);
46 }
47 OACutGenerator2::~OACutGenerator2()
48 {
49 delete subMip_;
50 }
51
52 /// virutal method to decide if local search is performed
53 bool
54 OACutGenerator2::doLocalSearch(BabInfo * babInfo) const
55 {
56 return (nLocalSearch_<parameters_.maxLocalSearch_ &&
57 numSols_ < parameters_.maxSols_ &&
58 CoinCpuTime() - timeBegin_ < parameters_.maxLocalSearchTime_);
59 }
60 /// virtual method which performs the OA algorithm by modifying lp and nlp.
61 double
62 OACutGenerator2::performOa(OsiCuts &cs,
63 solverManip &lpManip,
64 BabInfo * babInfo,
65 double & cutoff, const CglTreeInfo & info) const
66 {
67
68 double lastPeriodicLog = CoinCpuTime();
69
70 //const int numcols = nlp_->getNumCols();
71 double gap_tol = this->parameter().gap_tol_;
72
73 bool isInteger = false;
74
75 subMip_->setLpSolver(lpManip.si());
76 OsiSolverInterface * lp = subMip_->solver();
77 lp->resolve();
78
79 if(IsValid(nlp_->linearizer())){
80 nlp_->linearizer()->get_refined_oa(cs);
81 installCuts(*lp, cs, cs.sizeRowCuts());
82 }
83 lp->resolve();
84
85 OsiBranchingInformation branch_info(lp, false);
86 bool milpOptimal = 1;
87
88
89 double milpBound = -COIN_DBL_MAX;
90 //bool milpFeasible = 1;
91 bool feasible = 1;
92
93 subMip_->solve(cutoff, parameters_.subMilpLogLevel_,
94 parameters_.maxLocalSearchTime_ + timeBegin_ - CoinCpuTime());
95
96 milpBound = std::max(milpBound, subMip_->lowBound());
97 milpOptimal = subMip_->optimal();
98
99 feasible = milpBound < cutoff;
100 //milpFeasible = feasible;
101 isInteger = (subMip_->getLastSolution() != NULL);
102 nLocalSearch_++;
103
104 if (milpOptimal)
105 handler_->message(SOLVED_LOCAL_SEARCH, messages_)<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol;
106 else
107 {
108 handler_->message(LOCAL_SEARCH_ABORT, messages_)<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol;
109 }
110
111 int nodeCount = subMip_->nodeCount();
112
113 int numberPasses = 0;
114
115 #ifdef OA_DEBUG
116 bool foundSolution = 0;
117 #endif
118 double * nlpSol = NULL;
119 double ub = cutoff;
120 double gap = 1;
121 while (isInteger && feasible) {
122 numberPasses++;
123 //after a prescribed elapsed time give some information to user
124 double time = CoinCpuTime();
125 if (time - lastPeriodicLog > parameters_.logFrequency_) {
126 handler_->message(PERIODIC_MSG,messages_)
127 <<time - timeBegin_<<numberPasses<<cutoff
128 <<milpBound
129 <<CoinMessageEol;
130 lastPeriodicLog = CoinCpuTime();
131 }
132
133
134 //setup the nlp
135 int numberCutsBefore = cs.sizeRowCuts();
136
137 //Fix the variable which have to be fixed, after having saved the bounds
138 const double * colsol =
139 subMip_->getLastSolution();
140 branch_info.solution_ = colsol;
141
142 fixIntegers(*nlp_,branch_info, parameters_.cbcIntegerTolerance_,objects_, nObjects_);
143
144 nlp_->resolve(txt_id);
145 if (post_nlp_solve(babInfo, cutoff)) {
146 //nlp solved and feasible
147 // Update the cutoff
148 ub = std::min(nlp_->getObjValue(), ub);
149 cutoff = ub > 0 ? ub *(1 - parameters_.cbcCutoffIncrement_) : ub*(1 + parameters_.cbcCutoffIncrement_);
150 assert(cutoff < ub);
151 // Update the lp solver cutoff
152 lp->setDblParam(OsiDualObjectiveLimit, cutoff);
153 numSols_++;
154 }
155
156 nlpSol = const_cast<double *>(nlp_->getColSolution());
157
158 // Get the cuts outer approximation at the current point
159 if(parameter().addOnlyViolated_){
160 nlp_->getOuterApproximation(cs, nlpSol, 1, colsol,
161 parameter().global_);
162 }
163 else {
164 nlp_->getOuterApproximation(cs, nlpSol, 1, NULL,
165 parameter().global_);
166 }
167
168 int numberCuts = cs.sizeRowCuts() - numberCutsBefore;
169 assert(numberCuts);
170 installCuts(*lp, cs, numberCuts);
171
172 lp->resolve();
173
174 double objvalue = lp->getObjValue();
175 //milpBound = max(milpBound, lp->getObjValue());
176 feasible = (lp->isProvenOptimal() &&
177 !lp->isDualObjectiveLimitReached() && (objvalue<cutoff)) ;
178 //if value of integers are unchanged then we have to get out
179 bool changed = !feasible;//if lp is infeasible we don't have to check anything
180 branch_info.solution_ = lp->getColSolution();
181 if (!changed)
182 changed = isDifferentOnIntegers(*nlp_, objects_, nObjects_,
183 parameters_.cbcIntegerTolerance_,
184 nlp_->getColSolution(), lp->getColSolution());
185 if (changed) {
186
187 isInteger = integerFeasible(*lp, branch_info, parameters_.cbcIntegerTolerance_,
188 objects_, nObjects_);
189 }
190 else {
191 isInteger = 0;
192 // if(!fixed)//fathom on bounds
193 milpBound = 1e200;
194 }
195 #ifdef OA_DEBUG
196 printf("Obj value after cuts %g %d rows\n",lp->getObjValue(),
197 numberCuts) ;
198 #endif
199 //check time
200 if (CoinCpuTime() - timeBegin_ > parameters_.maxLocalSearchTime_)
201 break;
202
203 if(ub!=0)
204 gap = (ub - milpBound)/fabs(ub);
205 else
206 gap = -milpBound/(1e-10);
207 if (gap < gap_tol){
208 milpBound = 1e50;
209 feasible = 0;
210 }
211 //do we perform a new local search ?
212 if (feasible &&
213 nLocalSearch_ < parameters_.maxLocalSearch_ &&
214 numSols_ < parameters_.maxSols_) {
215
216 if(ub!=0)
217 gap = (ub - milpBound)/fabs(ub);
218 else
219 gap = -milpBound/(1e-10);
220 if (gap < gap_tol){
221 milpBound = 1e50;
222 feasible = 0;
223 }
224 nLocalSearch_++;
225
226 subMip_->solve(cutoff, parameters_.subMilpLogLevel_,
227 parameters_.maxLocalSearchTime_ + timeBegin_ - CoinCpuTime());
228
229 milpBound = std::max(milpBound, subMip_->lowBound());
230
231 if (subMip_->optimal())
232 handler_->message(SOLVED_LOCAL_SEARCH, messages_)<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol;
233 else
234 handler_->message(LOCAL_SEARCH_ABORT, messages_)<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol;
235
236 nodeCount += subMip_->nodeCount();
237
238 colsol = const_cast<double *> (subMip_->getLastSolution());
239 isInteger = (colsol != 0);
240
241 feasible = (milpBound < cutoff);
242
243 if (feasible && isInteger) {
244 bool changed = false;
245 changed = isDifferentOnIntegers(*nlp_, objects_, nObjects_,
246 0.1,
247 nlp_->getColSolution(), colsol);
248 //solution problem is solved
249 if (!changed) {
250 feasible = 0;
251 milpBound = 1e50;
252 }
253 //milpFeasible = feasible;
254 }
255 if (subMip_->optimal())
256 milpOptimal = 1;
257 else {
258 milpOptimal = 0;
259 }
260 if (milpBound < cutoff)
261 handler_->message(UPDATE_LB, messages_)<<milpBound<<CoinCpuTime() - timeBegin_<<CoinMessageEol;
262 else {
263 milpBound = 1e50;
264 feasible = 0;
265 }
266 }
267
268 }
269
270 if(milpBound >= cutoff){
271 handler_->message(OASUCCESS, messages_)<<"OA "<<CoinCpuTime() - timeBegin_
272 <<ub<<milpBound<<CoinMessageEol;
273 }
274 else {
275 handler_->message(OAABORT, messages_)<<"OA "<<CoinCpuTime() - timeBegin_
276 <<ub<<milpBound<<CoinMessageEol;
277 }
278 handler_->message(OA_STATS, messages_)<<numberPasses<<nodeCount
279 <<CoinMessageEol;
280 #ifdef OA_DEBUG
281 debug_.printEndOfProcedureDebugMessage(cs, foundSolution, cutoff, milpBound, isInteger, feasible, std::cout);
282 #endif
283 return milpBound;
284 }
285
286 /** Register OA options.*/
287 void
288 OACutGenerator2::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions)
289 {
290 roptions->SetRegisteringCategory("Outer Approximation Decomposition (B-OA)", RegisteredOptions::BonminCategory);
291 roptions->AddStringOption2("oa_decomposition", "If yes do initial OA decomposition",
292 "no",
293 "no","",
294 "yes","",
295 "");
296 roptions->setOptionExtraInfo("oa_decomposition",19);
297
298 roptions->SetRegisteringCategory("Output and Loglevel", RegisteredOptions::BonminCategory);
299 roptions->AddBoundedIntegerOption("oa_log_level",
300 "specify OA iterations log level.",
301 0,2,1,
302 "Set the level of output of OA decomposition solver : "
303 "0 - none, 1 - normal, 2 - verbose"
304 );
305 roptions->setOptionExtraInfo("oa_log_level", 25);
306
307 roptions->AddLowerBoundedNumberOption("oa_log_frequency",
308 "display an update on lower and upper bounds in OA every n seconds",
309 0.,1.,100.,
310 "");
311 roptions->setOptionExtraInfo("oa_log_frequency", 25);
312 }
313 }/* End namespace Bonmin. */
314