/home/coin/SVN-release/OS-2.0.0/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     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     Number * mlam = NULL;
00143     if (*m > 0) {
00144       mlam = new Number[*m];
00145     }
00146     for (int i = 0; i<*m; i++) {
00147       mlam[i] = -lam[*n+i];
00148     }
00149     Number * values = new 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 Index* rowIndices;
00164     const 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 Index* iRow,
00175       const 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       Index* tmpRow = new Index[nnz];
00185       Index* tmpCol = new Index[nnz];
00186       for (int i=0; i<nnz; i++) {
00187         const Index& irow = iRow[i];
00188         const 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 #if 0
00243   // Convert a sparse matrix from triplet format to row ordered packed matrix
00244   void TMat2RowPMat_(fint n, fint m, int nnz, const Index* iRow,
00245       const Index* iCol, int * permutation2,
00246       fint * lws, int offset)
00247   {
00248     for (int i = 0 ; i < nnz ; i++)
00249       permutation2[i] = i;
00250 
00251     Transposer lt;
00252     lt.rowIndices = iRow;
00253     lt.colIndices = iCol;
00254 
00255     std::sort(permutation2, &permutation2[nnz], lt);
00256 
00257     fint row = 1;
00258     lws[0] = nnz + offset + 1;
00259     fint * inds = lws + offset + 1;
00260     fint * start = inds + nnz + 1;
00261 
00262     for (fint i = 0 ; i < nnz ; i++) {
00263       inds[i] = iCol[permutation2[i]];
00264       //DBG_ASSERT(RowJac[permutation2[i]] >= row);
00265       if (iRow[permutation2[i]] >= row) {
00266         for (;row <= iRow[permutation2[i]] ; row++)
00267           *start++ = i + offset + 1;
00268       }
00269     }
00270     for (;row <= m+1 ; row++)
00271       *start++ = nnz + offset +1;
00272 
00273     //deleteme
00274     for (int i = 0; i<offset+1; i++)
00275       printf("alws[%3d] = %3d\n", i, lws[i]);
00276     for (int i = offset+1; i<offset+nnz+1; i++)
00277       printf("alws[%3d] = %3d  [%3d,%3d]\n", i, lws[i], lt.rowIndices[permutation2[i-offset-1]], lt.colIndices[permutation2[i-offset-1]]);
00278     for (int i = offset+nnz+1; i<lws[0]+m+2; i++)
00279       printf("alws[%3d] = %3d\n", i, lws[i]);
00280   }
00281 
00282 
00283   // Convert a sparse matrix from triplet format to row ordered packed matrix
00284   void TMat2ColPMat_(fint n, fint m, int nnz, const Index* iRow,
00285       const Index* iCol,
00286       int* permutationHess2, fint * lws, int offset)
00287   {
00288     for (int i = 0 ; i < nnz ; i++)
00289       permutationHess2[i] = i;
00290 
00291     fint col = 1;
00292     lws[0] = nnz + offset + 1;
00293     fint * inds = lws + 1;
00294     fint * start = inds + nnz + offset;
00295 
00296     Transposer lt;
00297     lt.rowIndices = iCol;
00298     lt.colIndices = iRow;
00299 
00300     std::sort(permutationHess2, permutationHess2 + nnz, lt);
00301 
00302     for (fint i = 0 ; i < nnz ; i++) {
00303       inds[offset + i] = iRow[permutationHess2[i]];
00304       if (iCol[permutationHess2[i]] >= col) {
00305         for (;col <= iCol[permutationHess2[i]] ; col++)
00306           *start++ = i + offset + 1;
00307       }
00308     }
00309     for (;col <= n+1 ; col++)
00310       *start++ = nnz + offset +1;
00311 
00312     //deleteme
00313     for (int i = 0; i<offset+1; i++)
00314       printf("blws[%3d] = %3d\n", i, lws[i]);
00315     for (int i = offset+1; i<offset+nnz+1; i++)
00316       printf("blws[%3d] = %3d  [%3d,%3d]\n", i, lws[i], lt.rowIndices[permutationHess2[i-offset-1]], lt.colIndices[permutationHess2[i-offset-1]]);
00317     for (int i = offset+nnz+1; i<lws[0]+m+2; i++)
00318       printf("blws[%3d] = %3d\n", i, lws[i]);
00319   }
00320 #endif
00321 
00322 
00323   std::string FilterSolver::solverName_ = "filter SQP";
00324 
00325   void
00326   FilterSolver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions)
00327   {
00328     roptions->SetRegisteringCategory("FilterSQP options", RegisteredOptions::FilterCategory);
00329     roptions->AddLowerBoundedNumberOption("eps", "Tolerance for SQP solver",
00330         0., 1, 1e-08, "");
00331     roptions->AddLowerBoundedNumberOption("infty","A large number (1E20)",0.,1, 1e20, "");
00332     roptions->AddBoundedIntegerOption("iprint", "Print level (0=silent, 3=verbose)", 0,6,0);
00333     roptions->AddLowerBoundedIntegerOption("kmax", "Dimension of null-space",
00334         -1, -1, "");
00335     roptions->AddLowerBoundedIntegerOption("maxf","Maximum filter length",0,100);
00336     roptions->AddLowerBoundedIntegerOption("maxiter", "Maximum number of iterations",0,1000);
00337     roptions->AddLowerBoundedIntegerOption("mlp","Maximum level for degeneracy (bqpd)",0, 1000);
00338     roptions->AddLowerBoundedIntegerOption("mxlws", "FINTEGER workspace increment", 0, 500000);
00339     roptions->AddLowerBoundedIntegerOption("mxws", "REAL workspace increment",
00340         0,2000000);
00341     roptions->AddLowerBoundedNumberOption("rho_init", "Initial trust region size",0,1,10.);
00342     //  roption->AddLowerBoundedIntegerOption("timing", "whether to time evaluations (1 = yes)");
00343     roptions->AddLowerBoundedNumberOption("tt", "Parameter for upper bound on filter",0,1, 125e-2);
00344     roptions->AddLowerBoundedNumberOption("ubd", "Parameter for upper bound on filter", 0 , 1,1e2);
00345 
00346   }
00347 
00348 
00349   FilterSolver::FilterSolver(bool createEmpty /* = false */)
00350       :
00351       TNLPSolver(),
00352       warmF_(NULL),
00353       cached_(NULL)
00354   {}
00355 
00356   FilterSolver::FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
00357       Ipopt::SmartPtr<Ipopt::OptionsList> options,
00358       Ipopt::SmartPtr<Ipopt::Journalist> journalist):
00359       TNLPSolver(roptions, options, journalist),
00360       warmF_(NULL),
00361       cached_(NULL)
00362   {}
00363 
00364 
00365   Ipopt::SmartPtr <TNLPSolver>
00366   FilterSolver::clone()
00367   {
00368     Ipopt::SmartPtr<FilterSolver> retval = new FilterSolver(true);
00369     *retval->options_ = *options_; // Copy the options
00370     retval->roptions_ = roptions_; // only copy pointers of registered options
00371     retval->journalist_ = journalist_; // and journalist
00372     retval->warmF_ = (warmF_.IsValid()) ? dynamic_cast<FilterWarmStart *>(warmF_->clone()):NULL;
00373     return GetRawPtr(retval);
00374   }
00375 
00376   FilterSolver::~FilterSolver()
00377   {}
00378 
00379   bool
00380   FilterSolver::Initialize(std::string optFile)
00381   {
00382     std::ifstream is;
00383     if (optFile != "") {
00384       try {
00385         is.open(optFile.c_str());
00386       }
00387       catch (std::bad_alloc) {
00388         journalist_->Printf(Ipopt::J_SUMMARY, Ipopt::J_MAIN, "\nEXIT: Not enough memory.\n");
00389         return false;
00390       }
00391       catch (...) {
00392         Ipopt::IpoptException E("Unknown Exception caught in ipopt", "Unknown File", -1);
00393         E.ReportException(*journalist_);
00394         return false;
00395       }
00396     }
00397     bool retval = Initialize(is);
00398     if (is) {
00399       is.close();
00400     }
00401     return retval;
00402   }
00403 
00404   bool
00405   FilterSolver::Initialize(std::istream &is)
00406   {
00407 
00408     Index ivalue;
00409     options_->GetIntegerValue("print_level", ivalue, "");
00410     EJournalLevel print_level = (EJournalLevel)ivalue;
00411     SmartPtr<Journal> stdout_jrnl = journalist_->GetJournal("console");
00412     if (IsValid(stdout_jrnl)) {
00413       // Set printlevel for stdout
00414       stdout_jrnl->SetAllPrintLevels(print_level);
00415       stdout_jrnl->SetPrintLevel(J_DBG, J_NONE);
00416     }
00417 
00418     if (is.good()) {
00419       options_->ReadFromStream(*journalist_, is);
00420     }
00421     return true;
00422   }
00423 
00425   TNLPSolver::ReturnStatus
00426   FilterSolver::OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp)
00427   {
00428     if (cached_.IsNull() || !cached_->use_warm_start_in_cache_) {
00429       cached_ = new cachedInfo(tnlp, options_);
00430     }
00431     cached_->load_ws(warmF_);
00432     return callOptimizer();
00433   }
00434 
00436   TNLPSolver::ReturnStatus
00437   FilterSolver::ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp)
00438   {
00439     assert(tnlp == cached_->tnlp_);
00440     cached_->load_ws(warmF_);
00441     //rescan bounds which may have changed
00442     assert(cached_->bounds);
00443     int n = cached_->n;
00444     int m = cached_->m;
00445     tnlp->get_bounds_info(n, cached_->bounds, &cached_->bounds[n+m],
00446         m, &cached_->bounds[n], &cached_->bounds[2*n + m]);
00447 
00448     tnlpSolved = static_cast<Ipopt::TNLP *>(Ipopt::GetRawPtr(tnlp));
00449     nnz_h = cached_->nnz_h_;
00450 
00451     hStruct = cached_->hStruct_;
00452 
00453 //Permutation to apply to jacobian in order to get it row ordered
00454     permutationJac = cached_->permutationJac_;
00455     permutationHess = cached_->permutationHess_;
00456 
00457 
00458     return callOptimizer();
00459   }
00460 
00461 
00462 
00463   void
00464   FilterSolver::cachedInfo::initialize(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp,
00465       Ipopt::SmartPtr<Ipopt::OptionsList>& options)
00466   {
00467     // 1) Get some dimensions
00468     // 1.a) First from ampl
00469     int  nnz_jac_g;
00470 
00471     Ipopt::TNLP::IndexStyleEnum index_style;
00472     Ipopt::Index nv, nc, nnz_j, nnz_hess;
00473     tnlp->get_nlp_info( nv, nc,
00474         nnz_j, (Ipopt::Index&) nnz_hess,
00475         index_style);
00476     n = nv;
00477     m = nc;
00478     nnz_jac_g = nnz_j;
00479     nnz_h_ = nnz_hess;
00480 
00481     nnz_h = nnz_h_;
00482 
00483 
00484     // 1.b) then from options
00485     Ipopt::Index kmax_ipt;
00486     options->GetIntegerValue("kmax", kmax_ipt, "filter.");
00487     if (kmax_ipt == -1) {
00488       kmax = n;
00489     }
00490     else {
00491       kmax = kmax_ipt;
00492       kmax = std::min(kmax,n);
00493     }
00494     Ipopt::Index mlp_ipt;
00495     options->GetIntegerValue("mlp", mlp_ipt,"filter.");
00496     mlp = mlp_ipt;
00497 
00498     Ipopt::Index  maxf_ipt;
00499     options->GetIntegerValue("maxf", maxf_ipt,"filter.");
00500     maxf = maxf_ipt;
00501 
00502     fint mxwk0;
00503     Ipopt::Index mxwk0_ipt;
00504     options->GetIntegerValue("mxws", mxwk0_ipt, "filter.");
00505     mxwk0 = mxwk0_ipt;
00506 
00507     fint mxiwk0;
00508     Ipopt::Index mxiwk0_ipt;
00509     options->GetIntegerValue("mxlws",  mxiwk0_ipt, "filter.");
00510     mxiwk0 = mxiwk0_ipt;
00511     // Setup storage for Filter
00512     int nplusm = n + m;
00513     //Starting point
00514     x = new real [n];
00515 
00516     //tnlp->get_starting_point(n, 1, x, 0, NULL, NULL, m, 0, NULL);
00517     use_warm_start_in_cache_ = false;
00518     //for(int i = 0 ; i < n ; i++) x[i] = 0;
00519     lam = new real [n+m];
00520 #ifdef InitializeAll
00521     for (int i = 0 ; i < n+m ; i++) lam[i] = 0.;
00522 #endif
00523     //bounds
00524     bounds = new real [2*n + 2*m];
00525 
00526     tnlp->get_bounds_info(n, bounds, bounds + nplusm, m, bounds + n, bounds + n + nplusm);
00527 
00528 #if 0
00529     double infty = F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).infty;
00530     // AW: I don't think we need this, it isn't done for ReOptimize either
00531     for (int i = 0 ; i < nplusm ; i++) {
00532       if (bounds[i] < -infty) bounds[i] = - infty;
00533     }
00534 
00535     real * ubounds = bounds + nplusm;
00536     for (int i = 0 ; i < nplusm ; i++) {
00537       if (ubounds[i] > infty) ubounds[i] = infty;
00538     }
00539 #endif
00540 
00541     maxa = n + nnz_jac_g;
00542     fint maxia = n + nnz_jac_g + m + 3;
00543     a = new real[maxa];
00544     la = new fint [maxia];
00545 
00546     int * RowJac = new int [nnz_jac_g];
00547     int * ColJac = new int [nnz_jac_g];
00548 
00549     la[nnz_jac_g + n + 1] = 1;
00550 
00551     for (fint i = 1; i <= n ; i++)
00552       la[i] = i;// - (index_style == Ipopt::TNLP::C_STYLE);
00553     tnlp->eval_jac_g(  nv, NULL, 0, nc , nnz_j,  RowJac,  ColJac, NULL);
00554 
00555     permutationJac = permutationJac_ = new int [nnz_jac_g];
00556     TMat2RowPMat(false, n, m, nnz_jac_g,  RowJac, ColJac, permutationJac,
00557         la, n, 1, index_style);
00558 
00559     delete [] RowJac;
00560     delete [] ColJac;
00561 
00562     // Now setup hessian
00563     permutationHess = permutationHess_ = new int[nnz_h];
00564     hStruct_ = new fint[nnz_h + n + 3];
00565     int * cache = new int[2*nnz_h + 1];
00566     F77_FUNC(hessc,HESSC).phl = 1;
00567     tnlp->eval_h((Ipopt::Index&) n, NULL, 0, 1., (Ipopt::Index&) m, NULL, 0, (Ipopt::Index&) nnz_h, cache + nnz_h, cache  , NULL);
00568 
00569     TMat2RowPMat(true, n, n, nnz_h, cache, cache + nnz_h, permutationHess,
00570         hStruct_, 0, 0, index_style);
00571 
00572     delete [] cache;
00573     // work arrays
00574     fint lh1 = nnz_h + 8 + 2 * n + m;
00575     maxWk = 21*n + 8*m + mlp + 8*maxf + lh1 + kmax*(kmax+9)/2 + mxwk0;
00576     maxiWk = 13*n + 4*m + mlp + lh1 + kmax + 113 + mxiwk0;
00577 
00578     ws = new real[maxWk];
00579     lws = new fint[maxiWk];
00580 #ifdef InitializeAll
00581     for (int i = 0 ; i < maxWk ; i++) ws[i] = 0;
00582     for (int i = 0 ; i < maxiWk ; i++) lws[i] = 0;
00583 #endif
00584 
00585     // Setup global variables and static variables
00586     hStruct = hStruct_;
00587     tnlpSolved = static_cast<Ipopt::TNLP *>(Ipopt::GetRawPtr(tnlp));
00588 
00589     options->GetNumericValue("ubd",F77_FUNC(ubdc,UBDC).ubd, "filter.");
00590     options->GetNumericValue("tt", F77_FUNC(ubdc,UBDC).tt, "filter.");
00591     options->GetNumericValue("eps", F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).eps, "filter.");
00592     options->GetNumericValue("infty", F77_FUNC_(nlp_eps_inf,NLP_EPS_INF).infty, "filter.");
00593     rho = 10.;
00594     maxiter = 1000;
00595     options->GetIntegerValue("maxiter", (Ipopt::Index &) maxiter, "filter.");
00596     options->GetNumericValue("rho_init",rho,"filter.");
00597 
00598 
00599     // Set up scaling
00600     F77_FUNC(scalec,SCALEC).scale_mode = 0;
00601     s = new real [n+m];
00602 
00603     istat = new fint[14];
00604     rstat = new real[7];
00605 #ifdef InitializeAll
00606     for (int i=0; i<14; i++) {
00607       istat[0] = 43;
00608     }
00609     for (int i=0; i<7; i++) {
00610       rstat[0] = 42.;
00611     }
00612 #endif
00613 
00614     fmin = -1e100;
00615     Ipopt::Index bufy;
00616     options->GetIntegerValue("iprint",bufy, "filter.");
00617     iprint = bufy;
00618     nout = 6;
00619     cstype = new char[m];
00620     Ipopt::TNLP::LinearityType * const_types =
00621       new Ipopt::TNLP::LinearityType[m];
00622     tnlp->get_constraints_linearity(m, const_types);
00623     for (int i = 0 ; i < m ; i++) {
00624       if (const_types[i] == Ipopt::TNLP::LINEAR) {
00625         cstype[i] = 'L';
00626       }
00627       else
00628         cstype[i] = 'N';
00629     }
00630     delete [] const_types;
00631     c = new double[m];
00632     tnlp_ = Ipopt::GetRawPtr(tnlp);
00633   }
00634 
00636   TNLPSolver::ReturnStatus
00637   FilterSolver::callOptimizer()
00638   {
00639     cached_->optimize();
00640 
00641     TNLPSolver::ReturnStatus optimizationStatus = TNLPSolver::exception;
00642     Ipopt::SolverReturn status = Ipopt::INTERNAL_ERROR;
00643     fint ifail = cached_->ifail;
00644     switch (ifail) {
00645     case 0:
00646       optimizationStatus = TNLPSolver::solvedOptimal;
00647       status = Ipopt::SUCCESS;
00648       break;
00649     case 1:
00650       optimizationStatus = TNLPSolver::unbounded;
00651       status = Ipopt::DIVERGING_ITERATES;
00652     case 2:
00653     case 3:
00654     case 4:
00655       optimizationStatus = TNLPSolver::provenInfeasible;
00656       status = Ipopt::LOCAL_INFEASIBILITY;
00657       break;
00658     case 5:
00659     case 6:
00660     case 8:
00661       optimizationStatus = TNLPSolver::iterationLimit;
00662       status = Ipopt::MAXITER_EXCEEDED;
00663       break;
00664     case 7:
00665       optimizationStatus = TNLPSolver::externalException;
00666       status = Ipopt::INTERNAL_ERROR;
00667       break;
00668     case 9:
00669     case 10:
00670       optimizationStatus = TNLPSolver::exception;
00671       status = Ipopt::INTERNAL_ERROR;
00672       break;
00673     }
00674 
00675     Number* mlam = NULL;
00676     if (cached_->m>0) {
00677       mlam = new Number[cached_->m];
00678     }
00679     for (int i = 0; i<cached_->m; i++) {
00680       mlam[i] = -cached_->lam[cached_->n+i];
00681     }
00682     Number* z_L = new Number[cached_->n];
00683     Number* z_U = new Number[cached_->n];
00684     const int os = cached_->n+cached_->m;
00685     for (int i=0; i<cached_->n; i++) {
00686       if (cached_->x[i] == cached_->bounds[i]) {
00687         z_L[i] = Max(0.,cached_->lam[i]);
00688       }
00689       else {
00690         z_L[i] = 0.;
00691       }
00692       if (cached_->x[i] == cached_->bounds[os+i]) {
00693         z_U[i] = Max(0.,-cached_->lam[i]);
00694       }
00695       else {
00696         z_U[i] = 0.;
00697       }
00698     }
00699     cached_->tnlp_->finalize_solution(status, cached_->n,
00700         cached_->x, z_L, z_U,
00701         cached_->m, cached_->c, mlam,
00702         cached_->f, NULL, NULL);
00703     delete [] mlam;
00704     delete [] z_L;
00705     delete [] z_U;
00706     return optimizationStatus;
00707   }
00709   void
00710   FilterSolver::cachedInfo::load_ws(Coin::SmartPtr<FilterWarmStart> warmF){
00711     if(!warmF.IsValid()) return;
00712     const fint xsize = warmF->primalSize();
00713     const real* xarray = warmF->primal();
00714     for (int i = 0; i<xsize; i++) {
00715       x[i] = xarray[i];
00716     }
00717     CoinCopyN(warmF->dual(), warmF->dualSize(), lam);
00718     CoinCopyN(warmF->lwsArray(), warmF->lwsSize(), lws);
00719     for (int i = 0 ; i < 14 ; i ++) {
00720       istat[i] = warmF->istat()[i];
00721     }
00722     use_warm_start_in_cache_ = true;
00723   }
00725   void
00726   FilterSolver::cachedInfo::optimize()
00727   {
00728     if (use_warm_start_in_cache_) {
00729       ifail = -1;
00730       use_warm_start_in_cache_ = false;
00731     }
00732     else {
00733       tnlp_->get_starting_point(n, 1, x, 0, NULL, NULL, m, 0, NULL);
00734       ifail = 0;
00735     }
00736     cpuTime_ = - CoinCpuTime();
00737     fint cstype_len = 1;
00738     rho = 10;
00739     //  rho = 1e6;
00740     //  printf("rho = %e\n", rho);
00741 #if 0
00742     printf("========= 3333333333333333 =============\n");
00743     for (int i=0; i<n; i++) {
00744       printf("xL[%3d] = %15.8e  xU[%3d] = %15.8e\n", i, bounds[i], i, bounds[m+n+i]);
00745     }
00746     for (int i=0; i<m; i++) {
00747       printf("gL[%3d] = %15.8e  gU[%3d] = %15.8e\n", i, bounds[n+i], i, bounds[m+2*n+i]);
00748     }
00749 #endif
00750 #if 0
00751     for (int i=0; i<n; i++) {
00752       printf("fxstart[%2d] = %23.16e\n", i, x[i]);
00753     }
00754 #endif
00755     F77_FUNC(filtersqp,FILTERSQP)(&n, &m, &kmax, & maxa, &maxf, &mlp, &maxWk,
00756         &maxiWk, &iprint, &nout, &ifail, &rho, x,
00757         c, &f, &fmin, bounds,
00758         bounds + n + m,
00759         s, a, la,
00760         ws, lws, lam, cstype,
00761         NULL, NULL,
00762         &maxiter, istat, rstat,
00763         cstype_len);
00764 #if 0
00765     for (int i=0; i<n; i++) {
00766       printf("fxsol[%2d] = %23.16e\n", i, x[i]);
00767     }
00768 #endif
00769 #if 0
00770     printf("final f = %e\n", f);
00771     printf("ifail = %d\n", ifail);
00772 #endif
00773     cpuTime_ += CoinCpuTime();
00774   }
00775 
00776   std::string
00777   FilterSolver::UnsolvedFilterError::errorNames_[1] =
00778     {"Internal error in Filter SQP."};
00779 
00780   std::string
00781   FilterSolver::UnsolvedFilterError::solverName_ =
00782     "filterSqp";
00783 
00784   const std::string&
00785   FilterSolver::UnsolvedFilterError::errorName() const
00786   {
00787     return errorNames_[0];
00788   }
00789 
00790   const std::string&
00791   FilterSolver::UnsolvedFilterError::solverName() const
00792   {
00793     return solverName_;
00794   }
00795 
00796   bool
00797   FilterSolver::setWarmStart(const CoinWarmStart * warm,
00798       Ipopt::SmartPtr<TMINLP2TNLP> tnlp)
00799   {
00800     if (warm == NULL || cached_.IsNull()) {
00801       cached_ = new cachedInfo(GetRawPtr(tnlp), options_);
00802     }
00803     if(warm == NULL) return 1;
00804     const FilterWarmStart * warmF = dynamic_cast<const FilterWarmStart *> (warm);
00805     assert(warmF);
00806     if (warmF->empty())//reset initial point and leave
00807     {
00808       warmF_ = NULL;
00809       disableWarmStart();
00810       return 1;
00811     }
00812     enableWarmStart();
00813     warmF_ = dynamic_cast<FilterWarmStart *> (warmF->clone());
00814     return true;
00815   }
00816 
00817   CoinWarmStart *
00818   FilterSolver::getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const
00819   {
00820     return new FilterWarmStart(cached_->n, cached_->x,
00821         cached_->n+cached_->m, cached_->lam,
00822         cached_->maxiWk, cached_->lws, cached_->istat);
00823   }
00824 
00825   CoinWarmStart *
00826   FilterSolver::getEmptyWarmStart() const
00827   {
00828     return new FilterWarmStart;
00829   }
00830 
00831 
00833   bool 
00834   FilterSolver::warmStartIsValid(const CoinWarmStart * ws) const{
00835     const FilterWarmStart* fws = dynamic_cast<const FilterWarmStart*>(ws);
00836     if (fws && ! fws->empty()) {
00837       return true;
00838     }
00839     return false;
00840   }
00841 
00842 
00843 }//end namespace Bonmin

Generated on Mon Aug 3 03:02:19 2009 by  doxygen 1.4.7