// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef L1DYNOBJECTIVE_HPP #define L1DYNOBJECTIVE_HPP #include "ROL_Objective.hpp" #include "../../TOOLS/ltiobjective.hpp" #include "../../TOOLS/meshreader.hpp" #include "../../TOOLS/pdevector.hpp" template class L1_Dyn_Objective : public ROL::Objective { using size_type = typename std::vector::size_type; private: Real theta_, beta_;//, T_; const size_type Nt_; const std::vector> ts_; const ROL::Ptr> zl_; const ROL::Ptr> zu_; ROL::PartitionedVector &partition ( ROL::Vector& x ) const { return static_cast&>(x); } const ROL::PartitionedVector &partition ( const ROL::Vector& x ) const { return static_cast&>(x); } ROL::Ptr > getConstParameter(const ROL::Vector &x) const { ROL::Ptr > xp; try { xp = dynamic_cast&>(x).getVector(); } catch (std::exception &e) { try { ROL::Ptr > xvec = dynamic_cast&>(x).getParameter(); if ( xvec == ROL::nullPtr ) { xp = ROL::nullPtr; } else { xp = xvec->getVector(); } } catch (std::exception &ee) { xp = ROL::nullPtr; } } return xp; } ROL::Ptr > getParameter(ROL::Vector &x) const { ROL::Ptr > xp; try { xp = dynamic_cast&>(x).getVector(); } catch (std::exception &e) { try { ROL::Ptr > xvec = dynamic_cast&>(x).getParameter(); if ( xvec == ROL::nullPtr ) { xp = ROL::nullPtr; } else { xp = xvec->getVector(); } } catch (std::exception &ee) { xp = ROL::nullPtr; } } return xp; } public: L1_Dyn_Objective(ROL::ParameterList &parlist, const std::vector> &timeStamp, const ROL::Ptr> &zl, const ROL::Ptr> &zu ) : Nt_(timeStamp.size()), ts_(timeStamp), zl_(zl), zu_(zu) { theta_ = parlist.sublist("Reduced Dynamic Objective").sublist("Time Discretization").get("Theta", 1.0); beta_ = parlist.sublist("Problem").get("L1 Control Cost", 1.e-2); } // value Real value(const ROL::Vector &z, Real &tol ){ const ROL::PartitionedVector &zp = partition(z); const Real one(1); Real dt(0), val(0); bool isinf(false); for (size_type k = 0; k < Nt_; ++k){//dynamic obj ROL::Ptr> zlk = getConstParameter(*zl_->get(k)); ROL::Ptr> zuk = getConstParameter(*zu_->get(k)); ROL::Ptr> zpk = getConstParameter(*zp.get(k)); // ROL vector of partition/kth timestep for (size_type i = 0; i < zpk->size(); ++i){ if ((*zpk)[i] < (*zlk)[i] || (*zpk)[i]>(*zuk)[i]){ val = ROL::ROL_INF(); isinf=true; break; } } } if (!isinf) { for (size_type k = 1; k < Nt_; ++k){//dynamic obj ROL::Ptr> zpn = getConstParameter(*zp.get(k)); // ROL vector of partition/kth timestep ROL::Ptr> zpo = getConstParameter(*zp.get(k-1)); // ROL vector of partition/kth timestep dt = ts_[k].t[1] - ts_[k].t[0]; for (size_type i = 0; i < zpn->size(); ++i){ val += dt*((one - theta_)*std::abs((*zpo)[i]) + theta_*std::abs((*zpn)[i])); } // end i for }// end k for } return beta_*val; } //end value // prox void prox(ROL::Vector &Pz, const ROL::Vector &z, Real t, Real &tol ) { //partitioned vectors const ROL::PartitionedVector &zp = partition(z); ROL::PartitionedVector &Pzp = partition(Pz); //constants const Real one(1), zero(0); Real hk(0), hkplus(0); Real l1param(0); for (size_type k = 0; kNt inclusive ROL::Ptr> zlk = getConstParameter(*zl_->get(k)); ROL::Ptr> zuk = getConstParameter(*zu_->get(k)); ROL::Ptr> zk = getConstParameter(zp[k]); //kth timestep ROL::Ptr> Pzk = getParameter(Pzp[k]); // update prox parameter if (k == 0){ hkplus = ts_[k+1].t[1] - ts_[k+1].t[0]; l1param = t*hkplus*beta_*(one - theta_); } else if (k == (Nt_ - 1)) { hk = ts_[k].t[1] - ts_[k].t[0]; l1param = t*hk*beta_*theta_; } else { hk = ts_[k].t[1] - ts_[k].t[0]; hkplus = ts_[k+1].t[1] - ts_[k+1].t[0]; l1param = t*beta_*(hkplus*(one - theta_) + theta_*hk); } for (size_type i = 0; i < zk->size(); ++i){ if ((*zk)[i] > l1param) { (*Pzk)[i] = (*zk)[i] - l1param; } else if ((*zk)[i] < -l1param) {(*Pzk)[i] = (*zk)[i] + l1param;} else { (*Pzk)[i] = zero;} (*Pzk)[i] = std::min((*zuk)[i], std::max((*zlk)[i], (*Pzk)[i])); } // end i loop }//end k for }//end prox }; #endif