ROL
Loading...
Searching...
No Matches
ROL_SeparableObjective_Def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef ROL_SEPARABLEOBJECTIVE_DEF_H
11#define ROL_SEPARABLEOBJECTIVE_DEF_H
12
13namespace ROL {
14
15template<typename Real>
17 : obj_(obj), size_(obj.size()) {}
18
19template<typename Real>
20const Ptr<Objective<Real>> SeparableObjective<Real>::get(unsigned i) const {
21 ROL_TEST_FOR_EXCEPTION( (size_ <= i ) , std::invalid_argument,
22 ">>> ERROR (ROL_SeparableObjective, get): "
23 "Dimension mismatch.");
24 return obj_[i];
25}
26
27template<typename Real>
29 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
30 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
31 ">>> ERROR (ROL_SeparableObjective, update): "
32 "Dimension mismatch.");
33 for(unsigned i = 0; i < size_; ++i) obj_[i]->update(*xp.get(i),type,iter);
34}
35
36template<typename Real>
37void SeparableObjective<Real>::update(const Vector<Real> &x, bool flag, int iter) {
38 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
39 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
40 ">>> ERROR (ROL_SeparableObjective, update): "
41 "Dimension mismatch.");
42 for(unsigned i = 0; i < size_; ++i) obj_[i]->update(*xp.get(i),flag,iter);
43}
44
45template<typename Real>
46Real SeparableObjective<Real>::value( const Vector<Real> &x, Real &tol ) {
47 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
48 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
49 ">>> ERROR (ROL_SeparableObjective, value): "
50 "Dimension mismatch.");
51 Real val(0);
52 for(unsigned i = 0; i < size_; ++i) val += obj_[i]->value(*xp.get(i),tol);
53 return val;
54}
55
56template<typename Real>
59 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
60 ROL_TEST_FOR_EXCEPTION( (size_ != gp.numVectors() ) , std::invalid_argument,
61 ">>> ERROR (ROL_SeparableObjective, gradient): "
62 "Dimension mismatch.");
63 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
64 ">>> ERROR (ROL_SeparableObjective, gradient): "
65 "Dimension mismatch.");
66 for(unsigned i = 0; i < size_; ++i) obj_[i]->gradient(*gp.get(i),*xp.get(i),tol);
67}
68
69template<typename Real>
70void SeparableObjective<Real>::hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
71 PartitionedVector<Real> &hvp = dynamic_cast<PartitionedVector<Real>&>(hv);
72 const PartitionedVector<Real> &vp = dynamic_cast<const PartitionedVector<Real>&>(v);
73 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
74 ROL_TEST_FOR_EXCEPTION( (size_ != hvp.numVectors() ) , std::invalid_argument,
75 ">>> ERROR (ROL_SeparableObjective, gradient): "
76 "Dimension mismatch.");
77 ROL_TEST_FOR_EXCEPTION( (size_ != vp.numVectors() ) , std::invalid_argument,
78 ">>> ERROR (ROL_SeparableObjective, hessVec): "
79 "Dimension mismatch.");
80 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
81 ">>> ERROR (ROL_SeparableObjective, hessVec): "
82 "Dimension mismatch.");
83 for(unsigned i = 0; i < size_; ++i) obj_[i]->hessVec(*hvp.get(i),*vp.get(i),*xp.get(i),tol);
84}
85
86template<typename Real>
88 PartitionedVector<Real> &hvp = dynamic_cast<PartitionedVector<Real>&>(hv);
89 const PartitionedVector<Real> &vp = dynamic_cast<const PartitionedVector<Real>&>(v);
90 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
91 ROL_TEST_FOR_EXCEPTION( (size_ != hvp.numVectors() ) , std::invalid_argument,
92 ">>> ERROR (ROL_SeparableObjective, gradient): "
93 "Dimension mismatch.");
94 ROL_TEST_FOR_EXCEPTION( (size_ != vp.numVectors() ) , std::invalid_argument,
95 ">>> ERROR (ROL_SeparableObjective, hessVec): "
96 "Dimension mismatch.");
97 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
98 ">>> ERROR (ROL_SeparableObjective, hessVec): "
99 "Dimension mismatch.");
100 for(unsigned i = 0; i < size_; ++i) obj_[i]->invHessVec(*hvp.get(i),*vp.get(i),*xp.get(i),tol);
101}
102
103template<typename Real>
105 PartitionedVector<Real> &hvp = dynamic_cast<PartitionedVector<Real>&>(hv);
106 const PartitionedVector<Real> &vp = dynamic_cast<const PartitionedVector<Real>&>(v);
107 const PartitionedVector<Real> &xp = dynamic_cast<const PartitionedVector<Real>&>(x);
108 ROL_TEST_FOR_EXCEPTION( (size_ != hvp.numVectors() ) , std::invalid_argument,
109 ">>> ERROR (ROL_SeparableObjective, gradient): "
110 "Dimension mismatch.");
111 ROL_TEST_FOR_EXCEPTION( (size_ != vp.numVectors() ) , std::invalid_argument,
112 ">>> ERROR (ROL_SeparableObjective, hessVec): "
113 "Dimension mismatch.");
114 ROL_TEST_FOR_EXCEPTION( (size_ != xp.numVectors() ) , std::invalid_argument,
115 ">>> ERROR (ROL_SeparableObjective, hessVec): "
116 "Dimension mismatch.");
117 for(unsigned i = 0; i < size_; ++i) obj_[i]->precond(*hvp.get(i),*vp.get(i),*xp.get(i),tol);
118}
119
120template<typename Real>
121void SeparableObjective<Real>::setParameter(const std::vector<Real> &param) {
123 for(unsigned i = 0; i < size_; ++i) obj_[i]->setParameter(param);
124}
125
126} // namespace ROL
127
128#endif
const Ptr< Obj > obj_
Provides the interface to evaluate objective functions.
virtual void setParameter(const std::vector< Real > &param)
Defines the linear algebra of vector space on a generic partitioned vector.
ROL::Ptr< const Vector< Real > > get(size_type i) const
void update(const Vector< Real > &x, UpdateType type, int iter=-1) override
Update objective function.
Real value(const Vector< Real > &x, Real &tol) override
Compute value.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol) override
Compute gradient.
void precond(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply preconditioner to vector.
void setParameter(const std::vector< Real > &param) override
SeparableObjective(const std::vector< Ptr< Objective< Real > > > &obj)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply Hessian approximation to vector.
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply inverse Hessian approximation to vector.
const Ptr< Objective< Real > > get(unsigned i) const
Defines the linear algebra or vector space interface.
void setParameter(ROL::ParameterList &parlist, const std::vector< std::string > &location, const std::vector< std::string >::iterator iter, ParameterType value)
ROL::Objective_SerialSimOpt Objective_SimOpt value(const V &u, const V &z, Real &tol) override
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1) override