10 #ifndef ROL_PROBLEM_DEF_HPP
11 #define ROL_PROBLEM_DEF_HPP
17 template<
typename Real>
21 : isFinalized_(false), hasBounds_(false),
22 hasEquality_(false), hasInequality_(false),
23 hasLinearEquality_(false), hasLinearInequality_(false),
24 cnt_econ_(0), cnt_icon_(0), cnt_linear_econ_(0), cnt_linear_icon_(0),
25 obj_(nullPtr), xprim_(nullPtr), xdual_(nullPtr), bnd_(nullPtr),
26 con_(nullPtr), mul_(nullPtr), res_(nullPtr), proj_(nullPtr),
37 template<
typename Real>
39 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
40 ">>> ROL::Problem: Cannot add bounds after problem is finalized!");
46 template<
typename Real>
48 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
49 ">>> ROL::Problem: Cannot remove bounds after problem is finalized!");
55 template<
typename Real>
57 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
58 ">>> ROL::Problem: Cannot add prox objective after problem is finalized!");
64 template<
typename Real>
66 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
67 ">>> ROL::Problem: Cannot remove prox objective after problem is finalized!");
69 INPUT_prox_ = nullPtr;
73 template<
typename Real>
79 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
80 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
82 if (reset) INPUT_con_.clear();
84 auto it = INPUT_con_.find(name);
85 ROL_TEST_FOR_EXCEPTION(it != INPUT_con_.end(),std::invalid_argument,
86 ">>> ROL::Problem: Constraint names must be distinct!");
93 template<
typename Real>
100 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
101 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
103 if (reset) INPUT_con_.clear();
105 auto it = INPUT_con_.find(name);
106 ROL_TEST_FOR_EXCEPTION(it != INPUT_con_.end(),std::invalid_argument,
107 ">>> ROL::Problem: Constraint names must be distinct!");
110 hasInequality_ =
true;
114 template<
typename Real>
116 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
117 ">>> ROL::Problem: Cannot remove constraint after problem is finalized!");
119 auto it = INPUT_con_.find(name);
120 if (it!=INPUT_con_.end()) {
121 if (it->second.bounds==nullPtr) cnt_econ_--;
123 INPUT_con_.erase(it);
125 if (cnt_econ_==0) hasEquality_ =
false;
126 if (cnt_icon_==0) hasInequality_ =
false;
129 template<
typename Real>
135 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
136 ">>> ROL::Problem: Cannot add linear constraint after problem is finalized!");
138 if (reset) INPUT_linear_con_.clear();
140 auto it = INPUT_linear_con_.find(name);
141 ROL_TEST_FOR_EXCEPTION(it != INPUT_linear_con_.end(),std::invalid_argument,
142 ">>> ROL::Problem: Linear constraint names must be distinct!");
145 hasLinearEquality_ =
true;
149 template<
typename Real>
156 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
157 ">>> ROL::Problem: Cannot add linear constraint after problem is finalized!");
159 if (reset) INPUT_linear_con_.clear();
161 auto it = INPUT_linear_con_.find(name);
162 ROL_TEST_FOR_EXCEPTION(it != INPUT_linear_con_.end(),std::invalid_argument,
163 ">>> ROL::Problem: Linear constraint names must be distinct!");
165 INPUT_linear_con_.insert({name,
ConstraintData<Real>(linear_icon,linear_imul,linear_ires,linear_ibnd)});
166 hasLinearInequality_ =
true;
170 template<
typename Real>
172 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
173 ">>> ROL::Problem: Cannot remove linear inequality after problem is finalized!");
175 auto it = INPUT_linear_con_.find(name);
176 if (it!=INPUT_linear_con_.end()) {
177 if (it->second.bounds==nullPtr) cnt_linear_econ_--;
178 else cnt_linear_icon_--;
179 INPUT_linear_con_.erase(it);
181 if (cnt_linear_econ_==0) hasLinearEquality_ =
false;
182 if (cnt_linear_icon_==0) hasLinearInequality_ =
false;
185 template<
typename Real>
187 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
188 ">>> ROL::Problem: Cannot set polyhedral projection algorithm after problem is finalized!");
193 template<
typename Real>
196 std::unordered_map<std::string,ConstraintData<Real>> con, lcon, icon;
197 bool hasEquality = hasEquality_;
198 bool hasLinearEquality = hasLinearEquality_;
199 bool hasInequality = hasInequality_;
200 bool hasLinearInequality = hasLinearInequality_;
201 con.insert(INPUT_con_.begin(),INPUT_con_.end());
202 if (lumpConstraints) {
203 con.insert(INPUT_linear_con_.begin(),INPUT_linear_con_.end());
204 hasEquality = (hasEquality || hasLinearEquality);
205 hasInequality = (hasInequality || hasLinearInequality);
206 hasLinearEquality =
false;
207 hasLinearInequality =
false;
210 lcon.insert(INPUT_linear_con_.begin(),INPUT_linear_con_.end());
214 bool proxCompatible =
false;
215 if (!hasLinearEquality && !hasLinearInequality) {
217 if (!hasEquality && !hasInequality && !hasBounds_) {
220 xprim_ = INPUT_xprim_;
221 xdual_ = INPUT_xdual_;
226 if (hasProx_) { prox_ = INPUT_prox_; }
227 else { prox_ = nullPtr; }
228 proxCompatible =
true;
230 else if (!hasEquality && !hasInequality && hasBounds_) {
233 xprim_ = INPUT_xprim_;
234 xdual_ = INPUT_xdual_;
240 else if (hasEquality && !hasInequality && !hasBounds_) {
244 xprim_ = INPUT_xprim_;
245 xdual_ = INPUT_xdual_;
256 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
267 if (!hasBounds_ && !hasLinearInequality) {
272 if (!hasEquality && !hasInequality) {
274 obj_ = rlc_->transform(INPUT_obj_);
275 xprim_ = xfeas_->clone(); xprim_->zero();
283 for (
auto it = con.begin(); it != con.end(); ++it) {
286 it->second.multiplier,it->second.residual,it->second.bounds)));
288 Ptr<Vector<Real>> xtmp = xfeas_->clone(); xtmp->zero();
291 xdual_ = cm1.getDualOptVector();
292 con_ = cm1.getConstraint();
293 mul_ = cm1.getMultiplier();
294 res_ = cm1.getResidual();
295 if (!hasInequality) {
297 obj_ = rlc_->transform(INPUT_obj_);
302 obj_ = makePtr<SlacklessObjective<Real>>(rlc_->transform(INPUT_obj_));
303 bnd_ = cm1.getBoundConstraint();
307 else if ((hasBounds_ || hasLinearInequality) && !hasEquality && !hasInequality) {
312 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
320 proj_ = PolyhedralProjectionFactory<Real>(*xprim_,*xdual_,bnd_,
328 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
336 proj_ = PolyhedralProjectionFactory<Real>(*xprim_,*xdual_,bnd_,
342 std::stringstream out;
343 out <<
">>> ROL::Problem: Cannot solve ";
344 if (problemType_ ==
TYPE_B) out <<
"TypeB";
345 else if (problemType_ ==
TYPE_E) out <<
"TypeE";
346 else if (problemType_ ==
TYPE_EB) out <<
"TypeG";
347 else out <<
"TypeU with linear constraints";
348 out <<
" problems with a prox objective!";
349 ROL_TEST_FOR_EXCEPTION(hasProx_&&!proxCompatible,std::invalid_argument,out.str());
353 outStream << std::endl;
354 outStream <<
" ROL::Problem::finalize" << std::endl;
355 outStream <<
" Problem Summary:" << std::endl;
356 outStream <<
" Has Bound Constraint? .............. " << (hasBounds_ ?
"yes" :
"no") << std::endl;
357 outStream <<
" Has Equality Constraint? ........... " << (hasEquality ?
"yes" :
"no") << std::endl;
360 for (
auto it = con.begin(); it != con.end(); ++it) {
361 if (it->second.bounds==nullPtr) {
363 outStream <<
" Names: ........................... ";
369 outStream << it->first << std::endl;
372 outStream <<
" Total: ........................... " << cnt_econ_+(lumpConstraints ? cnt_linear_econ_ : 0) << std::endl;
374 outStream <<
" Has Inequality Constraint? ......... " << (hasInequality ?
"yes" :
"no") << std::endl;
377 for (
auto it = con.begin(); it != con.end(); ++it) {
378 if (it->second.bounds!=nullPtr) {
380 outStream <<
" Names: ........................... ";
386 outStream << it->first << std::endl;
389 outStream <<
" Total: ........................... " << cnt_icon_+(lumpConstraints ? cnt_linear_icon_ : 0) << std::endl;
391 if (!lumpConstraints) {
392 outStream <<
" Has Linear Equality Constraint? .... " << (hasLinearEquality ?
"yes" :
"no") << std::endl;
393 if (hasLinearEquality) {
395 for (
auto it = lcon.begin(); it != lcon.end(); ++it) {
396 if (it->second.bounds==nullPtr) {
398 outStream <<
" Names: ........................... ";
404 outStream << it->first << std::endl;
407 outStream <<
" Total: ........................... " << cnt_linear_econ_ << std::endl;
409 outStream <<
" Has Linear Inequality Constraint? .. " << (hasLinearInequality ?
"yes" :
"no") << std::endl;
410 if (hasLinearInequality) {
412 for (
auto it = lcon.begin(); it != lcon.end(); ++it) {
413 if (it->second.bounds!=nullPtr) {
415 outStream <<
" Names: ........................... ";
421 outStream << it->first << std::endl;
424 outStream <<
" Total: ........................... " << cnt_linear_icon_ << std::endl;
427 outStream << std::endl;
432 outStream << std::endl;
433 outStream <<
" ROL::Problem::finalize" << std::endl;
434 outStream <<
" Problem already finalized!" << std::endl;
435 outStream << std::endl;
440 template<
typename Real>
446 template<
typename Real>
452 template<
typename Real>
458 template<
typename Real>
464 template<
typename Real>
470 template<
typename Real>
476 template<
typename Real>
482 template<
typename Real>
488 template<
typename Real>
494 template<
typename Real>
496 std::ios_base::fmtflags state(outStream.flags());
498 outStream << std::setprecision(8) << std::scientific;
499 outStream << std::endl;
500 outStream <<
" ROL::Problem::checkLinearity" << std::endl;
502 const Real one(1), two(2), eps(1e-2*std::sqrt(ROL_EPSILON<Real>()));
503 Real tol(std::sqrt(ROL_EPSILON<Real>())), cnorm(0), err(0), maxerr(0);
504 Ptr<Vector<Real>> x = INPUT_xprim_->clone(); x->randomize(-one,one);
505 Ptr<Vector<Real>> y = INPUT_xprim_->clone(); y->randomize(-one,one);
506 Ptr<Vector<Real>> z = INPUT_xprim_->clone(); z->zero();
507 Ptr<Vector<Real>> xy = INPUT_xprim_->clone();
508 Real alpha = two*
static_cast<Real
>(rand())/static_cast<Real>(RAND_MAX)-one;
509 xy->set(*x); xy->axpy(alpha,*y);
510 Ptr<Vector<Real>> c1, c2;
511 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
512 c1 = it->second.residual->clone();
513 c2 = it->second.residual->clone();
515 it->second.constraint->value(*c1,*xy,tol);
518 it->second.constraint->value(*c2,*x,tol);
521 it->second.constraint->value(*c2,*y,tol);
522 c1->axpy(-alpha,*c2);
524 it->second.constraint->value(*c2,*z,tol);
527 maxerr = std::max(err,maxerr);
529 outStream <<
" Constraint " << it->first;
530 outStream <<
": ||c(x+alpha*y) - (c(x)+alpha*(c(y)-c(0)))|| = " << err << std::endl;
531 if (err > eps*cnorm) {
532 outStream <<
" Constraint " << it->first <<
" may not be linear!" << std::endl;
537 outStream << std::endl;
539 outStream.flags(state);
543 template<
typename Real>
546 Ptr<Vector<Real>> x, y;
548 x = INPUT_xprim_->clone(); x->randomize(-one,one);
549 y = INPUT_xprim_->clone(); y->randomize(-one,one);
551 outStream << std::endl <<
" Check primal optimization space vector" << std::endl;
553 INPUT_xprim_->checkVector(*x,*y,printToStream,outStream);
556 x = INPUT_xdual_->clone(); x->randomize(-one,one);
557 y = INPUT_xdual_->clone(); y->randomize(-one,one);
559 outStream << std::endl <<
" Check dual optimization space vector" << std::endl;
561 INPUT_xdual_->checkVector(*x,*y,printToStream,outStream);
564 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
566 x = it->second.residual->clone(); x->randomize(-one,one);
567 y = it->second.residual->clone(); y->randomize(-one,one);
569 outStream << std::endl <<
" " << it->first <<
": Check primal constraint space vector" << std::endl;
571 it->second.residual->checkVector(*x,*y,printToStream,outStream);
574 x = it->second.multiplier->clone(); x->randomize(-one,one);
575 y = it->second.multiplier->clone(); y->randomize(-one,one);
577 outStream << std::endl <<
" " << it->first <<
": Check dual constraint space vector" << std::endl;
579 it->second.multiplier->checkVector(*x,*y,printToStream,outStream);
583 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
585 x = it->second.residual->clone(); x->randomize(-one,one);
586 y = it->second.residual->clone(); y->randomize(-one,one);
588 outStream << std::endl <<
" " << it->first <<
": Check primal linear constraint space vector" << std::endl;
590 it->second.residual->checkVector(*x,*y,printToStream,outStream);
593 x = it->second.multiplier->clone(); x->randomize(-one,one);
594 y = it->second.multiplier->clone(); y->randomize(-one,one);
596 outStream << std::endl <<
" " << it->first <<
": Check dual linear constraint space vector" << std::endl;
598 it->second.multiplier->checkVector(*x,*y,printToStream,outStream);
602 template<
typename Real>
605 Ptr<Vector<Real>> x, d, v, g, c, w;
607 x = INPUT_xprim_->clone(); x->randomize(-one,one);
608 d = INPUT_xprim_->clone(); d->randomize(-one,one);
609 v = INPUT_xprim_->clone(); v->randomize(-one,one);
610 g = INPUT_xdual_->clone(); g->randomize(-one,one);
612 outStream << std::endl <<
" Check objective function" << std::endl << std::endl;
614 INPUT_obj_->checkGradient(*x,*g,*d,printToStream,outStream);
615 INPUT_obj_->checkHessVec(*x,*g,*d,printToStream,outStream);
616 INPUT_obj_->checkHessSym(*x,*g,*d,*v,printToStream,outStream);
619 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
620 c = it->second.residual->clone(); c->randomize(-one,one);
621 w = it->second.multiplier->clone(); w->randomize(-one,one);
623 outStream << std::endl <<
" " << it->first <<
": Check constraint function" << std::endl << std::endl;
625 it->second.constraint->checkApplyJacobian(*x,*v,*c,printToStream,outStream);
626 it->second.constraint->checkAdjointConsistencyJacobian(*w,*v,*x,printToStream,outStream);
627 it->second.constraint->checkApplyAdjointHessian(*x,*w,*v,*g,printToStream,outStream);
631 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
632 c = it->second.residual->clone(); c->randomize(-one,one);
633 w = it->second.multiplier->clone(); w->randomize(-one,one);
635 outStream << std::endl <<
" " << it->first <<
": Check constraint function" << std::endl << std::endl;
637 it->second.constraint->checkApplyJacobian(*x,*v,*c,printToStream,outStream);
638 it->second.constraint->checkAdjointConsistencyJacobian(*w,*v,*x,printToStream,outStream);
639 it->second.constraint->checkApplyAdjointHessian(*x,*w,*v,*g,printToStream,outStream);
643 template<
typename Real>
645 checkVectors(printToStream,outStream);
646 if (hasLinearEquality_ || hasLinearInequality_) {
647 checkLinearity(printToStream,outStream);
649 checkDerivatives(printToStream,outStream);
652 template<
typename Real>
657 template<
typename Real>
659 isFinalized_ =
false;
664 template<
typename Real>
666 if (rlc_ != nullPtr) {
667 if (!hasInequality_) {
668 rlc_->project(*INPUT_xprim_,*xprim_);
669 INPUT_xprim_->plus(*rlc_->getFeasibleVector());
674 rlc_->project(*INPUT_xprim_,*xprim);
675 INPUT_xprim_->plus(*rlc_->getFeasibleVector());
682 #endif // ROL_NEWOPTIMIZATIONPROBLEM_DEF_HPP
void removeProxObjective()
Remove an existing prox objective.
void check(bool printToStream=false, std::ostream &outStream=std::cout) const
Run vector, linearity and derivative checks for user-supplied vectors, objective function and constra...
Provides the interface to evaluate objective functions.
const Ptr< Constraint< Real > > & getConstraint()
Get the equality constraint.
void checkDerivatives(bool printToStream=false, std::ostream &outStream=std::cout) const
Run derivative checks for user-supplied objective function and constraints.
const Ptr< Vector< Real > > & getPrimalOptimizationVector()
Get the primal optimization space vector.
void addBoundConstraint(const Ptr< BoundConstraint< Real >> &bnd)
Add a bound constraint.
ROL::Ptr< const Vector< Real > > get(size_type i) const
void addLinearConstraint(std::string name, const Ptr< Constraint< Real >> &linear_econ, const Ptr< Vector< Real >> &linear_emul, const Ptr< Vector< Real >> &linear_eres=nullPtr, bool reset=false)
Add a linear equality constraint.
void removeBoundConstraint()
Remove an existing bound constraint.
Defines the linear algebra of vector space on a generic partitioned vector.
const Ptr< BoundConstraint< Real > > & getBoundConstraint() const
void removeLinearConstraint(std::string name)
Remove an existing linear constraint.
const Ptr< BoundConstraint< Real > > & getBoundConstraint()
Get the bound constraint.
const Ptr< Constraint< Real > > & getConstraint() const
Defines the linear algebra or vector space interface.
void addProxObjective(const Ptr< ProxObjective< Real >> &prox)
Add a prox objective.
virtual void finalize(bool lumpConstraints=false, bool printToStream=false, std::ostream &outStream=std::cout)
Tranform user-supplied constraints to consist of only bounds and equalities. Optimization problem can...
Ptr< Objective< Real > > INPUT_obj_
const Ptr< Vector< Real > > & getLinearResidual() const
bool isFinalized() const
Indicate whether or no finalize has been called.
Ptr< Vector< Real > > INPUT_xprim_
const Ptr< Objective< Real > > & getObjective()
Get the objective function.
Problem(const Ptr< Objective< Real >> &obj, const Ptr< Vector< Real >> &x, const Ptr< Vector< Real >> &g=nullPtr)
Default constructor for OptimizationProblem.
std::unordered_map< std::string, ConstraintData< Real > > INPUT_linear_con_
std::unordered_map< std::string, ConstraintData< Real > > INPUT_con_
Ptr< Vector< Real > > INPUT_xdual_
const Ptr< Vector< Real > > & getResidualVector()
Get the primal constraint space vector.
const Ptr< Vector< Real > > & getMultiplierVector()
Get the dual constraint space vector.
void finalizeIteration()
Transform the optimization variables to the native parameterization after an optimization algorithm h...
Provides a wrapper for multiple constraints.
const Ptr< Vector< Real > > & getMultiplier() const
Provides the interface to apply upper and lower bound constraints.
Real checkLinearity(bool printToStream=false, std::ostream &outStream=std::cout) const
Check if user-supplied linear constraints are affine.
void addConstraint(std::string name, const Ptr< Constraint< Real >> &econ, const Ptr< Vector< Real >> &emul, const Ptr< Vector< Real >> &eres=nullPtr, bool reset=false)
Add an equality constraint.
const Ptr< PolyhedralProjection< Real > > & getPolyhedralProjection()
Get the polyhedral projection object. This is a null pointer if no linear constraints and/or bounds a...
EProblem getProblemType()
Get the optimization problem type (U, B, E, or G).
void removeConstraint(std::string name)
Remove an existing constraint.
const Ptr< Vector< Real > > & getDualOptVector() const
Ptr< BoundConstraint< Real > > INPUT_bnd_
virtual void edit()
Resume editting optimization problem after finalize has been called.
const Ptr< Vector< Real > > & getOptVector() const
const Ptr< Constraint< Real > > & getLinearConstraint() const
const Ptr< Vector< Real > > & getDualOptimizationVector()
Get the dual optimization space vector.
const Ptr< Vector< Real > > & getLinearMultiplier() const
void checkVectors(bool printToStream=false, std::ostream &outStream=std::cout) const
Run vector checks for user-supplied vectors.
const Ptr< Vector< Real > > & getResidual() const
Defines the general constraint operator interface.
void setProjectionAlgorithm(ParameterList &parlist)
Set polyhedral projection algorithm.
bool hasInequality() const