Debian Codegraph @

packages/rol/src/oed/factors/ROL_OED_Factors_Def.hpp

raw
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_OED_FACTORS_DEF_HPP
11 #define ROL_OED_FACTORS_DEF_HPP
12
13 namespace ROL {
14 namespace OED {
15
16 template<typename Real>
17 void Factors<Real>::evaluateModel(Vector<Real> &g, const std::vector<Real> &param) const {
18 startTimer("evaluateModel");
19 bool isComputed = storage_ ? g_storage_->get(g,param) : false;
20 if (!isComputed) {
21 Real tol = std::sqrt(ROL_EPSILON<Real>());
22 model_->setParameter(param);
23 model_->applyAdjointJacobian(g,*c_,*theta_,tol);
24 if (storage_) g_storage_->set(g,param);
25 }
26 stopTimer("evaluateModel");
27 }
28
29 template<typename Real>
30 void Factors<Real>::setFactors() {
31 startTimer("setFactors");
32 const int nsamples = sampler_->numMySamples();
33 g_storage_ = makePtr<SampledVector<Real>>();
34 X_.clear(); X_.resize(nsamples,nullPtr);
35 for (int i = 0; i < nsamples; ++i) {
36 X_[i] = theta_->dual().clone();
37 evaluateModel(*X_[i],sampler_->getMyPoint(i));
38 }
39 stopTimer("setFactors");
40 }
41
42 template<typename Real>
43 Factors<Real>::Factors(const Ptr<Constraint<Real>> &model,
44 const Ptr<Vector<Real>> &theta,
45 const Ptr<Vector<Real>> &obs,
46 const Ptr<SampleGenerator<Real>> &sampler,
47 bool storage,
48 const Ptr<Vector<Real>> &c)
49 : ProfiledClass<Real,std::string>("OED::Factors"),
50 model_(model), theta_(theta), obs_(obs), obs0_(obs_->clone()),
51 c_(c == nullPtr ? obs_->dual().clone() : c), sampler_(sampler),
52 storage_(storage), obs1d_(obs_->dimension()==1) {
53 if (c == nullPtr) c_->setScalar(static_cast<Real>(1));
54 setFactors();
55 }
56
57 template<typename Real>
58 Factors<Real>::Factors(const Ptr<Objective<Real>> &model,
59 const Ptr<Vector<Real>> &theta,
60 const Ptr<SampleGenerator<Real>> &sampler,
61 bool storage)
62 : Factors<Real>(makePtr<ConstraintFromObjective<Real>>(model),
63 theta,makePtr<SingletonVector<Real>>(),sampler,storage) {}
64
65 template<typename Real>
66 void Factors<Real>::setPredictionVector(const Vector<Real> &c) {
67 if (!obs1d_) {
68 c_->set(c);
69 setFactors();
70 }
71 }
72
73 template<typename Real>
74 void Factors<Real>::getPredictionVector(Vector<Real> &c) const {
75 c.set(*c_);
76 }
77
78 // Create a vector in the parameter space
79 template<typename Real>
80 Ptr<Vector<Real>> Factors<Real>::createParameterVector(bool dual) const {
81 return dual ? theta_->dual().clone() : theta_->clone();
82 }
83
84 // Create a vector in the observation space
85 template<typename Real>
86 Ptr<Vector<Real>> Factors<Real>::createObservationVector(bool dual) const {
87 if (obs1d_) return makePtr<SingletonVector<Real>>();
88 return dual ? c_->clone() : obs_->clone();
89 }
90
91 // Compute c^T F[k] x
92 template<typename Real>
93 Real Factors<Real>::apply(const Vector<Real> &x, int k) const {
94 startTimer("apply");
95 if (k < 0 || k >= sampler_->numMySamples())
96 throw Exception::NotImplemented(">>> ROL::OED::Factors::apply : Index is out of bounds!");
97 Real val = x.dot(X_[k]->dual());
98 stopTimer("apply");
99 return val;
100 }
101
102 // Compute Fx = F[k] x
103 template<typename Real>
104 void Factors<Real>::apply(Vector<Real> &Fx, const Vector<Real> &x, int k) const {
105 startTimer("apply");
106 if (k < 0 || k >= sampler_->numMySamples())
107 throw Exception::NotImplemented(">>> ROL::OED::Factors::apply : Index is out of bounds!");
108 if (obs1d_) {
109 Fx.setScalar(apply(x,k));
110 }
111 else {
112 Real tol(std::sqrt(ROL_EPSILON<Real>()));
113 Fx.zero();
114 model_->setParameter(sampler_->getMyPoint(k));
115 model_->applyJacobian(Fx,x,*theta_,tol);
116 }
117 stopTimer("apply");
118 }
119
120 // Compute Mx = F[k]^T R F[k] x
121 template<typename Real>
122 void Factors<Real>::applyProduct(Vector<Real> &Mx, const Vector<Real> &x, int k) const {
123 startTimer("applyProduct");
124 if (k < 0 || k >= sampler_->numMySamples())
125 throw Exception::NotImplemented(">>> ROL::OED::Factors::applyProduct : Index is out of bounds!");
126 if (obs1d_) {
127 Mx.set(*get(k));
128 Mx.scale(apply(x,k));
129 }
130 else {
131 Real tol(std::sqrt(ROL_EPSILON<Real>()));
132 Mx.zero(); obs_->zero();
133 model_->setParameter(sampler_->getMyPoint(k));
134 model_->applyJacobian(*obs_,x,*theta_,tol);
135 model_->applyAdjointJacobian(Mx,obs_->dual(),*theta_,tol);
136 }
137 stopTimer("applyProduct");
138 }
139
140 // Compute y^T F[k]^T R F[k] x
141 template<typename Real>
142 Real Factors<Real>::applyProduct2(const Vector<Real> &x, const Vector<Real> &y, int k) const {
143 startTimer("applyProduct2");
144 if (k < 0 || k >= sampler_->numMySamples())
145 throw Exception::NotImplemented(">>> ROL::OED::Factors::applyProduct2 : Index is out of bounds!");
146 Real val(0);
147 if (obs1d_) {
148 Real Fx = get(k)->dot(x.dual());
149 Real Fy = get(k)->dot(y.dual());
150 val = Fx * Fy;
151 }
152 else {
153 Real tol(std::sqrt(ROL_EPSILON<Real>()));
154 obs_->zero(); obs0_->zero();
155 model_->setParameter(sampler_->getMyPoint(k));
156 model_->applyJacobian(*obs_,x,*theta_,tol);
157 model_->applyJacobian(*obs0_,y,*theta_,tol);
158 val = obs_->dot(*obs0_);
159 }
160 stopTimer("applyProduct2");
161 return val;
162 }
163
164 // Get F[k]^T c
165 template<typename Real>
166 const Ptr<const Vector<Real>> Factors<Real>::get(int k) const {
167 startTimer("get");
168 if (k < 0 || k >= sampler_->numMySamples())
169 throw Exception::NotImplemented(">>> ROL::OED::Factors::get : Index is out of bounds!");
170 stopTimer("get");
171 return X_[k];
172 }
173
174 // Compute F(param)^T c
175 template<typename Real>
176 void Factors<Real>::evaluate(Vector<Real> &F, const std::vector<Real> &param) const {
177 startTimer("evaluate");
178 evaluateModel(F,param);
179 stopTimer("evaluate");
180 }
181
182 template<typename Real>
183 int Factors<Real>::numFactors() const {
184 return theta_->dimension();
185 }
186
187 template<typename Real>
188 int Factors<Real>::numMySamples() const {
189 return sampler_->numMySamples();
190 }
191
192 template<typename Real>
193 std::vector<Real> Factors<Real>::getSample(int k) const {
194 return sampler_->getMyPoint(k);
195 }
196
197 } // End OED Namespace
198 } // End ROL Namespace
199
200 #endif