44 #ifndef ROL_OPTIMIZATIONPROBLEM_HPP
45 #define ROL_OPTIMIZATIONPROBLEM_HPP
121 Ptr<BoundConstraint<Real>>
bnd_;
145 int esize =
static_cast<int>(econ.size());
146 int isize =
static_cast<int>(icon.size());
147 std::vector<Ptr<Constraint<Real>>> cvec;
148 std::vector<Ptr<Vector<Real>>> lvec;
149 std::vector<Ptr<BoundConstraint<Real>>> bvec;
150 for (
int i = 0; i < esize; ++i) {
151 if ( econ[i] != nullPtr ) {
156 cvec.push_back(econ[i]);
158 lvec.push_back(emul[i]);
159 bvec.push_back(nullPtr);
162 for (
int i = 0; i < isize; ++i) {
163 if ( icon[i] != nullPtr ) {
168 cvec.push_back(icon[i]);
170 lvec.push_back(imul[i]);
171 bvec.push_back(ibnd[i]);
175 conManager_ = makePtr<ConstraintManager<Real>>(cvec,lvec,bvec,x,bnd);
180 Ptr<Objective<Real>> obj0;
188 obj_ = makePtr<SlacklessObjective<Real>>(obj0);
195 if(
bnd_ == nullPtr || !
bnd_->isActivated() ) {
203 if(
bnd_ == nullPtr || !
bnd_->isActivated() ) {
216 return makePtr<RiskLessConstraint<Real>>(con);
225 return makePtr<RiskLessObjective<Real>>(obj);
234 int dim = sampler->getMyPoint(0).size(), nsamp = sampler->numMySamples();
235 std::vector<Real> loc(
dim), mean(
dim), pt(
dim);
237 for (
int i = 0; i < nsamp; i++) {
238 pt = sampler->getMyPoint(i);
239 wt = sampler->getMyWeight(i);
240 for (
int j = 0; j <
dim; j++) {
244 sampler->sumAll(&loc[0],&mean[0],
dim);
273 Real statObj =
parlistObj_->sublist(
"SOL").get(
"Initial Statistic",1.0);
277 for (
int i = 0; i < nc; ++i) {
279 Real statCon =
parlistCon_[i]->sublist(
"SOL").get(
"Initial Statistic",1.0);
329 std::vector<Ptr<Constraint<Real>>> econ0(1,econ);
330 std::vector<Ptr<Vector<Real>>> emul0(1,emul);
350 std::vector<Ptr<Constraint<Real>>> icon0(1,icon);
351 std::vector<Ptr<Vector<Real>>> imul0(1,imul);
352 std::vector<Ptr<BoundConstraint<Real>>> ibnd0(1,ibnd);
372 std::vector<Ptr<Constraint<Real>>> econ0(1,econ);
373 std::vector<Ptr<Vector<Real>>> emul0(1,emul);
374 std::vector<Ptr<Constraint<Real>>> icon0(1,icon);
375 std::vector<Ptr<Vector<Real>>> imul0(1,imul);
376 std::vector<Ptr<BoundConstraint<Real>>> ibnd0(1,ibnd);
568 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setMeanValueObjective: Objective function value sampler is null!");
597 const bool storage =
true) {
603 if ( gsampler == nullPtr ) {
606 if ( hsampler == nullPtr ) {
611 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setRiskNeutralObjective: Objective function value sampler is null!");
645 if ( gsampler == nullPtr ) {
648 if ( hsampler == nullPtr ) {
653 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setRiskAverseObjective: Objective function value sampler is null!");
673 std::string type = parlist.sublist(
"SOL").get(
"Stochastic Component Type",
"Risk Neutral");
674 if ( type ==
"Risk Neutral" ) {
675 bool storage = parlist.sublist(
"SOL").get(
"Store Sampled Value and Gradient",
true);
678 else if ( type ==
"Risk Averse" ||
679 type ==
"Deviation" ||
682 type ==
"Probability" ) {
685 else if ( type ==
"Mean Value" ) {
689 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setStochasticObjective: Invalid stochastic optimization type!");
702 if (
INPUT_econ_[index] != nullPtr && sampler != nullPtr ) {
709 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setMeanValueEquality: Either SampleGenerator or Constraint is NULL!");
720 const int index = 0) {
725 && xsampler != nullPtr
726 && cbman != nullPtr ) {
728 = makePtr<RiskNeutralConstraint<Real>>(
INPUT_econ_[index],xsampler,cbman);
732 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetRiskNeutralEquality: Either SampleGenerator, Constraint or BatchManager is NULL!");
742 const int index = 0) {
745 if (
INPUT_econ_[index] != nullPtr && sampler != nullPtr ) {
746 int nsamp = sampler->numMySamples();
748 = makePtr<AlmostSureConstraint<Real>>(sampler,
INPUT_econ_[index]);
749 std::vector<Ptr<Vector<Real>>> emul(nsamp,nullPtr);
750 for (
int j = 0; j < nsamp; ++j) {
755 = makePtr<DualSimulatedVector<Real>>(emul, sampler->getBatchManager(), sampler);
758 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetAlmostSureEquality: Either SampleGenerator or Constraint is NULL!");
773 if ( nc !=
static_cast<int>(xsampler.size()) || nc !=
static_cast<int>(cbman.size()) ) {
774 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setStochasticEquality: Constraint vector and SampleGenerator vector are not the same size!");
776 for (
int i = 0; i < nc; ++i) {
777 if (xsampler[i] != nullPtr) {
778 std::string type = parlist[i].sublist(
"SOL").get(
"Stochastic Component Type",
"Risk Neutral");
779 if ( type ==
"Risk Neutral" ) {
782 else if ( type ==
"Almost Sure" ) {
785 else if ( type ==
"Mean Value" ) {
789 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetStochasticEquality: Invalid stochastic constraint type!");
807 std::vector<ParameterList> cparlist(1,parlist);
808 std::vector<Ptr<SampleGenerator<Real>>> cxsampler(1,xsampler);
809 std::vector<Ptr<BatchManager<Real>>> ccbman(1,cbman);
815 const int index = 0) {
818 if (
INPUT_icon_[index] != nullPtr && sampler != nullPtr ) {
826 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetMeanValueInequality: Either Constraint or SampleGenerator is NULL!");
837 const int index = 0) {
842 && xsampler != nullPtr
843 && cbman != nullPtr ) {
845 = makePtr<RiskNeutralConstraint<Real>>(
INPUT_icon_[index],xsampler,cbman);
850 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetRiskNeutralInequality: Either Constraint, SampleGenerator or BatchManager is NULL!");
861 const int index = 0) {
864 if (
INPUT_icon_[index] != nullPtr && sampler != nullPtr ) {
868 = makePtr<StochasticConstraint<Real>>(
INPUT_icon_[index],sampler,parlist,index);
873 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetRiskAverseInequality: Either Constraint or SampleGenerator is NULL!");
883 const int index = 0) {
886 if (
INPUT_icon_[index] != nullPtr && sampler != nullPtr ) {
887 int nsamp = sampler->numMySamples();
889 = makePtr<AlmostSureConstraint<Real>>(sampler,
INPUT_icon_[index]);
890 std::vector<Ptr<Vector<Real>>> imul(nsamp,nullPtr);
891 for (
int j = 0; j < nsamp; ++j) {
896 = makePtr<DualSimulatedVector<Real>>(imul, sampler->getBatchManager(), sampler);
898 = makePtr<SimulatedBoundConstraint<Real>>(sampler,
INPUT_ibnd_[index]);
901 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetAlmostSureInequality: Either Constraint or SampleGenerator is NULL!");
915 if ( nc !=
static_cast<int>(xsampler.size()) || nc !=
static_cast<int>(cbman.size()) ) {
916 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::setStochasticInequality: Constraint vector and SampleGenerator vector are not the same size!");
918 for (
int i = 0; i < nc; ++i) {
919 if ( xsampler[i] != nullPtr ) {
920 std::string type = parlist[i].sublist(
"SOL").get(
"Stochastic Component Type",
"Risk Neutral");
921 if ( type ==
"Risk Neutral" ) {
924 else if ( type ==
"Risk Averse" ||
925 type ==
"Deviation" ||
928 type ==
"Probability" ) {
931 else if ( type ==
"Almost Sure" ) {
934 else if ( type ==
"Mean Value" ) {
938 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::SetStochasticInequality: Invalid stochastic constraint type!");
957 std::vector<ParameterList> cparlist(1,parlist);
958 std::vector<Ptr<SampleGenerator<Real>>> cxsampler(1,xsampler);
959 std::vector<Ptr<BatchManager<Real>>> ccbman(1,cbman);
974 catch (std::exception &e) {
975 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::getSolutionStatistic: Objective does not have computeStatistic function!");
978 else if (comp == 1) {
980 if (np <= index || index < 0) {
986 catch (std::exception &e) {
987 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::getSolutionStatistic: Constraint does not have computeStatistic function!");
998 Ptr<std::vector<Real>> stat
1000 if (stat != nullPtr) {
1004 std::vector<Real> empty;
1008 catch (std::exception &e) {
1009 std::vector<Real> empty;
1016 Ptr<std::vector<Real>> stat
1018 if (stat != nullPtr) {
1022 std::vector<Real> empty;
1026 catch (std::exception &e) {
1027 std::vector<Real> empty;
1043 std::ostream &outStream = std::cout ) {
1053 std::ostream &outStream = std::cout ) {
1057 if (
obj_ != nullPtr) {
1058 outStream <<
"\nPerforming OptimizationProblem diagnostics." << std::endl << std::endl;
1060 outStream <<
"Checking vector operations in optimization vector space X." << std::endl;
1068 std::ostream &outStream = std::cout,
1070 const int order = 1 ) {
1079 std::ostream &outStream = std::cout,
1081 const int order = 1 ) {
1085 if (
obj_ != nullPtr) {
1086 outStream << std::endl <<
"Performing OptimizationProblem diagnostics."
1087 << std::endl << std::endl;
1088 outStream <<
"Checking objective function." << std::endl;
1090 outStream << std::endl;
1092 outStream << std::endl;
1094 outStream << std::endl;
1101 std::ostream &outStream = std::cout ) {
1110 std::ostream &outStream = std::cout ) {
1114 if(
con_ != nullPtr) {
1115 outStream <<
"\nPerforming OptimizationProblem diagnostics." << std::endl << std::endl;
1117 outStream <<
"Checking vector operations in constraint multiplier space C*." << std::endl;
1127 std::ostream &outStream = std::cout,
1129 const int order = 1 ) {
1140 std::ostream &outStream = std::cout,
1142 const int order = 1 ) {
1146 if(
con_ != nullPtr) {
1147 outStream <<
"\nPerforming OptimizationProblem diagnostics." << std::endl << std::endl;
1149 outStream <<
"Checking equality constraint." << std::endl;
1151 outStream << std::endl;
1153 outStream << std::endl;
1155 outStream << std::endl;
1160 void check( std::ostream &outStream = std::cout,
1162 const int order = 1 ) {
1164 check(data,outStream,numSteps,order);
1168 std::ostream &outStream = std::cout,
1170 const int order = 1 ) {
1175 Ptr<Vector<Real>> x, y, u, v;
1177 x =
sol_->clone(); x->randomize();
1178 y =
sol_->clone(); y->randomize();
1179 u =
sol_->clone(); u->randomize();
1180 v =
sol_->clone(); v->randomize();
1185 catch (std::exception &e) {
1189 if(
con_ != nullPtr) {
1190 Ptr<Vector<Real>> c, l, w, q;
1192 c =
mul_->dual().clone(); c->randomize();
1193 l =
mul_->clone(); l->randomize();
1194 w =
mul_->clone(); w->randomize();
1195 q =
mul_->clone(); q->randomize();
1200 catch (std::exception &e) {
1201 throw Exception::NotImplemented(
">>> ROL::OptimizationProblem::check: Elementwise is not implemented for constraint space vectors");
1209 template<
template<
typename>
class V,
1210 template<
typename>
class Obj,
1211 typename Real,
typename P=Ptr<OptimizationProblem<Real>>,
typename...Args>
1212 inline typename std::enable_if<std::is_base_of<Objective<Real>,
Obj<Real>>
::value &&
1215 return makePtr<OptimizationProblem<Real>>(obj,x,std::forward<Args>(args)...);
1220 #endif // ROL_OPTIMIZATIONPROBLEM_HPP