// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "Teuchos_ParameterList.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" #include "Teuchos_oblackholestream.hpp" #include "Teuchos_LAPACK.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_Comm.hpp" #include "Teuchos_DefaultComm.hpp" #include "Teuchos_CommHelpers.hpp" // ROL_Types contains predefined constants and objects #include "ROL_StdVector.hpp" #include "ROL_Bounds.hpp" #include "ROL_Solver.hpp" #include "ROL_PEBBL_IntegerConstraint.hpp" template class Constraint_SimpleBinary : public ROL::Constraint { private: const Real budget_; ROL::Ptr> getVector(ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } ROL::Ptr> getConstVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } public: Constraint_SimpleBinary(int budget = 4) : budget_(budget) {} void value(ROL::Vector &c, const ROL::Vector &x, Real &tol) { ROL::Ptr> cp = getVector(c); ROL::Ptr> xp = getConstVector(x); (*cp)[0] = -static_cast(budget_); for (const auto & y : *xp) (*cp)[0] += y; } void applyJacobian(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ROL::Ptr> jvp = getVector(jv); ROL::Ptr> vp = getConstVector(v); (*jvp)[0] = static_cast(0); for (const auto & y : *vp) (*jvp)[0] += y; } void applyAdjointJacobian(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ROL::Ptr> jvp = getVector(ajv); ROL::Ptr> vp = getConstVector(v); jvp->assign(jvp->size(),(*vp)[0]); } void applyAdjointHessian(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ahwv.zero(); } }; template class Objective_SimpleBinary : public ROL::Objective { private: const std::vector alpha_, beta_; ROL::Ptr> getVector(ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } ROL::Ptr> getConstVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } public: Objective_SimpleBinary(const std::vector &alpha) : alpha_(alpha), beta_(std::vector(alpha.size(),0)) {} Objective_SimpleBinary(const std::vector &alpha, const std::vector &beta) : alpha_(alpha), beta_(beta) {} Real value(const ROL::Vector &x, Real &tol) { ROL::Ptr> xp = getConstVector(x); const Real half(0.5); Real val(0); const int dim(xp->size()); for (int i = 0; i < dim; ++i) val += half * alpha_[i] * (*xp)[i] * (*xp)[i] - beta_[i] * (*xp)[i]; return val; } void gradient(ROL::Vector &g, const ROL::Vector &x, Real &tol) { ROL::Ptr> gp = getVector(g); ROL::Ptr> xp = getConstVector(x); const int dim(xp->size()); for (int i = 0; i < dim; ++i) (*gp)[i] = alpha_[i] * (*xp)[i] - beta_[i]; } void hessVec(ROL::Vector &hv, const ROL::Vector &v, const ROL::Vector &x, Real &tol ) { ROL::Ptr> hvp = getVector(hv); ROL::Ptr> vp = getConstVector(v); const int dim(vp->size()); for (int i = 0; i < dim; ++i) (*hvp)[i] = alpha_[i] * (*vp)[i]; } };