Debian Codegraph @

Bonmin/src/Interfaces/Filter/BonFilterSolver.cpp

raw
1 // (C) Copyright International Business Machines Corporation, Carnegie Mellon University 2006, 2008
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, International Business Machines Corporation
7 //
8 // Date : 10/02/2006
9
10 #include "BonminConfig.h"
11
12 #include "BonFilterSolver.hpp"
13 #include "BonFilterWarmStart.hpp"
14
15 #include <fstream>
16
17 #include "CoinTime.hpp"
18 #include<algorithm>
19 typedef Bonmin::FilterSolver::fint fint;
20 typedef Bonmin::FilterSolver::real real;
21
22 //#define InitializeAll
23
24
25 typedef long ftnlen;
26 extern "C"
27 {
28 void F77_FUNC(filtersqp,FILTERSQP)(
29 fint *n, fint *m, fint *kmax, fint *maxa,
30 fint *maxf, fint *mlp, fint *mxwk, fint *mxiwk,
31 fint *iprint, fint *nout, fint *ifail, real *rho,
32 real *x, real *c, real *f, real *fmin, real *bl,
33 real *bu, real *s, real *a, fint *la, real *ws,
34 fint *lws, real *lam, char *cstype, real *user,
35 fint *iuser, fint *maxiter, fint *istat,
36 real *rstat, ftnlen cstype_len);
37 }
38
39 //Static variables
40 static Ipopt::TNLP * tnlpSolved = NULL;
41 static fint nnz_h = -1;
42
43 static fint * hStruct = NULL;
44
45 //Permutation to apply to jacobian in order to get it row ordered
46 static int * permutationJac = NULL;
47 static int * permutationHess = NULL;
48 //static int * cache = NULL;
49
50
51 extern "C"
52 {
53 //Access to filter common bloc
54 /* common block for problemname */
55 extern struct {
56 fint char_l;
57 char pname[10];
58 }
59 F77_FUNC(cpname,CPNAME);
60
61 /* common block for Hessian storage set to 0, i.e. NO Hessian */
62 extern struct {
63 fint phl, phr, phc;
64 }
65 F77_FUNC(hessc,HESSC);
66
67 /* common block for upper bound on filter */
68 extern struct {
69 real ubd, tt;
70 }
71 F77_FUNC(ubdc,UBDC);
72
73 /* common block for infinity & epslon */
74 extern struct {
75 real infty, eps;
76 }
77 F77_FUNC_(nlp_eps_inf,NLP_EPS_INF);
78
79 /* common block for prfinting from QP solver */
80 extern struct {
81 fint n_bqpd_calls, n_bqpd_prfint;
82 }
83 F77_FUNC_(bqpd_count,BQPD_COUNT);
84
85 /* common for scaling: scale_mode = 0 (none), 1 (variables), 2 (vars+cons) */
86 extern struct {
87 fint scale_mode, phe;
88 }
89 F77_FUNC(scalec,SCALEC);
90 }
91
92 extern "C"
93 {
94
95 /// Objective function evaluation
96 void F77_FUNC(objfun,OBJFUN)(real *x, fint *n, real * f, real *user, fint * iuser, fint * errflag) {
97 (*errflag) = !tnlpSolved->eval_f(*n, x, 1, *f);
98 }
99
100 /** Constraint functions evaluation. */
101 void
102 F77_FUNC(confun,CONFUN)(real * x, fint * n , fint *m, real *c, real *a, fint * la, real * user, fint * iuser,
103 fint * errflag) {
104 (*errflag) = !tnlpSolved->eval_g(*n, x, 1, *m, c);
105 }
106
107 void
108 F77_FUNC(gradient,GRADIENT)(fint *n, fint *m, fint * mxa, real * x, real *a, fint * la,
109 fint * maxa, real * user, fint * iuser, fint * errflag) {
110 (*errflag) = !tnlpSolved->eval_grad_f(*n, x, 1, a);
111 /// ATTENTION: Filter expect the jacobian to be ordered by row
112 int nnz = la[0] - *n - 1;
113 double * values = new double [nnz];
114 (*errflag) = !tnlpSolved->eval_jac_g(*n, x, 1, *m, nnz, NULL, NULL, values) || (*errflag);
115 a+= *n;
116 for (int i = 0 ; i < nnz ; i++) {
117 int indice = permutationJac[i];
118 if (indice > nnz) {
119 #ifndef NDEBUG
120 std::cout<<"Error in gradient computation, i: "<<i
121 <<" in row order "<<permutationJac[i]<<std::endl;
122 #endif
123 }
124 *a++ = values[indice];
125 }
126 delete [] values;
127 }
128
129 /* evaluation of the Hessian of the Lagrangian */
130 void
131 F77_FUNC(hessian,HESSIAN)(real *x, fint *n, fint *m, fint *phase, real *lam,
132 real *ws, fint *lws, real *user, fint *iuser,
133 fint *l_hess, fint *li_hess, fint *errflag) {
134 Ipopt::Number obj_factor = (*phase == 1)? 0. : 1.;
135 fint end = nnz_h + (*n) + 2;
136
137 for (int i = 0 ; i < end ; i++) {
138 lws[i] = hStruct[i];
139 }
140 *l_hess = nnz_h;
141 *li_hess = nnz_h + *n + 3;
142 Ipopt::Number * mlam = NULL;
143 if (*m > 0) {
144 mlam = new Ipopt::Number[*m];
145 }
146 for (int i = 0; i<*m; i++) {
147 mlam[i] = -lam[*n+i];
148 }
149 Ipopt::Number * values = new Ipopt::Number [nnz_h];
150 (*errflag) = !tnlpSolved->eval_h(*n, x, 1, obj_factor, *m, mlam ,1, hStruct[0] - 1, NULL, NULL, values);
151 delete [] mlam;
152 for (int i = 0 ; i < nnz_h ; i++) ws[i] = values[permutationHess[i]];
153 delete [] values;
154 }
155
156 }
157
158 namespace Bonmin
159 {
160
161 struct Transposer
162 {
163 const Ipopt::Index* rowIndices;
164 const Ipopt::Index* colIndices;
165 bool operator()(int i, int j)
166 {
167 return rowIndices[i]<rowIndices[j] ||
168 (rowIndices[i]==rowIndices[j] && colIndices[i] < colIndices[j]);
169 }
170 };
171
172 // Convert a sparse matrix from triplet format to row ordered packed matrix
173 void FilterSolver::TMat2RowPMat(bool symmetric, fint n, fint m, int nnz,
174 const Ipopt::Index* iRow,
175 const Ipopt::Index* iCol, int * permutation2,
176 fint * lws, int nnz_offset, int n_offset,
177 Ipopt::TNLP::IndexStyleEnum index_style)
178 {
179 for (int i = 0 ; i < nnz ; i++)
180 permutation2[i] = i;
181
182 Transposer lt;
183 if (symmetric) {
184 Ipopt::Index* tmpRow = new Ipopt::Index[nnz];
185 Ipopt::Index* tmpCol = new Ipopt::Index[nnz];
186 for (int i=0; i<nnz; i++) {
187 const Ipopt::Index& irow = iRow[i];
188 const Ipopt::Index& jcol = iCol[i];
189 if (irow > jcol) {
190 tmpRow[i] = irow;
191 tmpCol[i] = jcol;
192 }
193 else {
194 tmpRow[i] = jcol;
195 tmpCol[i] = irow;
196 }
197 }
198 lt.rowIndices = tmpRow;
199 lt.colIndices = tmpCol;
200 }
201 else {
202 lt.rowIndices = iRow;
203 lt.colIndices = iCol;
204 }
205
206 std::sort(permutation2, permutation2 + nnz, lt);
207
208 const int idx_offset = (index_style == Ipopt::TNLP::C_STYLE);
209 fint row = 1-idx_offset;
210 lws[0] = nnz + nnz_offset + 1;
211 fint * inds = lws + nnz_offset + 1;
212 fint * start = inds + nnz + n_offset;
213 *start++ = 1 + nnz_offset;
214 for (fint i = 0 ; i < nnz ; i++) {
215 inds[i] = lt.colIndices[permutation2[i]] + idx_offset;
216 //DBG_ASSERT(RowJac[permutation2[i]] >= row);
217 if (lt.rowIndices[permutation2[i]] > row) {
218 for (;row < lt.rowIndices[permutation2[i]] ; row++)
219 *start++ = i + nnz_offset + 1;
220 }
221 }
222 for (;row <= m-idx_offset ; row++)
223 *start++ = nnz + nnz_offset +1;
224
225 #if 0
226 for (int i = 0; i<nnz_offset+1; i++)
227 printf("lws[%3d] = %3d\n", i, lws[i]);
228 for (int i = nnz_offset+1; i<nnz_offset+nnz+1; i++)
229 printf("lws[%3d] = %3d [%3d,%3d]\n", i, lws[i], lt.rowIndices[permutation2[i-nnz_offset-1]], lt.colIndices[permutation2[i-nnz_offset-1]]);
230 for (int i = nnz_offset+nnz+1; i<lws[0]+m+2; i++)
231 printf("lws[%3d] = %3d\n", i, lws[i]);
232 #endif
233
234 if (symmetric) {
235 delete [] lt.rowIndices;
236 delete [] lt.colIndices;
237 }
238
239
240 }
241
242
243
244 std::string FilterSolver::solverName_ = "filter SQP";
245
246 void
247 FilterSolver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions)
248 {
249 roptions->SetRegisteringCategory("FilterSQP options", RegisteredOptions::FilterCategory);
250 roptions->AddLowerBoundedNumberOption("eps", "Tolerance for SQP solver",
251 0., 1, 1e-08, "");
252 roptions->AddLowerBoundedNumberOption("infty", "A large number",0.,1, 1e20, "");
253 roptions->AddBoundedIntegerOption("iprint", "Print level (0=silent, 3=verbose)", 0,6,0);
254 roptions->AddLowerBoundedIntegerOption("kmax", "Dimension of null-space",
255 -1, -1, "");
256 roptions->AddLowerBoundedIntegerOption("maxf","Maximum filter length",0,100);
257 roptions->AddLowerBoundedIntegerOption("maxiter", "Maximum number of iterations",0,1000);
258 roptions->AddLowerBoundedIntegerOption("mlp","Maximum level for degeneracy (bqpd)",0, 1000);
259 roptions->AddLowerBoundedIntegerOption("mxlws", "FINTEGER workspace increment", 0, 500000);
260 roptions->AddLowerBoundedIntegerOption("mxws", "REAL workspace increment",
261 0,2000000);
262 roptions->AddLowerBoundedNumberOption("rho_init", "Initial trust region size",0,1,10.);
263 // roption->AddLowerBoundedIntegerOption("timing", "whether to time evaluations (1 = yes)");
264 roptions->AddLowerBoundedNumberOption("tt", "Parameter for upper bound on filter",0,1, 125e-2);
265 roptions->AddLowerBoundedNumberOption("ubd", "Parameter for upper bound on filter", 0 , 1,1e2);
266
267 }
268
269
270 FilterSolver::FilterSolver(bool createEmpty /* = false */)
271 :
272 TNLPSolver(),
273 warmF_(NULL),
274 cached_(NULL)
275 {}
276
277 FilterSolver::FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
278 Ipopt::SmartPtr<Ipopt::OptionsList> options,
279 Ipopt::SmartPtr<Ipopt::Journalist> journalist,
280 const std::string & prefix):
281 TNLPSolver(roptions, options, journalist, prefix),
282 warmF_(NULL),
283 cached_(NULL)
284 {}
285
286 FilterSolver::FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
287 Ipopt::SmartPtr<Ipopt::OptionsList> options,
288 Ipopt::SmartPtr<Ipopt::Journalist> journalist):
289 TNLPSolver(roptions, options, journalist, "bonmin."),
290 warmF_(NULL),
291 cached_(NULL)
292 {}
293
294
295 FilterSolver::FilterSolver(const FilterSolver & other):
296 TNLPSolver(other),
297 warmF_(NULL),
298 cached_(NULL)
299 {
300 warmF_ = (other.warmF_.IsValid()) ? dynamic_cast<FilterWarmStart *>(other.warmF_->clone()):NULL;
301 }
302
303 Ipopt::SmartPtr <TNLPSolver>
304 FilterSolver::clone()
305 {
306 Ipopt::SmartPtr<FilterSolver> retval = new FilterSolver(*this);
307 return GetRawPtr(retval);
308 }
309
310 FilterSolver::~FilterSolver()
311 {}
312
313 bool
314 FilterSolver::Initialize(std::string optFile)
315 {
316 std::ifstream is;
317 if (optFile != "") {
318 try {
319 is.open(optFile.c_str());
320 }
321 catch (std::bad_alloc) {
322 journalist_->Printf(Ipopt::J_SUMMARY, Ipopt::J_MAIN, "\nEXIT: Not enough memory.\n");
323 return false;
324 }
325 #ifndef NO_CATCH_ALL
326 catch (...) {
327 Ipopt::IpoptException E("Unknown Exception caught in ipopt", "Unknown File", -1);
328 E.ReportException(*journalist_);
329 return false;
330 }
331 #endif
332 }
333 bool retval = Initialize(is);
334 if (is) {
335 is.close();
336 }
337 if(!options_->GetIntegerValue("print_level",default_log_level_,""))
338 default_log_level_ = 1;
339 return retval;
340 }
341
342 bool
343 FilterSolver::Initialize(std::istream &is)
344 {
345
346 Ipopt::Index ivalue;
347 options_->GetIntegerValue("print_level", ivalue, "");
348 Ipopt::EJournalLevel print_level = (Ipopt::EJournalLevel)ivalue;
349 Ipopt::SmartPtr<Ipopt::Journal> stdout_jrnl = journalist_->GetJournal("console");
350 if (IsValid(stdout_jrnl)) {
351 // Set printlevel for stdout
352 stdout_jrnl->SetAllPrintLevels(print_level);
353 stdout_jrnl->SetPrintLevel(Ipopt::J_DBG, Ipopt::J_NONE);
354 }
355
356 if (is.good()) {
357 options_->ReadFromStream(*journalist_, is);
358 }
359 return true;
360 }
361
362 /// Solves a problem expresses as a TNLP
363 TNLPSolver::ReturnStatus
364 FilterSolver::OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp)
365 {
366 if (cached_.IsNull() || !cached_->use_warm_start_in_cache_) {
367 cached_ = new cachedInfo(tnlp, options_);
368 }
369 cached_->load_ws(warmF_);
370 return callOptimizer();
371 }
372
373 /// Solves a problem expressed as a TNLP
374 TNLPSolver::ReturnStatus
375 FilterSolver::ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp)
376 {
377 assert(tnlp == cached_->tnlp_);
378 cached_->load_ws(warmF_);
379 //rescan bounds which may have changed
380 assert(cached_->bounds);
381 int n = cached_->n;
382 int m = cached_->m;
383 tnlp->get_bounds_info(n, cached_->bounds, &cached_->bounds[n+m],
384 m, &cached_->bounds[n], &cached_->bounds[2*n + m]);
385
386 tnlpSolved = static_cast<Ipopt::TNLP *>(Ipopt::GetRawPtr(tnlp));
387 nnz_h = cached_->nnz_h_;
388
389 hStruct = cached_->hStruct_;
390
391 //Permutation to apply to jacobian in order to get it row ordered
392 permutationJac = cached_->permutationJac_;
393 permutationHess = cached_->permutationHess_;
394
395
396 return callOptimizer();
397 }
398
399
400
401 void
402 FilterSolver::cachedInfo::initialize(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp,
403 Ipopt::SmartPtr<Ipopt::OptionsList>& options)
404 {
405 // 1) Get some dimensions
406 // 1.a) First from ampl
407 int nnz_jac_g;
408
409 Ipopt::TNLP::IndexStyleEnum index_style;
410 Ipopt::Index nv, nc, nnz_j, nnz_hess;
411 tnlp->get_nlp_info( nv, nc,
412 nnz_j, (Ipopt::Index&) nnz_hess,
413 index_style);
414 n = nv;
415 m = nc;
416 nnz_jac_g = nnz_j;
417 nnz_h_ = nnz_hess;
418
419 nnz_h = nnz_h_;
420
421
422 // 1.b) then from options
423 Ipopt::Index kmax_ipt;
424 options->GetIntegerValue("kmax", kmax_ipt, "filter.");
425 if (kmax_ipt == -1) {
426 kmax = n;
427 }
428 else {
429 kmax = kmax_ipt;
430 kmax = std::min(kmax,n);
431 }
432 Ipopt::Index mlp_ipt;
433 options->GetIntegerValue("mlp", mlp_ipt,"filter.");
434 mlp = mlp_ipt;
435
436 Ipopt::Index maxf_ipt;
437 options->GetIntegerValue("maxf", maxf_ipt,"filter.");
438 maxf = maxf_ipt;
439
440 fint mxwk0;
441 Ipopt::Index mxwk0_ipt;
442 options->GetIntegerValue("mxws", mxwk0_ipt, "filter.");
443 mxwk0 = mxwk0_ipt;
444
445 fint mxiwk0;
446 Ipopt::Index mxiwk0_ipt;
447 options->GetIntegerValue("mxlws", mxiwk0_ipt, "filter.");
448 mxiwk0 = mxiwk0_ipt;
449 // Setup storage for Filter
450 int nplusm = n + m;
451 //Starting point
452 x = new real [n];
453
454 //tnlp->get_starting_point(n, 1, x, 0, NULL, NULL, m, 0, NULL);
455 use_warm_start_in_cache_ = false;
456 //for(int i = 0 ; i < n ; i++) x[i] = 0;
457 lam = new real [n+m];
458 //#ifdef InitializeAll // This should be initialized
459 for (int i = 0 ; i < n+m ; i++) lam[i] = 0.;
460 //#endif
461 //bounds
462 bounds = new real [2*n + 2*m];
463
464 tnlp->get_bounds_info(n, bounds, bounds + nplusm, m, bounds + n, bounds + n + nplusm);
465
466 #if 0
467 double infty = F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).infty;
468 // AW: I don't think we need this, it isn't done for ReOptimize either
469 for (int i = 0 ; i < nplusm ; i++) {
470 if (bounds[i] < -infty) bounds[i] = - infty;
471 }
472
473 real * ubounds = bounds + nplusm;
474 for (int i = 0 ; i < nplusm ; i++) {
475 if (ubounds[i] > infty) ubounds[i] = infty;
476 }
477 #endif
478 maxa = n + nnz_jac_g;
479 fint maxia = n + nnz_jac_g + m + 3;
480 a = new real[maxa];
481 la = new fint [maxia];
482
483 int * RowJac = new int [nnz_jac_g];
484 int * ColJac = new int [nnz_jac_g];
485
486 la[nnz_jac_g + n + 1] = 1;
487
488 for (fint i = 1; i <= n ; i++)
489 la[i] = i;// - (index_style == Ipopt::TNLP::C_STYLE);
490 tnlp->eval_jac_g( nv, NULL, 0, nc , nnz_j, RowJac, ColJac, NULL);
491
492 permutationJac = permutationJac_ = new int [nnz_jac_g];
493 TMat2RowPMat(false, n, m, nnz_jac_g, RowJac, ColJac, permutationJac,
494 la, n, 1, index_style);
495
496 delete [] RowJac;
497 delete [] ColJac;
498
499 // Now setup hessian
500 permutationHess = permutationHess_ = new int[nnz_h];
501 hStruct_ = new fint[nnz_h + n + 3];
502 int * cache = new int[2*nnz_h + 1];
503 F77_FUNC(hessc,HESSC).phl = 1;
504 tnlp->eval_h((Ipopt::Index&) n, NULL, 0, 1., (Ipopt::Index&) m, NULL, 0, (Ipopt::Index&) nnz_h, cache + nnz_h, cache , NULL);
505
506 TMat2RowPMat(true, n, n, nnz_h, cache, cache + nnz_h, permutationHess,
507 hStruct_, 0, 0, index_style);
508
509 delete [] cache;
510 // work arrays
511 fint lh1 = nnz_h + 8 + 2 * n + m;
512 maxWk = 21*n + 8*m + mlp + 8*maxf + lh1 + kmax*(kmax+9)/2 + mxwk0;
513 maxiWk = 13*n + 4*m + mlp + lh1 + kmax + 113 + mxiwk0;
514
515 ws = new real[maxWk];
516 lws = new fint[maxiWk];
517 #ifdef InitializeAll // ToDo: This shouldn't have to be initialized
518 for (int i = 0 ; i < maxWk ; i++) ws[i] = 0;
519 for (int i = 0 ; i < maxiWk ; i++) lws[i] = 0;
520 #endif
521
522 // Setup global variables and static variables
523 hStruct = hStruct_;
524 tnlpSolved = static_cast<Ipopt::TNLP *>(Ipopt::GetRawPtr(tnlp));
525
526 options->GetNumericValue("ubd",F77_FUNC(ubdc,UBDC).ubd, "filter.");
527 options->GetNumericValue("tt", F77_FUNC(ubdc,UBDC).tt, "filter.");
528 options->GetNumericValue("eps", F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).eps, "filter.");
529 options->GetNumericValue("infty", F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).infty, "filter.");
530 rho = 10.;
531 maxiter = 1000;
532 options->GetIntegerValue("maxiter", (Ipopt::Index &) maxiter, "filter.");
533 options->GetNumericValue("rho_init",rho,"filter.");
534
535
536 // Set up scaling
537 F77_FUNC(scalec,SCALEC).scale_mode = 0;
538 s = new real [n+m];
539
540 istat = new fint[14];
541 rstat = new real[7];
542 //#ifdef InitializeAll ToDo: He will do that later
543 for (int i=0; i<14; i++) {
544 istat[0] = 43;
545 }
546 for (int i=0; i<7; i++) {
547 rstat[0] = 42.;
548 }
549 //#endif
550
551 fmin = -1e100;
552 Ipopt::Index bufy;
553 options->GetIntegerValue("iprint",bufy, "filter.");
554 iprint = bufy;
555 nout = 6;
556 cstype = new char[m];
557 Ipopt::TNLP::LinearityType * const_types =
558 new Ipopt::TNLP::LinearityType[m];
559 tnlp->get_constraints_linearity(m, const_types);
560 for (int i = 0 ; i < m ; i++) {
561 if (const_types[i] == Ipopt::TNLP::LINEAR) {
562 cstype[i] = 'L';
563 }
564 else
565 cstype[i] = 'N';
566 }
567 delete [] const_types;
568 c = new double[m];
569 tnlp_ = Ipopt::GetRawPtr(tnlp);
570 }
571
572 /// Solves a problem expresses as a TNLP
573 TNLPSolver::ReturnStatus
574 FilterSolver::callOptimizer()
575 {
576 cached_->optimize();
577
578 TNLPSolver::ReturnStatus optimizationStatus = TNLPSolver::exception;
579 Ipopt::SolverReturn status = Ipopt::INTERNAL_ERROR;
580 fint ifail = cached_->ifail;
581 switch (ifail) {
582 case 0:
583 optimizationStatus = TNLPSolver::solvedOptimal;
584 status = Ipopt::SUCCESS;
585 break;
586 case 1:
587 optimizationStatus = TNLPSolver::unbounded;
588 status = Ipopt::DIVERGING_ITERATES;
589 case 2:
590 case 3:
591 case 4:
592 optimizationStatus = TNLPSolver::provenInfeasible;
593 status = Ipopt::LOCAL_INFEASIBILITY;
594 break;
595 case 5:
596 case 6:
597 case 8:
598 optimizationStatus = TNLPSolver::iterationLimit;
599 status = Ipopt::MAXITER_EXCEEDED;
600 break;
601 case 7:
602 optimizationStatus = TNLPSolver::externalException;
603 status = Ipopt::INTERNAL_ERROR;
604 break;
605 case 9:
606 case 10:
607 optimizationStatus = TNLPSolver::exception;
608 status = Ipopt::INTERNAL_ERROR;
609 break;
610 }
611
612 Ipopt::Number* mlam = NULL;
613 if (cached_->m>0) {
614 mlam = new Ipopt::Number[cached_->m];
615 }
616 for (int i = 0; i<cached_->m; i++) {
617 mlam[i] = -cached_->lam[cached_->n+i];
618 }
619 Ipopt::Number* z_L = new Ipopt::Number[cached_->n];
620 Ipopt::Number* z_U = new Ipopt::Number[cached_->n];
621 const int os = cached_->n+cached_->m;
622 for (int i=0; i<cached_->n; i++) {
623 if (cached_->x[i] == cached_->bounds[i]) {
624 z_L[i] = std::max(0.,cached_->lam[i]);
625 }
626 else {
627 z_L[i] = 0.;
628 }
629 if (cached_->x[i] == cached_->bounds[os+i]) {
630 z_U[i] = std::max(0.,-cached_->lam[i]);
631 }
632 else {
633 z_U[i] = 0.;
634 }
635 }
636 cached_->tnlp_->finalize_solution(status, cached_->n,
637 cached_->x, z_L, z_U,
638 cached_->m, cached_->c, mlam,
639 cached_->f, NULL, NULL);
640 delete [] mlam;
641 delete [] z_L;
642 delete [] z_U;
643 return optimizationStatus;
644 }
645 /** Load warm-start info into cache with filter.*/
646 void
647 FilterSolver::cachedInfo::load_ws(Coin::SmartPtr<FilterWarmStart> warmF){
648 if(!warmF.IsValid()) return;
649 const fint xsize = warmF->primalSize();
650 const real* xarray = warmF->primal();
651 for (int i = 0; i<xsize; i++) {
652 x[i] = xarray[i];
653 }
654 CoinCopyN(warmF->dual(), warmF->dualSize(), lam);
655 CoinCopyN(warmF->lwsArray(), warmF->lwsSize(), lws);
656 for (int i = 0 ; i < 14 ; i ++) {
657 istat[i] = warmF->istat()[i];
658 }
659 use_warm_start_in_cache_ = true;
660 }
661 /** Optimize problem described by cache with filter.*/
662 void
663 FilterSolver::cachedInfo::optimize()
664 {
665 if (use_warm_start_in_cache_) {
666 ifail = -1;
667 use_warm_start_in_cache_ = false;
668 }
669 else {
670 tnlp_->get_starting_point(n, 1, x, 0, NULL, NULL, m, 0, NULL);
671 ifail = 0;
672 }
673 cpuTime_ = - CoinCpuTime();
674 fint cstype_len = 1;
675 rho = 10;
676 // rho = 1e6;
677 // printf("rho = %e\n", rho);
678 #if 0
679 printf("========= 3333333333333333 =============\n");
680 for (int i=0; i<n; i++) {
681 printf("xL[%3d] = %15.8e xU[%3d] = %15.8e\n", i, bounds[i], i, bounds[m+n+i]);
682 }
683 for (int i=0; i<m; i++) {
684 printf("gL[%3d] = %15.8e gU[%3d] = %15.8e\n", i, bounds[n+i], i, bounds[m+2*n+i]);
685 }
686 #endif
687 #if 0
688 for (int i=0; i<n; i++) {
689 printf("fxstart[%2d] = %23.16e\n", i, x[i]);
690 }
691 #endif
692 F77_FUNC(filtersqp,FILTERSQP)(&n, &m, &kmax, & maxa, &maxf, &mlp, &maxWk,
693 &maxiWk, &iprint, &nout, &ifail, &rho, x,
694 c, &f, &fmin, bounds,
695 bounds + n + m,
696 s, a, la,
697 ws, lws, lam, cstype,
698 NULL, NULL,
699 &maxiter, istat, rstat,
700 cstype_len);
701 #if 0
702 for (int i=0; i<n; i++) {
703 printf("fxsol[%2d] = %23.16e\n", i, x[i]);
704 }
705 #endif
706 #if 0
707 printf("final f = %e\n", f);
708 printf("ifail = %d\n", ifail);
709 #endif
710 if(ifail == 3){
711 f = rstat[4];
712 }
713
714 cpuTime_ += CoinCpuTime();
715 }
716
717 std::string
718 FilterSolver::UnsolvedFilterError::errorNames_[1] =
719 {"Internal error in Filter SQP."};
720
721 std::string
722 FilterSolver::UnsolvedFilterError::solverName_ =
723 "filterSqp";
724
725 const std::string&
726 FilterSolver::UnsolvedFilterError::errorName() const
727 {
728 return errorNames_[0];
729 }
730
731 const std::string&
732 FilterSolver::UnsolvedFilterError::solverName() const
733 {
734 return solverName_;
735 }
736
737 bool
738 FilterSolver::setWarmStart(const CoinWarmStart * warm,
739 Ipopt::SmartPtr<TMINLP2TNLP> tnlp)
740 {
741 if (warm == NULL || cached_.IsNull()) {
742 cached_ = new cachedInfo(GetRawPtr(tnlp), options_);
743 }
744 if(warm == NULL) return 1;
745 const FilterWarmStart * warmF = dynamic_cast<const FilterWarmStart *> (warm);
746 assert(warmF);
747 if (warmF->empty())//reset initial point and leave
748 {
749 warmF_ = NULL;
750 disableWarmStart();
751 return 1;
752 }
753 enableWarmStart();
754 warmF_ = dynamic_cast<FilterWarmStart *> (warmF->clone());
755 return true;
756 }
757
758 CoinWarmStart *
759 FilterSolver::getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const
760 {
761 return new FilterWarmStart(cached_->n, cached_->x,
762 cached_->n+cached_->m, cached_->lam,
763 cached_->maxiWk, cached_->lws, cached_->istat);
764 }
765
766 CoinWarmStart *
767 FilterSolver::getEmptyWarmStart() const
768 {
769 return new FilterWarmStart;
770 }
771
772
773 /** Check that warm start object is valid.*/
774 bool
775 FilterSolver::warmStartIsValid(const CoinWarmStart * ws) const{
776 const FilterWarmStart* fws = dynamic_cast<const FilterWarmStart*>(ws);
777 if (fws && ! fws->empty()) {
778 return true;
779 }
780 return false;
781 }
782
783
784 }//end namespace Bonmin