23 #include "ROL_ParameterList.hpp"
28 #include "Teuchos_GlobalMPISession.hpp"
29 #include "Teuchos_Comm.hpp"
30 #include "Teuchos_DefaultComm.hpp"
31 #include "Teuchos_CommHelpers.hpp"
46 int main(
int argc,
char *argv[]) {
48 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
50 auto comm = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
53 int iprint = argc - 1;
54 bool print = (iprint>0);
55 ROL::Ptr<std::ostream> outStream;
58 outStream = ROL::makePtrFromRef(std::cout);
60 outStream = ROL::makePtrFromRef(bhs);
62 bool print0 = print && !comm->getRank();
63 ROL::Ptr<std::ostream> outStream0;
65 outStream0 = ROL::makePtrFromRef(std::cout);
67 outStream0 = ROL::makePtrFromRef(bhs);
82 ROL::Ptr<BurgersFEM<RealT> > fem
83 = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
84 fem->test_inverse_mass(*outStream0);
85 fem->test_inverse_H1(*outStream0);
89 ROL::Ptr<std::vector<RealT> > ud_ptr
90 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
91 ROL::Ptr<ROL::Vector<RealT> > ud
92 = ROL::makePtr<L2VectorPrimal<RealT>>(ud_ptr,fem);
93 ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj
94 = ROL::makePtr<Objective_BurgersControl<RealT>>(fem,ud,alpha);
99 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon
100 = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
105 ROL::Ptr<std::vector<RealT> > z_ptr
106 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
107 ROL::Ptr<std::vector<RealT> > gz_ptr
108 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
109 ROL::Ptr<std::vector<RealT> > yz_ptr
110 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
111 for (
int i=0; i<nx+2; i++) {
112 (*yz_ptr)[i] = 2.0*random<RealT>(comm)-1.0;
114 ROL::Ptr<ROL::Vector<RealT> > zp
115 = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
116 ROL::Ptr<ROL::Vector<RealT> > gzp
117 = ROL::makePtr<DualControlVector>(gz_ptr,fem);
118 ROL::Ptr<ROL::Vector<RealT> > yzp
119 = ROL::makePtr<PrimalControlVector>(yz_ptr,fem);
120 RealT zvar = 0.0*random<RealT>(comm);
121 RealT gvar = random<RealT>(comm);
122 RealT yvar = random<RealT>(comm);
123 ROL::Ptr<ROL::ParameterList> hmcrlist = ROL::makePtr<ROL::ParameterList>();
124 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",
"HMCR");
127 ROL::Ptr<std::vector<RealT> > u_ptr
128 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
129 ROL::Ptr<std::vector<RealT> > gu_ptr
130 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
131 ROL::Ptr<ROL::Vector<RealT> > up
132 = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
133 ROL::Ptr<ROL::Vector<RealT> > gup
134 = ROL::makePtr<DualStateVector>(gu_ptr,fem);
136 ROL::Ptr<std::vector<RealT> > c_ptr
137 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
138 ROL::Ptr<std::vector<RealT> > l_ptr
139 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
140 for (
int i=0; i<nx; i++) {
141 (*l_ptr)[i] = random<RealT>(comm);
143 ROL::Ptr<ROL::Vector<RealT> > cp
144 = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
145 ROL::Ptr<ROL::Vector<RealT> > lp
146 = ROL::makePtr<DualConstraintVector>(l_ptr,fem);
150 int dim = 4, nSamp = 1000;
151 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
152 std::vector<std::vector<RealT> > bounds(dim,tmp);
153 ROL::Ptr<ROL::BatchManager<RealT> > bman
154 = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
155 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
156 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
157 nSamp,bounds,bman,
false,
false,100);
161 bool storage =
true, fdhess =
false;
162 ROL::Ptr<ROL::Objective<RealT> > robj
163 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(
164 pobj,pcon,up,zp,lp,gup,gzp,cp,storage,fdhess);
169 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Order",2);
170 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Confidence Level",0.95);
171 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Convex Combination Parameter",0.0);
172 ROL::Ptr<ROL::Objective<RealT> > obj
173 = ROL::makePtr<ROL::StochasticObjective<RealT> >(robj,*hmcrlist,sampler);
178 bool derivcheck =
false;
180 int nranks = sampler->numBatches();
181 for (
int pid = 0; pid < nranks; pid++) {
182 if ( pid == sampler->batchID() ) {
183 for (
int i = sampler->start(); i < sampler->numMySamples(); i++) {
184 *outStream <<
"Sample " << i <<
" Rank " << sampler->batchID() <<
"\n";
185 *outStream <<
"(" << sampler->getMyPoint(i)[0] <<
", "
186 << sampler->getMyPoint(i)[1] <<
", "
187 << sampler->getMyPoint(i)[2] <<
", "
188 << sampler->getMyPoint(i)[3] <<
")\n";
189 pcon->setParameter(sampler->getMyPoint(i));
190 pcon->checkSolve(*up,*zp,*cp,print,*outStream);
191 robj->setParameter(sampler->getMyPoint(i));
193 robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
194 robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
195 *outStream <<
"\n\n";
201 obj->checkGradient(z,g,y,print0,*outStream0);
202 obj->checkHessVec(z,g,y,print0,*outStream0);
207 std::string filename =
"input.xml";
208 auto parlist = ROL::getParametersFromXmlFile( filename );
210 ROL::Ptr<ROL::Step<RealT>>
211 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
212 ROL::Ptr<ROL::StatusTest<RealT>>
213 status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
217 algo.
run(z, g, *obj, print0, *outStream0);
222 for (
int i = 0; i < nx+2; i++ ) {
223 *outStream0 << std::scientific << std::setprecision(10);
224 *outStream0 << std::setw(20) << std::left << (
RealT)i/((
RealT)nx+1.0);
225 *outStream0 << std::setw(20) << std::left << (*z_ptr)[i];
229 *outStream0 <<
"Scalar Parameter: " << z.getStatistic(0) <<
"\n";
231 catch (std::logic_error& err) {
232 *outStream << err.what() <<
"\n";
238 std::cout <<
"End Result: TEST FAILED\n";
240 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
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
H1VectorDual< RealT > DualStateVector
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[])
H1VectorPrimal< RealT > PrimalStateVector
H1VectorPrimal< RealT > DualConstraintVector