Bonmin  1.7
BonFilterSolver.hpp
Go to the documentation of this file.
00001 // (C) Copyright International Business Machines Corporation, 2006, 2007
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 #ifndef FilterSolver_H
00011 #define FilterSolver_H
00012 
00013 #include "BonTNLPSolver.hpp"
00014 #include "BonFilterTypes.hpp"
00015 #include "BonBqpdSolver.hpp"
00016 #include "BonFilterWarmStart.hpp"
00017 
00018 namespace Bonmin
00019 {
00020   class FilterSolver : public TNLPSolver
00021   {
00022   public:
00023 
00024     friend struct BqpdSolver::cachedInfo;
00025 
00026   class UnsolvedFilterError: public TNLPSolver::UnsolvedError
00027     {
00028     public:
00029       UnsolvedFilterError(int errorNum,
00030           Ipopt::SmartPtr<TMINLP2TNLP> model,
00031           const std::string &name):
00032           TNLPSolver::UnsolvedError(errorNum, model, name)
00033       {}
00034       virtual const std::string& errorName() const;
00035 
00036       virtual const std::string& solverName() const;
00037       virtual ~UnsolvedFilterError()
00038       {}
00039 
00040     private:
00041       static std::string errorNames_[1];
00042       static std::string solverName_;
00043     };
00044 
00046     typedef FilterTypes::fint fint;
00048     typedef FilterTypes::real real;
00049 
00050 
00051     virtual UnsolvedError * newUnsolvedError(int num,
00052         Ipopt::SmartPtr<TMINLP2TNLP> problem,
00053         std::string name)
00054     {
00055       return new UnsolvedFilterError(num, problem, name);
00056     }
00057 
00058 
00060     FilterSolver(bool createEmpty = false);
00061 
00062 
00064     FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
00065         Ipopt::SmartPtr<Ipopt::OptionsList> options,
00066         Ipopt::SmartPtr<Ipopt::Journalist> journalist,
00067         const std::string & prefix
00068                 );
00069 
00071     FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
00072         Ipopt::SmartPtr<Ipopt::OptionsList> options,
00073         Ipopt::SmartPtr<Ipopt::Journalist> journalist
00074                 );
00075 
00077     FilterSolver(const FilterSolver & other);
00079     virtual ~FilterSolver();
00080 
00083     virtual bool Initialize(std::string params_file);
00084 
00087     virtual bool Initialize(std::istream& is);
00088 
00091 
00092     virtual ReturnStatus OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp);
00093 
00095     virtual ReturnStatus ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp);
00096 
00098     virtual bool setWarmStart(const CoinWarmStart * warm,
00099         Ipopt::SmartPtr<TMINLP2TNLP> tnlp);
00100 
00102    virtual CoinWarmStart * getUsedWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const{
00103      if(warmF_.IsValid())
00104        return new FilterWarmStart(*warmF_);
00105      else return NULL;
00106    }
00107 
00108 
00110     virtual CoinWarmStart * getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const;
00111 
00112     virtual CoinWarmStart * getEmptyWarmStart() const;
00113 
00115     virtual bool warmStartIsValid(const CoinWarmStart * ws) const;
00116 
00117 
00118     virtual void enableWarmStart()
00119     {//No options to be set
00120     }
00121     virtual void disableWarmStart()
00122     {
00123       warmF_ = NULL;
00124      //No options to be set
00125     }
00127 
00129     virtual Ipopt::SmartPtr<TNLPSolver> clone();
00130 
00132     virtual double CPUTime()
00133     {
00134       return (cached_.IsValid()) ? cached_->cpuTime_: 0.;
00135     }
00136 
00138     virtual int IterationCount()
00139     {
00140       return (cached_.IsValid()) ? cached_->istat[1]:0;
00141     }
00142 
00144     virtual void setOutputToDefault()
00145     {
00146       if (cached_.IsValid()) cached_->iprint = default_log_level_;
00147     }
00149     virtual void forceSolverOutput(int log_level)
00150     {
00151       if (cached_.IsValid()) cached_->iprint = log_level;
00152     }
00153 
00155     virtual std::string & solverName()
00156     {
00157       return solverName_;
00158     }
00159 
00161     void registerOptions()
00162     {
00163       registerOptions(roptions_);
00164     }
00165 
00167     virtual int errorCode() const
00168     {
00169       return -1;
00170     }
00172     static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions);
00173   private:
00177     TNLPSolver::ReturnStatus callOptimizer();
00179     Coin::SmartPtr<FilterWarmStart> warmF_;
00180 
00182   struct cachedInfo : public Coin::ReferencedObject
00183     {
00184       fint n;
00185       fint m;
00186       fint nnz_h_;
00187       fint kmax;
00188       fint maxa;
00189       fint maxf;
00190       fint mlp;
00191       fint maxWk;
00192       fint maxiWk;
00193       fint iprint;
00194       fint nout;
00195       fint ifail;
00196       real rho;
00197       real * x;
00198       real * c;
00199       real f;
00200       real fmin;
00201       real * bounds;
00202       real * s;
00203       real * a;
00204       fint * la;
00205       real * ws;
00206       fint * lws;
00207       real * lam;
00208       char * cstype;
00209       fint maxiter;
00210       fint * istat;
00211       real * rstat;
00212       Ipopt::TNLP * tnlp_;
00213       fint * hStruct_;
00214       int * permutationJac_;
00215       int * permutationHess_;
00217       double cpuTime_;
00220       bool use_warm_start_in_cache_;
00221 
00222 
00224       cachedInfo():
00225           n(-1),
00226           m(-1),
00227           nnz_h_(-1),
00228           kmax(-1),
00229           maxa(-1),
00230           maxf(-1),
00231           mlp(-1),
00232           maxWk(-1),
00233           maxiWk(-1),
00234           iprint(-1),
00235           nout(6),
00236           ifail(-100),
00237           rho(0),
00238           x(NULL),
00239           c(NULL),
00240           f(1e100),
00241           fmin(-1e100),
00242           bounds(NULL),
00243           s(NULL),
00244           a(NULL),
00245           la(NULL),
00246           ws(NULL),
00247           lws(NULL),
00248           lam(NULL),
00249           cstype(NULL),
00250           maxiter(1000),
00251           istat(NULL),
00252           rstat(NULL),
00253           tnlp_(NULL),
00254           hStruct_(NULL),
00255           permutationJac_(NULL),
00256           permutationHess_(NULL),
00257           cpuTime_(0),
00258           use_warm_start_in_cache_(false)
00259       {}
00260 
00261       cachedInfo(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp,
00262           Ipopt::SmartPtr<Ipopt::OptionsList>& options):
00263           n(-1),
00264           m(-1),
00265           nnz_h_(-1),
00266           kmax(-1),
00267           maxa(-1),
00268           maxf(-1),
00269           mlp(-1),
00270           maxWk(-1),
00271           maxiWk(-1),
00272           iprint(-1),
00273           nout(6),
00274           ifail(0),
00275           rho(0),
00276           x(NULL),
00277           c(NULL),
00278           f(1e100),
00279           fmin(-1e100),
00280           bounds(NULL),
00281           s(NULL),
00282           a(NULL),
00283           la(NULL),
00284           ws(NULL),
00285           lws(NULL),
00286           lam(NULL),
00287           cstype(NULL),
00288           maxiter(1000),
00289           istat(NULL),
00290           rstat(NULL),
00291           tnlp_(NULL),
00292           hStruct_(NULL),
00293           permutationJac_(NULL),
00294           permutationHess_(NULL),
00295           cpuTime_(0),
00296           use_warm_start_in_cache_(false)
00297       {
00298         initialize(tnlp, options);
00299       }
00300 
00302       void initialize(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp,
00303           Ipopt::SmartPtr<Ipopt::OptionsList>& options);
00304 
00306       void optimize();
00307 
00309       ~cachedInfo()
00310       {
00311         delete [] x;
00312         delete [] c;
00313         delete [] bounds;
00314         delete [] s;
00315         delete [] a;
00316         delete [] la;
00317         delete [] ws;
00318         delete [] lws;
00319         delete [] lam;
00320         delete [] cstype;
00321         delete [] istat;
00322         delete [] rstat;
00323         delete [] permutationJac_;
00324         delete [] permutationHess_;
00325         delete [] hStruct_;
00326         tnlp_ = NULL;
00327       }
00328 
00329       void load_ws(Coin::SmartPtr<FilterWarmStart>);
00330     };
00331 
00333     Coin::SmartPtr<cachedInfo> cached_;
00334 
00335     //name of solver (Filter)
00336     static std::string  solverName_;
00337 
00339      int default_log_level_;
00341     static void TMat2RowPMat(bool symmetric, fint n, fint m, int nnz, const Ipopt::Index* iRow,
00342         const Ipopt::Index* iCol, int * permutation2,
00343         fint * lws, int nnz_offset, int n_offset,
00344         Ipopt::TNLP::IndexStyleEnum index_style);
00345   };
00346 
00347 }// end namespace Bonmin
00348 #endif