optimizer_tests.cc

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00050 #define EPSILON 1.0e-4
00051 #include "optimizer.h"
00052 class Rosen {
00053 public:
00054   Rosen() {
00055     dimension_ = 2;
00056     initval_.Init(dimension_);
00057     initval_[0]=0.;
00058     initval_[1]=0.;
00059   };
00060 
00061   ~Rosen(){}; 
00062 
00063   void Init(Vector &x) {
00064     dimension_ = x.length();
00065     initval_.Copy(x);
00066   }
00067 
00068   void GiveInit(Vector *vec) {
00069     (*vec)[0]=initval_[0];
00070     (*vec)[1]=initval_[1]; 
00071   }
00072 
00073   void ComputeObjective(Vector &x, double *value) {
00074     double x1=x[0];
00075     double x2=x[1];
00076     double f1=(x2-x1*x1);
00077     double f2=1.-x1;
00078     *value = 100. *f1*f1+f2*f2;
00079   } 
00080 
00081   void ComputeGradient(Vector &x, Vector *gx) {
00082     double x1=x[0];
00083     double x2=x[1];
00084     double f1=(x2-x1*x1);
00085     double f2=1.-x1;
00086     (*gx)[0]=-400.*f1*x1-2.*f2;
00087     (*gx)[1]=200.*f1;
00088   }
00089 
00090   void ComputeHessian(Vector &x, Matrix *hx) {
00091     double x1=x[0];
00092     double x2=x[1];
00093     double f1=(x2-x1*x1);
00094     hx->set(0,0,-400.*f1+800.*x1*x1 + 2.);
00095     hx->set(0,1,-400.*x1);
00096     hx->set(1,0,-400.*x1);
00101     hx->set(1,1,200.); 
00102 
00103   }
00104 
00105   void GetBoundConstraint(Vector *lb, Vector *ub) {
00106     (*lb)[0] = -0.5;
00107     (*lb)[1] = -0.5;
00108     (*ub)[0] = 0.5;
00109     (*ub)[1] = 0.5;
00110   }
00111 
00112   void GetLinearEquality(Matrix *a_mat, Vector *b_vec) {
00113     // Providing 1 linear equality constraint
00114 
00115     // Initializing the matrix and vector
00116     a_mat->Init(1, dimension_);
00117     b_vec->Init(1);
00118 
00119     a_mat->set(0,0,1.);
00120     a_mat->set(0,1,1.);
00121     (*b_vec)[0] = 1.;
00122   }
00123 
00124   void GetLinearInequality(Matrix *a_mat, Vector *lb_vec, Vector *ub_vec) {
00125     // Providing 2X2 linear inequality constraints
00126 
00127     // Initializing the matrix and vector
00128     a_mat->Init(2, dimension_);
00129     lb_vec->Init(2);
00130     ub_vec->Init(2);
00131 
00132     a_mat->set(0,0,1.);
00133     a_mat->set(0,1,1.);
00134     a_mat->set(1,0,1.);
00135     a_mat->set(1,1,-2.);
00136     (*lb_vec)[0] = 0.;
00137     (*ub_vec)[0] = 1.;
00138     (*lb_vec)[1] = -0.5;
00139     (*ub_vec)[1] = 0.5;
00140   }
00141 
00142   index_t num_of_non_linear_equalities() {
00143 
00144   }
00145 
00146   void ComputeNonLinearEqualityConstraints(Vector &x, Vector *c) {
00147 
00148   }
00149 
00150   void ComputeNonLinearEqualityConstraintsJacobian(Vector &x, Matrix *c_jacob) {
00151 
00152   }
00153 
00154   index_t num_of_non_linear_inequalities() {
00155 
00156   }
00157 
00158   void ComputeNonLinearInequalityConstraints(Vector &x, Vector *c) {
00159 
00160   }
00161 
00162   void ComputeNonLinearInequalityConstraintsJacobian(Vector &x, Matrix *c_jacobi) {
00163 
00164   }
00165 
00166   void GetNonLinearInequalityConstraintBounds(Vector *lb, Vector *ub) {
00167 
00168   }
00169 
00170   index_t dimension() {
00171     return dimension_;
00172   } 
00173 
00174 private:
00175   index_t dimension_;
00176   Vector initval_;
00177     
00178 };
00179 
00180 
00181 class StaticOptppOptimizerTest {
00182 public:
00183   StaticOptppOptimizerTest(fx_module *module) {
00184     module_ = module;
00185     trueval_.Init(2);
00186     trueval_[0] = 1.0;
00187     trueval_[1] = 1.0;
00188   }
00189 
00190   void TestLBFGS() {
00191     Rosen rosen;
00192     optimizer_LBFGS_.Init(module_, &rosen);
00193     Vector result;
00194     optimizer_LBFGS_.Optimize(&result);  
00195     for (index_t i = 0; i < trueval_.length(); i++) {
00196       DEBUG_WARNING_MSG_IF(fabs(result[i] - trueval_[i]) > EPSILON,
00197                            "LBFGS:True %"LI"d:%lg, Computed %"LI"d:%lg",
00198                            i,trueval_[i], i,result[i]);
00199     }
00200   }
00201 
00202   void TestLBFGS_BC() {
00203     Rosen rosen;
00204     optimizer_LBFGS_BC_.Init(module_, &rosen);
00205     Vector result;
00206     optimizer_LBFGS_BC_.Optimize(&result);
00207   }
00208 
00209   void TestLBFGS_LE() {
00210     Rosen rosen;
00211     optimizer_LBFGS_LE_.Init(module_, &rosen);
00212     Vector result;
00213     optimizer_LBFGS_LE_.Optimize(&result);
00214   }
00215 
00216   void TestLBFGS_LI() {
00217     Rosen rosen;
00218     optimizer_LBFGS_LI_.Init(module_, &rosen);
00219     Vector result;
00220     optimizer_LBFGS_LI_.Optimize(&result);
00221   }
00222 
00223   // Tests to be written
00224   void TestLBFGS_NLE() {}
00225 
00226   void TestLBFGS_NLI() {}
00227 
00228   void TestCG() {
00229     Rosen rosen;
00230     optimizer_CG_.Init(module_, &rosen);
00231     Vector result;
00232     optimizer_CG_.Optimize(&result);  
00233     for (index_t i = 0; i < trueval_.length(); i++) {
00234       DEBUG_WARNING_MSG_IF(fabs(result[i] - trueval_[i]) > EPSILON,
00235                            "CG:True %"LI"d:%lg, Computed %"LI"d:%lg",
00236                            i,trueval_[i], i,result[i]);
00237     }
00238   }
00239   void TestQNewton() {
00240     Rosen rosen;
00241     optimizer_QNewton_.Init( module_, &rosen );
00242     Vector result;
00243     optimizer_QNewton_.Optimize(&result);  
00244     for (index_t i = 0; i < trueval_.length(); i++) {
00245       DEBUG_WARNING_MSG_IF(fabs(result[i] - trueval_[i]) > EPSILON,
00246                            "QNewton:True %"LI"d:%lg, Computed %"LI"d:%lg",
00247                            i,trueval_[i], i,result[i]);
00248     }
00249   }
00250   void TestBFGS() {
00251     Rosen rosen;
00252     optimizer_BFGS_.Init(module_, &rosen);
00253     Vector result;
00254     optimizer_BFGS_.Optimize(&result);  
00255     for (index_t i = 0; i < trueval_.length(); i++) {
00256       DEBUG_WARNING_MSG_IF(fabs(result[i] - trueval_[i]) > EPSILON,
00257                            "BFGS:True %"LI"d:%lg, Computed %"LI"d:%lg",
00258                            i,trueval_[i], i,result[i]);
00259     }
00260   }
00261   void TestFDNewton() {
00262     Rosen rosen;
00263     optimizer_FDNewton_.Init(module_, &rosen);
00264     Vector result;
00265     optimizer_FDNewton_.Optimize(&result);  
00266     for (index_t i = 0; i < trueval_.length(); i++) {
00267       DEBUG_WARNING_MSG_IF(fabs(result[i] - trueval_[i]) > EPSILON,
00268                            "FDNewton:True %"LI"d:%lg, Computed %"LI"d:%lg",
00269                            i,trueval_[i], i,result[i]);
00270     }
00271   }
00272   void TestNewton() {
00273     Rosen rosen;
00274     optimizer_Newton_.Init(module_, &rosen);
00275     Vector result;
00276     optimizer_Newton_.Optimize(&result);  
00277     for (index_t i = 0; i < trueval_.length(); i++) {
00278       DEBUG_WARNING_MSG_IF(fabs(result[i] - trueval_[i]) > EPSILON,
00279                            "Newton:True %"LI"d:%lg, Computed %"LI"d:%lg",
00280                            i,trueval_[i], i,result[i]);
00281     }
00282   }
00283   void TestAll() {
00284     TestLBFGS();
00285     TestLBFGS_BC();
00286     TestLBFGS_LE();
00287     TestLBFGS_LI();
00288     TestLBFGS_NLE();
00289     TestLBFGS_NLI();
00290 
00291     TestCG();
00292     TestQNewton();
00293     TestBFGS();
00294     TestFDNewton();
00295     TestNewton();
00296   }
00297 private:
00298   fx_module *module_;
00299   optim::optpp::StaticOptppOptimizer<optim::optpp::LBFGS, Rosen> optimizer_LBFGS_;
00300   optim::optpp::StaticOptppOptimizer<optim::optpp::LBFGS, Rosen,
00301                                      optim::optpp::BoundConstraint> optimizer_LBFGS_BC_;
00302   optim::optpp::StaticOptppOptimizer<optim::optpp::LBFGS, Rosen, 
00303                                      optim::optpp::LinearEquality> optimizer_LBFGS_LE_;
00304   optim::optpp::StaticOptppOptimizer<optim::optpp::LBFGS, Rosen, 
00305                                      optim::optpp::LinearInequality> optimizer_LBFGS_LI_;
00306   optim::optpp::StaticOptppOptimizer<optim::optpp::CG, Rosen> optimizer_CG_;
00307   optim::optpp::StaticOptppOptimizer<optim::optpp::QNewton, Rosen> optimizer_QNewton_;
00308   optim::optpp::StaticOptppOptimizer<optim::optpp::BFGS, Rosen> optimizer_BFGS_;
00309   optim::optpp::StaticOptppOptimizer<optim::optpp::FDNewton, Rosen> optimizer_FDNewton_;
00310   optim::optpp::StaticOptppOptimizer<optim::optpp::Newton, Rosen> optimizer_Newton_;
00311   Vector trueval_;
00312 };
00313 
00314 int main(int argc, char *argv[]) {
00315   fx_module *fx_root = fx_init(argc, argv, NULL);
00316   StaticOptppOptimizerTest test(fx_root);
00317   test.TestAll();
00318   fx_done(fx_root);
00319   return 0;
00320 }
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3