49 Real
random(
const ROL::Ptr<
const Teuchos::Comm<int> > &comm) {
51 if ( Teuchos::rank<int>(*comm)==0 ) {
52 val = (Real)rand()/(Real)RAND_MAX;
54 Teuchos::broadcast<int,Real>(*comm,0,1,&val);
58 int main(
int argc,
char* argv[]) {
60 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
61 ROL::Ptr<const Teuchos::Comm<int> > comm
62 = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
65 int iprint = argc - 1;
66 ROL::Ptr<std::ostream> outStream;
68 if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
69 outStream = ROL::makePtrFromRef(std::cout);
71 outStream = ROL::makePtrFromRef(bhs);
80 std::string filename =
"input.xml";
81 auto parlist = ROL::getParametersFromXmlFile( filename );
83 parlist->sublist(
"Status Test").set(
"Gradient Tolerance",1.e-7);
84 parlist->sublist(
"Status Test").set(
"Step Tolerance",1.e-14);
85 parlist->sublist(
"Status Test").set(
"Iteration Limit",100);
92 ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0);
93 ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtr<ROL::StdVector<RealT>>(z_ptr);
94 ROL::Ptr<std::vector<RealT> > x1_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0);
95 ROL::Ptr<ROL::Vector<RealT> > x1p = ROL::makePtr<ROL::StdVector<RealT>>(x1_ptr);
96 ROL::Ptr<std::vector<RealT> > x2_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0);
97 ROL::Ptr<ROL::Vector<RealT> > x2p = ROL::makePtr<ROL::StdVector<RealT>>(x2_ptr);
98 ROL::Ptr<std::vector<RealT> > x3_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0);
99 ROL::Ptr<ROL::Vector<RealT> > x3p = ROL::makePtr<ROL::StdVector<RealT>>(x3_ptr);
100 std::vector<ROL::Ptr<ROL::Vector<RealT> > > xvec = {x1p, x2p, x3p};
102 ROL::Ptr<std::vector<RealT> > xr_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0);
104 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0);
106 for (
int i = 0; i < nx+2; i++ ) {
107 (*xr_ptr)[i] = random<RealT>(comm);
108 (*d_ptr)[i] = random<RealT>(comm);
111 ROL::Ptr<std::vector<RealT> > u_ptr = ROL::makePtr<std::vector<RealT>>(nx,1);
112 ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtr<ROL::StdVector<RealT>>(u_ptr);
113 ROL::Ptr<std::vector<RealT> > p_ptr = ROL::makePtr<std::vector<RealT>>(nx,0);
114 ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtr<ROL::StdVector<RealT>>(p_ptr);
119 int dim = 4, nSamp = 100;
120 std::vector<RealT> tmp = {-1, 1};
121 std::vector<std::vector<RealT> > bounds(dim,tmp);
122 ROL::Ptr<ROL::BatchManager<RealT> > bman
123 = ROL::makePtr<ROL::StdTeuchosBatchManager<RealT,int>>(comm);
124 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
125 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman,
false,
false,100);
131 ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobjSimOpt
132 = ROL::makePtr<Objective_BurgersControl<RealT>>(alpha,nx);
133 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pconSimOpt
134 = ROL::makePtr<Constraint_BurgersControl<RealT>>(nx);
135 pconSimOpt->setSolveParameters(*parlist);
136 ROL::Ptr<ROL::Objective<RealT> > pObj
137 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(pobjSimOpt,pconSimOpt,up,zp,pp);
139 *outStream <<
"Check Derivatives of Parametrized Objective Function\n";
141 pObj->setParameter(sampler->getMyPoint(0));
142 pObj->checkGradient(*xvec[0],d,
true,*outStream);
143 pObj->checkHessVec(*xvec[0],d,
true,*outStream);
147 const RealT cl(0.9), cc(1), lb(-0.5), ub(0.5);
148 const std::string ra =
"Risk Averse", rm =
"CVaR", dist =
"Parabolic";
149 const bool storage =
true;
151 std::vector<RealT> stat(3,0);
152 ROL::Ptr<ROL::Algorithm<RealT> > algo;
153 ROL::Ptr<ROL::OptimizationProblem<RealT> > optProb;
154 for (
int i = 0; i < 3; ++i) {
155 *outStream <<
"\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
157 ROL::ParameterList list;
158 list.sublist(
"SOL").set(
"Stochastic Component Type",ra);
159 list.sublist(
"SOL").set(
"Store Sampled Value and Gradient",storage);
160 list.sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",rm);
161 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Confidence Level",cl);
162 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Convex Combination Parameter",cc);
163 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Smoothing Parameter",eps);
164 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").set(
"Name",dist);
165 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").sublist(dist).set(
"Lower Bound",lb);
166 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").sublist(dist).set(
"Upper Bound",ub);
168 if ( i==0 ) { xvec[i]->zero(); }
169 else { xvec[i]->set(*xvec[i-1]); }
170 optProb = ROL::makePtr<ROL::OptimizationProblem<RealT>>(pObj,xvec[i]);
172 if ( i > 0 ) { init_stat = stat[i-1]; }
173 list.sublist(
"SOL").set(
"Initial Statistic",init_stat);
174 optProb->setStochasticObjective(list,sampler);
175 optProb->check(*outStream);
177 algo = ROL::makePtr<ROL::Algorithm<RealT>>(
"Trust Region",*parlist,
false);
178 clock_t start = clock();
179 algo->run(*optProb,
true,*outStream);
180 *outStream <<
"Optimization time: " << (
RealT)(clock()-start)/(
RealT)CLOCKS_PER_SEC <<
" seconds.\n";
182 stat[i] = optProb->getSolutionStatistic();
184 eps *=
static_cast<RealT>(1.e-2);
189 *outStream <<
"\nSOLVE NONSMOOTH CVAR PROBLEM WITH BUNDLE TRUST REGION\n";
190 ROL::ParameterList list;
191 list.sublist(
"SOL").set(
"Stochastic Component Type",ra);
192 list.sublist(
"SOL").set(
"Store Sampled Value and Gradient",storage);
193 list.sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",rm);
194 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Confidence Level",cl);
195 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Convex Combination Parameter",cc);
196 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Smoothing Parameter",0.);
197 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").set(
"Name",
"Dirac");
198 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").sublist(
"Dirac").set(
"Location",0.);
201 optProb = ROL::makePtr<ROL::OptimizationProblem<RealT>>(pObj,zp);
202 list.sublist(
"SOL").set(
"Initial Statistic",stat[2]);
203 optProb->setStochasticObjective(list,sampler);
204 optProb->check(*outStream);
206 parlist->sublist(
"Status Test").set(
"Iteration Limit",1000);
207 parlist->sublist(
"Step").sublist(
"Bundle").set(
"Epsilon Solution Tolerance",1.e-7);
208 algo = ROL::makePtr<ROL::Algorithm<RealT>>(
"Bundle",*parlist,
false);
209 clock_t start = clock();
210 algo->run(*optProb,
true,*outStream);
211 *outStream <<
"Optimization time: " << (
RealT)(clock()-start)/(
RealT)CLOCKS_PER_SEC <<
" seconds.\n";
215 ROL::Ptr<ROL::Vector<RealT> > cErr = zp->clone();
216 RealT zstat = optProb->getSolutionStatistic();
217 *outStream <<
"\nSUMMARY:\n";
218 *outStream <<
" ---------------------------------------------\n";
219 *outStream <<
" True Value-At-Risk = " << zstat <<
"\n";
220 *outStream <<
" ---------------------------------------------\n";
221 RealT VARerror = std::abs(zstat-stat[0]);
222 cErr->set(*xvec[0]); cErr->axpy(-1.0,*zp);
223 RealT CTRLerror = cErr->norm();
224 RealT TOTerror1 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
225 *outStream <<
" Value-At-Risk (1.e-2) = " << stat[0] <<
"\n";
226 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
227 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
228 *outStream <<
" Total Error = " << TOTerror1 <<
"\n";
229 *outStream <<
" ---------------------------------------------\n";
230 VARerror = std::abs(zstat-stat[1]);
231 cErr->set(*xvec[1]); cErr->axpy(-1.0,*zp);
232 CTRLerror = cErr->norm();
233 RealT TOTerror2 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
234 *outStream <<
" Value-At-Risk (1.e-4) = " << stat[1] <<
"\n";
235 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
236 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
237 *outStream <<
" Total Error = " << TOTerror2 <<
"\n";
238 *outStream <<
" ---------------------------------------------\n";
239 VARerror = std::abs(zstat-stat[2]);
240 cErr->set(*xvec[2]); cErr->axpy(-1.0,*zp);
241 CTRLerror = cErr->norm();
242 RealT TOTerror3 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
243 *outStream <<
" Value-At-Risk (1.e-6) = " << stat[2] <<
"\n";
244 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
245 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
246 *outStream <<
" Total Error = " << TOTerror3 <<
"\n";
247 *outStream <<
" ---------------------------------------------\n\n";
249 errorFlag += ((TOTerror1 < 90.*TOTerror2) && (TOTerror2 < 90.*TOTerror3)) ? 1 : 0;
252 std::ofstream control;
253 control.open(
"example04_control.txt");
254 for (
int n = 0; n < nx+2; n++) {
255 control << std::scientific << std::setprecision(15)
256 << std::setw(25) <<
static_cast<RealT>(n)/static_cast<RealT>(nx+1)
257 << std::setw(25) << (*z_ptr)[n]
263 catch (std::logic_error err) {
264 *outStream << err.what() <<
"\n";
269 std::cout <<
"End Result: TEST FAILED\n";
271 std::cout <<
"End Result: TEST PASSED\n";
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Real random(const ROL::Ptr< const Teuchos::Comm< int > > &comm)
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])