ROL
test_05.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
48 #define USE_HESSVEC 1
49 
50 #include "ROL_TestObjectives.hpp"
52 #include "ROL_Algorithm.hpp"
53 #include "Teuchos_oblackholestream.hpp"
54 #include "Teuchos_GlobalMPISession.hpp"
55 #include "Teuchos_XMLParameterListHelpers.hpp"
56 
57 #include <iostream>
58 
59 typedef double RealT;
60 
61 int main(int argc, char *argv[]) {
62 
63  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
64 
65  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
66  int iprint = argc - 1;
67  Teuchos::RCP<std::ostream> outStream;
68  Teuchos::oblackholestream bhs; // outputs nothing
69  if (iprint > 0)
70  outStream = Teuchos::rcp(&std::cout, false);
71  else
72  outStream = Teuchos::rcp(&bhs, false);
73 
74  int errorFlag = 0;
75 
76  // *** Test body.
77 
78  try {
79 
80  std::string filename = "input.xml";
81  Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
82  Teuchos::updateParametersFromXmlFile( filename, Teuchos::Ptr<Teuchos::ParameterList>(&*parlist) );
83  parlist->set("Use Inexact Hessian-Times-A-Vector",true);
84 #if USE_HESSVEC
85  parlist->set("Use Inexact Hessian-Times-A-Vector",false);
86 #endif
87 
88  // Define Status Test
89  RealT gtol = parlist->get("Gradient Tolerance",1.e-6);
90  RealT stol = parlist->get("Step Tolerance",1.e-12);
91  int maxit = parlist->get("Maximum Number of Iterations",100);
92  ROL::StatusTest<RealT> status(gtol,stol,maxit);
93 
94  // Krylov parameters.
95  parlist->set("Krylov Method", "Conjugate Residuals");
96  parlist->set("Absolute Krylov Tolerance", 1.e-8);
97  parlist->set("Relative Krylov Tolerance", 1.e-4);
98  parlist->set("Maximum Number of Krylov Iterations", 50);
99 
101  if ( prob != ROL::TESTOPTPROBLEM_HS5 ) {
102  // PDAS parameters.
103  switch (prob) {
109  parlist->set("PDAS Relative Step Tolerance", 1.e-10);
110  parlist->set("PDAS Relative Gradient Tolerance", 1.e-8);
111  parlist->set("PDAS Maximum Number of Iterations", 1);
112  parlist->set("PDAS Dual Scaling", 1.e8);
113  break;
115  parlist->set("PDAS Relative Step Tolerance", 1.e-10);
116  parlist->set("PDAS Relative Gradient Tolerance", 1.e-8);
117  parlist->set("PDAS Maximum Number of Iterations", 10);
118  parlist->set("PDAS Dual Scaling", 1.e-2);
119  break;
121  parlist->set("PDAS Relative Step Tolerance", 1.e-10);
122  parlist->set("PDAS Relative Gradient Tolerance", 1.e-8);
123  parlist->set("PDAS Maximum Number of Iterations", 10);
124  parlist->set("PDAS Dual Scaling", 1.e10);
125  break;
127  parlist->set("PDAS Relative Step Tolerance", 1.e-10);
128  parlist->set("PDAS Relative Gradient Tolerance", 1.e-8);
129  parlist->set("PDAS Maximum Number of Iterations", 1);
130  parlist->set("PDAS Dual Scaling", 1.e-3);
131  break;
133  parlist->set("PDAS Relative Step Tolerance", 1.e-10);
134  parlist->set("PDAS Relative Gradient Tolerance", 1.e-8);
135  parlist->set("PDAS Maximum Number of Iterations", 1);
136  parlist->set("PDAS Dual Scaling", 1.e0);
137  break;
138  case ROL::TESTOPTPROBLEM_LAST: break;
139  }
140  *outStream << "\n\n" << ROL:: ETestOptProblemToString(prob) << "\n\n";
141 
142  // Initial Guess Vector
143  Teuchos::RCP<std::vector<RealT> > x0_rcp = Teuchos::rcp( new std::vector<RealT> );
144  ROL::StdVector<RealT> x0(x0_rcp);
145 
146  // Exact Solution Vector
147  Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp( new std::vector<RealT> );
148  ROL::StdVector<RealT> z(z_rcp);
149 
150  // Get Objective Function
151  Teuchos::RCP<ROL::Objective<RealT> > obj = Teuchos::null;
152  Teuchos::RCP<ROL::BoundConstraint<RealT> > con = Teuchos::null;
153  ROL::getTestObjectives<RealT>(obj,con,x0,z,prob);
154 
155  // Get Dimension of Problem
156  int dim =
157  Teuchos::rcp_const_cast<std::vector<RealT> >(
158  (Teuchos::dyn_cast<ROL::StdVector<RealT> >(x0)).getVector())->size();
159  parlist->set("Maximum Number of Krylov Iterations", 2*dim);
160 
161  // Check Derivatives
162  //Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 1.0) );
163  //ROL::StdVector<RealT> d(d_rcp);
164  //obj->checkGradient(x0,d);
165  //obj->checkHessVec(x0,d);
166 
167  // Iteration Vector
168  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
169  ROL::StdVector<RealT> x(x_rcp);
170  x.set(x0);
171 
172  // Error Vector
173  Teuchos::RCP<std::vector<RealT> > e_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
174  ROL::StdVector<RealT> e(e_rcp);
175  e.zero();
176 
177  // Define Step
179 
180  // Define Algorithm
181  ROL::DefaultAlgorithm<RealT> algo(step,status,false);
182 
183  // Run Algorithm
184  x.set(x0);
185  std::vector<std::string> output = algo.run(x, *obj, *con);
186  for ( unsigned i = 0; i < output.size(); i++ ) {
187  *outStream << output[i];
188  }
189 
190  // Compute Error
191  e.set(x);
192  e.axpy(-1.0,z);
193  *outStream << "\nNorm of Error: " << e.norm() << "\n";
194 
195  // Update error flag
196  Teuchos::RCP<const ROL::AlgorithmState<RealT> > state = algo.getState();
197  errorFlag += ((e.norm() < std::max(1.e-6*z.norm(),1.e-8) || (state->gnorm < gtol)) ? 0 : 1);
198  }
199  }
200  }
201  catch (std::logic_error err) {
202  *outStream << err.what() << "\n";
203  errorFlag = -1000;
204  }; // end try
205 
206  if (errorFlag != 0)
207  std::cout << "End Result: TEST FAILED\n";
208  else
209  std::cout << "End Result: TEST PASSED\n";
210 
211  return 0;
212 
213 }
214 
ETestOptProblem
Enumeration of test optimization problems.
Definition: ROL_Types.hpp:848
Implements the computation of optimization steps with the Newton primal-dual active set method...
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:141
Teuchos::RCP< const AlgorithmState< Real > > getState(void) const
Contains definitions of test objective functions.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
double RealT
Definition: test_05.cpp:59
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
std::string ETestOptProblemToString(ETestOptProblem to)
Definition: ROL_Types.hpp:861
Provides an interface to check status of optimization algorithms.
int main(int argc, char *argv[])
Definition: test_05.cpp:61
void set(const Vector< Real > &x)
Set where .
double RealT