// @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_MixedVector.hpp" #include "ROL_PEBBL_IntegerConstraint.hpp" #include "ROL_PEBBL_IntegerProblemFactory.hpp" #include "ROL_PEBBL_BranchAndBound.hpp" #include "ROL_PEBBL_StdBranchHelper.hpp" template class EqualityConstraint_FacilityLocation : public ROL::Constraint { private: ROL::Ptr> getIntegerVector(ROL::Vector &x) const { return dynamic_cast&>(x).getIntegerVariables(); } ROL::Ptr> getContinuousVector(ROL::Vector &x) const { return dynamic_cast&>(x).getContinuousVariables(); } ROL::Ptr> getIntegerVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getIntegerVariables(); } ROL::Ptr> getContinuousVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getContinuousVariables(); } 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: EqualityConstraint_FacilityLocation() {} void value(ROL::Vector &c, const ROL::Vector &x, Real &tol) { ROL::Ptr> cp = getVector(c); ROL::Ptr> xi = getConstVector(*getIntegerVector(x)); ROL::Ptr> xc = getConstVector(*getContinuousVector(x)); const Real one(1); const int M = xi->size(), N = xc->size()/M; for (int j = 0; j < N; ++j) { (*cp)[j] = -one; for (int i = 0; i < M; ++i) { (*cp)[j] += (*xc)[i + j*M]; } } } void applyJacobian(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ROL::Ptr> jvp = getVector(jv); ROL::Ptr> vi = getConstVector(*getIntegerVector(v)); ROL::Ptr> vc = getConstVector(*getContinuousVector(v)); const Real zero(0); const int M = vi->size(), N = vc->size()/M; for (int j = 0; j < N; ++j) { (*jvp)[j] = zero; for (int i = 0; i < M; ++i) { (*jvp)[j] += (*vc)[i + j*M]; } } } void applyAdjointJacobian(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ROL::Ptr> jvi = getVector(*getIntegerVector(ajv)); ROL::Ptr> jvc = getVector(*getContinuousVector(ajv)); ROL::Ptr> vp = getConstVector(v); const int M = jvi->size(), N = jvc->size()/M; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { (*jvc)[i + j*M] = (*vp)[j]; } } } void applyAdjointHessian(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ahwv.zero(); } }; template class InequalityConstraint_FacilityLocation : public ROL::Constraint { private: ROL::Ptr> getIntegerVector(ROL::Vector &x) const { return dynamic_cast&>(x).getIntegerVariables(); } ROL::Ptr> getContinuousVector(ROL::Vector &x) const { return dynamic_cast&>(x).getContinuousVariables(); } ROL::Ptr> getIntegerVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getIntegerVariables(); } ROL::Ptr> getContinuousVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getContinuousVariables(); } 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: InequalityConstraint_FacilityLocation() {} void value(ROL::Vector &c, const ROL::Vector &x, Real &tol) { ROL::Ptr> cp = getVector(c); ROL::Ptr> xi = getConstVector(*getIntegerVector(x)); ROL::Ptr> xc = getConstVector(*getContinuousVector(x)); const int M = xi->size(), N = xc->size()/M; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { (*cp)[i + j*M] = (*xc)[i + j*M] - (*xi)[i]; } } } void applyJacobian(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ROL::Ptr> jvp = getVector(jv); ROL::Ptr> vi = getConstVector(*getIntegerVector(v)); ROL::Ptr> vc = getConstVector(*getContinuousVector(v)); const int M = vi->size(), N = vc->size()/M; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { (*jvp)[i + j*M] = (*vc)[i + j*M] - (*vi)[i]; } } } void applyAdjointJacobian(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ROL::Ptr> jvi = getVector(*getIntegerVector(ajv)); ROL::Ptr> jvc = getVector(*getContinuousVector(ajv)); ROL::Ptr> vp = getConstVector(v); const Real zero(0); const int M = jvi->size(), N = jvc->size()/M; for (int i = 0; i < M; ++i) { (*jvi)[i] = zero; for (int j = 0; j < N; ++j) { (*jvc)[i + j*M] = (*vp)[i + j*M]; (*jvi)[i] -= (*vp)[i + j*M]; } } } void applyAdjointHessian(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &x, Real &tol) { ahwv.zero(); } }; template class Objective_FacilityLocation : public ROL::Objective { private: const std::vector c_, q_; const int M_, N_; ROL::Ptr> getIntegerVector(ROL::Vector &x) const { return dynamic_cast&>(x).getIntegerVariables(); } ROL::Ptr> getContinuousVector(ROL::Vector &x) const { return dynamic_cast&>(x).getContinuousVariables(); } ROL::Ptr> getIntegerVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getIntegerVariables(); } ROL::Ptr> getContinuousVector(const ROL::Vector &x) const { return dynamic_cast&>(x).getContinuousVariables(); } 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_FacilityLocation(const std::vector &c, const std::vector &q) : c_(c), q_(q), M_(c_.size()), N_(q_.size()/M_) {} Real value(const ROL::Vector &x, Real &tol) { Real val(0); ROL::Ptr> xi = getConstVector(*getIntegerVector(x)); ROL::Ptr> xc = getConstVector(*getContinuousVector(x)); const Real two(2); for (int i = 0; i < M_; ++i) { val += c_[i] * (*xi)[i]; for (int j = 0; j < N_; j++) { val += q_[i + j*M_]*std::pow((*xc)[i + j*M_],two); } } return val; } void gradient(ROL::Vector &g, const ROL::Vector &x, Real &tol) { g.zero(); ROL::Ptr> gi = getVector(*getIntegerVector(g)); ROL::Ptr> gc = getVector(*getContinuousVector(g)); ROL::Ptr> xi = getConstVector(*getIntegerVector(x)); ROL::Ptr> xc = getConstVector(*getContinuousVector(x)); const Real two(2); for (int i = 0; i < M_; ++i) { (*gi)[i] = c_[i]; for (int j = 0; j < N_; ++j) { (*gc)[i + j*M_] = two*q_[i + j*M_]*(*xc)[i + j*M_]; } } } void hessVec(ROL::Vector &hv, const ROL::Vector &v, const ROL::Vector &x, Real &tol ) { hv.zero(); ROL::Ptr> hc = getVector(*getContinuousVector(hv)); ROL::Ptr> vc = getConstVector(*getContinuousVector(v)); const Real two(2); for (int i = 0; i < M_; ++i) { for (int j = 0; j < N_; ++j) { (*hc)[i + j*M_] = two*q_[i + j*M_]*(*vc)[i + j*M_]; } } } }; template class FacilityLocationFactory : public ROL::PEBBL::IntegerProblemFactory { private: ROL::Ptr> x_, emul_, imul_; ROL::Ptr> bnd_, ibnd_; ROL::Ptr> obj_; ROL::Ptr> econ_, icon_; bool useLinearCon_; public: FacilityLocationFactory(const std::vector &c, const std::vector &q, ROL::ParameterList &pl) { const int M = c.size(); const int N = q.size()/M; obj_ = ROL::makePtr>(c,q); ROL::Ptr> xc, xi, xl, xu, zl, zu, lo, up, iup; xc = ROL::makePtr>(M*N,0.0); xi = ROL::makePtr>(M,0.0); x_ = ROL::makePtr>(xc,xi); xl = ROL::makePtr>(M*N,0.0); xu = ROL::makePtr>(M*N,1.0); zl = ROL::makePtr>(M,0.0); zu = ROL::makePtr>(M,1.0); lo = ROL::makePtr>(xl,zl); up = ROL::makePtr>(xu,zu); bnd_ = ROL::makePtr>(lo,up); econ_ = ROL::makePtr>(); emul_ = ROL::makePtr>(N,0.0); icon_ = ROL::makePtr>(); imul_ = ROL::makePtr>(M*N,0.0); iup = ROL::makePtr>(M*N,0.0); ibnd_ = ROL::makePtr>(*iup,false); useLinearCon_ = pl.sublist("Problem").get("Maintain Linear Constraints",true); } ROL::Ptr> build(void) { ROL::Ptr> x = x_->clone(); ROL::Ptr> emul = emul_->clone(); ROL::Ptr> imul = imul_->clone(); ROL::Ptr> problem = ROL::makePtr>(obj_,x); problem->addBoundConstraint(bnd_); if (useLinearCon_) { problem->addLinearConstraint("Equality",econ_,emul); problem->addLinearConstraint("Inequality",icon_,imul,ibnd_); } else { problem->addConstraint("Equality",econ_,emul); problem->addConstraint("Inequality",icon_,imul,ibnd_); } return problem; } }; template class FacilityLocationBranchSub; template class FacilityLocationBranching : public ROL::PEBBL::Branching { private: using ROL::PEBBL::Branching::verbosity_; using ROL::PEBBL::Branching::outStream_; using ROL::PEBBL::Branching::parlist_; public: FacilityLocationBranching(const ROL::Ptr> &factory, const ROL::Ptr &parlist, const ROL::Ptr> &bHelper, int verbosity = 0, const ROL::Ptr &outStream = ROL::nullPtr) : ROL::PEBBL::Branching(factory,parlist,bHelper,verbosity,outStream) {} pebbl::branchSub* blankSub() { return new FacilityLocationBranchSub(parlist_,ROL::makePtrFromRef>(*this),verbosity_,outStream_); } }; // FacilityLocationBranching template class FacilityLocationBranchSub : public ROL::PEBBL::BranchSub { private: Real ctol_; ROL::Ptr> x_, c_; ROL::Ptr> con_; ROL::Ptr> proj_; using ROL::PEBBL::BranchSub::anyChild; using ROL::PEBBL::BranchSub::index_; using ROL::PEBBL::BranchSub::branching_; using ROL::PEBBL::BranchSub::problem0_; using ROL::PEBBL::BranchSub::solution_; using ROL::PEBBL::BranchSub::rndSolution_; using ROL::PEBBL::BranchSub::verbosity_; using ROL::PEBBL::BranchSub::outStream_; ROL::Ptr> getData(ROL::Vector &x) const { return ROL::dynamicPtrCast>( ROL::dynamicPtrCast>(get(x,0))->getIntegerVariables())->getVector(); } ROL::Ptr> getData(ROL::Vector &x, int comp) const { if (comp == 0) return ROL::dynamicPtrCast>( ROL::dynamicPtrCast>(get(x,0))->getIntegerVariables())->getVector(); else return ROL::dynamicPtrCast>( ROL::dynamicPtrCast>(get(x,0))->getContinuousVariables())->getVector(); } ROL::Ptr> getConstData(const ROL::Vector &x) const { return ROL::dynamicPtrCast>( ROL::dynamicPtrCast>(get(x,0))->getContinuousVariables())->getVector(); } ROL::Ptr> get(ROL::Vector &x, int ind) const { return dynamic_cast&>(x).get(ind); } ROL::Ptr> get(const ROL::Vector &x, int ind) const { return dynamic_cast&>(x).get(ind); } void zeroSlack(ROL::Vector &x) const { size_t nv = dynamic_cast&>(x).numVectors(); for (size_t i = 1; i < nv; ++i) { get(x,i)->zero(); } } void setSlack(ROL::Vector &x, const ROL::Vector &c) const { size_t nv = dynamic_cast&>(x).numVectors(); for (size_t i = 1; i < nv; ++i) { get(x,i)->set(*get(c,i)); } problem0_->getBoundConstraint()->project(x); } Real infeasibility(ROL::Vector &x) { Real tol = std::sqrt(ROL::ROL_EPSILON()); zeroSlack(x); con_->update(x,ROL::UpdateType::Temp); con_->value(*c_,x,tol); setSlack(x,*c_); x_->set(x); proj_->project(*x_); x_->axpy(static_cast(-1),x); Real infeas = x_->norm(); return infeas; } public: FacilityLocationBranchSub(const ROL::Ptr &parlist, const ROL::Ptr> &branching, int verbosity = 0, const ROL::Ptr &outStream = ROL::nullPtr) : ROL::PEBBL::BranchSub(branching, verbosity, outStream) { ctol_ = parlist->sublist("Status Test").get("Constraint Tolerance",1e-8); x_ = solution_->clone(); bool useLinearCon = parlist->sublist("Problem").get("Maintain Linear Constraints",true); if (useLinearCon) { proj_ = problem0_->getPolyhedralProjection(); c_ = proj_->getResidual()->clone(); con_ = proj_->getLinearConstraint(); } else { c_ = problem0_->getResidualVector()->clone(); con_ = problem0_->getConstraint(); proj_ = ROL::PolyhedralProjectionFactory( *problem0_->getPrimalOptimizationVector(), *problem0_->getDualOptimizationVector(), problem0_->getBoundConstraint(), con_,*problem0_->getMultiplierVector(),*c_,*parlist); } } FacilityLocationBranchSub(const FacilityLocationBranchSub &rpbs) : ROL::PEBBL::BranchSub(rpbs), ctol_(rpbs.ctol_), x_(rpbs.x_->clone()), c_(rpbs.c_->clone()), con_(rpbs.con_), proj_(rpbs.proj_) {} void incumbentHeuristic() { Real tol = std::sqrt(ROL::ROL_EPSILON()); const Real zero(0), one(1); // Max Rounding //rndSolution_->set(*solution_); //ROL::Ptr> idata = getData(*rndSolution_); //ROL::Ptr> cdata = getConstData(*rndSolution_); //const int M = idata->size(); //const int N = cdata->size()/M; //Real maxj(0); //for (int i = 0; i < M; ++i) { // maxj = (*cdata)[i]; // for (int j = 1; j < N; ++j) { // maxj = std::max(maxj,(*cdata)[i + j*M]); // } // (*idata)[i] = (maxj > tol ? one : zero); //} // Real cnorm = infeasibility(*rndSolution_); // Randomized Rounding ROL::Ptr> idata = getData(*rndSolution_,0); ROL::Ptr> cdata = getData(*rndSolution_,1); const int M = idata->size(); const int N = cdata->size()/M; Real z(0), r(0), cnorm(0), sum(0); int cnt(0); while (true) { rndSolution_->set(*solution_); std::vector ind; for (int i = 0; i < M; ++i) { r = static_cast(rand())/static_cast(RAND_MAX); z = (*idata)[i]; if (r <= z) { (*idata)[i] = zero; for (int j = 0; j < N; ++j) (*cdata)[i + j*M] = zero; } else { (*idata)[i] = one; ind.push_back(i); } } for (int j = 0; j < N; ++j) { sum = zero; for (const auto i : ind) sum += (*cdata)[i + j*M]; for (const auto i : ind) (*cdata)[i + j*M] /= sum; } cnorm = infeasibility(*rndSolution_); cnt++; if (cnorm < ctol_) break; if (verbosity_ > 1) { *outStream_ << " cnt = " << cnt << " infeasibility = " << cnorm << std::endl; } } problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); Real val = problem0_->getObjective()->value(*rndSolution_,tol); branching_->foundSolution(new ROL::PEBBL::IntegerSolution(*rndSolution_,val)); if (verbosity_ > 0) { *outStream_ << "FacilityLocationBranchSub::incumbentHeuristic" << std::endl; *outStream_ << " Incumbent Value: " << val << std::endl; *outStream_ << " Incumbent Infeasibility: " << cnorm << std::endl; } } pebbl::branchSub* makeChild(int whichChild = anyChild) override { ROL_TEST_FOR_EXCEPTION(whichChild==anyChild,std::logic_error, ">>> FacilityLocationBranchSub::makeChild: whichChild is equal to anyChild!"); FacilityLocationBranchSub* child = new FacilityLocationBranchSub(*this); child->updateFixed(index_, (whichChild==0 ? static_cast(1) : static_cast(0))); return child; } }; // class FacilityLocationBranchSub