55 #include "ROL_ParameterList.hpp"
60 #include "Teuchos_GlobalMPISession.hpp"
61 #include "Teuchos_Comm.hpp"
62 #include "Teuchos_DefaultComm.hpp"
63 #include "Teuchos_CommHelpers.hpp"
78 int main(
int argc,
char *argv[]) {
80 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
82 auto comm = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
85 int iprint = argc - 1;
86 bool print = (iprint>0);
87 ROL::Ptr<std::ostream> outStream;
90 outStream = ROL::makePtrFromRef(std::cout);
92 outStream = ROL::makePtrFromRef(bhs);
94 bool print0 = print && !comm->getRank();
95 ROL::Ptr<std::ostream> outStream0;
97 outStream0 = ROL::makePtrFromRef(std::cout);
99 outStream0 = ROL::makePtrFromRef(bhs);
114 ROL::Ptr<BurgersFEM<RealT> > fem
115 = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
116 fem->test_inverse_mass(*outStream0);
117 fem->test_inverse_H1(*outStream0);
121 ROL::Ptr<std::vector<RealT> > ud_ptr
122 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
123 ROL::Ptr<ROL::Vector<RealT> > ud
124 = ROL::makePtr<L2VectorPrimal<RealT>>(ud_ptr,fem);
125 ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobj
126 = ROL::makePtr<Objective_BurgersControl<RealT>>(fem,ud,alpha);
131 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon
132 = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
137 ROL::Ptr<std::vector<RealT> > z_ptr
138 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
139 ROL::Ptr<std::vector<RealT> > gz_ptr
140 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
141 ROL::Ptr<std::vector<RealT> > yz_ptr
142 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.0);
143 for (
int i=0; i<nx+2; i++) {
144 (*yz_ptr)[i] = 2.0*random<RealT>(comm)-1.0;
146 ROL::Ptr<ROL::Vector<RealT> > zp
147 = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
148 ROL::Ptr<ROL::Vector<RealT> > gzp
149 = ROL::makePtr<DualControlVector>(gz_ptr,fem);
150 ROL::Ptr<ROL::Vector<RealT> > yzp
151 = ROL::makePtr<PrimalControlVector>(yz_ptr,fem);
152 RealT zvar = 0.0*random<RealT>(comm);
153 RealT gvar = random<RealT>(comm);
154 RealT yvar = random<RealT>(comm);
155 ROL::Ptr<ROL::ParameterList> hmcrlist = ROL::makePtr<ROL::ParameterList>();
156 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",
"HMCR");
159 ROL::Ptr<std::vector<RealT> > u_ptr
160 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
161 ROL::Ptr<std::vector<RealT> > gu_ptr
162 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
163 ROL::Ptr<ROL::Vector<RealT> > up
164 = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
165 ROL::Ptr<ROL::Vector<RealT> > gup
166 = ROL::makePtr<DualStateVector>(gu_ptr,fem);
168 ROL::Ptr<std::vector<RealT> > c_ptr
169 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
170 ROL::Ptr<std::vector<RealT> > l_ptr
171 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
172 for (
int i=0; i<nx; i++) {
173 (*l_ptr)[i] = random<RealT>(comm);
175 ROL::Ptr<ROL::Vector<RealT> > cp
176 = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
177 ROL::Ptr<ROL::Vector<RealT> > lp
178 = ROL::makePtr<DualConstraintVector>(l_ptr,fem);
182 int dim = 4, nSamp = 1000;
183 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
184 std::vector<std::vector<RealT> > bounds(dim,tmp);
185 ROL::Ptr<ROL::BatchManager<RealT> > bman
186 = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
187 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
188 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
189 nSamp,bounds,bman,
false,
false,100);
193 bool storage =
true, fdhess =
false;
194 ROL::Ptr<ROL::Objective<RealT> > robj
195 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(
196 pobj,pcon,up,zp,lp,gup,gzp,cp,storage,fdhess);
201 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Order",2);
202 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Confidence Level",0.95);
203 hmcrlist->sublist(
"SOL").sublist(
"Risk Measure").sublist(
"HMCR").set(
"Convex Combination Parameter",0.0);
204 ROL::Ptr<ROL::Objective<RealT> > obj
205 = ROL::makePtr<ROL::StochasticObjective<RealT> >(robj,*hmcrlist,sampler);
210 bool derivcheck =
false;
212 int nranks = sampler->numBatches();
213 for (
int pid = 0; pid < nranks; pid++) {
214 if ( pid == sampler->batchID() ) {
215 for (
int i = sampler->start(); i < sampler->numMySamples(); i++) {
216 *outStream <<
"Sample " << i <<
" Rank " << sampler->batchID() <<
"\n";
217 *outStream <<
"(" << sampler->getMyPoint(i)[0] <<
", "
218 << sampler->getMyPoint(i)[1] <<
", "
219 << sampler->getMyPoint(i)[2] <<
", "
220 << sampler->getMyPoint(i)[3] <<
")\n";
221 pcon->setParameter(sampler->getMyPoint(i));
222 pcon->checkSolve(*up,*zp,*cp,print,*outStream);
223 robj->setParameter(sampler->getMyPoint(i));
225 robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
226 robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
227 *outStream <<
"\n\n";
233 obj->checkGradient(z,g,y,print0,*outStream0);
234 obj->checkHessVec(z,g,y,print0,*outStream0);
239 std::string filename =
"input.xml";
240 auto parlist = ROL::getParametersFromXmlFile( filename );
245 algo.
run(z, g, *obj, print0, *outStream0);
250 for (
int i = 0; i < nx+2; i++ ) {
251 *outStream0 << std::scientific << std::setprecision(10);
252 *outStream0 << std::setw(20) << std::left << (
RealT)i/((
RealT)nx+1.0);
253 *outStream0 << std::setw(20) << std::left << (*z_ptr)[i];
257 *outStream0 <<
"Scalar Parameter: " << z.getStatistic(0) <<
"\n";
259 catch (std::logic_error err) {
260 *outStream << err.what() <<
"\n";
266 std::cout <<
"End Result: TEST FAILED\n";
268 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