54 #include "ROL_ParameterList.hpp"
57 #include "Teuchos_GlobalMPISession.hpp"
58 #include "Teuchos_Comm.hpp"
59 #include "Teuchos_DefaultComm.hpp"
60 #include "Teuchos_CommHelpers.hpp"
76 int main(
int argc,
char *argv[]) {
78 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
79 ROL::Ptr<const Teuchos::Comm<int> > comm
80 = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
83 int iprint = argc - 1;
84 bool print = (iprint>0);
85 ROL::Ptr<std::ostream> outStream;
88 outStream = ROL::makePtrFromRef(std::cout);
90 outStream = ROL::makePtrFromRef(bhs);
92 bool print0 = print && !comm->getRank();
93 ROL::Ptr<std::ostream> outStream0;
95 outStream0 = ROL::makePtrFromRef(std::cout);
97 outStream0 = ROL::makePtrFromRef(bhs);
112 ROL::Ptr<BurgersFEM<RealT> > fem
113 = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
114 fem->test_inverse_mass(*outStream0);
115 fem->test_inverse_H1(*outStream0);
119 ROL::Ptr<std::vector<RealT> > ud_ptr
120 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
121 ROL::Ptr<ROL::Vector<RealT> > ud
122 = ROL::makePtr<L2VectorPrimal<RealT>>(ud_ptr,fem);
123 ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj
124 = ROL::makePtr<Objective_BurgersControl<RealT>>(fem,ud,alpha);
129 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon
130 = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
135 ROL::Ptr<std::vector<RealT> > z_ptr
136 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
137 ROL::Ptr<std::vector<RealT> > gz_ptr
138 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
139 ROL::Ptr<std::vector<RealT> > yz_ptr
140 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
141 for (
int i=0; i<nx+2; i++) {
142 (*yz_ptr)[i] = 2.0*random<RealT>(comm)-1.0;
144 ROL::Ptr<ROL::Vector<RealT> > zp
145 = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
146 ROL::Ptr<ROL::Vector<RealT> > gzp
147 = ROL::makePtr<DualControlVector>(gz_ptr,fem);
148 ROL::Ptr<ROL::Vector<RealT> > yzp
149 = ROL::makePtr<PrimalControlVector>(yz_ptr,fem);
151 ROL::Ptr<std::vector<RealT> > u_ptr
152 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
153 ROL::Ptr<std::vector<RealT> > gu_ptr
154 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
155 ROL::Ptr<ROL::Vector<RealT> > up
156 = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
157 ROL::Ptr<ROL::Vector<RealT> > gup
158 = ROL::makePtr<DualStateVector>(gu_ptr,fem);
160 ROL::Ptr<std::vector<RealT> > c_ptr
161 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
162 ROL::Ptr<std::vector<RealT> > l_ptr
163 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
164 for (
int i=0; i<nx; i++) {
165 (*l_ptr)[i] = random<RealT>(comm);
167 ROL::Ptr<ROL::Vector<RealT> > cp
168 = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
169 ROL::Ptr<ROL::Vector<RealT> > lp
170 = ROL::makePtr<DualConstraintVector>(l_ptr,fem);
174 int dim = 4, nSamp = 1000;
175 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
176 std::vector<std::vector<RealT> > bounds(dim,tmp);
177 ROL::Ptr<ROL::BatchManager<RealT> > bman
178 = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
179 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
180 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
181 nSamp,bounds,bman,
false,
false,100);
185 bool storage =
true, fdhess =
false;
186 ROL::Ptr<ROL::Objective<RealT> > robj
187 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(
188 pobj,pcon,up,zp,lp,gup,gzp,cp,storage,fdhess);
192 std::vector<RealT> Zlo(nx+2,0.0), Zhi(nx+2,10.0);
193 for (
int i = 0; i < nx+2; i++) {
194 if ( i < (
int)((nx+2)/3) ) {
198 if ( i >= (
int)((nx+2)/3) && i < (
int)(2*(nx+2)/3) ) {
202 if ( i >= (
int)(2*(nx+2)/3) ) {
207 ROL::Ptr<ROL::BoundConstraint<RealT> > Zbnd
208 = ROL::makePtr<L2BoundConstraint<RealT>>(Zlo,Zhi,fem);
212 ROL::ParameterList SOLlist;
213 SOLlist.sublist(
"SOL").set(
"Stochastic Component Type",
"Risk Averse");
214 SOLlist.sublist(
"SOL").set(
"Store Sampled Value and Gradient",storage);
215 SOLlist.sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",
"KL Divergence");
216 SOLlist.sublist(
"SOL").sublist(
"Risk Measure").sublist(
"KL Divergence").set(
"Threshold",1.e-2);
223 bool derivcheck =
false;
225 int nranks = sampler->numBatches();
226 for (
int pid = 0; pid < nranks; pid++) {
227 if ( pid == sampler->batchID() ) {
228 for (
int i = sampler->start(); i < sampler->numMySamples(); i++) {
229 *outStream <<
"Sample " << i <<
" Rank " << sampler->batchID() <<
"\n";
230 *outStream <<
"(" << sampler->getMyPoint(i)[0] <<
", "
231 << sampler->getMyPoint(i)[1] <<
", "
232 << sampler->getMyPoint(i)[2] <<
", "
233 << sampler->getMyPoint(i)[3] <<
")\n";
234 pcon->setParameter(sampler->getMyPoint(i));
235 pcon->checkSolve(*up,*zp,*cp,print,*outStream);
236 robj->setParameter(sampler->getMyPoint(i));
238 robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
239 robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
240 *outStream <<
"\n\n";
246 optProb.
check(*outStream0);
251 std::string filename =
"input.xml";
252 auto parlist = ROL::getParametersFromXmlFile( filename );
257 algo.
run(optProb,print0,*outStream0);
263 ofs.open(
"output_example_08.txt",std::ofstream::out);
264 for (
int i = 0; i < nx+2; i++ ) {
265 ofs << std::scientific << std::setprecision(10);
266 ofs << std::setw(20) << std::left << (
RealT)i/((
RealT)nx+1.0);
267 ofs << std::setw(20) << std::left << (*z_ptr)[i];
274 catch (std::logic_error err) {
275 *outStream << err.what() <<
"\n";
281 std::cout <<
"End Result: TEST FAILED\n";
283 std::cout <<
"End Result: TEST PASSED\n";
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
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
Provides an interface to run optimization algorithms.
H1VectorDual< RealT > PrimalConstraintVector
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])
void check(std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
H1VectorPrimal< RealT > PrimalStateVector
H1VectorPrimal< RealT > DualConstraintVector