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::OptimizationProblem<RealT>> optProb;
153 ROL::Ptr<ROL::OptimizationSolver<RealT>> solver;
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 parlist->sublist(
"Step").set(
"Type",
"Trust Region");
178 solver = ROL::makePtr<ROL::OptimizationSolver<RealT>>(*optProb,*parlist);
179 clock_t start = clock();
180 solver->solve(*outStream);
181 *outStream <<
"Optimization time: " << (
RealT)(clock()-start)/(
RealT)CLOCKS_PER_SEC <<
" seconds.\n";
183 stat[i] = optProb->getSolutionStatistic();
185 eps *=
static_cast<RealT>(1.e-2);
190 *outStream <<
"\nSOLVE NONSMOOTH CVAR PROBLEM WITH BUNDLE TRUST REGION\n";
191 ROL::ParameterList list;
192 list.sublist(
"SOL").set(
"Stochastic Component Type",ra);
193 list.sublist(
"SOL").set(
"Store Sampled Value and Gradient",storage);
194 list.sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",rm);
195 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Confidence Level",cl);
196 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Convex Combination Parameter",cc);
197 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).set(
"Smoothing Parameter",0.);
198 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").set(
"Name",
"Dirac");
199 list.sublist(
"SOL").sublist(
"Risk Measure").sublist(rm).sublist(
"Distribution").sublist(
"Dirac").set(
"Location",0.);
202 optProb = ROL::makePtr<ROL::OptimizationProblem<RealT>>(pObj,zp);
203 list.sublist(
"SOL").set(
"Initial Statistic",stat[2]);
204 optProb->setStochasticObjective(list,sampler);
205 optProb->check(*outStream);
207 parlist->sublist(
"Status Test").set(
"Iteration Limit",1000);
208 parlist->sublist(
"Step").sublist(
"Bundle").set(
"Epsilon Solution Tolerance",1.e-7);
209 parlist->sublist(
"Step").set(
"Type",
"Bundle");
210 solver = ROL::makePtr<ROL::OptimizationSolver<RealT>>(*optProb,*parlist);
211 clock_t start = clock();
212 solver->solve(*outStream);
213 *outStream <<
"Optimization time: " << (
RealT)(clock()-start)/(
RealT)CLOCKS_PER_SEC <<
" seconds.\n";
217 ROL::Ptr<ROL::Vector<RealT> > cErr = zp->clone();
218 RealT zstat = optProb->getSolutionStatistic();
219 *outStream <<
"\nSUMMARY:\n";
220 *outStream <<
" ---------------------------------------------\n";
221 *outStream <<
" True Value-At-Risk = " << zstat <<
"\n";
222 *outStream <<
" ---------------------------------------------\n";
223 RealT VARerror = std::abs(zstat-stat[0]);
224 cErr->set(*xvec[0]); cErr->axpy(-1.0,*zp);
225 RealT CTRLerror = cErr->norm();
226 RealT TOTerror1 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
227 *outStream <<
" Value-At-Risk (1.e-2) = " << stat[0] <<
"\n";
228 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
229 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
230 *outStream <<
" Total Error = " << TOTerror1 <<
"\n";
231 *outStream <<
" ---------------------------------------------\n";
232 VARerror = std::abs(zstat-stat[1]);
233 cErr->set(*xvec[1]); cErr->axpy(-1.0,*zp);
234 CTRLerror = cErr->norm();
235 RealT TOTerror2 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
236 *outStream <<
" Value-At-Risk (1.e-4) = " << stat[1] <<
"\n";
237 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
238 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
239 *outStream <<
" Total Error = " << TOTerror2 <<
"\n";
240 *outStream <<
" ---------------------------------------------\n";
241 VARerror = std::abs(zstat-stat[2]);
242 cErr->set(*xvec[2]); cErr->axpy(-1.0,*zp);
243 CTRLerror = cErr->norm();
244 RealT TOTerror3 = std::sqrt(std::pow(VARerror,2)+std::pow(CTRLerror,2));
245 *outStream <<
" Value-At-Risk (1.e-6) = " << stat[2] <<
"\n";
246 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
247 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
248 *outStream <<
" Total Error = " << TOTerror3 <<
"\n";
249 *outStream <<
" ---------------------------------------------\n\n";
251 errorFlag += ((TOTerror1 < 90.*TOTerror2) && (TOTerror2 < 90.*TOTerror3)) ? 1 : 0;
254 std::ofstream control;
255 control.open(
"example04_control.txt");
256 for (
int n = 0; n < nx+2; n++) {
257 control << std::scientific << std::setprecision(15)
258 << std::setw(25) <<
static_cast<RealT>(n)/static_cast<RealT>(nx+1)
259 << std::setw(25) << (*z_ptr)[n]
265 catch (std::logic_error& err) {
266 *outStream << err.what() <<
"\n";
271 std::cout <<
"End Result: TEST FAILED\n";
273 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[])