1 //
2 // Test program for Quasi-Newton with trust region PDS on an NLF1
3 //
4 
5 #ifdef HAVE_CONFIG_H
6 #include "OPT++_config.h"
7 #endif
8 
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 #ifdef HAVE_STD
13 #include <cstdio>
14 #else
15 #include <stdio.h>
16 #endif
17 
18 #ifdef OPTPP_HAVE_MPI
19 #include "mpi.h"
20 #endif
21 
22 #include "NLF.h"
23 #include "OptQNewton.h"
24 #include "ioformat.h"
25 
26 #include "tstfcn.h"
27 
28 using Teuchos::SerialDenseVector;
29 using std::cerr;
30 using std::endl;
31 
32 using namespace OPTPP;
33 
34 void SetupTestProblem(std::string test_id, USERFCN0 *test_problem,
35 		      INITFCN *init_problem);
update_model(int,int,SerialDenseVector<int,double>)36 void update_model(int, int, SerialDenseVector<int,double>) {}
37 
38 
main(int argc,char * argv[])39 int main (int argc, char* argv[])
40 {
41 
42   int ndim = 2;
43   double time0, opt_time;
44   //  SymmetricMatrix H0(4);
45 
46   // USERFCN0 test_problem;
47   // INITFCN  init_problem;
48 
49   // std::string test_id;
50 
51   //
52   // Setup the test problem
53   // test_problem is a pointer to the function (fcn) to optimize
54   // init_problem is a pointer to the function that initializes fcn
55   // test_id is a character string identifying the test problem
56   // ndim is the dimension of the problem
57 
58 #ifdef OPTPP_HAVE_MPI
59   int me;
60 
61   MPI_Init(&argc, &argv);
62   MPI_Comm_rank(MPI_COMM_WORLD, &me);
63 #endif
64 
65   //if (argc != 3) {
66   //  cout << "Usage: tstnewtpds problem_name ndim\n";
67   //  exit(1);
68   //}
69 
70   //test_id = argv[1];
71   //ndim    = atoi(argv[2]);
72 
73   //SetupTestProblem(test_id, &test_problem, &init_problem);
74   //ColumnVector x(ndim);
75 
76   //
77   // Generate file name based on the problem name
78   //
79   char status_file[80];
80   //strcpy(status_file,test_id.c_str());
81   strcpy(status_file, "tstnewtpds");
82 #ifdef OPTPP_HAVE_MPI
83   sprintf(status_file,"%s.out.%d", status_file, me);
84 #else
85   strcat(status_file,".out");
86 #endif
87 
88 //----------------------------------------------------------------------------
89 // 1. Quasi-Newton with trust region PDS
90 //----------------------------------------------------------------------------
91 
92   //  Create a Nonlinear problem object
93 
94   //FDNLF1 nlp(ndim,test_problem, init_problem);
95   NLF1 nlp(ndim, erosen1, init_erosen);
96   //  Initialize and evaluate the function at x
97 
98   nlp.setSpecOption(NoSpec);
99   nlp.setIsExpensive(true);
100 
101   //  Build a Quasi-Newton object and optimize
102 
103   OptQNewton objfcn(&nlp,update_model);
104   if (!objfcn.setOutputFile(status_file, 0))
105     cerr << "main: output file open failed" << endl;
106   std::ostream* optout = objfcn.getOutputFile();
107   //*optout << "Test problem: " << test_id << endl;
108   *optout << "Test problem: " << "erosen1" << endl;
109   *optout << "Dimension   : " << ndim    << endl;
110   objfcn.setSearchStrategy(TrustPDS);
111   objfcn.setMaxFeval(10000);
112   //  objfcn.setTRSize(100.);
113 
114   time0 = get_wall_clock_time();
115   objfcn.optimize();
116   opt_time = get_wall_clock_time() - time0;
117   *optout << "wall clock time =" << e(opt_time,12,4) << endl;
118 
119   objfcn.printStatus("Solution from quasi-newton");
120   objfcn.cleanup();
121 
122 #ifdef REG_TEST
123   ColumnVector x_sol = nlp.getXc();
124   double f_sol = nlp.getF();
125   if ((1.0 - x_sol(0) <= 1.e-2) && (1.0 - x_sol(1) <= 1.e-2) && (f_sol
126 								 <=
127 								 1.e-2))
128     *optout << "UTRPDS 1 PASSED" << endl;
129   else
130     *optout << "UTRPDS 1 FAILED" << endl;
131 #endif
132 
133 #ifdef OPTPP_HAVE_MPI
134   MPI_Finalize();
135 #endif
136 
137 }
138