6 #include "ROL_StdTeuchosBatchManager.hpp"
9 #include "ROL_ParameterList.hpp"
14 #include "Teuchos_Time.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);
25 auto comm = ROL::toPtr( Teuchos::DefaultComm<int>::getComm() );
28 int iprint = argc - 1;
29 ROL::Ptr<std::ostream> outStream;
31 if (iprint > 0 && comm->getRank()==0)
32 outStream = ROL::makePtrFromRef(std::cout);
34 outStream = ROL::makePtrFromRef(bhs);
46 std::string filename =
"input_ex03.xml";
47 auto parlist = ROL::getParametersFromXmlFile( filename );
49 if ( parlist->sublist(
"Problem Data").get(
"Display Option",0) && (comm->getRank() > 0) ) {
50 parlist->set(
"Display Option",0);
57 int nSamp = parlist->sublist(
"Problem Data").get(
"Number of Monte Carlo Samples",1000);
58 std::vector<double> tmp(2); tmp[0] = -1.0; tmp[1] = 1.0;
59 std::vector<std::vector<double> > bounds(dim,tmp);
60 ROL::Ptr<ROL::BatchManager<double> > bman
61 = ROL::makePtr<ROL::StdTeuchosBatchManager<double,int>>(comm);
62 ROL::Ptr<ROL::SampleGenerator<double> > sampler
63 = ROL::makePtr<ROL::MonteCarloGenerator<double>>(nSamp,bounds,bman,
false);
68 int nx = parlist->sublist(
"Problem Data").get(
"Number of Elements", 128);
69 ROL::Ptr<std::vector<double> > z_ptr = ROL::makePtr<std::vector<double>>(nx+1, 0.0);
70 ROL::Ptr<ROL::Vector<double> > z = ROL::makePtr<ROL::StdVector<double>>(z_ptr);
71 ROL::Ptr<ROL::Vector<double> > u = ROL::makePtr<ROL::StdVector<double>>(nx-1);
72 ROL::Ptr<ROL::Vector<double> > p = ROL::makePtr<ROL::StdVector<double>>(nx-1);
77 double alpha = parlist->sublist(
"Problem Data").get(
"Penalty Parameter", 1.e-4);
78 ROL::Ptr<FEM<double> > fem = ROL::makePtr<FEM<double>>(nx);
79 ROL::Ptr<ROL::Objective_SimOpt<double> > pObj
80 = ROL::makePtr<DiffusionObjective<double>>(fem, alpha);
81 ROL::Ptr<ROL::Constraint_SimOpt<double> > pCon
82 = ROL::makePtr<DiffusionConstraint<double>>(fem);
83 ROL::Ptr<ROL::Objective<double> > robj
84 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<double>>(pObj,pCon,u,z,p);
85 robj->setParameter({0.0,0.0});
90 bool runBundle = parlist->sublist(
"Problem Data").get(
"Run Bundle",
false);
94 ROL::Ptr<ROL::OptimizationProblem<double>> problem2
95 = ROL::makePtr<ROL::OptimizationProblem<double>>(robj, z);
96 problem2->setStochasticObjective(*parlist, sampler);
97 parlist->sublist(
"Step").set(
"Type",
"Bundle");
98 parlist->sublist(
"Step").sublist(
"Bundle").set(
"Distance Measure Coefficient",0.0);
100 solver2.
solve(*outStream);
103 ROL::Ptr<ROL::OptimizationProblem<double>> problem
104 = ROL::makePtr<ROL::OptimizationProblem<double>>(robj, z);
106 if (parlist->sublist(
"Problem Data").get(
"Run Derivative Check",
false)) {
107 problem->check(*outStream);
108 solver.check(*outStream);
110 solver.run(*outStream);
115 int my_number_samples = sampler->numMySamples(), number_samples = 0;
116 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_samples,&number_samples);
117 int my_number_solves = ROL::dynamicPtrCast<DiffusionConstraint<double> >(pCon)->getNumSolves(), number_solves = 0;
118 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_solves,&number_solves);
119 if (comm->getRank() == 0) {
120 std::cout <<
"Number of Samples = " << number_samples <<
"\n";
121 std::cout <<
"Number of Solves = " << number_solves <<
"\n";
124 if ( comm->getRank() == 0 ) {
126 file.open(
"control.txt");
127 std::vector<double> xmesh(fem->nz(),0.0);
128 fem->build_mesh(xmesh);
129 for (
int i = 0; i < fem->nz(); i++ ) {
130 file << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << xmesh[i] <<
" "
131 << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << (*z_ptr)[i]
137 catch (std::logic_error& err) {
138 *outStream << err.what() <<
"\n";
143 std::cout <<
"End Result: TEST FAILED\n";
145 std::cout <<
"End Result: TEST PASSED\n";
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides a simplified interface for solving a wide range of optimization problems.
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.