| 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_COVARIANCE_OPERATOR_DEF_HPP |
| 11 |
#define ROL_OED_COVARIANCE_OPERATOR_DEF_HPP |
| 12 |
|
| 13 |
namespace ROL { |
| 14 |
namespace OED { |
| 15 |
|
| 16 |
/***************************************************************************/ |
| 17 |
/* Begin Accessor Functions */ |
| 18 |
/***************************************************************************/ |
| 19 |
template<typename Real> |
| 20 |
Real MomentOperator<Real>::get(const Vector<Real> &x, int i) const { |
| 21 |
return dynamic_cast<const ProbabilityVector<Real>&>(x).getProbability(i); |
| 22 |
} |
| 23 |
|
| 24 |
template<typename Real> |
| 25 |
void MomentOperator<Real>::set(Vector<Real> &x, int i, Real val) const { |
| 26 |
dynamic_cast<ProbabilityVector<Real>&>(x).setProbability(i,val); |
| 27 |
} |
| 28 |
|
| 29 |
template<typename Real> |
| 30 |
std::vector<Real>& MomentOperator<Real>::getLocalDesign(Vector<Real> &p) const { |
| 31 |
return *dynamic_cast<StdVector<Real>&>(p).getVector(); |
| 32 |
} |
| 33 |
|
| 34 |
template<typename Real> |
| 35 |
const std::vector<Real>& MomentOperator<Real>::getConstLocalDesign(const Vector<Real> &p) const { |
| 36 |
return *dynamic_cast<const StdVector<Real>&>(p).getVector(); |
| 37 |
} |
| 38 |
|
| 39 |
template<typename Real> |
| 40 |
void MomentOperator<Real>::sumAll(const Vector<Real> &p, Real* input, Real* output, int size) const { |
| 41 |
dynamic_cast<const ProbabilityVector<Real>&>(p).getBatchManager()->sumAll(input,output,size); |
| 42 |
} |
| 43 |
|
| 44 |
template<typename Real> |
| 45 |
void MomentOperator<Real>::applyPerturbation(Vector<Real> &Px, const Vector<Real> &x) const { |
| 46 |
Real tol = std::sqrt(ROL_EPSILON<Real>()); |
| 47 |
pOp_->apply(Px,x,tol); |
| 48 |
} |
| 49 |
/***************************************************************************/ |
| 50 |
/* End Accessor Functions */ |
| 51 |
/***************************************************************************/ |
| 52 |
|
| 53 |
template<typename Real> |
| 54 |
MomentOperator<Real>::MomentOperator(RegressionType regType, |
| 55 |
bool homNoise, |
| 56 |
const Ptr<Noise<Real>> &noise) |
| 57 |
: ProfiledClass<Real,std::string>("OED::MomentOperator"), |
| 58 |
regType_(regType), homNoise_(homNoise), noise_(noise), matNum_(0), |
| 59 |
sum_(nullPtr), setSolver_(true) {} |
| 60 |
|
| 61 |
template<typename Real> |
| 62 |
Ptr<MomentOperator<Real>> MomentOperator<Real>::clone() const { |
| 63 |
RegressionType type; |
| 64 |
bool hom; |
| 65 |
Ptr<Noise<Real>> noise; |
| 66 |
MomentOperator<Real>::getRegressionInfo(type,hom,noise); |
| 67 |
return makePtr<MomentOperator<Real>>(type,hom,noise); |
| 68 |
} |
| 69 |
|
| 70 |
template<typename Real> |
| 71 |
void MomentOperator<Real>::setMatrixNumber(int matNum) { |
| 72 |
matNum_ = matNum; |
| 73 |
} |
| 74 |
|
| 75 |
template<typename Real> |
| 76 |
void MomentOperator<Real>::setFactors(const Ptr<Factors<Real>> &factors) { |
| 77 |
factors_ = factors; |
| 78 |
} |
| 79 |
|
| 80 |
template<typename Real> |
| 81 |
void MomentOperator<Real>::setPerturbation(const Ptr<LinearOperator<Real>> &pOp) { |
| 82 |
pOp_ = pOp; |
| 83 |
} |
| 84 |
|
| 85 |
// Compute M(p)x where M(p) = p_1 X_1 S_1 X_1' + ... + p_N X_N S_N X_N' |
| 86 |
template<typename Real> |
| 87 |
void MomentOperator<Real>::apply(Vector<Real> &Mx, |
| 88 |
const Vector<Real> &x, |
| 89 |
const Vector<Real> &p) { |
| 90 |
startTimer("apply"); |
| 91 |
if (sum_ == nullPtr) sum_ = Mx.clone(); |
| 92 |
int nsamples = factors_->numMySamples(); |
| 93 |
if (nsamples > 0) { |
| 94 |
sum_->zero(); |
| 95 |
for (int i = 0; i < nsamples; ++i) { |
| 96 |
factors_->applyProduct(Mx,x,i); |
| 97 |
sum_->axpy(get(p,i)*getNoise(i),Mx); |
| 98 |
} |
| 99 |
Mx.zero(); |
| 100 |
dynamic_cast<const ProbabilityVector<Real>&>(p).getBatchManager()->sumAll(*sum_,Mx); |
| 101 |
if (pOp_ != nullPtr) { |
| 102 |
sum_->zero(); |
| 103 |
applyPerturbation(*sum_,x); |
| 104 |
Mx.plus(*sum_); |
| 105 |
} |
| 106 |
} |
| 107 |
else { |
| 108 |
throw Exception::NotImplemented(">>> OED::MomentOperator::apply : Factors have not been set!"); |
| 109 |
} |
| 110 |
stopTimer("apply"); |
| 111 |
} |
| 112 |
|
| 113 |
// Compute M(q)x where M(q) = q_1 X_1 S_1 X_1' + ... + q_N X_N S_N X_N' |
| 114 |
// This function is distinct from apply to allow the user to store M(p) |
| 115 |
template<typename Real> |
| 116 |
void MomentOperator<Real>::applyDeriv(Vector<Real> &Mx, |
| 117 |
const Vector<Real> &x, |
| 118 |
const Vector<Real> &q) { |
| 119 |
startTimer("applyDeriv"); |
| 120 |
if (sum_ == nullPtr) sum_ = Mx.clone(); |
| 121 |
int nsamples = factors_->numMySamples(); |
| 122 |
if (nsamples > 0) { |
| 123 |
sum_->zero(); |
| 124 |
for (int i = 0; i < nsamples; ++i) { |
| 125 |
factors_->applyProduct(Mx,x,i); |
| 126 |
sum_->axpy(get(q,i)*getNoise(i),Mx); |
| 127 |
} |
| 128 |
Mx.zero(); |
| 129 |
dynamic_cast<const ProbabilityVector<Real>&>(q).getBatchManager()->sumAll(*sum_,Mx); |
| 130 |
} |
| 131 |
else { |
| 132 |
throw Exception::NotImplemented(">>> OED::MomentOperator::applyDeriv : Factors have not been set!"); |
| 133 |
} |
| 134 |
stopTimer("applyDeriv"); |
| 135 |
} |
| 136 |
|
| 137 |
// Compute inv(M(p))x where M(p) = p_1 X_1 S_1 X_1' + ... + p_N X_N S_N X_N' |
| 138 |
template<typename Real> |
| 139 |
void MomentOperator<Real>::applyInverse(Vector<Real> &Mx, |
| 140 |
const Vector<Real> &x, |
| 141 |
const Vector<Real> &p) { |
| 142 |
startTimer("applyInverse"); |
| 143 |
// Set up Krylov solver |
| 144 |
if (setSolver_) { |
| 145 |
krylov_ = makePtr<ConjugateResiduals<Real>>(1e-10,1e0,1000); |
| 146 |
M_ = makePtr<moment>(factors_,pOp_); |
| 147 |
P_ = makePtr<precond>(); |
| 148 |
setSolver_ = false; |
| 149 |
} |
| 150 |
M_->setProbabilityVector(static_cast<const ProbabilityVector<Real>&>(p)); |
| 151 |
int iter(0), flag(0); |
| 152 |
krylov_->run(Mx,*M_,x,*P_,iter,flag); |
| 153 |
stopTimer("applyInverse"); |
| 154 |
} |
| 155 |
|
| 156 |
template<typename Real> |
| 157 |
void MomentOperator<Real>::applySampleMatrices(Vector<Real> &uXv, const Vector<Real> &u, const Vector<Real> &v) { |
| 158 |
startTimer("applySampleMatrices"); |
| 159 |
int nsamples = factors_->numMySamples(); |
| 160 |
if (nsamples > 0) { |
| 161 |
Real val(0); |
| 162 |
for (int i = 0; i < nsamples; ++i) { |
| 163 |
val = getNoise(i)*factors_->applyProduct2(u,v,i); |
| 164 |
set(uXv,i,val); |
| 165 |
} |
| 166 |
} |
| 167 |
else { |
| 168 |
throw Exception::NotImplemented(">>> OED::MomentOperator::applySampleMatrices : Factors have not been set!"); |
| 169 |
} |
| 170 |
stopTimer("applySampleMatrices"); |
| 171 |
} |
| 172 |
|
| 173 |
template<typename Real> |
| 174 |
void MomentOperator<Real>::setNoise(const Ptr<Noise<Real>> &noise, bool isHom) { |
| 175 |
noise_ = noise; |
| 176 |
homNoise_ = isHom; |
| 177 |
} |
| 178 |
|
| 179 |
template<typename Real> |
| 180 |
Real MomentOperator<Real>::getNoise(int k) const { |
| 181 |
Real val(1); |
| 182 |
if (noise_ != nullPtr && !homNoise_) { |
| 183 |
const Real one(1); |
| 184 |
Real noise = noise_->evaluate(factors_->getSample(k)); |
| 185 |
switch(regType_) { |
| 186 |
case(LEASTSQUARES) : val = (matNum_==1 ? one : std::pow(noise,2)); break; |
| 187 |
case(QUANTILE) : val = (matNum_==1 ? one/noise : one); break; |
| 188 |
case(SUPERQUANTILE) : val = (matNum_==1 ? one/noise : one); break; |
| 189 |
case(EXPONENTIAL) : val = (matNum_==1 ? one : noise_->mgf2(factors_->getSample(k))-one); break; |
| 190 |
case(REGTYPE_LAST) : val = one; |
| 191 |
} |
| 192 |
} |
| 193 |
return val; |
| 194 |
} |
| 195 |
|
| 196 |
template<typename Real> |
| 197 |
void MomentOperator<Real>::getRegressionInfo(RegressionType ®Type, bool &homNoise, |
| 198 |
Ptr<Noise<Real>> &noise) const { |
| 199 |
regType = regType_; |
| 200 |
homNoise = homNoise_; |
| 201 |
noise = noise_; |
| 202 |
} |
| 203 |
|
| 204 |
template<typename Real> |
| 205 |
Real MomentOperator<Real>::logDeterminant(const Vector<Real> &z) { |
| 206 |
throw Exception::NotImplemented(">>> OED::MomentOperator::logDeterminant is not implemented!"); |
| 207 |
} |
| 208 |
|
| 209 |
} // End OED Namespace |
| 210 |
} // End ROL Namespace |
| 211 |
|
| 212 |
#endif |