57 #include "ROL_ParameterList.hpp"
62 #include "Teuchos_GlobalMPISession.hpp"
63 #include "Teuchos_Comm.hpp"
64 #include "Teuchos_DefaultComm.hpp"
65 #include "Teuchos_CommHelpers.hpp"
80 int main(
int argc,
char *argv[]) {
82 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
84 auto comm = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
87 int iprint = argc - 1;
88 bool print = (iprint>0);
89 ROL::Ptr<std::ostream> outStream;
92 outStream = ROL::makePtrFromRef(std::cout);
94 outStream = ROL::makePtrFromRef(bhs);
96 bool print0 = print && !comm->getRank();
97 ROL::Ptr<std::ostream> outStream0;
99 outStream0 = ROL::makePtrFromRef(std::cout);
101 outStream0 = ROL::makePtrFromRef(bhs);
116 ROL::Ptr<BurgersFEM<RealT> > fem
117 = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
118 fem->test_inverse_mass(*outStream0);
119 fem->test_inverse_H1(*outStream0);
123 ROL::Ptr<std::vector<RealT> > ud_ptr
124 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
125 ROL::Ptr<ROL::Vector<RealT> > ud
126 = ROL::makePtr<L2VectorPrimal<RealT>>(ud_ptr,fem);
127 ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj
128 = ROL::makePtr<Objective_BurgersControl<RealT>>(fem,ud,alpha);
133 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon
134 = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
139 ROL::Ptr<std::vector<RealT> > z_ptr
140 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
141 ROL::Ptr<std::vector<RealT> > gz_ptr
142 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
143 ROL::Ptr<std::vector<RealT> > yz_ptr
144 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
145 for (
int i=0; i<nx+2; i++) {
146 (*yz_ptr)[i] = 2.0*random<RealT>(comm)-1.0;
148 ROL::Ptr<ROL::Vector<RealT> > zp
149 = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
150 ROL::Ptr<ROL::Vector<RealT> > gzp
151 = ROL::makePtr<DualControlVector>(gz_ptr,fem);
152 ROL::Ptr<ROL::Vector<RealT> > yzp
153 = ROL::makePtr<PrimalControlVector>(yz_ptr,fem);
154 RealT zvar = 0.0*random<RealT>(comm);
155 RealT gvar = random<RealT>(comm);
156 RealT yvar = random<RealT>(comm);
157 ROL::Ptr<ROL::ParameterList> hmcrlist = ROL::makePtr<ROL::ParameterList>();
158 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",
"HMCR");
161 ROL::Ptr<std::vector<RealT> > u_ptr
162 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
163 ROL::Ptr<std::vector<RealT> > gu_ptr
164 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
165 ROL::Ptr<ROL::Vector<RealT> > up
166 = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
167 ROL::Ptr<ROL::Vector<RealT> > gup
168 = ROL::makePtr<DualStateVector>(gu_ptr,fem);
170 ROL::Ptr<std::vector<RealT> > c_ptr
171 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
172 ROL::Ptr<std::vector<RealT> > l_ptr
173 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
174 for (
int i=0; i<nx; i++) {
175 (*l_ptr)[i] = random<RealT>(comm);
177 ROL::Ptr<ROL::Vector<RealT> > cp
178 = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
179 ROL::Ptr<ROL::Vector<RealT> > lp
180 = ROL::makePtr<DualConstraintVector>(l_ptr,fem);
184 int dim = 4, nSamp = 1000;
185 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
186 std::vector<std::vector<RealT> > bounds(dim,tmp);
187 ROL::Ptr<ROL::BatchManager<RealT> > bman
188 = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
189 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
190 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
191 nSamp,bounds,bman,
false,
false,100);
195 bool storage =
true, fdhess =
false;
196 ROL::Ptr<ROL::Objective<RealT> > robj
197 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(
198 pobj,pcon,up,zp,lp,gup,gzp,cp,storage,fdhess);
203 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Order",2);
204 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Confidence Level",0.95);
205 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Convex Combination Parameter",0.0);
206 ROL::Ptr<ROL::Objective<RealT> > obj
207 = ROL::makePtr<ROL::StochasticObjective<RealT> >(robj,*hmcrlist,sampler);
212 bool derivcheck =
false;
214 int nranks = sampler->numBatches();
215 for (
int pid = 0; pid < nranks; pid++) {
216 if ( pid == sampler->batchID() ) {
217 for (
int i = sampler->start(); i < sampler->numMySamples(); i++) {
218 *outStream <<
"Sample " << i <<
" Rank " << sampler->batchID() <<
"\n";
219 *outStream <<
"(" << sampler->getMyPoint(i)[0] <<
", "
220 << sampler->getMyPoint(i)[1] <<
", "
221 << sampler->getMyPoint(i)[2] <<
", "
222 << sampler->getMyPoint(i)[3] <<
")\n";
223 pcon->setParameter(sampler->getMyPoint(i));
224 pcon->checkSolve(*up,*zp,*cp,print,*outStream);
225 robj->setParameter(sampler->getMyPoint(i));
227 robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
228 robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
229 *outStream <<
"\n\n";
235 obj->checkGradient(z,g,y,print0,*outStream0);
236 obj->checkHessVec(z,g,y,print0,*outStream0);
241 std::string filename =
"input.xml";
242 auto parlist = ROL::getParametersFromXmlFile( filename );
244 ROL::Ptr<ROL::Step<RealT>>
245 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
246 ROL::Ptr<ROL::StatusTest<RealT>>
247 status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
251 algo.
run(z, g, *obj, print0, *outStream0);
256 for (
int i = 0; i < nx+2; i++ ) {
257 *outStream0 << std::scientific << std::setprecision(10);
258 *outStream0 << std::setw(20) << std::left << (
RealT)i/((
RealT)nx+1.0);
259 *outStream0 << std::setw(20) << std::left << (*z_ptr)[i];
263 *outStream0 <<
"Scalar Parameter: " << z.getStatistic(0) <<
"\n";
265 catch (std::logic_error& err) {
266 *outStream << err.what() <<
"\n";
272 std::cout <<
"End Result: TEST FAILED\n";
274 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