Debian Codegraph @

Bonmin/src/Interfaces/Filter/BonBqpdSolver.hpp

raw
1 // (C) Copyright International Business Machines Corporation, 2007
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Andreas Waechter, International Business Machines Corporation
7 // based on BonFilterSolver.cpp
8 //
9 // Date : 07/09/2007
10
11 #ifndef BonBqpdSolver_H
12 #define BonBqpdSolver_H
13
14 #include "BonTNLPSolver.hpp"
15 #include "BonBranchingTQP.hpp"
16
17 //#define TIME_BQPD
18 namespace Bonmin
19 {
20 class BqpdSolver : public TNLPSolver
21 {
22 public:
23 friend class FilterSolver;
24
25
26 class UnsolvedBqpdError: public TNLPSolver::UnsolvedError
27 {
28 public:
29 UnsolvedBqpdError(int errorNum,
30 Ipopt::SmartPtr<TMINLP2TNLP> model,
31 const std::string &name):
32 TNLPSolver::UnsolvedError(errorNum, model, name)
33 {}
34 virtual const std::string& errorName() const;
35
36 virtual const std::string& solverName() const;
37 virtual ~UnsolvedBqpdError()
38 {}
39
40 private:
41 static std::string errorNames_[1];
42 static std::string solverName_;
43 };
44
45 /** Fortran type for integer used in filter. */
46 typedef ipfint fint;
47 /** Fortran type for double.used in filter */
48 typedef double real;
49
50 virtual UnsolvedError*
51 newUnsolvedError(int num,
52 Ipopt::SmartPtr<TMINLP2TNLP> problem,
53 std::string name)
54 {
55 return new UnsolvedBqpdError(num, problem, name);
56 }
57
58 ///Default constructor
59 BqpdSolver(bool createEmpty = false);
60
61 /// Constructor with passed journalist, roptions, options.
62 BqpdSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
63 Ipopt::SmartPtr<Ipopt::OptionsList> options,
64 Ipopt::SmartPtr<Ipopt::Journalist> journalist,
65 const std::string & prefix
66 );
67
68 ///destructor
69 virtual ~BqpdSolver();
70
71 /** Initialize the TNLPSolver (read options from params_file)
72 */
73 virtual bool Initialize(std::string params_file);
74
75 /** Initialize the TNLPSolver (read options from istream is)
76 */
77 virtual bool Initialize(std::istream& is);
78
79 /** @name Solve methods */
80 //@{
81 /// Solves a problem expresses as a TNLP
82 virtual ReturnStatus OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp);
83
84 /// Resolves a problem expresses as a TNLP
85 virtual ReturnStatus ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp);
86
87 /// Set the warm start in the solver
88 virtual bool setWarmStart(const CoinWarmStart * warm,
89 Ipopt::SmartPtr<TMINLP2TNLP> tnlp);
90
91 /// Safe the current state (after most recent solve that must have
92 /// been successful) as hot start information and use that for all
93 /// further solves, until unmarkHotStart is called.
94 virtual bool markHotStart(){return cached_->markHotStart();}
95
96 /// Get warm start used in last optimization
97 virtual CoinWarmStart * getUsedWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const{
98 throw CoinError(__PRETTY_FUNCTION__,"","Not implemented");
99 }
100
101 /// Get the warm start form the solver
102 virtual CoinWarmStart * getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const;
103
104 virtual CoinWarmStart * getEmptyWarmStart() const;
105
106 /** Check that warm start object is valid.*/
107 virtual bool warmStartIsValid(const CoinWarmStart * ws) const;
108
109 virtual void enableWarmStart()
110 {//No options to be set
111 }
112 virtual void disableWarmStart()
113 {//No options to be set
114 }
115 //@}
116
117 /// Virtual copy constructor
118 virtual Ipopt::SmartPtr<TNLPSolver> clone();
119
120 /// Get the CpuTime of the last optimization.
121 virtual double CPUTime()
122 {
123 return (Ipopt::IsValid(cached_)) ? cached_->cpuTime_: 0.;
124 }
125
126 /// Get the iteration count of the last optimization.
127 virtual int IterationCount()
128 {
129 return 0;
130 }
131
132
133 // turn off all output from the solver
134 virtual void setOutputToDefault()
135 {
136 if (Ipopt::IsValid(cached_)) cached_->iprint = default_log_level_;
137 }
138 /// turn on all output from the solver
139 virtual void forceSolverOutput(int log_level)
140 {
141 if (Ipopt::IsValid(cached_)) cached_->iprint = log_level;
142 }
143
144 /// Get the solver name
145 virtual std::string & solverName()
146 {
147 return solverName_;
148 }
149
150 /// Register this solver options into passed roptions
151 void registerOptions()
152 {
153 registerOptions(roptions_);
154 }
155
156 /** Error code (solver specific).*/
157 virtual int errorCode() const
158 {
159 return -1;
160 }
161 #ifdef TIME_BQPD
162 struct Times {
163 int numsolve;
164 double create;
165 double solve;
166 double resolve;
167 double warm_start;
168 int pivots;
169 Times(): numsolve(0), create(0), solve(0), resolve(0), warm_start(0), pivots(0){
170 }
171
172 Times & operator +=(Times &rhs){
173 numsolve += rhs.numsolve;
174 create += rhs.create;
175 solve += rhs.solve;
176 resolve += rhs.resolve;
177 warm_start += rhs.warm_start;
178 pivots += rhs.pivots;
179 return *this;
180 }
181 };
182 #endif
183
184 /// Register this solver options into passed roptions
185 static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);
186 private:
187 /** @name Private function members. */
188 /** @{ */
189 /** Perform optimization using data structure in cache. */
190 TNLPSolver::ReturnStatus callOptimizer();
191 /** @} */
192
193 /** @name User options */
194 //@{
195 /** Fill-in factor for QP factorization */
196 double fillin_factor_;
197 int kmax_ipt_;
198 int mlp_ipt_;
199 /** Hot start m0de.*/
200 static int m0de_;
201 /** Hot start reinitialization fequency.*/
202 static int reinit_freq_;
203 //@}
204
205 /** Cached information for reoptimizing. */
206 struct cachedInfo : public Ipopt::ReferencedObject
207 {
208 #ifdef TIME_BQPD
209 Times times_;
210 #endif
211
212 fint n;
213 fint m;
214 fint k;
215 fint kmax;
216 real* a;
217 fint* la;
218 real* x;
219 real* bl;
220 real* bu;
221 real f;
222 real* g;
223 real* r;
224 real* w;
225 real* e;
226 fint* ls;
227 real* alp;
228 fint* lp;
229 fint mlp;
230 fint peq;
231 real* ws;
232 fint* lws;
233 fint m0de;
234 fint ifail;
235 fint info[1];
236 fint iprint;
237 fint nout;
238
239 /// wsc common block
240 fint kk,ll,mxws,mxlws;
241
242 /** indicates if we should start from a hotstart **/
243 bool haveHotStart_;
244 /** @name All remaining information from all common blocks for
245 hot start **/
246 //@{
247 /// bqpdc common block
248 fint irh1,na,na1,nb,nb1,ka1,kb1,kc1,irg1,lu1,lv,lv1,ll1;
249 /// epsc common block
250 real eps,tol,emin;
251 /// vstepc common block
252 real vstep;
253 /// repc common block
254 real sgnf;
255 fint nrep,npiv,nres;
256 /// refactorc common block
257 fint nup,nfreq;
258 /// alphac common block
259 real alpha;
260 /// sparsec common block
261 fint ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,nprof,lc;
262 fint lc1,li,li1,lm,lm1,lp_,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1;
263 /// factorc common block
264 fint m1,m2,mp,mq,lastr,irow;
265 /// mxm1c common block
266 fint mxm1;
267 /// /minorc
268 real c;
269 //@}
270 fint kHot;
271 real* xHot;
272 real fHot;
273 real* gHot;
274 real* rHot;
275 real* wHot;
276 real* eHot;
277 fint* lsHot;
278 real* alpHot;
279 fint* lpHot;
280 fint peqHot;
281 real* wsHot;
282 fint* lwsHot;
283 fint infoHot[1];
284 fint kkkHot;
285 fint lllHot;
286 fint kkHot;
287 fint llHot;
288
289 Ipopt::SmartPtr<BranchingTQP> tqp_;
290 /** Elapsed CPU time in last optimization. */
291 double cpuTime_;
292 /** flag remembering if warm start information has been put into
293 cache */
294 bool use_warm_start_in_cache_;
295 bool bad_warm_start_info_;
296
297 /** Number of nonzeros in Jacobian and gradient */
298 int amax_;
299
300 /** Fill-in factor for QP factorization. This is a pointer to
301 the corresponding value in the BqpdSolver object, so that an
302 increase is not forgotten. */
303 double* fillin_factor_;
304 //@}
305
306 /** next reinitialization of hot start.*/
307 int next_reinit_;
308 /** Number of pivots.*/
309 int pivots_;
310 /** Constructor.*/
311 cachedInfo()
312 :
313 a(NULL),
314 la(NULL),
315 x(NULL),
316 bl(NULL),
317 bu(NULL),
318 g(NULL),
319 r(NULL),
320 w(NULL),
321 e(NULL),
322 ls(NULL),
323 alp(NULL),
324 lp(NULL),
325 ws(NULL),
326 lws(NULL),
327 haveHotStart_(false),
328 xHot(NULL),
329 gHot(NULL),
330 rHot(NULL),
331 wHot(NULL),
332 eHot(NULL),
333 lsHot(NULL),
334 alpHot(NULL),
335 lpHot(NULL),
336 wsHot(NULL),
337 lwsHot(NULL),
338 cpuTime_(0),
339 use_warm_start_in_cache_(false),
340 bad_warm_start_info_(false),
341 next_reinit_(0),
342 pivots_(0)
343 {}
344
345 cachedInfo(const Ipopt::SmartPtr<BranchingTQP> &tqp,
346 Ipopt::SmartPtr<Ipopt::OptionsList>& options,
347 int kmax_ipt, int mlp_ipt, double* fillin_factor):
348 a(NULL),
349 la(NULL),
350 x(NULL),
351 bl(NULL),
352 bu(NULL),
353 g(NULL),
354 r(NULL),
355 w(NULL),
356 e(NULL),
357 ls(NULL),
358 alp(NULL),
359 lp(NULL),
360 ws(NULL),
361 lws(NULL),
362 haveHotStart_(false),
363 xHot(NULL),
364 gHot(NULL),
365 rHot(NULL),
366 wHot(NULL),
367 eHot(NULL),
368 lsHot(NULL),
369 alpHot(NULL),
370 lpHot(NULL),
371 wsHot(NULL),
372 lwsHot(NULL),
373 tqp_(tqp),
374 cpuTime_(0),
375 use_warm_start_in_cache_(false),
376 bad_warm_start_info_(false),
377 next_reinit_(0),
378 pivots_(0)
379 {
380 initialize(tqp, options, kmax_ipt, mlp_ipt, fillin_factor);
381 }
382
383 /** Fill data structures for filter with info from tnlp. */
384 void initialize(const Ipopt::SmartPtr<BranchingTQP> &tqp,
385 Ipopt::SmartPtr<Ipopt::OptionsList>& options,
386 int kmax_ipt, int mlp_ipt, double* fillin_factor);
387
388 void re_initialize();
389 /** Optimize problem described by cache with filter.*/
390 void optimize();
391
392 /** Store most recent solution as hot start */
393 bool markHotStart();
394
395 /** Forget about the hot start info */
396 void unmarkHotStart();
397
398 /** Copy current values from hot start info */
399 void copyFromHotStart();
400
401 /** Destructor. */
402 ~cachedInfo();
403 };
404
405 /** Cached information on last problem optimized for reoptimization. */
406 Ipopt::SmartPtr<cachedInfo> cached_;
407
408 ///\name of solver (Bqpd)
409 static std::string solverName_;
410
411 /** To record default log level.*/
412 int default_log_level_;
413
414 public:
415
416 #ifdef TIME_BQPD
417 Times &times(){ return cached_->times_;}
418 #endif
419
420 };
421
422 }// end namespace Bonmin
423 #endif