tstcg.C

Test program for CG optimization objects

1. Nonlinear CG with More-Thuente Line Search on an NLF1

2. Nonlinear CG with backtracking Line Search on an NLF1

00001 
00011 #include <fstream>
00012 
00013 #include "OptCG.h"
00014 #include "NLF.h"
00015 #include "tstfcn.h"
00016 
00017 using NEWMAT::ColumnVector;
00018 using namespace OPTPP;
00019 
00020 #define true  1
00021 #define false 0
00022 
00023 void update_model(int, int, ColumnVector) {}
00024 
00025 int main ()
00026 {
00027   int n = 2;
00028   
00029   static char *status_file = {"tstcg.out"};
00030 
00031   //  Create a Nonlinear problem object
00032 
00033   NLF1 nlp(n,rosen,init_rosen);
00034   
00035   //  Build a CG object and optimize 
00036 
00037   OptCG objfcn(&nlp);   
00038   objfcn.setUpdateModel(update_model);
00039   if (!objfcn.setOutputFile(status_file, 0))
00040     cerr << "main: output file open failed" << endl;
00041   objfcn.setGradTol(1.e-6);
00042   objfcn.setMaxBacktrackIter(10);
00043   objfcn.optimize();
00044     
00045   objfcn.printStatus("Solution from CG: Fcn not Expensive");
00046 
00047 #ifdef REG_TEST
00048   ColumnVector x_sol = nlp.getXc();
00049   double f_sol = nlp.getF();
00050   ostream* optout = objfcn.getOutputFile();
00051   if ((1.0 - x_sol(1) <= 1.e-2) && (1.0 - x_sol(2) <= 1.e-2) && (f_sol
00052                                                                  <=
00053                                                                  1.e-2))
00054     *optout << "CG 1 PASSED" << endl;
00055   else
00056     *optout << "CG 1 FAILED" << endl;
00057 #endif
00058 
00059   objfcn.cleanup();
00060 
00061   //  Build a CG object and optimize 
00062 
00063   NLF1 nlp2(n,rosen,init_rosen);
00064   nlp2.setIsExpensive(true);
00065 
00066   OptCG objfcn2(&nlp2);   
00067   objfcn2.setUpdateModel(update_model);
00068   objfcn2.setOutputFile(status_file, 1);
00069   objfcn2.setGradTol(1.e-6);
00070   objfcn2.setLineSearchTol(1.e-4);
00071   objfcn2.setMaxBacktrackIter(10);
00072   objfcn2.optimize();
00073     
00074   objfcn2.printStatus("Solution from CG: Fcn Expensive");
00075 
00076 #ifdef REG_TEST
00077   x_sol = nlp2.getXc();
00078   f_sol = nlp2.getF();
00079   optout = objfcn2.getOutputFile();
00080   if ((1.0 - x_sol(1) <= 1.e-2) && (1.0 - x_sol(2) <= 1.e-2) && (f_sol
00081                                                                  <=
00082                                                                  1.e-2))
00083     *optout << "CG 2 PASSED" << endl;
00084   else
00085     *optout << "CG 2 FAILED" << endl;
00086 #endif
00087 
00088   objfcn2.cleanup();
00089 }

Bug Reports    OPT++ Developers    Copyright Information    GNU Lesser General Public License
Documentation, generated by , last revised August 30, 2006.