| 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_HPP |
| 11 |
#define ROL_OED_COVARIANCE_OPERATOR_HPP |
| 12 |
|
| 13 |
#include "ROL_Ptr.hpp" |
| 14 |
#include "ROL_SampleGenerator.hpp" |
| 15 |
#include "ROL_ProbabilityVector.hpp" |
| 16 |
#include "ROL_ConjugateResiduals.hpp" |
| 17 |
#include "ROL_UpdateType.hpp" |
| 18 |
#include "ROL_Types.hpp" |
| 19 |
#include "ROL_OED_Factors.hpp" |
| 20 |
#include "ROL_OED_Noise.hpp" |
| 21 |
#include "ROL_OED_ProfiledClass.hpp" |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
namespace ROL { |
| 25 |
namespace OED { |
| 26 |
|
| 27 |
enum RegressionType { |
| 28 |
LEASTSQUARES = 0, |
| 29 |
QUANTILE, |
| 30 |
SUPERQUANTILE, |
| 31 |
EXPONENTIAL, |
| 32 |
REGTYPE_LAST |
| 33 |
}; |
| 34 |
|
| 35 |
inline std::string RegressionTypeToString(RegressionType tr) { |
| 36 |
std::string retString; |
| 37 |
switch(tr) { |
| 38 |
case LEASTSQUARES: retString = "Least Squares"; break; |
| 39 |
case QUANTILE: retString = "Quantile"; break; |
| 40 |
case SUPERQUANTILE: retString = "Superquantile"; break; |
| 41 |
case EXPONENTIAL: retString = "Exponential"; break; |
| 42 |
case REGTYPE_LAST: retString = "Last Type (Dummy)"; break; |
| 43 |
default: retString = "INVALID RegressionType"; |
| 44 |
} |
| 45 |
return retString; |
| 46 |
} |
| 47 |
|
| 48 |
inline int isValidStep(RegressionType ls) { |
| 49 |
return( (ls == LEASTSQUARES) || |
| 50 |
(ls == QUANTILE) || |
| 51 |
(ls == SUPERQUANTILE) ) ; |
| 52 |
} |
| 53 |
|
| 54 |
inline RegressionType & operator++(RegressionType &type) { |
| 55 |
return type = static_cast<RegressionType>(type+1); |
| 56 |
} |
| 57 |
|
| 58 |
inline RegressionType operator++(RegressionType &type, int) { |
| 59 |
RegressionType oldval = type; |
| 60 |
++type; |
| 61 |
return oldval; |
| 62 |
} |
| 63 |
|
| 64 |
inline RegressionType & operator--(RegressionType &type) { |
| 65 |
return type = static_cast<RegressionType>(type-1); |
| 66 |
} |
| 67 |
|
| 68 |
inline RegressionType operator--(RegressionType &type, int) { |
| 69 |
RegressionType oldval = type; |
| 70 |
--type; |
| 71 |
return oldval; |
| 72 |
} |
| 73 |
|
| 74 |
inline RegressionType StringToRegressionType(std::string s) { |
| 75 |
s = removeStringFormat(s); |
| 76 |
for ( RegressionType st = LEASTSQUARES; st < REGTYPE_LAST; ++st ) { |
| 77 |
if ( !s.compare(removeStringFormat(RegressionTypeToString(st))) ) { |
| 78 |
return st; |
| 79 |
} |
| 80 |
} |
| 81 |
return REGTYPE_LAST; |
| 82 |
} |
| 83 |
|
| 84 |
template<typename Real> |
| 85 |
class MomentOperator : public ProfiledClass<Real,std::string> { |
| 86 |
private: |
| 87 |
RegressionType regType_; |
| 88 |
bool homNoise_; |
| 89 |
Ptr<Noise<Real>> noise_; |
| 90 |
int matNum_; |
| 91 |
Ptr<Vector<Real>> sum_; |
| 92 |
Ptr<Krylov<Real>> krylov_; |
| 93 |
bool setSolver_; |
| 94 |
Ptr<LinearOperator<Real>> pOp_; |
| 95 |
|
| 96 |
class moment : public LinearOperator<Real> { |
| 97 |
private: |
| 98 |
const Ptr<Factors<Real>> factors_; |
| 99 |
const Ptr<LinearOperator<Real>> pOp_; |
| 100 |
Ptr<Vector<Real>> sum_; |
| 101 |
Ptr<const ProbabilityVector<Real>> p_; |
| 102 |
public: |
| 103 |
moment(const Ptr<Factors<Real>> &factors, const Ptr<LinearOperator<Real>> &pOp) |
| 104 |
: factors_(factors), pOp_(pOp) { |
| 105 |
sum_ = factors_->createParameterVector(true); |
| 106 |
} |
| 107 |
void setProbabilityVector(const ProbabilityVector<Real> &p) { |
| 108 |
p_ = makePtrFromRef(p); |
| 109 |
} |
| 110 |
void apply(Vector<Real> &Mx, const Vector<Real> &x, Real &tol) const { |
| 111 |
int nsamples = factors_->numMySamples(); |
| 112 |
sum_->zero(); |
| 113 |
for (int i = 0; i < nsamples; ++i) { |
| 114 |
factors_->applyProduct(Mx,x,i); |
| 115 |
sum_->axpy(p_->getProbability(i),Mx); |
| 116 |
} |
| 117 |
Mx.zero(); |
| 118 |
p_->getBatchManager()->sumAll(*sum_,Mx); |
| 119 |
if (pOp_ != nullPtr) { |
| 120 |
sum_->zero(); |
| 121 |
pOp_->apply(*sum_,x,tol); |
| 122 |
Mx.plus(*sum_); |
| 123 |
} |
| 124 |
} |
| 125 |
}; |
| 126 |
class precond : public LinearOperator<Real> { |
| 127 |
public: |
| 128 |
void apply(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const { |
| 129 |
Hv.set(v.dual()); |
| 130 |
} |
| 131 |
void applyInverse(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const { |
| 132 |
Hv.set(v.dual()); |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
Ptr<moment> M_; |
| 137 |
Ptr<precond> P_; |
| 138 |
|
| 139 |
protected: |
| 140 |
Ptr<Factors<Real>> factors_; |
| 141 |
|
| 142 |
/***************************************************************************/ |
| 143 |
/* Begin Accessor Functions */ |
| 144 |
/***************************************************************************/ |
| 145 |
Real get(const Vector<Real> &x, int i) const; |
| 146 |
void set(Vector<Real> &x, int i, Real val) const; |
| 147 |
std::vector<Real>& getLocalDesign(Vector<Real> &p) const; |
| 148 |
const std::vector<Real>& getConstLocalDesign(const Vector<Real> &p) const; |
| 149 |
void sumAll(const Vector<Real> &p, Real* input, Real* output, int size) const; |
| 150 |
void applyPerturbation(Vector<Real> &Px, const Vector<Real> &x) const; |
| 151 |
/***************************************************************************/ |
| 152 |
/* End Accessor Functions */ |
| 153 |
/***************************************************************************/ |
| 154 |
|
| 155 |
using ProfiledClass<Real,std::string>::startTimer; |
| 156 |
using ProfiledClass<Real,std::string>::stopTimer; |
| 157 |
|
| 158 |
public: |
| 159 |
virtual ~MomentOperator() {} |
| 160 |
MomentOperator(RegressionType regType = LEASTSQUARES, |
| 161 |
bool homNoise = true, |
| 162 |
const Ptr<Noise<Real>> &noise = nullPtr); |
| 163 |
|
| 164 |
virtual Ptr<MomentOperator<Real>> clone() const; |
| 165 |
void setMatrixNumber(int matNum); |
| 166 |
virtual void update(const Vector<Real> &p, UpdateType type, int iter = -1) {} |
| 167 |
virtual void setFactors(const Ptr<Factors<Real>> &factors); |
| 168 |
virtual void setPerturbation(const Ptr<LinearOperator<Real>> &pOp); |
| 169 |
|
| 170 |
// Compute M(p)x where M(p) = p_1 X_1 S_1 X_1' + ... + p_N X_N S_N X_N' |
| 171 |
virtual void apply(Vector<Real> &Mx, |
| 172 |
const Vector<Real> &x, |
| 173 |
const Vector<Real> &p); |
| 174 |
|
| 175 |
// Compute M(q)x where M(q) = q_1 X_1 S_1 X_1' + ... + q_N X_N S_N X_N' |
| 176 |
// This function is distinct from apply to allow the user to store M(p) |
| 177 |
virtual void applyDeriv(Vector<Real> &Mx, |
| 178 |
const Vector<Real> &x, |
| 179 |
const Vector<Real> &q); |
| 180 |
|
| 181 |
// Compute inv(M(p))x where M(p) = p_1 X_1 S_1 X_1' + ... + p_N X_N S_N X_N' |
| 182 |
virtual void applyInverse(Vector<Real> &Mx, |
| 183 |
const Vector<Real> &x, |
| 184 |
const Vector<Real> &p); |
| 185 |
|
| 186 |
virtual void applySampleMatrices(Vector<Real> &uXv, const Vector<Real> &u, const Vector<Real> &v); |
| 187 |
|
| 188 |
void setNoise(const Ptr<Noise<Real>> &noise, bool isHom = false); |
| 189 |
|
| 190 |
Real getNoise(int k) const; |
| 191 |
|
| 192 |
void getRegressionInfo(RegressionType ®Type, bool &homNoise, |
| 193 |
Ptr<Noise<Real>> &noise) const; |
| 194 |
|
| 195 |
virtual Real logDeterminant(const Vector<Real> &z); |
| 196 |
|
| 197 |
}; // class MomentOperator |
| 198 |
|
| 199 |
} // End OED Namespace |
| 200 |
} // End ROL Namespace |
| 201 |
|
| 202 |
#include "ROL_OED_MomentOperator_Def.hpp" |
| 203 |
|
| 204 |
#endif |