tstnewton.C

Test program for Newton optimization objects

1. Newton with trust regions on an NLF2

2. Newton with More-Thuente Line Search on an NLF2

3. Newton with Backtracking Line Search on an NLF2

00001 
00011 #include <fstream>
00012 
00013 #include "OptNewton.h"
00014 #include "NLF.h"
00015 #include "tstfcn.h"
00016 
00017 using NEWMAT::ColumnVector;
00018 using namespace OPTPP;
00019 
00020 void update_model(int, int, ColumnVector) {}
00021 
00022 int main ()
00023 {
00024   int n = 2;
00025   
00026   static char *status_file = {"tstnewton.out"};
00027 
00028 //----------------------------------------------------------------------------
00029 // 1. Newton with trust regions
00030 //----------------------------------------------------------------------------
00031 
00032   //  Create a Nonlinear problem object
00033 
00034   NLF2 nlp(n,rosen2,init_rosen);
00035   
00036   //  Build a Newton object and optimize 
00037 
00038   OptNewton objfcn(&nlp, update_model);
00039   if (!objfcn.setOutputFile(status_file, 0))
00040     cerr << "main: output file open failed" << endl;
00041   objfcn.setTRSize(1.0e2);
00042   objfcn.optimize();
00043   objfcn.printStatus("Solution from newton: trust regions");
00044 
00045 #ifdef REG_TEST
00046   ColumnVector x_sol = nlp.getXc();
00047   double f_sol = nlp.getF();
00048   ostream* optout = objfcn.getOutputFile();
00049   if ((1.0 - x_sol(1) <= 1.e-2) && (1.0 - x_sol(2) <= 1.e-2) && (f_sol
00050                                                                  <=
00051                                                                  1.e-2))
00052     *optout << "Newton 1 PASSED" << endl;
00053   else
00054     *optout << "Newton 1 FAILED" << endl;
00055 #endif
00056 
00057   objfcn.cleanup();      
00058     
00059 //----------------------------------------------------------------------------
00060 // 2. Newton with More and Thuente's line search
00061 //----------------------------------------------------------------------------
00062 
00063   NLF2 nlp2(n,rosen2,init_rosen);
00064   
00065   OptNewton objfcn2(&nlp2,update_model);   
00066   objfcn2.setSearchStrategy(LineSearch);
00067   objfcn2.setOutputFile(status_file, 1);
00068   objfcn2.optimize();
00069   objfcn2.printStatus("Solution from newton: More and Thuente linesearch");
00070 
00071 #ifdef REG_TEST
00072   x_sol = nlp2.getXc();
00073   f_sol = nlp2.getF();
00074   optout = objfcn2.getOutputFile();
00075   if ((1.0 - x_sol(1) <= 1.e-2) && (1.0 - x_sol(2) <= 1.e-2) && (f_sol
00076                                                                  <=
00077                                                                  1.e-2))
00078     *optout << "Newton 2 PASSED" << endl;
00079   else
00080     *optout << "Newton 2 FAILED" << endl;
00081 #endif
00082 
00083   objfcn2.cleanup();     
00084     
00085 //----------------------------------------------------------------------------
00086 // 3. Newton with backtracking line search
00087 //----------------------------------------------------------------------------
00088 
00089   NLF2 nlp3(n,rosen2,init_rosen);
00090   nlp3.setIsExpensive(true);
00091   
00092   OptNewton objfcn3(&nlp3,update_model);   
00093   objfcn3.setOutputFile(status_file, 1);
00094   objfcn3.setSearchStrategy(LineSearch);
00095   objfcn3.optimize();
00096   objfcn3.printStatus("Solution from newton: backtracking linesearch");
00097 
00098 #ifdef REG_TEST
00099   x_sol = nlp3.getXc();
00100   f_sol = nlp3.getF();
00101   optout = objfcn3.getOutputFile();
00102   if ((1.0 - x_sol(1) <= 1.e-2) && (1.0 - x_sol(2) <= 1.e-2) && (f_sol
00103                                                                  <=
00104                                                                  1.e-2))
00105     *optout << "Newton 3 PASSED" << endl;
00106   else
00107     *optout << "Newton 3 FAILED" << endl;
00108 #endif
00109 
00110   objfcn3.cleanup();     
00111 }

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