ROL
step/test_13.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 "Teuchos_GlobalMPISession.hpp"
18
19
20#include <iostream>
21
22typedef double RealT;
23
24int main(int argc, char *argv[]) {
25
26 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
27
28 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29 int iprint = argc - 1;
30 ROL::Ptr<std::ostream> outStream;
31 ROL::nullstream bhs; // outputs nothing
32 if (iprint > 0)
33 outStream = ROL::makePtrFromRef(std::cout);
34 else
35 outStream = ROL::makePtrFromRef(bhs);
36
37 int errorFlag = 0;
38
39 // *** Test body.
40
41 try {
42 std::string filename = "input.xml";
43
44 auto parlist = ROL::getParametersFromXmlFile( filename );
45
46 // Setup optimization problem
47 ROL::Ptr<ROL::Vector<RealT> > x0;
48 std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
49 ROL::Ptr<ROL::OptimizationProblem<RealT> > optProblem;
50 ROL::GetTestProblem<RealT>(optProblem,x0,z,ROL::TESTOPTPROBLEM_HS1);
51
52 // Get Dimension of Problem
53 int dim = x0->dimension();
54 parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 2*dim);
55
56 // Check Derivatives
57 optProblem->check(*outStream);
58
59 // Error Vector
60 ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
61 e->zero();
62
63 // Setup optimization solver
64 parlist->sublist("Status Test").set("Gradient Tolerance",static_cast<RealT>(1e-6));
65 parlist->sublist("Step").set("Type", "Interior Point");
66 ROL::OptimizationSolver<RealT> optSolver(*optProblem,*parlist);
67 optSolver.solve(*outStream);
68
69 // Compute Error
70 RealT err(0);
71 for (int i = 0; i < static_cast<int>(z.size()); ++i) {
72 e->set(*x0);
73 e->axpy(-1.0,*z[i]);
74 if (i == 0) {
75 err = e->norm();
76 }
77 else {
78 err = std::min(err,e->norm());
79 }
80 }
81 *outStream << std::endl << "Norm of Error: " << err << std::endl;
82
83 RealT tol = static_cast<RealT>(1e-3)*std::max(z[0]->norm(),static_cast<RealT>(1));
84 errorFlag += ((err < tol) ? 0 : 1);
85 }
86 catch (std::logic_error& err) {
87 *outStream << err.what() << std::endl;
88 errorFlag = -1000;
89 }; // end try
90
91 if (errorFlag != 0)
92 std::cout << "End Result: TEST FAILED" << std::endl;
93 else
94 std::cout << "End Result: TEST PASSED" << std::endl;
95
96 return 0;
97
98}
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
constexpr auto dim