10#ifndef ROL_PROBLEM_DEF_HPP
11#define ROL_PROBLEM_DEF_HPP
17template<
typename Real>
19 const Ptr<Vector<Real>> &x,
20 const Ptr<Vector<Real>> &g)
21 : isFinalized_(false), hasBounds_(false),
22 hasEquality_(false), hasInequality_(false),
23 hasLinearEquality_(false), hasLinearInequality_(false),
24 hasProximableObjective_(false),
25 cnt_econ_(0), cnt_icon_(0), cnt_linear_econ_(0), cnt_linear_icon_(0),
26 obj_(nullPtr), nobj_(nullPtr), xprim_(nullPtr), xdual_(nullPtr), bnd_(nullPtr),
27 con_(nullPtr), mul_(nullPtr), res_(nullPtr), proj_(nullPtr),
30 INPUT_nobj_ = nullPtr;
34 INPUT_linear_con_.clear();
35 if (g==nullPtr) INPUT_xdual_ = x->dual().clone();
36 else INPUT_xdual_ = g;
39template<
typename Real>
40void Problem<Real>::addBoundConstraint(
const Ptr<BoundConstraint<Real>> &bnd) {
41 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
42 ">>> ROL::Problem: Cannot add bounds after problem is finalized!");
48template<
typename Real>
49void Problem<Real>::removeBoundConstraint() {
50 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
51 ">>> ROL::Problem: Cannot remove bounds after problem is finalized!");
57template<
typename Real>
59 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
60 ">>> ROL::Problem: Cannot add regularizer after problem is finalized!");
63 hasProximableObjective_ =
true;
66template<
typename Real>
68 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
69 ">>> ROL::Problem: Cannot remove regularizer after problem is finalized!");
71 INPUT_nobj_ = nullPtr;
72 hasProximableObjective_ =
false;
74template<
typename Real>
80 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
81 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
83 if (reset) INPUT_con_.clear();
85 auto it = INPUT_con_.find(name);
86 ROL_TEST_FOR_EXCEPTION(it != INPUT_con_.end(),std::invalid_argument,
87 ">>> ROL::Problem: Constraint names must be distinct!");
94template<
typename Real>
95void Problem<Real>::addConstraint( std::string name,
96 const Ptr<Constraint<Real>> &icon,
97 const Ptr<Vector<Real>> &imul,
98 const Ptr<BoundConstraint<Real>> &ibnd,
99 const Ptr<Vector<Real>> &ires,
101 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
102 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
104 if (reset) INPUT_con_.clear();
106 auto it = INPUT_con_.find(name);
107 ROL_TEST_FOR_EXCEPTION(it != INPUT_con_.end(),std::invalid_argument,
108 ">>> ROL::Problem: Constraint names must be distinct!");
110 INPUT_con_.insert({name,ConstraintData<Real>(icon,imul,ires,ibnd)});
111 hasInequality_ =
true;
115template<
typename Real>
116void Problem<Real>::removeConstraint(std::string name) {
117 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
118 ">>> ROL::Problem: Cannot remove constraint after problem is finalized!");
120 auto it = INPUT_con_.find(name);
121 if (it!=INPUT_con_.end()) {
122 if (it->second.bounds==nullPtr) cnt_econ_--;
124 INPUT_con_.erase(it);
126 if (cnt_econ_==0) hasEquality_ =
false;
127 if (cnt_icon_==0) hasInequality_ =
false;
130template<
typename Real>
131void Problem<Real>::addLinearConstraint( std::string name,
132 const Ptr<Constraint<Real>> &linear_econ,
133 const Ptr<Vector<Real>> &linear_emul,
134 const Ptr<Vector<Real>> &linear_eres,
136 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
137 ">>> ROL::Problem: Cannot add linear constraint after problem is finalized!");
139 if (reset) INPUT_linear_con_.clear();
141 auto it = INPUT_linear_con_.find(name);
142 ROL_TEST_FOR_EXCEPTION(it != INPUT_linear_con_.end(),std::invalid_argument,
143 ">>> ROL::Problem: Linear constraint names must be distinct!");
145 INPUT_linear_con_.insert({name,ConstraintData<Real>(linear_econ,linear_emul,linear_eres)});
146 hasLinearEquality_ =
true;
150template<
typename Real>
151void Problem<Real>::addLinearConstraint( std::string name,
152 const Ptr<Constraint<Real>> &linear_icon,
153 const Ptr<Vector<Real>> &linear_imul,
154 const Ptr<BoundConstraint<Real>> &linear_ibnd,
155 const Ptr<Vector<Real>> &linear_ires,
157 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
158 ">>> ROL::Problem: Cannot add linear constraint after problem is finalized!");
160 if (reset) INPUT_linear_con_.clear();
162 auto it = INPUT_linear_con_.find(name);
163 ROL_TEST_FOR_EXCEPTION(it != INPUT_linear_con_.end(),std::invalid_argument,
164 ">>> ROL::Problem: Linear constraint names must be distinct!");
166 INPUT_linear_con_.insert({name,ConstraintData<Real>(linear_icon,linear_imul,linear_ires,linear_ibnd)});
167 hasLinearInequality_ =
true;
171template<
typename Real>
172void Problem<Real>::removeLinearConstraint(std::string name) {
173 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
174 ">>> ROL::Problem: Cannot remove linear inequality after problem is finalized!");
176 auto it = INPUT_linear_con_.find(name);
177 if (it!=INPUT_linear_con_.end()) {
178 if (it->second.bounds==nullPtr) cnt_linear_econ_--;
179 else cnt_linear_icon_--;
180 INPUT_linear_con_.erase(it);
182 if (cnt_linear_econ_==0) hasLinearEquality_ =
false;
183 if (cnt_linear_icon_==0) hasLinearInequality_ =
false;
186template<
typename Real>
187void Problem<Real>::setProjectionAlgorithm(ParameterList &list) {
188 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
189 ">>> ROL::Problem: Cannot set polyhedral projection algorithm after problem is finalized!");
194template<
typename Real>
195void Problem<Real>::finalize(
bool lumpConstraints,
bool printToStream, std::ostream &outStream) {
197 std::unordered_map<std::string,ConstraintData<Real>> con, lcon, icon;
198 bool hasEquality = hasEquality_;
199 bool hasLinearEquality = hasLinearEquality_;
200 bool hasInequality = hasInequality_;
201 bool hasLinearInequality = hasLinearInequality_;
202 bool hasProximableObjective = hasProximableObjective_;
203 con.insert(INPUT_con_.begin(),INPUT_con_.end());
204 if (lumpConstraints) {
205 con.insert(INPUT_linear_con_.begin(),INPUT_linear_con_.end());
206 hasEquality = (hasEquality || hasLinearEquality);
207 hasInequality = (hasInequality || hasLinearInequality);
208 hasLinearEquality =
false;
209 hasLinearInequality =
false;
212 lcon.insert(INPUT_linear_con_.begin(),INPUT_linear_con_.end());
217 if (hasProximableObjective){
218 if (!hasEquality && !hasInequality && !hasBounds_ && !hasLinearEquality && !hasLinearInequality){
222 xprim_ = INPUT_xprim_;
223 xdual_ = INPUT_xdual_;
230 throw Exception::NotImplemented(
">>> ROL::TypeP - with constraints is not supported");
234 if (!hasLinearEquality && !hasLinearInequality) {
236 if (!hasEquality && !hasInequality && !hasBounds_ ) {
239 xprim_ = INPUT_xprim_;
240 xdual_ = INPUT_xdual_;
246 else if (!hasEquality && !hasInequality && hasBounds_) {
249 xprim_ = INPUT_xprim_;
250 xdual_ = INPUT_xdual_;
256 else if (hasEquality && !hasInequality && !hasBounds_) {
257 ConstraintAssembler<Real> cm(con,INPUT_xprim_,INPUT_xdual_);
260 xprim_ = INPUT_xprim_;
261 xdual_ = INPUT_xdual_;
263 con_ = cm.getConstraint();
264 mul_ = cm.getMultiplier();
265 res_ = cm.getResidual();
268 ConstraintAssembler<Real> cm(con,INPUT_xprim_,INPUT_xdual_,INPUT_bnd_);
271 if (cm.hasInequality()) {
272 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
274 xprim_ = cm.getOptVector();
275 xdual_ = cm.getDualOptVector();
276 bnd_ = cm.getBoundConstraint();
277 con_ = cm.getConstraint();
278 mul_ = cm.getMultiplier();
279 res_ = cm.getResidual();
283 if (!hasBounds_ && !hasLinearInequality) {
284 ConstraintAssembler<Real> cm(lcon,INPUT_xprim_,INPUT_xdual_);
285 xfeas_ = cm.getOptVector()->clone(); xfeas_->set(*cm.getOptVector());
286 rlc_ = makePtr<ReduceLinearConstraint<Real>>(cm.getConstraint(),xfeas_,cm.getResidual());
288 if (!hasEquality && !hasInequality) {
290 obj_ = rlc_->transform(INPUT_obj_);
291 xprim_ = xfeas_->clone(); xprim_->zero();
292 xdual_ = cm.getDualOptVector();
299 for (
auto it = con.begin(); it != con.end(); ++it) {
300 icon.insert(std::pair<std::string,ConstraintData<Real>>(it->first,
301 ConstraintData<Real>(rlc_->transform(it->second.constraint),
302 it->second.multiplier,it->second.residual,it->second.bounds)));
304 Ptr<Vector<Real>> xtmp = xfeas_->clone(); xtmp->zero();
305 ConstraintAssembler<Real> cm1(icon,xtmp,cm.getDualOptVector());
306 xprim_ = cm1.getOptVector();
307 xdual_ = cm1.getDualOptVector();
308 con_ = cm1.getConstraint();
309 mul_ = cm1.getMultiplier();
310 res_ = cm1.getResidual();
311 if (!hasInequality) {
313 obj_ = rlc_->transform(INPUT_obj_);
318 obj_ = makePtr<SlacklessObjective<Real>>(rlc_->transform(INPUT_obj_));
319 bnd_ = cm1.getBoundConstraint();
323 else if ((hasBounds_ || hasLinearInequality) && !hasEquality && !hasInequality) {
324 ConstraintAssembler<Real> cm(lcon,INPUT_xprim_,INPUT_xdual_,INPUT_bnd_);
327 if (cm.hasInequality()) {
328 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
330 xprim_ = cm.getOptVector();
331 xdual_ = cm.getDualOptVector();
332 bnd_ = cm.getBoundConstraint();
336 proj_ = PolyhedralProjectionFactory<Real>(*xprim_,*xdual_,bnd_,
337 cm.getConstraint(),*cm.getMultiplier(),*cm.getResidual(),ppa_list_);
340 ConstraintAssembler<Real> cm(con,lcon,INPUT_xprim_,INPUT_xdual_,INPUT_bnd_);
343 if (cm.hasInequality()) {
344 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
346 xprim_ = cm.getOptVector();
347 xdual_ = cm.getDualOptVector();
348 con_ = cm.getConstraint();
349 mul_ = cm.getMultiplier();
350 res_ = cm.getResidual();
351 bnd_ = cm.getBoundConstraint();
352 proj_ = PolyhedralProjectionFactory<Real>(*xprim_,*xdual_,bnd_,
353 cm.getLinearConstraint(),*cm.getLinearMultiplier(),
354 *cm.getLinearResidual(),ppa_list_);
360 outStream << std::endl;
361 outStream <<
" ROL::Problem::finalize" << std::endl;
362 outStream <<
" Problem Summary:" << std::endl;
363 outStream <<
" Has Proximable Objective? .......... " << (hasProximableObjective ?
"yes" :
"no") << std::endl;
364 outStream <<
" Has Bound Constraint? .............. " << (hasBounds_ ?
"yes" :
"no") << std::endl;
365 outStream <<
" Has Equality Constraint? ........... " << (hasEquality ?
"yes" :
"no") << std::endl;
368 for (
auto it = con.begin(); it != con.end(); ++it) {
369 if (it->second.bounds==nullPtr) {
371 outStream <<
" Names: ........................... ";
377 outStream << it->first << std::endl;
380 outStream <<
" Total: ........................... " << cnt_econ_+(lumpConstraints ? cnt_linear_econ_ : 0) << std::endl;
382 outStream <<
" Has Inequality Constraint? ......... " << (hasInequality ?
"yes" :
"no") << std::endl;
385 for (
auto it = con.begin(); it != con.end(); ++it) {
386 if (it->second.bounds!=nullPtr) {
388 outStream <<
" Names: ........................... ";
394 outStream << it->first << std::endl;
397 outStream <<
" Total: ........................... " << cnt_icon_+(lumpConstraints ? cnt_linear_icon_ : 0) << std::endl;
399 if (!lumpConstraints) {
400 outStream <<
" Has Linear Equality Constraint? .... " << (hasLinearEquality ?
"yes" :
"no") << std::endl;
401 if (hasLinearEquality) {
403 for (
auto it = lcon.begin(); it != lcon.end(); ++it) {
404 if (it->second.bounds==nullPtr) {
406 outStream <<
" Names: ........................... ";
412 outStream << it->first << std::endl;
415 outStream <<
" Total: ........................... " << cnt_linear_econ_ << std::endl;
417 outStream <<
" Has Linear Inequality Constraint? .. " << (hasLinearInequality ?
"yes" :
"no") << std::endl;
418 if (hasLinearInequality) {
420 for (
auto it = lcon.begin(); it != lcon.end(); ++it) {
421 if (it->second.bounds!=nullPtr) {
423 outStream <<
" Names: ........................... ";
429 outStream << it->first << std::endl;
432 outStream <<
" Total: ........................... " << cnt_linear_icon_ << std::endl;
435 outStream << std::endl;
440 outStream << std::endl;
441 outStream <<
" ROL::Problem::finalize" << std::endl;
442 outStream <<
" Problem already finalized!" << std::endl;
443 outStream << std::endl;
448template<
typename Real>
449const Ptr<Objective<Real>>& Problem<Real>::getObjective() {
454template<
typename Real>
460template<
typename Real>
466template<
typename Real>
467const Ptr<Vector<Real>>& Problem<Real>::getDualOptimizationVector() {
472template<
typename Real>
473const Ptr<BoundConstraint<Real>>& Problem<Real>::getBoundConstraint() {
478template<
typename Real>
479const Ptr<Constraint<Real>>& Problem<Real>::getConstraint() {
484template<
typename Real>
485const Ptr<Vector<Real>>& Problem<Real>::getMultiplierVector() {
490template<
typename Real>
491const Ptr<Vector<Real>>& Problem<Real>::getResidualVector() {
496template<
typename Real>
497const Ptr<PolyhedralProjection<Real>>& Problem<Real>::getPolyhedralProjection() {
502template<
typename Real>
503EProblem Problem<Real>::getProblemType() {
508template<
typename Real>
509Real Problem<Real>::checkLinearity(
bool printToStream, std::ostream &outStream)
const {
510 std::ios_base::fmtflags state(outStream.flags());
512 outStream << std::setprecision(8) << std::scientific;
513 outStream << std::endl;
514 outStream <<
" ROL::Problem::checkLinearity" << std::endl;
516 const Real one(1), two(2), eps(1e-2*std::sqrt(ROL_EPSILON<Real>()));
517 Real tol(std::sqrt(ROL_EPSILON<Real>())), cnorm(0), err(0), maxerr(0);
518 Ptr<Vector<Real>> x = INPUT_xprim_->clone(); x->randomize(-one,one);
519 Ptr<Vector<Real>> y = INPUT_xprim_->clone(); y->randomize(-one,one);
520 Ptr<Vector<Real>> z = INPUT_xprim_->clone(); z->zero();
521 Ptr<Vector<Real>> xy = INPUT_xprim_->clone();
522 Real alpha = two*
static_cast<Real
>(rand())/
static_cast<Real
>(RAND_MAX)-one;
523 xy->set(*x); xy->axpy(alpha,*y);
524 Ptr<Vector<Real>> c1, c2;
525 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
526 c1 = it->second.residual->clone();
527 c2 = it->second.residual->clone();
528 it->second.constraint->update(*xy,UpdateType::Temp);
529 it->second.constraint->value(*c1,*xy,tol);
531 it->second.constraint->update(*x,UpdateType::Temp);
532 it->second.constraint->value(*c2,*x,tol);
534 it->second.constraint->update(*y,UpdateType::Temp);
535 it->second.constraint->value(*c2,*y,tol);
536 c1->axpy(-alpha,*c2);
537 it->second.constraint->update(*z,UpdateType::Temp);
538 it->second.constraint->value(*c2,*z,tol);
541 maxerr = std::max(err,maxerr);
543 outStream <<
" Constraint " << it->first;
544 outStream <<
": ||c(x+alpha*y) - (c(x)+alpha*(c(y)-c(0)))|| = " << err << std::endl;
545 if (err > eps*cnorm) {
546 outStream <<
" Constraint " << it->first <<
" may not be linear!" << std::endl;
551 outStream << std::endl;
553 outStream.flags(state);
557template<
typename Real>
558void Problem<Real>::checkVectors(
bool printToStream, std::ostream &outStream)
const {
560 Ptr<Vector<Real>> x, y;
562 x = INPUT_xprim_->clone(); x->randomize(-one,one);
563 y = INPUT_xprim_->clone(); y->randomize(-one,one);
565 outStream << std::endl <<
" Check primal optimization space vector" << std::endl;
567 INPUT_xprim_->checkVector(*x,*y,printToStream,outStream);
570 x = INPUT_xdual_->clone(); x->randomize(-one,one);
571 y = INPUT_xdual_->clone(); y->randomize(-one,one);
573 outStream << std::endl <<
" Check dual optimization space vector" << std::endl;
575 INPUT_xdual_->checkVector(*x,*y,printToStream,outStream);
578 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
580 x = it->second.residual->clone(); x->randomize(-one,one);
581 y = it->second.residual->clone(); y->randomize(-one,one);
583 outStream << std::endl <<
" " << it->first <<
": Check primal constraint space vector" << std::endl;
585 it->second.residual->checkVector(*x,*y,printToStream,outStream);
588 x = it->second.multiplier->clone(); x->randomize(-one,one);
589 y = it->second.multiplier->clone(); y->randomize(-one,one);
591 outStream << std::endl <<
" " << it->first <<
": Check dual constraint space vector" << std::endl;
593 it->second.multiplier->checkVector(*x,*y,printToStream,outStream);
597 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
599 x = it->second.residual->clone(); x->randomize(-one,one);
600 y = it->second.residual->clone(); y->randomize(-one,one);
602 outStream << std::endl <<
" " << it->first <<
": Check primal linear constraint space vector" << std::endl;
604 it->second.residual->checkVector(*x,*y,printToStream,outStream);
607 x = it->second.multiplier->clone(); x->randomize(-one,one);
608 y = it->second.multiplier->clone(); y->randomize(-one,one);
610 outStream << std::endl <<
" " << it->first <<
": Check dual linear constraint space vector" << std::endl;
612 it->second.multiplier->checkVector(*x,*y,printToStream,outStream);
616template<
typename Real>
619 Ptr<Vector<Real>> x, d, v, g, c, w;
622 if (x == nullPtr) { x = INPUT_xprim_->clone(); x->randomize(-one,one); }
623 d = INPUT_xprim_->clone(); d->randomize(-scale,scale);
624 v = INPUT_xprim_->clone(); v->randomize(-scale,scale);
625 g = INPUT_xdual_->clone(); g->randomize(-scale,scale);
627 outStream << std::endl <<
" Check objective function" << std::endl << std::endl;
628 INPUT_obj_->checkGradient(*x,*g,*d,printToStream,outStream);
629 INPUT_obj_->checkHessVec(*x,*g,*d,printToStream,outStream);
630 INPUT_obj_->checkHessSym(*x,*g,*d,*v,printToStream,outStream);
633 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
634 c = it->second.residual->clone(); c->randomize(-scale,scale);
635 w = it->second.multiplier->clone(); w->randomize(-scale,scale);
637 outStream << std::endl <<
" " << it->first <<
": Check constraint function" << std::endl << std::endl;
638 it->second.constraint->checkApplyJacobian(*x,*v,*c,printToStream,outStream);
639 it->second.constraint->checkAdjointConsistencyJacobian(*w,*v,*x,printToStream,outStream);
640 it->second.constraint->checkApplyAdjointHessian(*x,*w,*v,*g,printToStream,outStream);
644 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
645 c = it->second.residual->clone(); c->randomize(-scale,scale);
646 w = it->second.multiplier->clone(); w->randomize(-scale,scale);
648 outStream << std::endl <<
" " << it->first <<
": Check constraint function" << std::endl << std::endl;
649 it->second.constraint->checkApplyJacobian(*x,*v,*c,printToStream,outStream);
650 it->second.constraint->checkAdjointConsistencyJacobian(*w,*v,*x,printToStream,outStream);
651 it->second.constraint->checkApplyAdjointHessian(*x,*w,*v,*g,printToStream,outStream);
655template<
typename Real>
657 checkVectors(printToStream,outStream);
658 if (hasLinearEquality_ || hasLinearInequality_)
659 checkLinearity(printToStream,outStream);
660 checkDerivatives(printToStream,outStream,x0,scale);
665template<
typename Real>
670template<
typename Real>
671void Problem<Real>::edit() {
672 isFinalized_ =
false;
677template<
typename Real>
678void Problem<Real>::finalizeIteration() {
679 if (rlc_ != nullPtr) {
680 if (!hasInequality_) {
681 rlc_->project(*INPUT_xprim_,*xprim_);
682 INPUT_xprim_->plus(*rlc_->getFeasibleVector());
685 Ptr<Vector<Real>> xprim =
dynamic_cast<PartitionedVector<Real>&
>(*xprim_).get(0)->clone();
686 xprim->set(*
dynamic_cast<PartitionedVector<Real>&
>(*xprim_).get(0));
687 rlc_->project(*INPUT_xprim_,*xprim);
688 INPUT_xprim_->plus(*rlc_->getFeasibleVector());
Defines the general constraint operator interface.
Provides the interface to evaluate objective functions.
Problem(const Ptr< Objective< Real > > &obj, const Ptr< Vector< Real > > &x, const Ptr< Vector< Real > > &g=nullPtr)
Default constructor for OptimizationProblem.
Defines the linear algebra or vector space interface.