ROL
step/fletcher/test_02.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
14#include "ROL_HS29.hpp"
15#include "ROL_Algorithm.hpp"
16#include "ROL_BoundFletcher.hpp"
17#include "ROL_Bounds.hpp"
18#include "ROL_Constraint.hpp"
20
21#include "ROL_Stream.hpp"
22#include "Teuchos_GlobalMPISession.hpp"
23
24
25typedef double RealT;
26
27int main(int argc, char *argv[]) {
28
29
30 typedef std::vector<RealT> vec;
31 typedef ROL::StdVector<RealT> SV;
32 typedef ROL::Ptr<ROL::Vector<RealT> > PtrV;
33
34 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
35
36 std::string filename = "input_ex02.xml";
37
38 auto parlist = ROL::getParametersFromXmlFile( filename );
39
40 int iprint = argc - 1;
41 ROL::Ptr<std::ostream> outStream;
42 ROL::nullstream bhs; // outputs nothing
43 if (iprint > 0)
44 outStream = ROL::makePtrFromRef(std::cout);
45 else
46 outStream = ROL::makePtrFromRef(bhs);
47
48 int errorFlag = 0;
49
50 try {
51
52 int xopt_dim = 3; // Dimension of optimization vectors
53 int ci_dim = 1; // Dimension of inequality constraint
54
55 ROL::Ptr<vec> xopt_ptr = ROL::makePtr<vec>(xopt_dim,1.0); // Feasible initial guess
56 ROL::Ptr<vec> li_ptr = ROL::makePtr<vec>(ci_dim,0.0);
57
58 PtrV xopt = ROL::makePtr<SV>(xopt_ptr);
59 PtrV li = ROL::makePtr<SV>(li_ptr);
60
61 // Original obective
64
65 ROL::Ptr<ROL::Objective<RealT> > obj_hs29 = ROL::makePtr<Objective_HS29<RealT>>();
66 ROL::Ptr<ROL::Constraint<RealT> > incon_hs29 = ROL::makePtr<InequalityConstraint_HS29<RealT>>();
67
68 ROL::Ptr<ROL::Vector<RealT> > bndc = li->clone();
69 ROL::Ptr<ROL::BoundConstraint<RealT> > bndcon = ROL::makePtr<ROL::Bounds<RealT> >(*bndc);
70
71 ROL::Ptr<vec> low_ptr = ROL::makePtr<vec>(xopt_dim, ROL::ROL_NINF<RealT>());
72 ROL::Ptr<vec> upp_ptr = ROL::makePtr<vec>(xopt_dim, ROL::ROL_INF<RealT>());
73 PtrV low = ROL::makePtr<SV>(low_ptr);
74 PtrV upp = ROL::makePtr<SV>(upp_ptr);
75 ROL::Ptr<ROL::BoundConstraint<RealT> > bndx = ROL::makePtr<ROL::Bounds<RealT> >(low, upp);
76
77 std::string stepname = "Trust Region";
78
79 ROL::OptimizationProblem<RealT> problem( obj_hs29, xopt, bndx, incon_hs29, li, bndcon );
80
81 ROL::Ptr<ROL::Objective<RealT> > obj = problem.getObjective();
82 ROL::Ptr<ROL::Constraint<RealT> > con = problem.getConstraint();
83 ROL::Ptr<ROL::BoundConstraint<RealT> > bndxs = problem.getBoundConstraint();
84 ROL::Ptr<ROL::Vector<RealT> > xs = problem.getSolutionVector();
85 ROL::Ptr<ROL::Vector<RealT> > lis = problem.getMultiplierVector();
86
87 ROL::Ptr<ROL::BoundFletcher<RealT> > fletcher_penalty = ROL::makePtr<ROL::BoundFletcher<RealT> >(obj, con, bndxs, *xs, *lis, *parlist);
88
89 ROL::Ptr<ROL::Vector<RealT> > v = xs->clone(); v->randomize();
90 std::vector<std::vector<RealT> > gCheck = fletcher_penalty->checkGradient(*xs, *v, true );
91
92 ROL::Ptr<ROL::Vector<RealT> > w = xs->clone(); w->randomize();
93 std::vector<RealT> hCheck = fletcher_penalty->checkHessSym( *xs, *v, *w, true, *outStream);
94
95 // Define algorithm.
96 // ROL::Ptr<ROL::Algorithm<RealT> > algo;
97 // algo = ROL::makePtr<ROL::Algorithm<RealT>>(stepname, *parlist);
98 // algo->run(*xs, *fletcher_penalty, *bndxs, true, *outStream);
99
100 ROL::OptimizationSolver<RealT> optSolver(problem, *parlist);
101 optSolver.solve(*outStream);
102
103 *outStream << std::endl << std::setw(20) << "Computed Minimizer" << std::endl;
104 for( int i=0;i<xopt_dim;++i ) {
105 *outStream << std::setw(20) << (*xopt_ptr)[i] << std::endl;
106 }
107
108 *outStream << "Exact minimizers: x* = (a,b,c), (a,-b,-c), (-a,b,-c), (-a,-b,c)" << std::endl;
109 *outStream << "Where a=4, b=" << 2*std::sqrt(2) << ", and c=2" << std::endl;
110
111 }
112 catch (std::logic_error& err) {
113 *outStream << err.what() << "\n";
114 errorFlag = -1000;
115 }; // end try
116
117 if (errorFlag != 0)
118 std::cout << "End Result: TEST FAILED\n";
119 else
120 std::cout << "End Result: TEST PASSED\n";
121
122 return 0;
123
124
125
126}
Contains definitions for W. Hock and K. Schittkowski 32nd test problem which contains only inequality...
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
virtual Ptr< Vector< Real > > getMultiplierVector(void)
virtual Ptr< Vector< Real > > getSolutionVector(void)
virtual Ptr< BoundConstraint< Real > > getBoundConstraint(void)
virtual Ptr< Constraint< Real > > getConstraint(void)
virtual Ptr< Objective< Real > > getObjective(void)
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.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
int main(int argc, char *argv[])
double RealT