Debian Codegraph @

packages/rol/adapters/sacado/test/test_01.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /*! \file test_01.cpp
11 \brief Test the NonlinearProblem interface with the Hock & Schittkowski
12 problem set
13 */
14
15 #define OPTIMIZATION_PROBLEM_REFACTOR
16
17 #include "HS_ProblemFactory.hpp"
18
19 #include "ROL_OptimizationSolver.hpp"
20 #include "ROL_Algorithm.hpp"
21 #include "ROL_RandomVector.hpp"
22 #include "ROL_ValidParameters.hpp"
23
24 #include "ROL_Stream.hpp"
25 #include "Teuchos_GlobalMPISession.hpp"
26 #include "Teuchos_XMLParameterListHelpers.hpp"
27
28 #include <iomanip>
29
30 typedef double RealT;
31
32 int main(int argc, char *argv[]) {
33
34 using namespace ROL;
35
36
37
38 typedef Teuchos::ParameterList PL;
39
40 typedef Vector<RealT> V;
41 typedef OptimizationProblem<RealT> OPT;
42 typedef NonlinearProgram<RealT> NLP;
43 typedef AlgorithmState<RealT> STATE;
44
45 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
46
47 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
48 int iprint = argc - 1;
49 ROL::Ptr<std::ostream> outStream;
50 ROL::nullstream bhs; // outputs nothing
51 if (iprint > 0)
52 outStream = ROL::makePtrFromRef(std::cout);
53 else
54 outStream = ROL::makePtrFromRef(bhs);
55
56 int errorFlag = 0;
57 int numProblems = 40;
58
59 RealT errtol = 1e-5; // Require norm of computed solution error be less than this
60
61 std::vector<int> passedTests;
62 std::vector<int> failedTests;
63 std::vector<std::string> converged;
64 std::vector<std::string> stepname;
65
66 HS::ProblemFactory<RealT> factory;
67
68 ROL::Ptr<NLP> nlp;
69 ROL::Ptr<OPT> opt;
70
71 // Get two copies so we can validate without setting defaults
72 ROL::Ptr<PL> parlist = ROL::makePtr<PL>();
73 ROL::Ptr<PL> parlistCopy = ROL::makePtr<PL>();
74 ROL::Ptr<PL> test = ROL::makePtr<PL>();
75
76 Teuchos::updateParametersFromXmlFile(std::string("hs_parameters.xml"),parlist.ptr());
77 Teuchos::updateParametersFromXmlFile(std::string("hs_parameters.xml"),parlistCopy.ptr());
78 Teuchos::updateParametersFromXmlFile(std::string("test_parameters.xml"),test.ptr());
79
80 ROL::Ptr<const PL> validParameters = getValidROLParameters();
81
82 parlistCopy->validateParametersAndSetDefaults(*validParameters);
83
84 ROL::Ptr<V> x;
85 ROL::Ptr<const STATE> algo_state;
86
87 bool problemSolved;
88
89 // *** Test body.
90 try {
91
92 for(int n=1; n<=numProblems; ++n) {
93 *outStream << "\nHock & Schittkowski Problem " << std::to_string(n) << std::endl;
94
95 nlp = factory.getProblem(n);
96 opt = nlp->getOptimizationProblem();
97 EProblem problemType = opt->getProblemType();
98
99 std::string str;
100
101 switch( problemType ) {
102
103 case TYPE_U:
104 str = test->get("Type-U Step","Trust Region");
105 break;
106
107 case TYPE_B:
108 str = test->get("Type-B Step","Trust Region");
109 break;
110
111 case TYPE_E:
112 str = test->get("Type-E Step","Composite Step");
113 break;
114
115 case TYPE_EB:
116 str = test->get("Type-EB Step","Augmented Lagrangian");
117 break;
118
119 case TYPE_LAST:
120 default:
121 ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,"Error: Unsupported problem type!");
122 break;
123 }
124
125 stepname.push_back(str);
126
127 parlist->sublist("Step").set("Type",str);
128
129 OptimizationSolver<RealT> solver( *opt, *parlist );
130
131 solver.solve(*outStream);
132
133 algo_state = solver.getAlgorithmState();
134
135 x = opt->getSolutionVector();
136
137 problemSolved = nlp->foundAcceptableSolution( *x, errtol );
138
139 RealT tol = std::sqrt(ROL_EPSILON<RealT>());
140
141 *outStream << "Target objective value = " << nlp->getSolutionObjectiveValue() << std::endl;
142 *outStream << "Attained objective value = " << opt->getObjective()->value(*x,tol) << std::endl;
143
144 if( problemSolved ) {
145 *outStream << "Computed an acceptable solution" << std::endl;
146 passedTests.push_back(n);
147 converged.push_back(std::to_string(algo_state->nfval));
148 }
149 else {
150 *outStream << "Failed to converge" << std::endl;
151 failedTests.push_back(n);
152 converged.push_back("Failed");
153 }
154
155 } // end for loop over HS problems
156
157
158 *outStream << "\nTests passed: ";
159 for( auto const& value : passedTests ) {
160 *outStream << value << " ";
161 }
162 *outStream << "\nTests failed: ";
163 for( auto const& value : failedTests ) {
164 *outStream << value << " ";
165 }
166
167 *outStream << std::endl;
168
169 if( passedTests.size() > failedTests.size() ) {
170 *outStream << "Most tests passed." << std::endl;
171 }
172 else {
173 *outStream << "Most tests failed." << std::endl;
174 errorFlag++;
175 }
176
177 *outStream << "\n\nPERFORMANCE SUMMARY:\n\n";
178 *outStream << std::setw(16) << "H&S Problem #"
179 << std::setw(24) << "Step Used"
180 << std::setw(12) << "#fval" << std::endl;
181 *outStream << std::string(52,'-') << std::endl;
182
183 for(int n=0; n<numProblems; ++n) {
184 *outStream << std::setw(16) << n+1
185 << std::setw(24) << stepname[n]
186 << std::setw(12) << converged[n] << std::endl;
187 }
188
189
190 }
191 catch (std::logic_error& err) {
192 *outStream << err.what() << "\n";
193 errorFlag = -1000;
194 }; // end try
195
196 if (errorFlag != 0)
197 std::cout << "End Result: TEST FAILED\n";
198 else
199 std::cout << "End Result: TEST PASSED\n";
200
201 return 0;
202 }
203
204