ROL
example_10.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 
10 #include "example_10.hpp"
11 
12 typedef double RealT;
13 
14 int main(int argc, char* argv[]) {
15 
16  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
17  ROL::Ptr<const Teuchos::Comm<int>> comm
18  = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
19 
20  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
21  int iprint = argc - 1;
22  ROL::Ptr<std::ostream> outStream;
23  ROL::nullstream bhs; // outputs nothing
24  if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
25  outStream = ROL::makePtrFromRef(std::cout);
26  else
27  outStream = ROL::makePtrFromRef(bhs);
28 
29  int errorFlag = 0;
30 
31  try {
32  // Get ROL parameterlist
33  auto parlist = ROL::getParametersFromXmlFile("input_ex10.xml");
34  /**********************************************************************************************/
35  /************************* CONSTRUCT VECTORS **************************************************/
36  /**********************************************************************************************/
37  int nx = 256;
38  ROL::Ptr<ROL::Vector<RealT>> z = ROL::makePtr<ROL::StdVector<RealT>>(nx+2,0.0);
39  ROL::Ptr<ROL::Vector<RealT>> u = ROL::makePtr<ROL::StdVector<RealT>>(nx,1.0);
40  ROL::Ptr<ROL::Vector<RealT>> p = ROL::makePtr<ROL::StdVector<RealT>>(nx,0.0);
41  /**********************************************************************************************/
42  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
43  /**********************************************************************************************/
44  // Build samplers
45  int dim = 4, nSamp = parlist->sublist("Problem").get("Number of Samples",100);
46  std::vector<RealT> tmp = {-1, 1};
47  std::vector<std::vector<RealT>> bounds(dim,tmp);
48  ROL::Ptr<ROL::BatchManager<RealT>> bman
49  = ROL::makePtr<ROL::StdTeuchosBatchManager<RealT,int>>(comm);
50  ROL::Ptr<ROL::SampleGenerator<RealT>> sampler
51  = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman);
52  /**********************************************************************************************/
53  /************************* CONSTRUCT OBJECTIVE FUNCTION ***************************************/
54  /**********************************************************************************************/
55  // Build risk-averse objective function
56  RealT alpha = 1.e-3;
57  ROL::Ptr<ROL::Objective_SimOpt<RealT>> objSimOpt
58  = ROL::makePtr<Objective_BurgersControl<RealT>>(alpha,nx);
59  ROL::Ptr<ROL::Constraint_SimOpt<RealT>> conSimOpt
60  = ROL::makePtr<Constraint_BurgersControl<RealT>>(nx);
61  conSimOpt->setSolveParameters(*parlist);
62  ROL::Ptr<ROL::Objective<RealT>> robj
63  = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(objSimOpt,conSimOpt,u,z,p);
64  /**********************************************************************************************/
65  /************************* SOLVE OPTIMIZATION PROBLEM *****************************************/
66  /**********************************************************************************************/
67  bool runBundle = parlist->sublist("Problem").get("Run Bundle",false);
68  // Solve using bundle
69  if (runBundle) {
70  z->zero();
71  ROL::Ptr<ROL::OptimizationProblem<double>> problem2
72  = ROL::makePtr<ROL::OptimizationProblem<double>>(robj, z);
73  problem2->setStochasticObjective(*parlist, sampler);
74  parlist->sublist("Step").set("Type","Bundle");
75  parlist->sublist("Step").sublist("Bundle").set("Distance Measure Coefficient",0.0);
76  ROL::OptimizationSolver<double> solver2(*problem2,*parlist);
77  solver2.solve(*outStream);
78  }
79 
80  ROL::Ptr<ROL::Problem<double>> problem
81  = ROL::makePtr<ROL::Problem<double>>(robj, z);
82  ROL::PrimalDualRisk<double> solver(problem, sampler, *parlist);
83  if (parlist->sublist("Problem").get("Run Derivative Check",false)) {
84  problem->check(true,*outStream);
85  solver.check(*outStream);
86  }
87  solver.run(*outStream);
88  }
89  catch (std::logic_error& err) {
90  *outStream << err.what() << "\n";
91  errorFlag = -1000;
92  }; // end try
93 
94  if (errorFlag != 0)
95  std::cout << "End Result: TEST FAILED\n";
96  else
97  std::cout << "End Result: TEST PASSED\n";
98 
99  return 0;
100 }
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
Provides a simplified interface for solving a wide range of optimization problems.
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