14 #include "ROL_StdTeuchosBatchManager.hpp"
17 #include "ROL_ParameterList.hpp"
23 #include "Teuchos_Time.hpp"
25 #include "Teuchos_GlobalMPISession.hpp"
26 #include "Teuchos_Comm.hpp"
27 #include "Teuchos_DefaultComm.hpp"
28 #include "Teuchos_CommHelpers.hpp"
30 int main(
int argc,
char *argv[] ) {
32 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
34 auto comm = ROL::toPtr( Teuchos::DefaultComm<int>::getComm() );
37 int iprint = argc - 1;
38 ROL::Ptr<std::ostream> outStream;
40 if (iprint > 0 && comm->getRank()==0)
41 outStream = ROL::makePtrFromRef(std::cout);
43 outStream = ROL::makePtrFromRef(bhs);
55 std::string filename =
"input_ex03.xml";
56 auto parlist = ROL::getParametersFromXmlFile( filename );
58 if ( parlist->sublist(
"Problem Data").get(
"Display Option",0) && (comm->getRank() > 0) ) {
59 parlist->set(
"Display Option",0);
66 int nSamp = parlist->sublist(
"Problem Data").get(
"Number of Monte Carlo Samples",1000);
67 std::vector<double> tmp(2); tmp[0] = -1.0; tmp[1] = 1.0;
68 std::vector<std::vector<double> > bounds(dim,tmp);
69 ROL::Ptr<ROL::BatchManager<double> > bman
70 = ROL::makePtr<ROL::StdTeuchosBatchManager<double,int>>(comm);
71 ROL::Ptr<ROL::SampleGenerator<double> > sampler
72 = ROL::makePtr<ROL::MonteCarloGenerator<double>>(nSamp,bounds,bman,
false);
77 int nx = parlist->sublist(
"Problem Data").get(
"Number of Elements", 128);
78 ROL::Ptr<std::vector<double> > z_ptr = ROL::makePtr<std::vector<double>>(nx+1, 0.0);
79 ROL::Ptr<ROL::Vector<double> > z = ROL::makePtr<ROL::StdVector<double>>(z_ptr);
80 ROL::Ptr<ROL::Vector<double> > u = ROL::makePtr<ROL::StdVector<double>>(nx-1);
81 ROL::Ptr<ROL::Vector<double> > p = ROL::makePtr<ROL::StdVector<double>>(nx-1);
86 double alpha = parlist->sublist(
"Problem Data").get(
"Penalty Parameter", 1.e-4);
87 ROL::Ptr<FEM<double> > fem = ROL::makePtr<FEM<double>>(nx);
88 ROL::Ptr<ROL::Objective_SimOpt<double> > pObj
89 = ROL::makePtr<DiffusionObjective<double>>(fem, alpha);
90 ROL::Ptr<ROL::Constraint_SimOpt<double> > pCon
91 = ROL::makePtr<DiffusionConstraint<double>>(fem);
92 ROL::Ptr<ROL::Objective<double> > robj
93 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<double>>(pObj,pCon,u,z,p);
94 robj->setParameter({0.0,0.0});
99 bool runBundle = parlist->sublist(
"Problem Data").get(
"Run Bundle",
false);
103 ROL::Ptr<ROL::StochasticProblem<double>> problem2
104 = ROL::makePtr<ROL::StochasticProblem<double>>(robj, z);
105 problem2->makeObjectiveStochastic(*parlist, sampler);
106 problem2->finalize(
false,
true,*outStream);
107 parlist->sublist(
"Step").set(
"Type",
"Bundle");
108 parlist->sublist(
"Step").sublist(
"Bundle").set(
"Distance Measure Coefficient",0.0);
110 solver2.
solve(*outStream);
113 ROL::Ptr<ROL::Problem<double>> problem
114 = ROL::makePtr<ROL::Problem<double>>(robj, z);
116 if (parlist->sublist(
"Problem Data").get(
"Run Derivative Check",
false)) {
117 problem->check(
true,*outStream);
118 solver.check(*outStream);
120 solver.run(*outStream);
125 int my_number_samples = sampler->numMySamples(), number_samples = 0;
126 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_samples,&number_samples);
127 int my_number_solves = ROL::dynamicPtrCast<DiffusionConstraint<double> >(pCon)->getNumSolves(), number_solves = 0;
128 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_solves,&number_solves);
129 if (comm->getRank() == 0) {
130 std::cout <<
"Number of Samples = " << number_samples <<
"\n";
131 std::cout <<
"Number of Solves = " << number_solves <<
"\n";
134 if ( comm->getRank() == 0 ) {
136 file.open(
"control.txt");
137 std::vector<double> xmesh(fem->nz(),0.0);
138 fem->build_mesh(xmesh);
139 for (
int i = 0; i < fem->nz(); i++ ) {
140 file << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << xmesh[i] <<
" "
141 << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << (*z_ptr)[i]
147 catch (std::logic_error& err) {
148 *outStream << err.what() <<
"\n";
153 std::cout <<
"End Result: TEST FAILED\n";
155 std::cout <<
"End Result: TEST PASSED\n";
Provides a simplified interface for solving a wide range of optimization problems.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
int solve(const Ptr< StatusTest< Real >> &status=nullPtr, bool combineStatus=true)
Solve optimization problem with no iteration output.
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])