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