OptLBFGS.h
00001 #ifndef OptLBFGS_h
00002 #define OptLBFGS_h
00003
00004
00005
00006
00007
00008 #ifndef Opt_h
00009 #include "Opt.h"
00010 #endif
00011
00012 namespace OPTPP {
00013
00022 class OptLBFGSLike: public OptimizeClass {
00023 protected:
00024 virtual NLP1 *nlprob() const = 0;
00025
00026 NEWMAT::ColumnVector gprev;
00027
00028 int grad_evals;
00029
00030 SearchStrategy strategy;
00031
00032 public:
00038 OptLBFGSLike(){}
00043 OptLBFGSLike(int n): OptimizeClass(n),
00044 gprev(n), grad_evals(0), strategy(LineSearch){}
00050 OptLBFGSLike(int n, TOLS t): OptimizeClass(n,t),
00051 gprev(n),grad_evals(0), strategy(LineSearch){}
00052
00056 virtual ~OptLBFGSLike(){}
00057
00059 void setSearchStrategy(SearchStrategy s) {strategy = s;}
00060
00064 SearchStrategy getSearchStrategy() const {return strategy;}
00065
00066 virtual void acceptStep(int, int) = 0;
00067 virtual int checkConvg();
00068 virtual int checkDeriv();
00069 virtual void optimize() {}
00070 virtual void readOptInput() {}
00071 virtual void updateModel(int, int, NEWMAT::ColumnVector) = 0;
00072
00073 };
00074
00099 class OptLBFGS: public OptLBFGSLike {
00100 private:
00101 NLP1* nlp;
00102
00103 int memM;
00104
00105 bool printXs;
00106
00107 protected:
00111 NLP1* nlprob() const { return nlp; }
00112
00113 void initMem(int n) {
00114
00115 if (n>=30) {memM = 15; return;}
00116 if (n>= 2) {memM = 2; return;}
00117 if (n>= 5) {memM = 3; return;}
00118 if (n>=10) {memM = 5; return;}
00119 if (n>=20) {memM = 10; return;}
00120 memM = 1;
00121 }
00122
00123 public:
00124
00130 OptLBFGS(): memM(15), printXs(false) {strcpy(method,"Limited Memory BFGS method");}
00131
00136 OptLBFGS(NLP1* p): OptLBFGSLike(p->getDim()), nlp(p), printXs(false)
00137 {strcpy(method,"Limited Memory BFGS method"); initMem(p->getDim());}
00138
00146 OptLBFGS(NLP1* p, int m): OptLBFGSLike(p->getDim()), nlp(p), printXs(false) {
00147 strcpy(method,"Limited Memory BFGS method");
00148 memM = (m <= p->getDim())? m : p->getDim();
00149 }
00150
00156 OptLBFGS(NLP1* p, TOLS t): OptLBFGSLike(p->getDim(),t), nlp(p), printXs(false)
00157 {strcpy(method,"Limited Memory BFGS method"); initMem(p->getDim());}
00158
00165 OptLBFGS(NLP1* p, TOLS t, int m): OptLBFGSLike(p->getDim(),t), nlp(p), printXs(false) {
00166 strcpy(method,"Limited Memory BFGS method");
00167 memM = (m <= p->getDim())? m : p->getDim();
00168 }
00169
00173 virtual ~OptLBFGS(){}
00174
00175 virtual NEWMAT::ColumnVector
00176 computeSearch(NEWMAT::SymmetricMatrix& ) {return NEWMAT::ColumnVector();}
00177
00178 virtual void acceptStep(int k, int step_type)
00179 {OptimizeClass::defaultAcceptStep(k, step_type);}
00180
00181 virtual void updateModel(int k, int ndim, NEWMAT::ColumnVector x)
00182 {OptimizeClass::defaultUpdateModel(k, ndim, x);}
00183
00184 void setPrintFinalX(bool b) { printXs = b;}
00185
00186
00187
00188
00189 virtual int checkDeriv();
00190 virtual int computeStep(NEWMAT::ColumnVector& sk, double stp=1.0);
00191 virtual void reset();
00192 virtual void initOpt();
00193 virtual void optimize();
00194 virtual real stepTolNorm() const;
00195 virtual void printStatus(char *c);
00196 virtual void printIter(int, double, double, double, double, int);
00197 };
00198
00199 }
00200 #endif