ROL
example_10.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 
44 #include "example_10.hpp"
45 
46 typedef double RealT;
47 
48 int main(int argc, char* argv[]) {
49 
50  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
51  ROL::Ptr<const Teuchos::Comm<int>> comm
52  = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
53 
54  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
55  int iprint = argc - 1;
56  ROL::Ptr<std::ostream> outStream;
57  ROL::nullstream bhs; // outputs nothing
58  if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
59  outStream = ROL::makePtrFromRef(std::cout);
60  else
61  outStream = ROL::makePtrFromRef(bhs);
62 
63  int errorFlag = 0;
64 
65  try {
66  // Get ROL parameterlist
67  auto parlist = ROL::getParametersFromXmlFile("input_ex10.xml");
68  /**********************************************************************************************/
69  /************************* CONSTRUCT VECTORS **************************************************/
70  /**********************************************************************************************/
71  int nx = 256;
72  ROL::Ptr<ROL::Vector<RealT>> z = ROL::makePtr<ROL::StdVector<RealT>>(nx+2,0.0);
73  ROL::Ptr<ROL::Vector<RealT>> u = ROL::makePtr<ROL::StdVector<RealT>>(nx,1.0);
74  ROL::Ptr<ROL::Vector<RealT>> p = ROL::makePtr<ROL::StdVector<RealT>>(nx,0.0);
75  /**********************************************************************************************/
76  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
77  /**********************************************************************************************/
78  // Build samplers
79  int dim = 4, nSamp = parlist->sublist("Problem").get("Number of Samples",100);
80  std::vector<RealT> tmp = {-1, 1};
81  std::vector<std::vector<RealT>> bounds(dim,tmp);
82  ROL::Ptr<ROL::BatchManager<RealT>> bman
83  = ROL::makePtr<ROL::StdTeuchosBatchManager<RealT,int>>(comm);
84  ROL::Ptr<ROL::SampleGenerator<RealT>> sampler
85  = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman);
86  /**********************************************************************************************/
87  /************************* CONSTRUCT OBJECTIVE FUNCTION ***************************************/
88  /**********************************************************************************************/
89  // Build risk-averse objective function
90  RealT alpha = 1.e-3;
91  ROL::Ptr<ROL::Objective_SimOpt<RealT>> objSimOpt
92  = ROL::makePtr<Objective_BurgersControl<RealT>>(alpha,nx);
93  ROL::Ptr<ROL::Constraint_SimOpt<RealT>> conSimOpt
94  = ROL::makePtr<Constraint_BurgersControl<RealT>>(nx);
95  conSimOpt->setSolveParameters(*parlist);
96  ROL::Ptr<ROL::Objective<RealT>> robj
97  = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(objSimOpt,conSimOpt,u,z,p);
98  /**********************************************************************************************/
99  /************************* SOLVE OPTIMIZATION PROBLEM *****************************************/
100  /**********************************************************************************************/
101  bool runBundle = parlist->sublist("Problem").get("Run Bundle",false);
102  // Solve using bundle
103  if (runBundle) {
104  z->zero();
105  ROL::Ptr<ROL::OptimizationProblem<double>> problem2
106  = ROL::makePtr<ROL::OptimizationProblem<double>>(robj, z);
107  problem2->setStochasticObjective(*parlist, sampler);
108  parlist->sublist("Step").set("Type","Bundle");
109  parlist->sublist("Step").sublist("Bundle").set("Distance Measure Coefficient",0.0);
110  ROL::OptimizationSolver<double> solver2(*problem2,*parlist);
111  solver2.solve(*outStream);
112  }
113 
114  ROL::Ptr<ROL::OptimizationProblem<double>> problem
115  = ROL::makePtr<ROL::OptimizationProblem<double>>(robj, z);
116  ROL::PrimalDualRisk<double> solver(problem, sampler, *parlist);
117  if (parlist->sublist("Problem").get("Run Derivative Check",false)) {
118  problem->check(*outStream);
119  solver.check(*outStream);
120  }
121  solver.run(*outStream);
122  }
123  catch (std::logic_error& err) {
124  *outStream << err.what() << "\n";
125  errorFlag = -1000;
126  }; // end try
127 
128  if (errorFlag != 0)
129  std::cout << "End Result: TEST FAILED\n";
130  else
131  std::cout << "End Result: TEST PASSED\n";
132 
133  return 0;
134 }
Provides a simplified interface for solving a wide range of optimization problems.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
constexpr auto dim