ROL
step/test_18.cpp
Go to the documentation of this file.
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
16#include "ROL_Stream.hpp"
17#include "ROL_GlobalMPISession.hpp"
18
19#include <iostream>
20
21typedef double RealT;
22
23int main(int argc, char *argv[]) {
24
25 ROL::GlobalMPISession mpiSession(&argc, &argv);
26
27 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
28 int iprint = argc - 1;
29 ROL::Ptr<std::ostream> outStream;
30 ROL::nullstream bhs; // outputs nothing
31 if (iprint > 0)
32 outStream = ROL::makePtrFromRef(std::cout);
33 else
34 outStream = ROL::makePtrFromRef(bhs);
35
36 int errorFlag = 0;
37
38 // *** Test body.
39
40 try {
41 std::string filename = "input.xml";
42 auto parlist = ROL::getParametersFromXmlFile( filename );
43 parlist->sublist("Step").sublist("Trust Region").set("Initial Radius",10.0);
44
45 // Setup optimization problem
46 ROL::Ptr<ROL::Vector<RealT>> x0;
47 std::vector<ROL::Ptr<ROL::Vector<RealT>>> z;
48 ROL::Ptr<ROL::ZOO::getCubic<RealT>> cubic;
49 ROL::Ptr<ROL::OptimizationProblem<RealT>> problem;
50 for (int i = 0; i < 3; ++i) {
51 cubic = ROL::makePtr<ROL::ZOO::getCubic<RealT>>(i);
52 cubic->get(problem,x0,z);
53
54 // Check Derivatives
55 problem->check(*outStream);
56
57 // Setup optimization solver
58 ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
59 solver.solve(*outStream);
60
61 // Compute Error
62 ROL::Ptr<ROL::Vector<RealT>> e = x0->clone();
63 e->zero();
64 RealT err(0);
65 for (int li = 0; li < static_cast<int>(z.size()); ++li) {
66 e->set(*x0);
67 e->axpy(-1.0,*z[li]);
68 if (li == 0) {
69 err = e->norm();
70 }
71 else {
72 err = std::min(err,e->norm());
73 }
74 }
75 *outStream << std::endl << "Norm of Error: " << err << std::endl;
76 RealT tol = static_cast<RealT>(1e-3)*std::max(z[0]->norm(),static_cast<RealT>(1));
77 errorFlag += ((err < tol) ? 0 : 1);
78 }
79 }
80 catch (std::logic_error& err) {
81 *outStream << err.what() << std::endl;
82 errorFlag = -1000;
83 }; // end try
84
85 if (errorFlag != 0)
86 std::cout << "End Result: TEST FAILED" << std::endl;
87 else
88 std::cout << "End Result: TEST PASSED" << std::endl;
89
90 return 0;
91
92}
93
Contains definitions of test objective functions.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides a simplified interface for solving a wide range of optimization problems.
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
int main(int argc, char *argv[])
double RealT