/home/coin/SVN-release/OS-2.4.1/Bonmin/src/Interfaces/Filter/BonFilterSolver.cpp

Go to the documentation of this file.
00001 // (C) Copyright International Business Machines Corporation, Carnegie Mellon University 2006, 2008
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // Authors :
00006 // Pierre Bonami, International Business Machines Corporation
00007 //
00008 // Date : 10/02/2006
00009 
00010 #include "BonminConfig.h"
00011 
00012 #include "BonFilterSolver.hpp"
00013 #include "BonFilterWarmStart.hpp"
00014 
00015 #include <fstream>
00016 
00017 #include "CoinTime.hpp"
00018 #include<algorithm>
00019 typedef Bonmin::FilterSolver::fint fint;
00020 typedef Bonmin::FilterSolver::real real;
00021 
00022 //#define InitializeAll
00023 
00024 
00025 typedef long ftnlen;
00026 extern "C"
00027 {
00028   void F77_FUNC(filtersqp,FILTERSQP)(
00029     fint *n, fint *m, fint *kmax, fint *maxa,
00030     fint *maxf, fint *mlp, fint *mxwk, fint *mxiwk,
00031     fint *iprint, fint *nout, fint *ifail, real *rho,
00032     real *x, real *c, real *f, real *fmin, real *bl,
00033     real *bu, real *s, real *a, fint *la, real *ws,
00034     fint *lws, real *lam, char *cstype, real *user,
00035     fint *iuser, fint *maxiter, fint *istat,
00036     real *rstat, ftnlen cstype_len);
00037 }
00038 
00039 //Static variables
00040 static Ipopt::TNLP * tnlpSolved = NULL;
00041 static fint nnz_h = -1;
00042 
00043 static fint * hStruct = NULL;
00044 
00045 //Permutation to apply to jacobian in order to get it row ordered
00046 static int * permutationJac = NULL;
00047 static int * permutationHess = NULL;
00048 //static int * cache = NULL;
00049 
00050 
00051 extern "C"
00052 {
00053 //Access to filter common bloc
00054   /* common block for problemname */
00055   extern struct {
00056       fint  char_l;
00057       char pname[10];
00058     }
00059   F77_FUNC(cpname,CPNAME);
00060 
00061   /* common block for Hessian storage set to 0, i.e. NO Hessian */
00062   extern struct {
00063       fint phl, phr, phc;
00064     }
00065   F77_FUNC(hessc,HESSC);
00066 
00067   /* common block for upper bound on filter */
00068   extern struct {
00069       real ubd, tt;
00070     }
00071   F77_FUNC(ubdc,UBDC);
00072 
00073   /* common block for infinity & epslon */
00074   extern struct {
00075       real infty, eps;
00076     }
00077   F77_FUNC_(nlp_eps_inf,NLP_EPS_INF);
00078 
00079   /* common block for prfinting from QP solver */
00080   extern struct {
00081       fint n_bqpd_calls, n_bqpd_prfint;
00082     }
00083   F77_FUNC_(bqpd_count,BQPD_COUNT);
00084 
00085   /* common for scaling: scale_mode = 0 (none), 1 (variables), 2 (vars+cons) */
00086   extern struct {
00087       fint scale_mode, phe;
00088     }
00089   F77_FUNC(scalec,SCALEC);
00090 }
00091 
00092 extern "C"
00093 {
00094 
00096   void F77_FUNC(objfun,OBJFUN)(real *x, fint *n, real * f, real *user, fint * iuser, fint * errflag) {
00097     (*errflag) = !tnlpSolved->eval_f(*n, x, 1, *f);
00098   }
00099 
00101   void
00102   F77_FUNC(confun,CONFUN)(real * x, fint * n , fint *m, real *c, real *a, fint * la, real * user, fint * iuser,
00103       fint * errflag) {
00104     (*errflag) = !tnlpSolved->eval_g(*n, x, 1, *m, c);
00105   }
00106 
00107   void
00108   F77_FUNC(gradient,GRADIENT)(fint *n, fint *m, fint * mxa, real * x, real *a, fint * la,
00109       fint * maxa, real * user, fint * iuser, fint * errflag) {
00110     (*errflag) = !tnlpSolved->eval_grad_f(*n, x, 1, a);
00112     int nnz = la[0] - *n - 1;
00113     double * values = new double [nnz];
00114     (*errflag) = !tnlpSolved->eval_jac_g(*n, x, 1, *m, nnz, NULL, NULL, values) || (*errflag);
00115     a+= *n;
00116     for (int i = 0 ; i < nnz ; i++) {
00117       int indice = permutationJac[i];
00118       if (indice > nnz) {
00119 #ifndef NDEBUG
00120         std::cout<<"Error in gradient computation, i: "<<i
00121         <<" in row order "<<permutationJac[i]<<std::endl;
00122 #endif
00123       }
00124       *a++ = values[indice];
00125     }
00126     delete [] values;
00127   }
00128 
00129   /* evaluation of the Hessian of the Lagrangian */
00130   void
00131   F77_FUNC(hessian,HESSIAN)(real *x, fint *n, fint *m, fint *phase, real *lam,
00132       real *ws, fint *lws, real *user, fint *iuser,
00133       fint *l_hess, fint *li_hess, fint *errflag) {
00134     Ipopt::Number obj_factor = (*phase == 1)? 0. : 1.;
00135     fint  end = nnz_h + (*n)  + 2;
00136 
00137     for (int i = 0 ; i < end ; i++) {
00138       lws[i] = hStruct[i];
00139     }
00140     *l_hess = nnz_h;
00141     *li_hess = nnz_h + *n + 3;
00142     Ipopt::Number * mlam = NULL;
00143     if (*m > 0) {
00144       mlam = new Ipopt::Number[*m];
00145     }
00146     for (int i = 0; i<*m; i++) {
00147       mlam[i] = -lam[*n+i];
00148     }
00149     Ipopt::Number * values = new Ipopt::Number [nnz_h];
00150     (*errflag) = !tnlpSolved->eval_h(*n, x, 1, obj_factor, *m, mlam ,1, hStruct[0] - 1, NULL, NULL, values);
00151     delete [] mlam;
00152     for (int i = 0 ; i < nnz_h ; i++) ws[i] = values[permutationHess[i]];
00153     delete [] values;
00154   }
00155 
00156 }
00157 
00158 namespace Bonmin
00159 {
00160 
00161   struct Transposer
00162   {
00163     const Ipopt::Index* rowIndices;
00164     const Ipopt::Index* colIndices;
00165     bool operator()(int i, int j)
00166     {
00167       return rowIndices[i]<rowIndices[j] ||
00168           (rowIndices[i]==rowIndices[j] && colIndices[i] < colIndices[j]);
00169     }
00170   };
00171 
00172   // Convert a sparse matrix from triplet format to row ordered packed matrix
00173   void FilterSolver::TMat2RowPMat(bool symmetric, fint n, fint m, int nnz,
00174       const Ipopt::Index* iRow,
00175       const Ipopt::Index* iCol, int * permutation2,
00176       fint * lws, int nnz_offset, int n_offset,
00177       Ipopt::TNLP::IndexStyleEnum index_style)
00178   {
00179     for (int i = 0 ; i < nnz ; i++)
00180       permutation2[i] = i;
00181 
00182     Transposer lt;
00183     if (symmetric) {
00184       Ipopt::Index* tmpRow = new Ipopt::Index[nnz];
00185       Ipopt::Index* tmpCol = new Ipopt::Index[nnz];
00186       for (int i=0; i<nnz; i++) {
00187         const Ipopt::Index& irow = iRow[i];
00188         const Ipopt::Index& jcol = iCol[i];
00189         if (irow > jcol) {
00190           tmpRow[i] = irow;
00191           tmpCol[i] = jcol;
00192         }
00193         else {
00194           tmpRow[i] = jcol;
00195           tmpCol[i] = irow;
00196         }
00197       }
00198       lt.rowIndices = tmpRow;
00199       lt.colIndices = tmpCol;
00200     }
00201     else {
00202       lt.rowIndices = iRow;
00203       lt.colIndices = iCol;
00204     }
00205 
00206     std::sort(permutation2, permutation2 + nnz, lt);
00207 
00208     const int idx_offset = (index_style == Ipopt::TNLP::C_STYLE);
00209     fint row = 1-idx_offset;
00210     lws[0] = nnz + nnz_offset + 1;
00211     fint * inds = lws + nnz_offset + 1;
00212     fint * start = inds + nnz + n_offset;
00213     *start++ = 1 + nnz_offset;
00214     for (fint i = 0 ; i < nnz ; i++) {
00215       inds[i] = lt.colIndices[permutation2[i]] + idx_offset;
00216       //DBG_ASSERT(RowJac[permutation2[i]] >= row);
00217       if (lt.rowIndices[permutation2[i]] > row) {
00218         for (;row < lt.rowIndices[permutation2[i]] ; row++)
00219           *start++ = i + nnz_offset + 1;
00220       }
00221     }
00222     for (;row <= m-idx_offset ; row++)
00223       *start++ = nnz + nnz_offset +1;
00224 
00225 #if 0
00226     for (int i = 0; i<nnz_offset+1; i++)
00227       printf("lws[%3d] = %3d\n", i, lws[i]);
00228     for (int i = nnz_offset+1; i<nnz_offset+nnz+1; i++)
00229       printf("lws[%3d] = %3d  [%3d,%3d]\n", i, lws[i], lt.rowIndices[permutation2[i-nnz_offset-1]], lt.colIndices[permutation2[i-nnz_offset-1]]);
00230     for (int i = nnz_offset+nnz+1; i<lws[0]+m+2; i++)
00231       printf("lws[%3d] = %3d\n", i, lws[i]);
00232 #endif
00233 
00234     if (symmetric) {
00235       delete [] lt.rowIndices;
00236       delete [] lt.colIndices;
00237     }
00238 
00239 
00240   }
00241 
00242 
00243 
00244   std::string FilterSolver::solverName_ = "filter SQP";
00245 
00246   void
00247   FilterSolver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions)
00248   {
00249     roptions->SetRegisteringCategory("FilterSQP options", RegisteredOptions::FilterCategory);
00250     roptions->AddLowerBoundedNumberOption("eps", "Tolerance for SQP solver",
00251         0., 1, 1e-08, "");
00252     roptions->AddLowerBoundedNumberOption("infty","A large number (1E20)",0.,1, 1e20, "");
00253     roptions->AddBoundedIntegerOption("iprint", "Print level (0=silent, 3=verbose)", 0,6,0);
00254     roptions->AddLowerBoundedIntegerOption("kmax", "Dimension of null-space",
00255         -1, -1, "");
00256     roptions->AddLowerBoundedIntegerOption("maxf","Maximum filter length",0,100);
00257     roptions->AddLowerBoundedIntegerOption("maxiter", "Maximum number of iterations",0,1000);
00258     roptions->AddLowerBoundedIntegerOption("mlp","Maximum level for degeneracy (bqpd)",0, 1000);
00259     roptions->AddLowerBoundedIntegerOption("mxlws", "FINTEGER workspace increment", 0, 500000);
00260     roptions->AddLowerBoundedIntegerOption("mxws", "REAL workspace increment",
00261         0,2000000);
00262     roptions->AddLowerBoundedNumberOption("rho_init", "Initial trust region size",0,1,10.);
00263     //  roption->AddLowerBoundedIntegerOption("timing", "whether to time evaluations (1 = yes)");
00264     roptions->AddLowerBoundedNumberOption("tt", "Parameter for upper bound on filter",0,1, 125e-2);
00265     roptions->AddLowerBoundedNumberOption("ubd", "Parameter for upper bound on filter", 0 , 1,1e2);
00266 
00267   }
00268 
00269 
00270   FilterSolver::FilterSolver(bool createEmpty /* = false */)
00271       :
00272       TNLPSolver(),
00273       warmF_(NULL),
00274       cached_(NULL)
00275   {}
00276 
00277   FilterSolver::FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
00278       Ipopt::SmartPtr<Ipopt::OptionsList> options,
00279       Ipopt::SmartPtr<Ipopt::Journalist> journalist,
00280       const std::string & prefix):
00281       TNLPSolver(roptions, options, journalist, prefix),
00282       warmF_(NULL),
00283       cached_(NULL)
00284   {}
00285 
00286   FilterSolver::FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
00287       Ipopt::SmartPtr<Ipopt::OptionsList> options,
00288       Ipopt::SmartPtr<Ipopt::Journalist> journalist):
00289       TNLPSolver(roptions, options, journalist, "bonmin."),
00290       warmF_(NULL),
00291       cached_(NULL)
00292   {}
00293 
00294 
00295   FilterSolver::FilterSolver(const FilterSolver & other):
00296       TNLPSolver(other),
00297       warmF_(NULL),
00298       cached_(NULL)
00299   {
00300     warmF_ = (other.warmF_.IsValid()) ? dynamic_cast<FilterWarmStart *>(other.warmF_->clone()):NULL;
00301   }
00302 
00303   Ipopt::SmartPtr <TNLPSolver>
00304   FilterSolver::clone()
00305   {
00306     Ipopt::SmartPtr<FilterSolver> retval = new FilterSolver(*this);
00307     return GetRawPtr(retval);
00308   }
00309 
00310   FilterSolver::~FilterSolver()
00311   {}
00312 
00313   bool
00314   FilterSolver::Initialize(std::string optFile)
00315   {
00316     std::ifstream is;
00317     if (optFile != "") {
00318       try {
00319         is.open(optFile.c_str());
00320       }
00321       catch (std::bad_alloc) {
00322         journalist_->Printf(Ipopt::J_SUMMARY, Ipopt::J_MAIN, "\nEXIT: Not enough memory.\n");
00323         return false;
00324       }
00325       catch (...) {
00326         Ipopt::IpoptException E("Unknown Exception caught in ipopt", "Unknown File", -1);
00327         E.ReportException(*journalist_);
00328         return false;
00329       }
00330     }
00331     bool retval = Initialize(is);
00332     if (is) {
00333       is.close();
00334     }
00335     if(!options_->GetIntegerValue("print_level",default_log_level_,""))
00336       default_log_level_ = 1;
00337     return retval;
00338   }
00339 
00340   bool
00341   FilterSolver::Initialize(std::istream &is)
00342   {
00343 
00344     Ipopt::Index ivalue;
00345     options_->GetIntegerValue("print_level", ivalue, "");
00346     Ipopt::EJournalLevel print_level = (Ipopt::EJournalLevel)ivalue;
00347     Ipopt::SmartPtr<Ipopt::Journal> stdout_jrnl = journalist_->GetJournal("console");
00348     if (IsValid(stdout_jrnl)) {
00349       // Set printlevel for stdout
00350       stdout_jrnl->SetAllPrintLevels(print_level);
00351       stdout_jrnl->SetPrintLevel(Ipopt::J_DBG, Ipopt::J_NONE);
00352     }
00353 
00354     if (is.good()) {
00355       options_->ReadFromStream(*journalist_, is);
00356     }
00357     return true;
00358   }
00359 
00361   TNLPSolver::ReturnStatus
00362   FilterSolver::OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp)
00363   {
00364     if (cached_.IsNull() || !cached_->use_warm_start_in_cache_) {
00365       cached_ = new cachedInfo(tnlp, options_);
00366     }
00367     cached_->load_ws(warmF_);
00368     return callOptimizer();
00369   }
00370 
00372   TNLPSolver::ReturnStatus
00373   FilterSolver::ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp)
00374   {
00375     assert(tnlp == cached_->tnlp_);
00376     cached_->load_ws(warmF_);
00377     //rescan bounds which may have changed
00378     assert(cached_->bounds);
00379     int n = cached_->n;
00380     int m = cached_->m;
00381     tnlp->get_bounds_info(n, cached_->bounds, &cached_->bounds[n+m],
00382         m, &cached_->bounds[n], &cached_->bounds[2*n + m]);
00383 
00384     tnlpSolved = static_cast<Ipopt::TNLP *>(Ipopt::GetRawPtr(tnlp));
00385     nnz_h = cached_->nnz_h_;
00386 
00387     hStruct = cached_->hStruct_;
00388 
00389 //Permutation to apply to jacobian in order to get it row ordered
00390     permutationJac = cached_->permutationJac_;
00391     permutationHess = cached_->permutationHess_;
00392 
00393 
00394     return callOptimizer();
00395   }
00396 
00397 
00398 
00399   void
00400   FilterSolver::cachedInfo::initialize(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp,
00401       Ipopt::SmartPtr<Ipopt::OptionsList>& options)
00402   {
00403     // 1) Get some dimensions
00404     // 1.a) First from ampl
00405     int  nnz_jac_g;
00406 
00407     Ipopt::TNLP::IndexStyleEnum index_style;
00408     Ipopt::Index nv, nc, nnz_j, nnz_hess;
00409     tnlp->get_nlp_info( nv, nc,
00410         nnz_j, (Ipopt::Index&) nnz_hess,
00411         index_style);
00412     n = nv;
00413     m = nc;
00414     nnz_jac_g = nnz_j;
00415     nnz_h_ = nnz_hess;
00416 
00417     nnz_h = nnz_h_;
00418 
00419 
00420     // 1.b) then from options
00421     Ipopt::Index kmax_ipt;
00422     options->GetIntegerValue("kmax", kmax_ipt, "filter.");
00423     if (kmax_ipt == -1) {
00424       kmax = n;
00425     }
00426     else {
00427       kmax = kmax_ipt;
00428       kmax = std::min(kmax,n);
00429     }
00430     Ipopt::Index mlp_ipt;
00431     options->GetIntegerValue("mlp", mlp_ipt,"filter.");
00432     mlp = mlp_ipt;
00433 
00434     Ipopt::Index  maxf_ipt;
00435     options->GetIntegerValue("maxf", maxf_ipt,"filter.");
00436     maxf = maxf_ipt;
00437 
00438     fint mxwk0;
00439     Ipopt::Index mxwk0_ipt;
00440     options->GetIntegerValue("mxws", mxwk0_ipt, "filter.");
00441     mxwk0 = mxwk0_ipt;
00442 
00443     fint mxiwk0;
00444     Ipopt::Index mxiwk0_ipt;
00445     options->GetIntegerValue("mxlws",  mxiwk0_ipt, "filter.");
00446     mxiwk0 = mxiwk0_ipt;
00447     // Setup storage for Filter
00448     int nplusm = n + m;
00449     //Starting point
00450     x = new real [n];
00451 
00452     //tnlp->get_starting_point(n, 1, x, 0, NULL, NULL, m, 0, NULL);
00453     use_warm_start_in_cache_ = false;
00454     //for(int i = 0 ; i < n ; i++) x[i] = 0;
00455     lam = new real [n+m];
00456     //#ifdef InitializeAll // This should be initialized
00457     for (int i = 0 ; i < n+m ; i++) lam[i] = 0.;
00458     //#endif
00459     //bounds
00460     bounds = new real [2*n + 2*m];
00461 
00462     tnlp->get_bounds_info(n, bounds, bounds + nplusm, m, bounds + n, bounds + n + nplusm);
00463 
00464 #if 0
00465     double infty = F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).infty;
00466     // AW: I don't think we need this, it isn't done for ReOptimize either
00467     for (int i = 0 ; i < nplusm ; i++) {
00468       if (bounds[i] < -infty) bounds[i] = - infty;
00469     }
00470 
00471     real * ubounds = bounds + nplusm;
00472     for (int i = 0 ; i < nplusm ; i++) {
00473       if (ubounds[i] > infty) ubounds[i] = infty;
00474     }
00475 #endif
00476     maxa = n + nnz_jac_g;
00477     fint maxia = n + nnz_jac_g + m + 3;
00478     a = new real[maxa];
00479     la = new fint [maxia];
00480 
00481     int * RowJac = new int [nnz_jac_g];
00482     int * ColJac = new int [nnz_jac_g];
00483 
00484     la[nnz_jac_g + n + 1] = 1;
00485 
00486     for (fint i = 1; i <= n ; i++)
00487       la[i] = i;// - (index_style == Ipopt::TNLP::C_STYLE);
00488     tnlp->eval_jac_g(  nv, NULL, 0, nc , nnz_j,  RowJac,  ColJac, NULL);
00489 
00490     permutationJac = permutationJac_ = new int [nnz_jac_g];
00491     TMat2RowPMat(false, n, m, nnz_jac_g,  RowJac, ColJac, permutationJac,
00492         la, n, 1, index_style);
00493 
00494     delete [] RowJac;
00495     delete [] ColJac;
00496 
00497     // Now setup hessian
00498     permutationHess = permutationHess_ = new int[nnz_h];
00499     hStruct_ = new fint[nnz_h + n + 3];
00500     int * cache = new int[2*nnz_h + 1];
00501     F77_FUNC(hessc,HESSC).phl = 1;
00502     tnlp->eval_h((Ipopt::Index&) n, NULL, 0, 1., (Ipopt::Index&) m, NULL, 0, (Ipopt::Index&) nnz_h, cache + nnz_h, cache  , NULL);
00503 
00504     TMat2RowPMat(true, n, n, nnz_h, cache, cache + nnz_h, permutationHess,
00505         hStruct_, 0, 0, index_style);
00506 
00507     delete [] cache;
00508     // work arrays
00509     fint lh1 = nnz_h + 8 + 2 * n + m;
00510     maxWk = 21*n + 8*m + mlp + 8*maxf + lh1 + kmax*(kmax+9)/2 + mxwk0;
00511     maxiWk = 13*n + 4*m + mlp + lh1 + kmax + 113 + mxiwk0;
00512 
00513     ws = new real[maxWk];
00514     lws = new fint[maxiWk];
00515 #ifdef InitializeAll // ToDo: This shouldn't have to be initialized
00516     for (int i = 0 ; i < maxWk ; i++) ws[i] = 0;
00517     for (int i = 0 ; i < maxiWk ; i++) lws[i] = 0;
00518 #endif
00519 
00520     // Setup global variables and static variables
00521     hStruct = hStruct_;
00522     tnlpSolved = static_cast<Ipopt::TNLP *>(Ipopt::GetRawPtr(tnlp));
00523 
00524     options->GetNumericValue("ubd",F77_FUNC(ubdc,UBDC).ubd, "filter.");
00525     options->GetNumericValue("tt", F77_FUNC(ubdc,UBDC).tt, "filter.");
00526     options->GetNumericValue("eps", F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).eps, "filter.");
00527     options->GetNumericValue("infty", F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).infty, "filter.");
00528     rho = 10.;
00529     maxiter = 1000;
00530     options->GetIntegerValue("maxiter", (Ipopt::Index &) maxiter, "filter.");
00531     options->GetNumericValue("rho_init",rho,"filter.");
00532 
00533 
00534     // Set up scaling
00535     F77_FUNC(scalec,SCALEC).scale_mode = 0;
00536     s = new real [n+m];
00537 
00538     istat = new fint[14];
00539     rstat = new real[7];
00540     //#ifdef InitializeAll ToDo: He will do that later
00541     for (int i=0; i<14; i++) {
00542       istat[0] = 43;
00543     }
00544     for (int i=0; i<7; i++) {
00545       rstat[0] = 42.;
00546     }
00547     //#endif
00548 
00549     fmin = -1e100;
00550     Ipopt::Index bufy;
00551     options->GetIntegerValue("iprint",bufy, "filter.");
00552     iprint = bufy;
00553     nout = 6;
00554     cstype = new char[m];
00555     Ipopt::TNLP::LinearityType * const_types =
00556       new Ipopt::TNLP::LinearityType[m];
00557     tnlp->get_constraints_linearity(m, const_types);
00558     for (int i = 0 ; i < m ; i++) {
00559       if (const_types[i] == Ipopt::TNLP::LINEAR) {
00560         cstype[i] = 'L';
00561       }
00562       else
00563         cstype[i] = 'N';
00564     }
00565     delete [] const_types;
00566     c = new double[m];
00567     tnlp_ = Ipopt::GetRawPtr(tnlp);
00568   }
00569 
00571   TNLPSolver::ReturnStatus
00572   FilterSolver::callOptimizer()
00573   {
00574     cached_->optimize();
00575 
00576     TNLPSolver::ReturnStatus optimizationStatus = TNLPSolver::exception;
00577     Ipopt::SolverReturn status = Ipopt::INTERNAL_ERROR;
00578     fint ifail = cached_->ifail;
00579     switch (ifail) {
00580     case 0:
00581       optimizationStatus = TNLPSolver::solvedOptimal;
00582       status = Ipopt::SUCCESS;
00583       break;
00584     case 1:
00585       optimizationStatus = TNLPSolver::unbounded;
00586       status = Ipopt::DIVERGING_ITERATES;
00587     case 2:
00588     case 3:
00589     case 4:
00590       optimizationStatus = TNLPSolver::provenInfeasible;
00591       status = Ipopt::LOCAL_INFEASIBILITY;
00592       break;
00593     case 5:
00594     case 6:
00595     case 8:
00596       optimizationStatus = TNLPSolver::iterationLimit;
00597       status = Ipopt::MAXITER_EXCEEDED;
00598       break;
00599     case 7:
00600       optimizationStatus = TNLPSolver::externalException;
00601       status = Ipopt::INTERNAL_ERROR;
00602       break;
00603     case 9:
00604     case 10:
00605       optimizationStatus = TNLPSolver::exception;
00606       status = Ipopt::INTERNAL_ERROR;
00607       break;
00608     }
00609 
00610     Ipopt::Number* mlam = NULL;
00611     if (cached_->m>0) {
00612       mlam = new Ipopt::Number[cached_->m];
00613     }
00614     for (int i = 0; i<cached_->m; i++) {
00615       mlam[i] = -cached_->lam[cached_->n+i];
00616     }
00617     Ipopt::Number* z_L = new Ipopt::Number[cached_->n];
00618     Ipopt::Number* z_U = new Ipopt::Number[cached_->n];
00619     const int os = cached_->n+cached_->m;
00620     for (int i=0; i<cached_->n; i++) {
00621       if (cached_->x[i] == cached_->bounds[i]) {
00622         z_L[i] = std::max(0.,cached_->lam[i]);
00623       }
00624       else {
00625         z_L[i] = 0.;
00626       }
00627       if (cached_->x[i] == cached_->bounds[os+i]) {
00628         z_U[i] = std::max(0.,-cached_->lam[i]);
00629       }
00630       else {
00631         z_U[i] = 0.;
00632       }
00633     }
00634     cached_->tnlp_->finalize_solution(status, cached_->n,
00635                                       cached_->x, z_L, z_U,
00636                                       cached_->m, cached_->c, mlam,
00637                                       cached_->f, NULL, NULL);
00638     delete [] mlam;
00639     delete [] z_L;
00640     delete [] z_U;
00641     return optimizationStatus;
00642   }
00644   void
00645   FilterSolver::cachedInfo::load_ws(Coin::SmartPtr<FilterWarmStart> warmF){
00646     if(!warmF.IsValid()) return;
00647     const fint xsize = warmF->primalSize();
00648     const real* xarray = warmF->primal();
00649     for (int i = 0; i<xsize; i++) {
00650       x[i] = xarray[i];
00651     }
00652     CoinCopyN(warmF->dual(), warmF->dualSize(), lam);
00653     CoinCopyN(warmF->lwsArray(), warmF->lwsSize(), lws);
00654     for (int i = 0 ; i < 14 ; i ++) {
00655       istat[i] = warmF->istat()[i];
00656     }
00657     use_warm_start_in_cache_ = true;
00658   }
00660   void
00661   FilterSolver::cachedInfo::optimize()
00662   {
00663     if (use_warm_start_in_cache_) {
00664       ifail = -1;
00665       use_warm_start_in_cache_ = false;
00666     }
00667     else {
00668       tnlp_->get_starting_point(n, 1, x, 0, NULL, NULL, m, 0, NULL);
00669       ifail = 0;
00670     }
00671     cpuTime_ = - CoinCpuTime();
00672     fint cstype_len = 1;
00673     rho = 10;
00674     //  rho = 1e6;
00675     //  printf("rho = %e\n", rho);
00676 #if 0
00677     printf("========= 3333333333333333 =============\n");
00678     for (int i=0; i<n; i++) {
00679       printf("xL[%3d] = %15.8e  xU[%3d] = %15.8e\n", i, bounds[i], i, bounds[m+n+i]);
00680     }
00681     for (int i=0; i<m; i++) {
00682       printf("gL[%3d] = %15.8e  gU[%3d] = %15.8e\n", i, bounds[n+i], i, bounds[m+2*n+i]);
00683     }
00684 #endif
00685 #if 0
00686     for (int i=0; i<n; i++) {
00687       printf("fxstart[%2d] = %23.16e\n", i, x[i]);
00688     }
00689 #endif
00690     F77_FUNC(filtersqp,FILTERSQP)(&n, &m, &kmax, & maxa, &maxf, &mlp, &maxWk,
00691         &maxiWk, &iprint, &nout, &ifail, &rho, x,
00692         c, &f, &fmin, bounds,
00693         bounds + n + m,
00694         s, a, la,
00695         ws, lws, lam, cstype,
00696         NULL, NULL,
00697         &maxiter, istat, rstat,
00698         cstype_len);
00699 #if 0
00700     for (int i=0; i<n; i++) {
00701       printf("fxsol[%2d] = %23.16e\n", i, x[i]);
00702     }
00703 #endif
00704 #if 0
00705     printf("final f = %e\n", f);
00706     printf("ifail = %d\n", ifail);
00707 #endif
00708     if(ifail == 3){
00709       f = rstat[4];
00710     }
00711 
00712     cpuTime_ += CoinCpuTime();
00713   }
00714 
00715   std::string
00716   FilterSolver::UnsolvedFilterError::errorNames_[1] =
00717     {"Internal error in Filter SQP."};
00718 
00719   std::string
00720   FilterSolver::UnsolvedFilterError::solverName_ =
00721     "filterSqp";
00722 
00723   const std::string&
00724   FilterSolver::UnsolvedFilterError::errorName() const
00725   {
00726     return errorNames_[0];
00727   }
00728 
00729   const std::string&
00730   FilterSolver::UnsolvedFilterError::solverName() const
00731   {
00732     return solverName_;
00733   }
00734 
00735   bool
00736   FilterSolver::setWarmStart(const CoinWarmStart * warm,
00737       Ipopt::SmartPtr<TMINLP2TNLP> tnlp)
00738   {
00739     if (warm == NULL || cached_.IsNull()) {
00740       cached_ = new cachedInfo(GetRawPtr(tnlp), options_);
00741     }
00742     if(warm == NULL) return 1;
00743     const FilterWarmStart * warmF = dynamic_cast<const FilterWarmStart *> (warm);
00744     assert(warmF);
00745     if (warmF->empty())//reset initial point and leave
00746     {
00747       warmF_ = NULL;
00748       disableWarmStart();
00749       return 1;
00750     }
00751     enableWarmStart();
00752     warmF_ = dynamic_cast<FilterWarmStart *> (warmF->clone());
00753     return true;
00754   }
00755 
00756   CoinWarmStart *
00757   FilterSolver::getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const
00758   {
00759     return new FilterWarmStart(cached_->n, cached_->x,
00760         cached_->n+cached_->m, cached_->lam,
00761         cached_->maxiWk, cached_->lws, cached_->istat);
00762   }
00763 
00764   CoinWarmStart *
00765   FilterSolver::getEmptyWarmStart() const
00766   {
00767     return new FilterWarmStart;
00768   }
00769 
00770 
00772   bool 
00773   FilterSolver::warmStartIsValid(const CoinWarmStart * ws) const{
00774     const FilterWarmStart* fws = dynamic_cast<const FilterWarmStart*>(ws);
00775     if (fws && ! fws->empty()) {
00776       return true;
00777     }
00778     return false;
00779   }
00780 
00781 
00782 }//end namespace Bonmin

Generated on Thu Nov 10 03:05:42 2011 by  doxygen 1.4.7