// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_STEFANBOLTZMANN_BRANCHING_H #define ROL_STEFANBOLTZMANN_BRANCHING_H #include "ROL_PEBBL_Interface.hpp" #include "../../TOOLS/pdevector.hpp" #include "hilbert.hpp" template class TpetraStefanBoltzmannBranchSub; template class StdStefanBoltzmannBranchSub; template class TpetraStefanBoltzmannBranching : public ROL::PEBBL::Branching { private: const int method_; ROL::Ptr> z0_; using ROL::PEBBL::Branching::verbosity_; using ROL::PEBBL::Branching::outStream_; using ROL::PEBBL::Branching::parlist_; public: TpetraStefanBoltzmannBranching(const ROL::Ptr> &factory, const ROL::Ptr &parlist, const ROL::Ptr> &bHelper, int verbosity = 0, const ROL::Ptr &outStream = ROL::nullPtr, int method = 0) : ROL::PEBBL::Branching(factory,parlist,bHelper,verbosity,outStream), method_(method) { z0_ = factory->buildSolutionVector(); } pebbl::branchSub* blankSub() { return new TpetraStefanBoltzmannBranchSub(ROL::makePtrFromRef>(*this),verbosity_,outStream_,method_); } // pebbl::solution* iniitalGuess() { // // } }; // StefanBoltzmannBranching template class TpetraStefanBoltzmannBranchSub : public ROL::PEBBL::BranchSub { private: const int method_; std::string methodName_; 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_; void round(ROL::Vector &rx, const ROL::Vector &x, Real t) const { rx.set(x); Teuchos::ArrayView data = getData(rx); Real val(0); for (auto it = data.begin(); it != data.end(); ++it) { val = *it; *it = (val < t ? std::floor(val) : std::ceil(val)); } } Teuchos::ArrayView getData(ROL::Vector &x) const { try { return (dynamic_cast&>(x).getVector()->getDataNonConst(0))(); } catch (std::exception &e) { return (dynamic_cast&>(x).getField()->getVector()->getDataNonConst(0))(); } } public: TpetraStefanBoltzmannBranchSub(const ROL::Ptr> &branching, int verbosity = 0, const ROL::Ptr &outStream = ROL::nullPtr, int method = 0) : ROL::PEBBL::BranchSub(branching, verbosity, outStream), method_(method) { switch (method_) { case 1: methodName_ = "Mass Preserving Rounding"; break; case 2: methodName_ = "Objective Gap Rounding"; break; case 3: methodName_ = "Sum Up Rounding"; break; default: methodName_ = "Naive Rounding"; } } TpetraStefanBoltzmannBranchSub(const TpetraStefanBoltzmannBranchSub &rpbs) : ROL::PEBBL::BranchSub(rpbs), method_(rpbs.method_), methodName_(rpbs.methodName_) {} void incumbentHeuristic() { Real tol = std::sqrt(ROL::ROL_EPSILON()); Real t(0), val(0); if (method_==1) { // mass preserving rounding const Real zero(0), one(1); rndSolution_->set(*solution_); Teuchos::ArrayView data = getData(*rndSolution_); std::map sdata; Real sum(0); for (size_t i = 0; i < static_cast(data.size()); ++i) { sdata.insert(std::pair(data[i],i)); sum += data[i]; } int rsum = static_cast(std::round(sum)), psum(0); for (auto it = sdata.rbegin(); it != sdata.rend(); ++it) { data[it->second] = (psum < rsum ? one : zero); t = (psum < rsum ? it->first : t); psum++; } } else if (method_==2) { // objective gap rounding const Real invphi(0.5*(std::sqrt(5.0)-1.0)); const Real invphi2(0.5*(3.0-std::sqrt(5.0))); const Real itol(std::sqrt(ROL::ROL_EPSILON())); Real a(0), b(1), c(0), d(1), h(b-a), fc(0), fd(0); // Evaluate at c c = a + invphi2 * h; round(*rndSolution_,*solution_,c); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fc = problem0_->getObjective()->value(*rndSolution_,tol); // Evaluate at d d = a + invphi * h; round(*rndSolution_,*solution_,d); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fd = problem0_->getObjective()->value(*rndSolution_,tol); while (std::abs(c-d) > itol) { h *= invphi; if (fc < fd) { b = d; d = c; fd = fc; c = a + invphi2 * h; round(*rndSolution_,*solution_,c); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fc = problem0_->getObjective()->value(*rndSolution_,tol); } else { a = c; c = d; fc = fd; d = a + invphi * h; round(*rndSolution_,*solution_,d); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fd = problem0_->getObjective()->value(*rndSolution_,tol); } } t = static_cast(0.5)*(a+b); round(*rndSolution_,*solution_,t); } else if (method_==3) { // Sum up rounding const Real zero(0), one(1); rndSolution_->set(*solution_); Teuchos::ArrayView data = getData(*rndSolution_); const int n = data.size(); const int m = (std::log2(n)-1)/2; Real g1(0), g2(0); if (n == std::pow(2,2*m+1)) { // Use Hilbert curves if n = 2^{2m+1} const int nx = std::pow(2,m+1); const int ny = std::pow(2,m); int x(0), y(0), ind(0); // Domain [0,1]x[0,1] for (int j = 0; j < 2; ++j) { for (int i = 0; i < ny*ny; ++i) { hilbert::d2xy(m,i,x,y); ind = x + y*nx + j*ny; g1 += data[ind]; g2 += one - data[ind]; if (g1 >= g2) { data[ind] = one; g1 -= one; } else { data[ind] = zero; g2 -= one; } } } } else { // Otherwise use native ordering of cells for (int i = 0; i < n; ++i) { g1 += data[i]; g2 += one - data[i]; if (g1 >= g2) { data[i] = one; g1 -= one; } else { data[i] = zero; g2 -= one; } } } } else { // naive rounding t = static_cast(0.5); round(*rndSolution_,*solution_,t); } problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); val = problem0_->getObjective()->value(*rndSolution_,tol); branching_->foundSolution(new ROL::PEBBL::IntegerSolution(*rndSolution_,val)); if (verbosity_ > 0) { *outStream_ << "StefanBoltzmannBranchSub::incumbentHeuristic: " << methodName_ << std::endl; *outStream_ << " Incumbent Value: " << val << std::endl; *outStream_ << " Rounding Threshold: " << t << std::endl; } } pebbl::branchSub* makeChild(int whichChild = anyChild) override { ROL_TEST_FOR_EXCEPTION(whichChild==anyChild,std::logic_error, ">>> StefanBoltzmannBranchSub::makeChild: whichChild is equal to anyChild!"); TpetraStefanBoltzmannBranchSub* child = new TpetraStefanBoltzmannBranchSub(*this); child->updateFixed(index_, (whichChild==0 ? static_cast(1) : static_cast(0))); return child; } }; // class StefanBoltzmannBranchSub template class StdStefanBoltzmannBranching : public ROL::PEBBL::Branching { private: const int method_; ROL::Ptr> z0_; using ROL::PEBBL::Branching::verbosity_; using ROL::PEBBL::Branching::outStream_; using ROL::PEBBL::Branching::parlist_; public: StdStefanBoltzmannBranching(const ROL::Ptr> &factory, const ROL::Ptr &parlist, const ROL::Ptr> &bHelper, int verbosity = 0, const ROL::Ptr &outStream = ROL::nullPtr, int method = 0) : ROL::PEBBL::Branching(factory,parlist,bHelper,verbosity,outStream), method_(method) { z0_ = factory->buildSolutionVector(); } pebbl::branchSub* blankSub() { return new StdStefanBoltzmannBranchSub(ROL::makePtrFromRef>(*this),verbosity_,outStream_,method_); } // pebbl::solution* iniitalGuess() { // // } }; // StefanBoltzmannBranching template class StdStefanBoltzmannBranchSub : public ROL::PEBBL::BranchSub { private: const int method_; std::string methodName_; 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_; void round(ROL::Vector &rx, const ROL::Vector &x, Real t) const { rx.set(x); ROL::Ptr> data = getData(rx); Real val(0); for (auto it = data->begin(); it != data->end(); ++it) { val = *it; *it = (val < t ? std::floor(val) : std::ceil(val)); } } ROL::Ptr> getData(ROL::Vector &x) const { try { return ROL::dynamicPtrCast>(dynamic_cast&>(x).get(0))->getVector(); } catch (std::exception &e) { return dynamic_cast&>(x).getVector(); } } public: StdStefanBoltzmannBranchSub(const ROL::Ptr> &branching, int verbosity = 0, const ROL::Ptr &outStream = ROL::nullPtr, int method = 0) : ROL::PEBBL::BranchSub(branching, verbosity, outStream), method_(method) { switch (method_) { case 1: methodName_ = "Mass Preserving Rounding"; break; case 2: methodName_ = "Objective Gap Rounding"; break; case 3: methodName_ = "Sum Up Rounding"; break; default: methodName_ = "Naive Rounding"; } } StdStefanBoltzmannBranchSub(const StdStefanBoltzmannBranchSub &rpbs) : ROL::PEBBL::BranchSub(rpbs), method_(rpbs.method_), methodName_(rpbs.methodName_) {} void incumbentHeuristic() { Real tol = std::sqrt(ROL::ROL_EPSILON()); Real t(0), val(0); if (method_==1) { // mass preserving rounding const Real zero(0), one(1); rndSolution_->set(*solution_); ROL::Ptr> data = getData(*rndSolution_); std::map sdata; Real sum(0); for (size_t i = 0; i < static_cast(data->size()); ++i) { sdata.insert(std::pair((*data)[i],i)); sum += (*data)[i]; } int rsum = static_cast(std::round(sum)), psum(0); for (auto it = sdata.rbegin(); it != sdata.rend(); ++it) { (*data)[it->second] = (psum < rsum ? one : zero); t = (psum < rsum ? it->first : t); psum++; } } else if (method_==2) { // objective gap rounding const Real invphi(0.5*(std::sqrt(5.0)-1.0)); const Real invphi2(0.5*(3.0-std::sqrt(5.0))); const Real itol(std::sqrt(ROL::ROL_EPSILON())); Real a(0), b(1), c(0), d(1), h(b-a), fc(0), fd(0); // Evaluate at c c = a + invphi2 * h; round(*rndSolution_,*solution_,c); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fc = problem0_->getObjective()->value(*rndSolution_,tol); // Evaluate at d d = a + invphi * h; round(*rndSolution_,*solution_,d); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fd = problem0_->getObjective()->value(*rndSolution_,tol); while (std::abs(c-d) > itol) { h *= invphi; if (fc < fd) { b = d; d = c; fd = fc; c = a + invphi2 * h; round(*rndSolution_,*solution_,c); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fc = problem0_->getObjective()->value(*rndSolution_,tol); } else { a = c; c = d; fc = fd; d = a + invphi * h; round(*rndSolution_,*solution_,d); problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); fd = problem0_->getObjective()->value(*rndSolution_,tol); } } t = static_cast(0.5)*(a+b); round(*rndSolution_,*solution_,t); } else { // naive rounding t = static_cast(0.5); round(*rndSolution_,*solution_,t); } problem0_->getObjective()->update(*rndSolution_,ROL::UpdateType::Temp); val = problem0_->getObjective()->value(*rndSolution_,tol); branching_->foundSolution(new ROL::PEBBL::IntegerSolution(*rndSolution_,val)); if (verbosity_ > 0) { *outStream_ << "StefanBoltzmannBranchSub::incumbentHeuristic: " << methodName_ << std::endl; *outStream_ << " Incumbent Value: " << val << std::endl; *outStream_ << " Rounding Threshold: " << t << std::endl; } } pebbl::branchSub* makeChild(int whichChild = anyChild) override { ROL_TEST_FOR_EXCEPTION(whichChild==anyChild,std::logic_error, ">>> StefanBoltzmannBranchSub::makeChild: whichChild is equal to anyChild!"); StdStefanBoltzmannBranchSub* child = new StdStefanBoltzmannBranchSub(*this); child->updateFixed(index_, (whichChild==0 ? static_cast(1) : static_cast(0))); return child; } }; // class StefanBoltzmannBranchSub #endif