13 #include "Teuchos_Time.hpp" 14 #include "Teuchos_oblackholestream.hpp" 15 #include "Teuchos_XMLParameterListHelpers.hpp" 16 #include "Teuchos_GlobalMPISession.hpp" 17 #include "Teuchos_Comm.hpp" 18 #include "Teuchos_DefaultComm.hpp" 19 #include "Teuchos_CommHelpers.hpp" 21 int main(
int argc,
char *argv[] ) {
23 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
24 Teuchos::RCP<const Teuchos::Comm<int> > comm =
25 Teuchos::DefaultComm<int>::getComm();
28 int iprint = argc - 1;
29 Teuchos::RCP<std::ostream> outStream;
30 Teuchos::oblackholestream bhs;
32 outStream = Teuchos::rcp(&std::cout,
false);
34 outStream = Teuchos::rcp(&bhs,
false);
46 std::string filename =
"example_02.xml";
47 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp(
new Teuchos::ParameterList() );
48 Teuchos::updateParametersFromXmlFile( filename, Teuchos::Ptr<Teuchos::ParameterList>(&*parlist) );
49 if ( parlist->get(
"Display Option",0) && (comm->getRank() > 0) ) {
50 parlist->set(
"Display Option",0);
53 filename =
"input.xml";
54 Teuchos::RCP<Teuchos::ParameterList> ROL_parlist = Teuchos::rcp(
new Teuchos::ParameterList() );
55 Teuchos::updateParametersFromXmlFile( filename, Teuchos::Ptr<Teuchos::ParameterList>(&*ROL_parlist) );
61 bool useSA = parlist->get(
"Use Stochastic Approximation",
false);
64 nSamp = parlist->get(
"Number of Monte Carlo Samples",1000);
66 std::vector<double> tmp(2); tmp[0] = -1.0; tmp[1] = 1.0;
67 std::vector<std::vector<double> > bounds(dim,tmp);
68 Teuchos::RCP<ROL::BatchManager<double> > bman
70 Teuchos::RCP<ROL::SampleGenerator<double> > sampler
76 int nx = parlist->get(
"Number of Elements", 128);
77 Teuchos::RCP<std::vector<double> > z_rcp = Teuchos::rcp(
new std::vector<double>(nx+1, 0.0) );
79 Teuchos::RCP<std::vector<double> > u_rcp = Teuchos::rcp(
new std::vector<double>(nx-1, 0.0) );
82 Teuchos::RCP<std::vector<double> > p_rcp = Teuchos::rcp(
new std::vector<double>(nx-1, 0.0) );
84 Teuchos::RCP<std::vector<double> > U_rcp = Teuchos::rcp(
new std::vector<double>(nx+1, 35.0) );
86 Teuchos::RCP<std::vector<double> > L_rcp = Teuchos::rcp(
new std::vector<double>(nx+1, -5.0) );
93 double alpha = parlist->get(
"Penalty Parameter", 1.e-4);
94 Teuchos::RCP<FEM<double> > fem = Teuchos::rcp(
new FEM<double>(nx));
95 Teuchos::RCP<ROL::Objective_SimOpt<double> > pObj
97 Teuchos::RCP<ROL::EqualityConstraint_SimOpt<double> > pCon
99 Teuchos::RCP<ROL::Objective<double> > robj
106 if (parlist->get(
"Run Derivative Check",
false)) {
108 Teuchos::RCP<std::vector<double> > dz_rcp = Teuchos::rcp(
new std::vector<double>(nx+1, 0.0) );
110 Teuchos::RCP<std::vector<double> > du_rcp = Teuchos::rcp(
new std::vector<double>(nx-1, 0.0) );
115 for (
int i=0; i<nx+1; i++) {
116 (*dz_rcp)[i] = 2.0*(double)rand()/(double)RAND_MAX - 1.0;
117 (*z_rcp)[i] = 2.0*(double)rand()/(double)RAND_MAX - 1.0;
119 for (
int i=0; i<nx-1; i++) {
120 (*du_rcp)[i] = 2.0*(double)rand()/(double)RAND_MAX - 1.0;
121 (*u_rcp)[i] = 2.0*(double)rand()/(double)RAND_MAX - 1.0;
124 std::vector<double> param(dim,0.0);
125 robj->setParameter(param);
126 if ( comm->getRank() == 0 ) {
127 std::cout <<
"\nRUN DERIVATIVE CHECK FOR PARAMETRIZED OBJECTIVE FUNCTION SIMOPT\n";
129 pObj->checkGradient(x,d,(comm->getRank()==0));
130 pObj->checkHessVec(x,d,(comm->getRank()==0));
131 if ( comm->getRank() == 0 ) {
132 std::cout <<
"\nRUN DERIVATIVE CHECK FOR PARAMETRIZED EQUALITY CONSTRAINT SIMOPT\n";
134 pCon->checkApplyJacobian(x,d,*p,(comm->getRank()==0));
135 pCon->checkApplyAdjointJacobian(x,*du,*p,x,(comm->getRank()==0));
136 pCon->checkApplyAdjointHessian(x,*du,d,x,(comm->getRank()==0));
137 if ( comm->getRank() == 0 ) {
138 std::cout <<
"\nRUN DERIVATIVE CHECK FOR PARAMETRIZED OBJECTIVE FUNCTION\n";
140 robj->checkGradient(*z,*dz,(comm->getRank()==0));
141 robj->checkHessVec(*z,*dz,(comm->getRank()==0));
143 if ( comm->getRank() == 0 ) {
144 std::cout <<
"\nRUN DERIVATIVE CHECK FOR RISK-NEUTRAL OBJECTIVE FUNCTION\n";
153 Teuchos::RCP<ROL::Algorithm<double> > algo;
155 ROL_parlist->sublist(
"General").set(
"Recompute Objective Function",
false);
156 ROL_parlist->sublist(
"Step").sublist(
"Line Search").set(
"Initial Step Size",0.1/alpha);
157 ROL_parlist->sublist(
"Step").sublist(
"Line Search").set(
"User Defined Initial Step Size",
true);
158 ROL_parlist->sublist(
"Step").sublist(
"Line Search").sublist(
"Line-Search Method").set(
"Type",
"Iteration Scaling");
159 ROL_parlist->sublist(
"Step").sublist(
"Line Search").sublist(
"Descent Method").set(
"Type",
"Steepest Descent");
160 ROL_parlist->sublist(
"Step").sublist(
"Line Search").sublist(
"Curvature Condition").set(
"Type",
"Null Curvature Condition");
170 Teuchos::Time timer(
"Optimization Time",
true);
172 algo->run(*z,obj,bnd,(comm->getRank()==0));
173 double optTime = timer.stop();
178 int my_number_samples = sampler->numMySamples(), number_samples = 0;
179 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_samples,&number_samples);
181 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_solves,&number_solves);
182 if (comm->getRank() == 0) {
183 std::cout <<
"Number of Samples = " << number_samples <<
"\n";
184 std::cout <<
"Number of Solves = " << number_solves <<
"\n";
185 std::cout <<
"Optimization Time = " << optTime <<
"\n\n";
188 if ( comm->getRank() == 0 ) {
191 file.open(
"control_SA.txt");
194 file.open(
"control_SAA.txt");
196 std::vector<double> xmesh(fem->nz(),0.0);
197 fem->build_mesh(xmesh);
198 for (
int i = 0; i < fem->nz(); i++ ) {
199 file << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << xmesh[i] <<
" " 200 << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << (*z_rcp)[i]
206 catch (std::logic_error err) {
207 *outStream << err.what() <<
"\n";
212 std::cout <<
"End Result: TEST FAILED\n";
214 std::cout <<
"End Result: TEST PASSED\n";
Defines the linear algebra or vector space interface for simulation-based optimization.
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
Provides the interface to apply upper and lower bound constraints.
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
int main(int argc, char *argv[])