10#ifndef ROL_PROBLEM_DEF_HPP
11#define ROL_PROBLEM_DEF_HPP
25template<
typename Real>
27 const Ptr<Vector<Real>> &x,
28 const Ptr<Vector<Real>> &g)
29 : isFinalized_(false), hasBounds_(false),
30 hasEquality_(false), hasInequality_(false),
31 hasLinearEquality_(false), hasLinearInequality_(false),
32 hasProximableObjective_(false),
33 cnt_econ_(0), cnt_icon_(0), cnt_linear_econ_(0), cnt_linear_icon_(0),
34 obj_(nullPtr), nobj_(nullPtr), xprim_(nullPtr), xdual_(nullPtr), bnd_(nullPtr),
35 con_(nullPtr), mul_(nullPtr), res_(nullPtr), proj_(nullPtr),
38 INPUT_nobj_ = nullPtr;
42 INPUT_linear_con_.clear();
43 if (g==nullPtr) INPUT_xdual_ = x->dual().clone();
44 else INPUT_xdual_ = g;
47template<
typename Real>
48void Problem<Real>::addBoundConstraint(
const Ptr<BoundConstraint<Real>> &bnd) {
49 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
50 ">>> ROL::Problem: Cannot add bounds after problem is finalized!");
56template<
typename Real>
57void Problem<Real>::removeBoundConstraint() {
58 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
59 ">>> ROL::Problem: Cannot remove bounds after problem is finalized!");
65template<
typename Real>
66void Problem<Real>::addConstraint( std::string name,
67 const Ptr<Constraint<Real>> &econ,
68 const Ptr<Vector<Real>> &emul,
69 const Ptr<Vector<Real>> &eres,
71 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
72 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
74 if (reset) INPUT_con_.clear();
76 bool isNameUsed =
false;
77 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
78 if (map.count(name)) {
83 ROL_TEST_FOR_EXCEPTION(isNameUsed,std::invalid_argument,
84 ">>> ROL::Problem: Constraint names must be distinct!");
86 INPUT_con_.insert({name,ConstraintData<Real>(econ,emul,eres)});
91template<
typename Real>
92void Problem<Real>::addConstraint( std::string name,
93 const Ptr<Constraint<Real>> &icon,
94 const Ptr<Vector<Real>> &imul,
95 const Ptr<BoundConstraint<Real>> &ibnd,
96 const Ptr<Vector<Real>> &ires,
98 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
99 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
101 if (reset) INPUT_con_.clear();
103 bool isNameUsed =
false;
104 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
105 if (map.count(name)) {
110 ROL_TEST_FOR_EXCEPTION(isNameUsed,std::invalid_argument,
111 ">>> ROL::Problem: Constraint names must be distinct!");
113 INPUT_con_.insert({name,ConstraintData<Real>(icon,imul,ires,ibnd)});
114 hasInequality_ =
true;
118template<
typename Real>
125 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
126 ">>> ROL::Problem: Cannot add constraint after problem is finalized!");
128 if (reset) INPUT_proj_.clear();
130 bool isNameUsed =
false;
131 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
132 if (map.count(name)) {
137 ROL_TEST_FOR_EXCEPTION(isNameUsed,std::invalid_argument,
138 ">>> ROL::Problem: Constraint names must be distinct!");
140 if (proj == nullPtr) {
149template<
typename Real>
151 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
152 ">>> ROL::Problem: Cannot remove constraint after problem is finalized!");
154 auto it = INPUT_con_.find(name);
155 if (it != INPUT_con_.end()) {
156 if (it->second.bounds==nullPtr) cnt_econ_--;
158 INPUT_con_.erase(it);
160 if (cnt_econ_==0) hasEquality_ =
false;
161 if (cnt_icon_==0) hasInequality_ =
false;
162 it = INPUT_proj_.find(name);
163 if (it != INPUT_proj_.end()) INPUT_proj_.erase(it);
166template<
typename Real>
167void Problem<Real>::addLinearConstraint( std::string name,
168 const Ptr<Constraint<Real>> &linear_econ,
169 const Ptr<Vector<Real>> &linear_emul,
170 const Ptr<Vector<Real>> &linear_eres,
172 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
173 ">>> ROL::Problem: Cannot add linear constraint after problem is finalized!");
175 if (reset) INPUT_linear_con_.clear();
177 bool isNameUsed =
false;
178 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
179 if (map.count(name)) {
184 ROL_TEST_FOR_EXCEPTION(isNameUsed,std::invalid_argument,
185 ">>> ROL::Problem: Constraint names must be distinct!");
187 INPUT_linear_con_.insert({name,ConstraintData<Real>(linear_econ,linear_emul,linear_eres)});
188 hasLinearEquality_ =
true;
192template<
typename Real>
193void Problem<Real>::addLinearConstraint( std::string name,
194 const Ptr<Constraint<Real>> &linear_icon,
195 const Ptr<Vector<Real>> &linear_imul,
196 const Ptr<BoundConstraint<Real>> &linear_ibnd,
197 const Ptr<Vector<Real>> &linear_ires,
199 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
200 ">>> ROL::Problem: Cannot add linear constraint after problem is finalized!");
202 if (reset) INPUT_linear_con_.clear();
204 bool isNameUsed =
false;
205 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
206 if (map.count(name)) {
211 ROL_TEST_FOR_EXCEPTION(isNameUsed,std::invalid_argument,
212 ">>> ROL::Problem: Constraint names must be distinct!");
214 INPUT_linear_con_.insert({name,ConstraintData<Real>(linear_icon,linear_imul,linear_ires,linear_ibnd)});
215 hasLinearInequality_ =
true;
219template<
typename Real>
220void Problem<Real>::removeLinearConstraint(std::string name) {
221 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
222 ">>> ROL::Problem: Cannot remove linear inequality after problem is finalized!");
224 auto it = INPUT_linear_con_.find(name);
225 if (it!=INPUT_linear_con_.end()) {
226 if (it->second.bounds==nullPtr) cnt_linear_econ_--;
227 else cnt_linear_icon_--;
228 INPUT_linear_con_.erase(it);
230 if (cnt_linear_econ_==0) hasLinearEquality_ =
false;
231 if (cnt_linear_icon_==0) hasLinearInequality_ =
false;
234template<
typename Real>
235void Problem<Real>::setProjectionAlgorithm(ParameterList &list) {
236 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
237 ">>> ROL::Problem: Cannot set polyhedral projection algorithm after problem is finalized!");
242template<
typename Real>
244 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
245 ">>> ROL::Problem: Cannot add regularizer after problem is finalized!");
248 hasProximableObjective_ =
true;
251template<
typename Real>
253 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
254 ">>> ROL::Problem: Cannot remove regularizer after problem is finalized!");
256 INPUT_nobj_ = nullPtr;
257 hasProximableObjective_ =
false;
260template<
typename Real>
262 const std::vector<std::string> &constraint_names) {
263 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
264 ">>> ROL::Problem: Cannot add constraint group after problem is finalized!");
267 for (
const auto& name : constraint_names) {
268 ROL_TEST_FOR_EXCEPTION(grouped_constraint_names_.count(name),std::invalid_argument,
269 ">>> ROL::Problem: Cannot include the same constraint in two augmented Lagrangian groups!");
271 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
272 if (map.count(name)) {
277 ROL_TEST_FOR_EXCEPTION(!isNameUsed,std::invalid_argument,
278 ">>> ROL::Problem: Constraint names must be distinct!");
280 for (
const auto& name : constraint_names) {
281 grouped_constraint_names_.insert(name);
282 groups_[group_name].push_back(name);
286template<
typename Real>
288 ROL_TEST_FOR_EXCEPTION(isFinalized_,std::invalid_argument,
289 ">>> ROL::Problem: Cannot remove augmented Lagrangian group after problem is finalized!");
290 auto it = groups_.find(group_name);
291 ROL_TEST_FOR_EXCEPTION(it==groups_.end(),std::invalid_argument,
292 ">>> ROL::Problem::removeConstraintGroup: Group name does not exist!");
293 const std::vector<std::string>& constraint_names = it->second;
294 for (
const auto& name : constraint_names)
295 grouped_constraint_names_.erase(name);
300template<
typename Real>
306 if (useSlackVariables) {
307 std::unordered_map<std::string,ConstraintData<Real>> con, lcon, icon;
308 bool hasEquality = hasEquality_;
309 bool hasLinearEquality = hasLinearEquality_;
310 bool hasInequality = hasInequality_;
311 bool hasLinearInequality = hasLinearInequality_;
312 bool hasProximableObjective = hasProximableObjective_;
313 con.insert(INPUT_con_.begin(),INPUT_con_.end());
314 if (lumpConstraints) {
315 con.insert(INPUT_linear_con_.begin(),INPUT_linear_con_.end());
316 hasEquality = (hasEquality || hasLinearEquality);
317 hasInequality = (hasInequality || hasLinearInequality);
318 hasLinearEquality =
false;
319 hasLinearInequality =
false;
322 lcon.insert(INPUT_linear_con_.begin(),INPUT_linear_con_.end());
327 if (hasProximableObjective){
328 if (!hasEquality && !hasInequality && !hasBounds_ && !hasLinearEquality && !hasLinearInequality){
332 xprim_ = INPUT_xprim_;
333 xdual_ = INPUT_xdual_;
344 if (!hasLinearEquality && !hasLinearInequality) {
346 if (!hasEquality && !hasInequality && !hasBounds_ ) {
349 xprim_ = INPUT_xprim_;
350 xdual_ = INPUT_xdual_;
356 else if (!hasEquality && !hasInequality && hasBounds_) {
359 xprim_ = INPUT_xprim_;
360 xdual_ = INPUT_xdual_;
366 else if (hasEquality && !hasInequality && !hasBounds_) {
370 xprim_ = INPUT_xprim_;
371 xdual_ = INPUT_xdual_;
382 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
393 if (!hasBounds_ && !hasLinearInequality) {
398 if (!hasEquality && !hasInequality) {
400 obj_ = rlc_->transform(INPUT_obj_);
401 xprim_ = xfeas_->clone(); xprim_->zero();
409 for (
auto it = con.begin(); it != con.end(); ++it) {
412 it->second.multiplier,it->second.residual,it->second.bounds)));
414 Ptr<Vector<Real>> xtmp = xfeas_->clone(); xtmp->zero();
421 if (!hasInequality) {
423 obj_ = rlc_->transform(INPUT_obj_);
428 obj_ = makePtr<SlacklessObjective<Real>>(rlc_->transform(INPUT_obj_));
433 else if ((hasBounds_ || hasLinearInequality) && !hasEquality && !hasInequality) {
438 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
446 proj_ = PolyhedralProjectionFactory<Real>(*xprim_,*xdual_,bnd_,
454 obj_ = makePtr<SlacklessObjective<Real>>(INPUT_obj_);
462 proj_ = PolyhedralProjectionFactory<Real>(*xprim_,*xdual_,bnd_,
470 outStream << std::endl;
471 outStream <<
" ROL::Problem::finalize" << std::endl;
472 outStream <<
" Problem Summary:" << std::endl;
473 outStream <<
" Has Proximable Objective? .......... " << (hasProximableObjective ?
"yes" :
"no") << std::endl;
474 outStream <<
" Has Bound Constraint? .............. " << (hasBounds_ ?
"yes" :
"no") << std::endl;
475 outStream <<
" Has Equality Constraint? ........... " << (hasEquality ?
"yes" :
"no") << std::endl;
478 for (
auto it = con.begin(); it != con.end(); ++it) {
479 if (it->second.bounds==nullPtr) {
481 outStream <<
" Names: ........................... ";
487 outStream << it->first << std::endl;
490 outStream <<
" Total: ........................... " << cnt_econ_+(lumpConstraints ? cnt_linear_econ_ : 0) << std::endl;
492 outStream <<
" Has Inequality Constraint? ......... " << (hasInequality ?
"yes" :
"no") << std::endl;
495 for (
auto it = con.begin(); it != con.end(); ++it) {
496 if (it->second.bounds!=nullPtr) {
498 outStream <<
" Names: ........................... ";
504 outStream << it->first << std::endl;
507 outStream <<
" Total: ........................... " << cnt_icon_+(lumpConstraints ? cnt_linear_icon_ : 0) << std::endl;
509 if (!lumpConstraints) {
510 outStream <<
" Has Linear Equality Constraint? .... " << (hasLinearEquality ?
"yes" :
"no") << std::endl;
511 if (hasLinearEquality) {
513 for (
auto it = lcon.begin(); it != lcon.end(); ++it) {
514 if (it->second.bounds==nullPtr) {
516 outStream <<
" Names: ........................... ";
522 outStream << it->first << std::endl;
525 outStream <<
" Total: ........................... " << cnt_linear_econ_ << std::endl;
527 outStream <<
" Has Linear Inequality Constraint? .. " << (hasLinearInequality ?
"yes" :
"no") << std::endl;
528 if (hasLinearInequality) {
530 for (
auto it = lcon.begin(); it != lcon.end(); ++it) {
531 if (it->second.bounds!=nullPtr) {
533 outStream <<
" Names: ........................... ";
539 outStream << it->first << std::endl;
542 outStream <<
" Total: ........................... " << cnt_linear_icon_ << std::endl;
545 outStream << std::endl;
553 std::unordered_set<std::string> ungrouped_constraints;
554 for (
const auto& kv : INPUT_con_) ungrouped_constraints.insert(kv.first);
555 for (
const auto& kv : INPUT_linear_con_) ungrouped_constraints.insert(kv.first);
556 for (
const auto& kv : INPUT_proj_) ungrouped_constraints.insert(kv.first);
557 for (
const auto& kv : groups_) {
558 for (
const auto& constraint_name : kv.second)
559 ungrouped_constraints.erase(constraint_name);
562 Ptr<ConstraintData<Real>> constraint_data;
563 Ptr<Projection<Real>> projection;
564 bool is_equality =
false;
565 for (
const std::string& constraint_name : ungrouped_constraints) {
566 constraint_data = getConstraintData(constraint_name);
567 is_equality = (dynamicPtrCast<ZeroProjection<Real>>(constraint_data->projection) != nullPtr);
569 std::vector<std::string> constraint_name_vector(1,constraint_name);
570 addConstraintGroup(constraint_name, constraint_name_vector);
572 else if (INPUT_linear_con_.count(constraint_name))
573 ungrouped_linear_equality_constraint_names_.push_back(constraint_name);
575 ungrouped_equality_constraint_names_.push_back(constraint_name);
580 xprim_ = INPUT_xprim_;
581 xdual_ = INPUT_xdual_;
591 outStream << std::endl;
592 outStream <<
" ROL::Problem::finalize" << std::endl;
593 outStream <<
" Problem already finalized!" << std::endl;
594 outStream << std::endl;
599template<
typename Real>
605template<
typename Real>
611template<
typename Real>
617template<
typename Real>
618const Ptr<Vector<Real>>& Problem<Real>::getDualOptimizationVector() {
623template<
typename Real>
624const Ptr<BoundConstraint<Real>>& Problem<Real>::getBoundConstraint() {
629template<
typename Real>
630const Ptr<Constraint<Real>>& Problem<Real>::getConstraint() {
635template<
typename Real>
636const Ptr<Vector<Real>>& Problem<Real>::getMultiplierVector() {
641template<
typename Real>
642const Ptr<Vector<Real>>& Problem<Real>::getResidualVector() {
647template<
typename Real>
648const Ptr<PolyhedralProjection<Real>>& Problem<Real>::getPolyhedralProjection() {
653template<
typename Real>
654EProblem Problem<Real>::getProblemType() {
659template<
typename Real>
661 Ptr<ConstraintData<Real>> constraint_data;
662 bool isConstraintMissing =
true;
663 for (
const auto& map : {INPUT_con_,INPUT_linear_con_,INPUT_proj_}) {
664 if (
const auto& it = map.find(constraint_name); it != map.end()) {
665 constraint_data = makePtr<ConstraintData<Real>>(it->second);
666 if (constraint_data->projection == nullPtr) {
667 if (constraint_data->bounds != nullPtr)
668 constraint_data->projection = makePtr<PolyhedralProjection<Real>>(constraint_data->bounds);
670 constraint_data->projection = makePtr<ZeroProjection<Real>>();
672 isConstraintMissing =
false;
676 ROL_TEST_FOR_EXCEPTION(isConstraintMissing,std::invalid_argument,
677 ">>> ROL::Problem::getConstraintData: Invalid constraint name!");
678 return constraint_data;
681template<
typename Real>
683 return ungrouped_equality_constraint_names_;
686template<
typename Real>
688 return ungrouped_linear_equality_constraint_names_;
691template<
typename Real>
693 std::vector<std::string> group_names;
694 for (
const auto& kv : groups_) group_names.push_back(kv.first);
698template<
typename Real>
700 ROL_TEST_FOR_EXCEPTION(groups_.count(group_name)==0,std::invalid_argument,
701 ">>> ROL::Problem::getGroupConstraintData A constraint group with the provided name is missing!");
702 if (groups_[group_name].size() == 1) {
703 std::string& constraint_name = groups_[group_name][0];
704 return getConstraintData(constraint_name);
706 Ptr<ConstraintData<Real>> constraint_data;
707 std::vector<Ptr<Constraint<Real>>> constraints;
708 std::vector<Ptr<Projection<Real>>> projections;
709 std::vector<Ptr<Vector<Real>>> con_vectors;
710 std::vector<Ptr<Vector<Real>>> mul_vectors;
711 for (
const std::string& constraint_name : groups_[group_name]) {
712 constraint_data = getConstraintData(constraint_name);
713 constraints.push_back(constraint_data->constraint);
714 mul_vectors.push_back(constraint_data->multiplier);
715 con_vectors.push_back(constraint_data->residual);
716 projections.push_back(constraint_data->projection);
718 Ptr<Constraint<Real>> partitioned_constraint = makePtr<Constraint_Partitioned<Real>>(constraints);
719 Ptr<Vector<Real>> partitioned_mul_vector = makePtr<PartitionedVector<Real>>(mul_vectors);
720 Ptr<Vector<Real>> partitioned_con_vector = makePtr<PartitionedVector<Real>>(con_vectors);
721 Ptr<Projection<Real>> partitioned_projection = makePtr<Projection_Partitioned<Real>>(projections);
722 Ptr<ConstraintData<Real>> new_constraint_data = makePtr<ConstraintData<Real>>(partitioned_constraint,
723 partitioned_mul_vector,
724 partitioned_con_vector,
726 partitioned_projection);
727 return new_constraint_data;
731template<
typename Real>
733 std::ios_base::fmtflags state(outStream.flags());
735 outStream << std::setprecision(8) << std::scientific;
736 outStream << std::endl;
737 outStream <<
" ROL::Problem::checkLinearity" << std::endl;
739 const Real one(1), two(2), eps(1e-2*std::sqrt(ROL_EPSILON<Real>()));
740 Real tol(std::sqrt(ROL_EPSILON<Real>())), cnorm(0), err(0), maxerr(0);
741 Ptr<Vector<Real>> x = INPUT_xprim_->clone(); x->randomize(-one,one);
742 Ptr<Vector<Real>> y = INPUT_xprim_->clone(); y->randomize(-one,one);
743 Ptr<Vector<Real>> z = INPUT_xprim_->clone(); z->zero();
744 Ptr<Vector<Real>> xy = INPUT_xprim_->clone();
745 Real alpha = two*
static_cast<Real
>(rand())/
static_cast<Real
>(RAND_MAX)-one;
746 xy->set(*x); xy->axpy(alpha,*y);
747 Ptr<Vector<Real>> c1, c2;
748 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
749 c1 = it->second.residual->clone();
750 c2 = it->second.residual->clone();
751 it->second.constraint->update(*xy,UpdateType::Temp);
752 it->second.constraint->value(*c1,*xy,tol);
754 it->second.constraint->update(*x,UpdateType::Temp);
755 it->second.constraint->value(*c2,*x,tol);
757 it->second.constraint->update(*y,UpdateType::Temp);
758 it->second.constraint->value(*c2,*y,tol);
759 c1->axpy(-alpha,*c2);
760 it->second.constraint->update(*z,UpdateType::Temp);
761 it->second.constraint->value(*c2,*z,tol);
764 maxerr = std::max(err,maxerr);
766 outStream <<
" Constraint " << it->first;
767 outStream <<
": ||c(x+alpha*y) - (c(x)+alpha*(c(y)-c(0)))|| = " << err << std::endl;
768 if (err > eps*cnorm) {
769 outStream <<
" Constraint " << it->first <<
" may not be linear!" << std::endl;
774 outStream << std::endl;
776 outStream.flags(state);
780template<
typename Real>
781void Problem<Real>::checkVectors(
bool printToStream, std::ostream &outStream)
const {
783 Ptr<Vector<Real>> x, y;
785 x = INPUT_xprim_->clone(); x->randomize(-one,one);
786 y = INPUT_xprim_->clone(); y->randomize(-one,one);
788 outStream << std::endl <<
" Check primal optimization space vector" << std::endl;
790 INPUT_xprim_->checkVector(*x,*y,printToStream,outStream);
793 x = INPUT_xdual_->clone(); x->randomize(-one,one);
794 y = INPUT_xdual_->clone(); y->randomize(-one,one);
796 outStream << std::endl <<
" Check dual optimization space vector" << std::endl;
798 INPUT_xdual_->checkVector(*x,*y,printToStream,outStream);
801 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
803 x = it->second.residual->clone(); x->randomize(-one,one);
804 y = it->second.residual->clone(); y->randomize(-one,one);
806 outStream << std::endl <<
" " << it->first <<
": Check primal constraint space vector" << std::endl;
808 it->second.residual->checkVector(*x,*y,printToStream,outStream);
811 x = it->second.multiplier->clone(); x->randomize(-one,one);
812 y = it->second.multiplier->clone(); y->randomize(-one,one);
814 outStream << std::endl <<
" " << it->first <<
": Check dual constraint space vector" << std::endl;
816 it->second.multiplier->checkVector(*x,*y,printToStream,outStream);
820 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
822 x = it->second.residual->clone(); x->randomize(-one,one);
823 y = it->second.residual->clone(); y->randomize(-one,one);
825 outStream << std::endl <<
" " << it->first <<
": Check primal linear constraint space vector" << std::endl;
827 it->second.residual->checkVector(*x,*y,printToStream,outStream);
830 x = it->second.multiplier->clone(); x->randomize(-one,one);
831 y = it->second.multiplier->clone(); y->randomize(-one,one);
833 outStream << std::endl <<
" " << it->first <<
": Check dual linear constraint space vector" << std::endl;
835 it->second.multiplier->checkVector(*x,*y,printToStream,outStream);
839template<
typename Real>
842 Ptr<Vector<Real>> x, d, v, g, c, w;
845 if (x == nullPtr) { x = INPUT_xprim_->clone(); x->randomize(-one,one); }
846 d = INPUT_xprim_->clone(); d->randomize(-scale,scale);
847 v = INPUT_xprim_->clone(); v->randomize(-scale,scale);
848 g = INPUT_xdual_->clone(); g->randomize(-scale,scale);
850 outStream << std::endl <<
" Check objective function" << std::endl << std::endl;
851 INPUT_obj_->checkGradient(*x,*g,*d,printToStream,outStream);
852 INPUT_obj_->checkHessVec(*x,*g,*d,printToStream,outStream);
853 INPUT_obj_->checkHessSym(*x,*g,*d,*v,printToStream,outStream);
856 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
857 c = it->second.residual->clone(); c->randomize(-scale,scale);
858 w = it->second.multiplier->clone(); w->randomize(-scale,scale);
860 outStream << std::endl <<
" " << it->first <<
": Check constraint function" << std::endl << std::endl;
861 it->second.constraint->checkApplyJacobian(*x,*v,*c,printToStream,outStream);
862 it->second.constraint->checkAdjointConsistencyJacobian(*w,*v,*x,printToStream,outStream);
863 it->second.constraint->checkApplyAdjointHessian(*x,*w,*v,*g,printToStream,outStream);
867 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
868 c = it->second.residual->clone(); c->randomize(-scale,scale);
869 w = it->second.multiplier->clone(); w->randomize(-scale,scale);
871 outStream << std::endl <<
" " << it->first <<
": Check constraint function" << std::endl << std::endl;
872 it->second.constraint->checkApplyJacobian(*x,*v,*c,printToStream,outStream);
873 it->second.constraint->checkAdjointConsistencyJacobian(*w,*v,*x,printToStream,outStream);
874 it->second.constraint->checkApplyAdjointHessian(*x,*w,*v,*g,printToStream,outStream);
878template<
typename Real>
880 checkVectors(printToStream,outStream);
881 if (hasLinearEquality_ || hasLinearInequality_)
882 checkLinearity(printToStream,outStream);
883 checkDerivatives(printToStream,outStream,x0,scale);
888template<
typename Real>
893template<
typename Real>
894void Problem<Real>::edit() {
895 isFinalized_ =
false;
900template<
typename Real>
901void Problem<Real>::finalizeIteration() {
902 if (rlc_ != nullPtr) {
903 if (!hasInequality_) {
904 rlc_->project(*INPUT_xprim_,*xprim_);
905 INPUT_xprim_->plus(*rlc_->getFeasibleVector());
908 Ptr<Vector<Real>> xprim =
dynamic_cast<PartitionedVector<Real>&
>(*xprim_).get(0)->clone();
909 xprim->set(*
dynamic_cast<PartitionedVector<Real>&
>(*xprim_).get(0));
910 rlc_->project(*INPUT_xprim_,*xprim);
911 INPUT_xprim_->plus(*rlc_->getFeasibleVector());
Provides a wrapper for multiple constraints.
const Ptr< Constraint< Real > > & getLinearConstraint() const
const Ptr< Vector< Real > > & getMultiplier() const
const Ptr< Vector< Real > > & getLinearResidual() const
const Ptr< Vector< Real > > & getDualOptVector() const
const Ptr< BoundConstraint< Real > > & getBoundConstraint() const
const Ptr< Constraint< Real > > & getConstraint() const
const Ptr< Vector< Real > > & getLinearMultiplier() const
const Ptr< Vector< Real > > & getOptVector() const
bool hasInequality() const
const Ptr< Vector< Real > > & getResidual() const
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.