47 Real
random(
const Teuchos::RCP<
const Teuchos::Comm<int> > &comm) {
49 if ( Teuchos::rank<int>(*comm)==0 ) {
50 val = (Real)rand()/(Real)RAND_MAX;
52 Teuchos::broadcast<int,Real>(*comm,0,1,&val);
56 int main(
int argc,
char* argv[]) {
58 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
59 Teuchos::RCP<const Teuchos::Comm<int> > comm
60 = Teuchos::DefaultComm<int>::getComm();
63 int iprint = argc - 1;
64 Teuchos::RCP<std::ostream> outStream;
65 Teuchos::oblackholestream bhs;
66 if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
67 outStream = Teuchos::rcp(&std::cout,
false);
69 outStream = Teuchos::rcp(&bhs,
false);
78 std::string filename =
"input.xml";
79 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp(
new Teuchos::ParameterList() );
80 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
82 parlist->sublist(
"Status Test").set(
"Gradient Tolerance",1.e-10);
83 parlist->sublist(
"Status Test").set(
"Step Tolerance",1.e-14);
84 parlist->sublist(
"Status Test").set(
"Iteration Limit",100);
85 Teuchos::RCP<ROL::Algorithm<double> > algo;
91 Teuchos::RCP<std::vector<double> > x1_rcp = Teuchos::rcp(
new std::vector<double>(nx+2,0.0) );
93 Teuchos::RCP<std::vector<double> > x2_rcp = Teuchos::rcp(
new std::vector<double>(nx+2,0.0) );
95 Teuchos::RCP<std::vector<double> > x3_rcp = Teuchos::rcp(
new std::vector<double>(nx+2,0.0) );
97 Teuchos::RCP<std::vector<double> > z_rcp = Teuchos::rcp(
new std::vector<double>(nx+2,0.0) );
99 Teuchos::RCP<std::vector<double> > xr_rcp = Teuchos::rcp(
new std::vector<double>(nx+2,0.0) );
101 Teuchos::RCP<std::vector<double> > d_rcp = Teuchos::rcp(
new std::vector<double>(nx+2,0.0) );
103 for (
int i = 0; i < nx+2; i++ ) {
104 (*xr_rcp)[i] = random<double>(comm);
105 (*d_rcp)[i] = random<double>(comm);
108 Teuchos::RCP<std::vector<double> > u_rcp = Teuchos::rcp(
new std::vector<double>(nx,0.0) );
110 Teuchos::RCP<std::vector<double> > p_rcp = Teuchos::rcp(
new std::vector<double>(nx,0.0) );
112 Teuchos::RCP<ROL::Vector<double> > up = Teuchos::rcp(&u,
false);
113 Teuchos::RCP<ROL::Vector<double> > pp = Teuchos::rcp(&p,
false);
120 std::vector<double> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
121 std::vector<std::vector<double> > bounds(dim,tmp);
122 Teuchos::RCP<ROL::BatchManager<double> > bman
124 Teuchos::RCP<ROL::SampleGenerator<double> > sampler
131 double alpha = 1.e-3;
132 Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<double> > pobjSimOpt
134 Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<double> > pconSimOpt
136 Teuchos::RCP<ROL::ParametrizedObjective<double> > pObj
138 Teuchos::RCP<ROL::Distribution<double> > dist;
139 Teuchos::RCP<ROL::PlusFunction<double> > pf;
140 Teuchos::RCP<ROL::RiskMeasure<double> > rm;
141 Teuchos::RCP<ROL::Objective<double> > obj;
143 *outStream <<
"Check Derivatives of Parametrized Objective Function\n";
145 pObj->setParameter(sampler->getMyPoint(0));
146 pObj->checkGradient(x1,d,
true,*outStream);
147 pObj->checkHessVec(x1,d,
true,*outStream);
151 *outStream <<
"\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
153 double prob = 0.99, coeff = 1.0, gamma = 1.e-2;
154 Teuchos::ParameterList distlist;
155 distlist.sublist(
"SOL").sublist(
"Distribution").set(
"Name",
"Parabolic");
156 distlist.sublist(
"SOL").sublist(
"Distribution").sublist(
"Parabolic").set(
"Lower Bound",-0.5);
157 distlist.sublist(
"SOL").sublist(
"Distribution").sublist(
"Parabolic").set(
"Upper Bound", 0.5);
158 dist = ROL::DistributionFactory<double>(distlist);
163 double x1v = 10.0*random<double>(comm)-5.0;
164 Teuchos::RCP<ROL::Vector<double> > x1p = Teuchos::rcp(&x1,
false);
169 clock_t start = clock();
170 algo->run(x1c,*obj,
true,*outStream);
171 *outStream <<
"Optimization time: " << (double)(clock()-start)/(
double)CLOCKS_PER_SEC <<
" seconds.\n";
175 *outStream <<
"\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
182 double x2v = 10.0*random<double>(comm)-5.0;
183 Teuchos::RCP<ROL::Vector<double> > x2p = Teuchos::rcp(&x2,
false);
189 algo->run(x2c,*obj,
true,*outStream);
190 *outStream <<
"Optimization time: " << (double)(clock()-start)/(
double)CLOCKS_PER_SEC <<
" seconds.\n";
194 *outStream <<
"\nSOLVE SMOOTHED CONDITIONAL VALUE AT RISK WITH TRUST REGION\n";
201 double x3v = 10.0*random<double>(comm)-5.0;
202 Teuchos::RCP<ROL::Vector<double> > x3p = Teuchos::rcp(&x3,
false);
208 algo->run(x3c,*obj,
true,*outStream);
209 *outStream <<
"Optimization time: " << (double)(clock()-start)/(
double)CLOCKS_PER_SEC <<
" seconds.\n";
213 *outStream <<
"\nSOLVE NONSMOOTH CVAR PROBLEM WITH BUNDLE TRUST REGION\n";
215 distlist.sublist(
"SOL").sublist(
"Distribution").set(
"Name",
"Dirac");
216 distlist.sublist(
"SOL").sublist(
"Distribution").sublist(
"Dirac").set(
"Location",0.);
217 dist = ROL::DistributionFactory<double>(distlist);
222 double zv = 10.0*random<double>(comm)-5.0;
223 Teuchos::RCP<ROL::Vector<double> > zp = Teuchos::rcp(&z,
false);
226 parlist->sublist(
"Status Test").set(
"Iteration Limit",10000);
227 parlist->sublist(
"Step").sublist(
"Bundle").set(
"Epsilon Solution Tolerance",1.e-8);
231 algo->run(zc,*obj,
true,*outStream);
232 *outStream <<
"Optimization time: " << (double)(clock()-start)/(
double)CLOCKS_PER_SEC <<
" seconds.\n";
236 *outStream <<
"\nSUMMARY:\n";
237 *outStream <<
" ---------------------------------------------\n";
238 *outStream <<
" True Value-At-Risk = " << zc.
getVaR() <<
"\n";
239 *outStream <<
" ---------------------------------------------\n";
241 Teuchos::RCP<ROL::Vector<double> > cErr = x1.
clone();
242 cErr->set(x1); cErr->axpy(-1.0,z);
243 double CTRLerror = cErr->norm();
245 cErr->set(x1c); cErr->axpy(-1.0,zc);
246 double TOTerror1 = cErr->norm();
247 *outStream <<
" Value-At-Risk (1.e-2) = " << x1c.
getVaR() <<
"\n";
248 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
249 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
250 *outStream <<
" Total Error = " << TOTerror1 <<
"\n";
251 *outStream <<
" ---------------------------------------------\n";
254 cErr->set(x2); cErr->axpy(-1.0,z);
255 CTRLerror = cErr->norm();
257 cErr->set(x2c); cErr->axpy(-1.0,zc);
258 double TOTerror2 = cErr->norm();
259 *outStream <<
" Value-At-Risk (1.e-4) = " << x2c.
getVaR() <<
"\n";
260 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
261 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
262 *outStream <<
" Total Error = " << TOTerror2 <<
"\n";
263 *outStream <<
" ---------------------------------------------\n";
266 cErr->set(x3); cErr->axpy(-1.0,z);
267 CTRLerror = cErr->norm();
269 cErr->set(x3c); cErr->axpy(-1.0,zc);
270 double TOTerror3 = cErr->norm();
271 *outStream <<
" Value-At-Risk (1.e-6) = " << x3c.
getVaR() <<
"\n";
272 *outStream <<
" Value-At-Risk Error = " << VARerror <<
"\n";
273 *outStream <<
" Control Error = " << CTRLerror <<
"\n";
274 *outStream <<
" Total Error = " << TOTerror3 <<
"\n";
275 *outStream <<
" ---------------------------------------------\n\n";
277 errorFlag += ((TOTerror1 < 90.*TOTerror2) && (TOTerror2 < 90.*TOTerror3)) ? 1 : 0;
279 catch (std::logic_error err) {
280 *outStream << err.what() <<
"\n";
285 std::cout <<
"End Result: TEST FAILED\n";
287 std::cout <<
"End Result: TEST PASSED\n";
int main(int argc, char *argv[])
virtual void zero()
Set to zero vector.
const Real getVaR() const
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Real random(const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
void set(const Vector< Real > &x)
Set where .
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
virtual void set(const Vector &x)
Set where .