ROL
example_07.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 
52 #include "ROL_ParameterList.hpp"
53 
54 #include "ROL_Stream.hpp"
55 #include "Teuchos_GlobalMPISession.hpp"
56 #include "Teuchos_Comm.hpp"
57 #include "Teuchos_DefaultComm.hpp"
58 #include "Teuchos_CommHelpers.hpp"
59 
60 #include <iostream>
61 #include <fstream>
62 #include <algorithm>
63 
64 #include "example_07.hpp"
65 
66 typedef double RealT;
73 
74 int main(int argc, char *argv[]) {
75 
76  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
77  ROL::Ptr<const Teuchos::Comm<int>> comm
78  = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
79 
80  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
81  int iprint = argc - 1;
82  bool print = (iprint>0);
83  ROL::Ptr<std::ostream> outStream;
84  ROL::nullstream bhs; // outputs nothing
85  if (print)
86  outStream = ROL::makePtrFromRef(std::cout);
87  else
88  outStream = ROL::makePtrFromRef(bhs);
89 
90  bool print0 = print && !(comm->getRank());
91  ROL::Ptr<std::ostream> outStream0;
92  if (print0)
93  outStream0 = ROL::makePtrFromRef(std::cout);
94  else
95  outStream0 = ROL::makePtrFromRef(bhs);
96 
97  int errorFlag = 0;
98 
99  // *** Example body.
100 
101  try {
102  /*************************************************************************/
103  /************* INITIALIZE BURGERS FEM CLASS ******************************/
104  /*************************************************************************/
105  int nx = 512; // Set spatial discretization.
106  RealT x = 0.0; // Set penalty parameter.
107  RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
108  RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
109  RealT cL2 = 0.0; // Scale for mass term in H1 norm.
110  ROL::Ptr<BurgersFEM<RealT>> fem
111  = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
112  fem->test_inverse_mass(*outStream0);
113  fem->test_inverse_H1(*outStream0);
114  /*************************************************************************/
115  /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
116  /*************************************************************************/
117  ROL::Ptr<ROL::Objective_SimOpt<RealT>> pobj
118  = ROL::makePtr<Objective_BurgersControl<RealT>>(fem,x);
119  /*************************************************************************/
120  /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
121  /*************************************************************************/
122  bool hess = true;
123  ROL::Ptr<ROL::Constraint_SimOpt<RealT>> pcon
124  = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
125  /*************************************************************************/
126  /************* INITIALIZE VECTOR STORAGE *********************************/
127  /*************************************************************************/
128  ROL::Ptr<std::vector<RealT>> z_ptr, u_ptr, c_ptr, l_ptr;
129  z_ptr = ROL::makePtr<std::vector<RealT>>(nx+2, 0.0);
130  u_ptr = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
131  c_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
132  l_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
133  ROL::Ptr<ROL::Vector<RealT>> zp, up, cp, lp;
134  zp = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
135  up = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
136  cp = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
137  lp = ROL::makePtr<DualConstraintVector>(l_ptr,fem);
138  /*************************************************************************/
139  /************* INITIALIZE SAMPLE GENERATOR *******************************/
140  /*************************************************************************/
141  int dim = 4, nSamp = 1000;
142  std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
143  std::vector<std::vector<RealT>> bounds(dim,tmp);
144  ROL::Ptr<ROL::BatchManager<RealT>> bman
145  = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
146  ROL::Ptr<ROL::SampleGenerator<RealT>> sampler
147  = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
148  nSamp,bounds,bman,false,false,100);
149  /*************************************************************************/
150  /************* INITIALIZE OBJECTIVE FUNCTION *****************************/
151  /*************************************************************************/
152  bool storage = true, fdhess = false;
153  ROL::Ptr<ROL::Objective<RealT>> robj
154  = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(
155  pobj,pcon,up,zp,lp,storage,fdhess);
156  /*************************************************************************/
157  /************* INITIALIZE BOUND CONSTRAINTS ******************************/
158  /*************************************************************************/
159  std::vector<RealT> Zlo(nx+2,0.0), Zhi(nx+2,10.0);
160  for (int i = 0; i < nx+2; i++) {
161  if ( i < (int)((nx+2)/3) ) {
162  Zlo[i] = -1.0;
163  Zhi[i] = 1.0;
164  }
165  if ( i >= (int)((nx+2)/3) && i < (int)(2*(nx+2)/3) ) {
166  Zlo[i] = 1.0;
167  Zhi[i] = 5.0;
168  }
169  if ( i >= (int)(2*(nx+2)/3) ) {
170  Zlo[i] = 5.0;
171  Zhi[i] = 10.0;
172  }
173  }
174  ROL::Ptr<ROL::BoundConstraint<RealT>> bnd
175  = ROL::makePtr<L2BoundConstraint<RealT>>(Zlo,Zhi,fem);
176  /*************************************************************************/
177  /************* INITIALIZE RISK-AVERSE OPTIMIZATION PROBLEM ***************/
178  /*************************************************************************/
179  RealT order = 2.0, threshold = -0.85*(1.0-x);
180  ROL::Ptr<ROL::ParameterList> bpoelist = ROL::makePtr<ROL::ParameterList>();
181  bpoelist->sublist("SOL").set("Store Sampled Value and Gradient",true);
182  bpoelist->sublist("SOL").set("Stochastic Component Type","Probability");
183  bpoelist->sublist("SOL").sublist("Probability").set("Name","bPOE");
184  bpoelist->sublist("SOL").sublist("Probability").sublist("bPOE").set("Threshold",threshold);
185  bpoelist->sublist("SOL").sublist("Probability").sublist("bPOE").set("Moment Order",order);
186  ROL::OptimizationProblem<RealT> problem(robj,zp,bnd);
187  problem.setStochasticObjective(*bpoelist,sampler);
188  // CHECK OBJECTIVE DERIVATIVES
189  bool derivcheck = false;
190  if (derivcheck) {
191  problem.check(*outStream0);
192  }
193  /*************************************************************************/
194  /************* RUN OPTIMIZATION ******************************************/
195  /*************************************************************************/
196  // READ IN XML INPUT
197  std::string filename = "input.xml";
198  auto parlist = ROL::getParametersFromXmlFile( filename );
199  // RUN OPTIMIZATION
200  ROL::OptimizationSolver<RealT> solver(problem,*parlist);
201  solver.solve(*outStream);
202  /*************************************************************************/
203  /************* PRINT CONTROL AND STATE TO SCREEN *************************/
204  /*************************************************************************/
205  if ( print0 ) {
206  std::ofstream ofs;
207  ofs.open("output_example_09.txt",std::ofstream::out);
208  for ( int i = 0; i < nx+2; i++ ) {
209  ofs << std::scientific << std::setprecision(10);
210  ofs << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0);
211  ofs << std::setw(20) << std::left << (*z_ptr)[i];
212  ofs << "\n";
213  }
214  ofs.close();
215  }
216  *outStream0 << "Scalar Parameter: " << problem.getSolutionStatistic() << "\n\n";
217  }
218  catch (std::logic_error& err) {
219  *outStream << err.what() << "\n";
220  errorFlag = -1000;
221  }; // end try
222 
223  comm->barrier();
224  if (errorFlag != 0)
225  std::cout << "End Result: TEST FAILED\n";
226  else
227  std::cout << "End Result: TEST PASSED\n";
228 
229  return 0;
230 }
L2VectorPrimal< RealT > PrimalControlVector
void setStochasticObjective(ParameterList &parlist, const Ptr< SampleGenerator< Real >> &vsampler, const Ptr< SampleGenerator< Real >> &gsampler=nullPtr, const Ptr< SampleGenerator< Real >> &hsampler=nullPtr)
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
H1VectorDual< RealT > DualStateVector
Real getSolutionStatistic(int comp=0, int index=0)
Returns the statistic from the soluton vector.
L2VectorDual< RealT > DualControlVector
H1VectorDual< RealT > PrimalConstraintVector
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[])
void check(std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
H1VectorPrimal< RealT > PrimalStateVector
constexpr auto dim
H1VectorPrimal< RealT > DualConstraintVector