/usr/include/trilinos/ROL_MeanDeviation.hpp is in libtrilinos-rol-dev 12.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | // @HEADER
// ************************************************************************
//
// Rapid Optimization Library (ROL) Package
// Copyright (2014) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact lead developers:
// Drew Kouri (dpkouri@sandia.gov) and
// Denis Ridzal (dridzal@sandia.gov)
//
// ************************************************************************
// @HEADER
#ifndef ROL_MEANDEVIATION_HPP
#define ROL_MEANDEVIATION_HPP
#include "ROL_RiskMeasure.hpp"
#include "ROL_PositiveFunction.hpp"
#include "ROL_PlusFunction.hpp"
#include "ROL_AbsoluteValue.hpp"
#include "Teuchos_ParameterList.hpp"
#include "Teuchos_Array.hpp"
/** @ingroup risk_group
\class ROL::MeanDeviation
\brief Provides an interface for the mean plus a sum of arbitrary order
deviations.
The mean plus deviations risk measure is
\f[
\mathcal{R}(X) = \mathbb{E}[X]
+ \sum_{k=1}^n c_k \mathbb{E}[\wp(X-\mathbb{E}[X])^{p_k}]^{1/p_k}
\f]
where \f$\wp:\mathbb{R}\to[0,\infty)\f$ is either the absolute value
or \f$(x)_+ = \max\{0,x\}\f$, \f$c_k > 0\f$ and \f$p_k\in\mathbb{N}\f$.
In general, \f$\mathcal{R}\f$ is law-invariant, but not coherent.
In the specific case that \f$\wp(x) = (x)_+\f$ and \f$c_k\in[0,1]\f$,
\f$\mathcal{R}\f$ is coherent. On the other hand,
the common mean-plus-standard-deviation risk measure (i.e.,
\f$\wp(x) = |x|\f$, \f$n=1\f$ and \f$p_1 = 2\f$) is not coherent since
it violates monotonicity.
When using derivative-based optimization, the user can
provide a smooth approximation of \f$(\cdot)_+\f$ using the
ROL::PositiveFunction class.
*/
namespace ROL {
template<class Real>
class MeanDeviation : public RiskMeasure<Real> {
typedef typename std::vector<Real>::size_type uint;
private:
Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
Teuchos::RCP<Vector<Real> > dualVector1_;
Teuchos::RCP<Vector<Real> > dualVector2_;
std::vector<Real> order_;
std::vector<Real> coeff_;
uint NumMoments_;
std::vector<Real> weights_;
std::vector<Real> value_storage_;
std::vector<Real> gradvec_storage_;
std::vector<Teuchos::RCP<Vector<Real> > > gradient_storage_;
std::vector<Teuchos::RCP<Vector<Real> > > hessvec_storage_;
std::vector<Real> dev0_;
std::vector<Real> dev1_;
std::vector<Real> dev2_;
std::vector<Real> dev3_;
std::vector<Real> des0_;
std::vector<Real> des1_;
std::vector<Real> des2_;
std::vector<Real> des3_;
std::vector<Real> devp_;
std::vector<Real> gvp1_;
std::vector<Real> gvp2_;
std::vector<Real> gvp3_;
std::vector<Real> gvs1_;
std::vector<Real> gvs2_;
std::vector<Real> gvs3_;
bool firstReset_;
void initialize(void) {
dev0_.clear(); dev1_.clear(); dev2_.clear(); dev3_.clear();
des0_.clear(); des1_.clear(); des2_.clear(); des3_.clear();
devp_.clear();
gvp1_.clear(); gvp2_.clear(); gvp3_.clear();
gvs1_.clear(); gvs2_.clear(); gvs3_.clear();
dev0_.resize(NumMoments_); dev1_.resize(NumMoments_);
dev2_.resize(NumMoments_); dev3_.resize(NumMoments_);
des0_.resize(NumMoments_); des1_.resize(NumMoments_);
des2_.resize(NumMoments_); des3_.resize(NumMoments_);
devp_.resize(NumMoments_);
gvp1_.resize(NumMoments_); gvp2_.resize(NumMoments_);
gvp3_.resize(NumMoments_);
gvs1_.resize(NumMoments_); gvs2_.resize(NumMoments_);
gvs3_.resize(NumMoments_);
}
void clear(void) {
Real zero(0);
dev0_.assign(NumMoments_,zero); dev1_.assign(NumMoments_,zero);
dev2_.assign(NumMoments_,zero); dev3_.assign(NumMoments_,zero);
des0_.assign(NumMoments_,zero); des1_.assign(NumMoments_,zero);
des2_.assign(NumMoments_,zero); des3_.assign(NumMoments_,zero);
devp_.assign(NumMoments_,zero);
gvp1_.assign(NumMoments_,zero); gvp2_.assign(NumMoments_,zero);
gvp3_.assign(NumMoments_,zero);
gvs1_.assign(NumMoments_,zero); gvs2_.assign(NumMoments_,zero);
gvs3_.assign(NumMoments_,zero);
value_storage_.clear();
gradient_storage_.clear();
gradvec_storage_.clear();
hessvec_storage_.clear();
weights_.clear();
}
void checkInputs(void) const {
int oSize = order_.size(), cSize = coeff_.size();
TEUCHOS_TEST_FOR_EXCEPTION((oSize!=cSize),std::invalid_argument,
">>> ERROR (ROL::MeanDeviation): Order and coefficient arrays have different sizes!");
Real zero(0), two(2);
for (int i = 0; i < oSize; i++) {
TEUCHOS_TEST_FOR_EXCEPTION((order_[i] < two), std::invalid_argument,
">>> ERROR (ROL::MeanDeviation): Element of order array out of range!");
TEUCHOS_TEST_FOR_EXCEPTION((coeff_[i] < zero), std::invalid_argument,
">>> ERROR (ROL::MeanDeviation): Element of coefficient array out of range!");
}
TEUCHOS_TEST_FOR_EXCEPTION(positiveFunction_ == Teuchos::null, std::invalid_argument,
">>> ERROR (ROL::MeanDeviation): PositiveFunction pointer is null!");
}
public:
/** \brief Constructor.
@param[in] order is the deviation order
@param[in] coeff is the weight for deviation term
@param[in] pf is the plus function or an approximation
This constructor produces a mean plus deviation risk measure
with a single deviation.
*/
MeanDeviation( const Real order, const Real coeff,
const Teuchos::RCP<PositiveFunction<Real> > &pf )
: RiskMeasure<Real>(), positiveFunction_(pf), firstReset_(true) {
order_.clear(); order_.push_back(order);
coeff_.clear(); coeff_.push_back(coeff);
checkInputs();
NumMoments_ = order_.size();
initialize();
}
/** \brief Constructor.
@param[in] order is a vector of deviation orders
@param[in] coeff is a vector of weights for the deviation terms
@param[in] pf is the plus function or an approximation
This constructor produces a mean plus deviation risk measure
with an arbitrary number of deviations.
*/
MeanDeviation( const std::vector<Real> &order,
const std::vector<Real> &coeff,
const Teuchos::RCP<PositiveFunction<Real> > &pf )
: RiskMeasure<Real>(), positiveFunction_(pf), firstReset_(true) {
order_.clear(); coeff_.clear();
for ( uint i = 0; i < order.size(); i++ ) {
order_.push_back(order[i]);
}
for ( uint i = 0; i < coeff.size(); i++ ) {
coeff_.push_back(coeff[i]);
}
checkInputs();
NumMoments_ = order_.size();
initialize();
}
/** \brief Constructor.
@param[in] parlist is a parameter list specifying inputs
parlist should contain sublists "SOL"->"Risk Measure"->"Mean Plus Deviation" and
within the "Mean Plus Deviation" sublist should have the following parameters
\li "Orders" (array of unsigned integers)
\li "Coefficients" (array of positive scalars)
\li "Deviation Type" (eighter "Upper" or "Absolute")
\li A sublist for positive function information.
*/
MeanDeviation( Teuchos::ParameterList &parlist )
: RiskMeasure<Real>(), firstReset_(true) {
Teuchos::ParameterList &list
= parlist.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Deviation");
// Get data from parameter list
Teuchos::Array<Real> order
= Teuchos::getArrayFromStringParameter<double>(list,"Orders");
order_ = order.toVector();
Teuchos::Array<Real> coeff
= Teuchos::getArrayFromStringParameter<double>(list,"Coefficients");
coeff_ = coeff.toVector();
// Build (approximate) positive function
std::string type = list.get<std::string>("Deviation Type");
if ( type == "Upper" ) {
positiveFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
}
else if ( type == "Absolute" ) {
positiveFunction_ = Teuchos::rcp(new AbsoluteValue<Real>(list));
}
else {
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
">>> (ROL::MeanDeviation): Deviation type is not recoginized!");
}
// Check inputs
checkInputs();
NumMoments_ = order.size();
initialize();
}
void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
RiskMeasure<Real>::reset(x0,x);
if ( firstReset_ ) {
dualVector1_ = (x0->dual()).clone();
dualVector2_ = (x0->dual()).clone();
firstReset_ = false;
}
dualVector1_->zero(); dualVector2_->zero();
clear();
}
void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
reset(x0,x);
v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
}
void update(const Real val, const Real weight) {
RiskMeasure<Real>::val_ += weight * val;
value_storage_.push_back(val);
weights_.push_back(weight);
}
void update(const Real val, const Vector<Real> &g, const Real weight) {
RiskMeasure<Real>::val_ += weight * val;
RiskMeasure<Real>::g_->axpy(weight,g);
value_storage_.push_back(val);
gradient_storage_.push_back(g.clone());
typename std::vector<Teuchos::RCP<Vector<Real> > >::iterator it = gradient_storage_.end();
it--;
(*it)->set(g);
weights_.push_back(weight);
}
void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
const Real weight) {
RiskMeasure<Real>::val_ += weight * val;
RiskMeasure<Real>::gv_ += weight * gv;
RiskMeasure<Real>::g_->axpy(weight,g);
RiskMeasure<Real>::hv_->axpy(weight,hv);
value_storage_.push_back(val);
gradient_storage_.push_back(g.clone());
typename std::vector<Teuchos::RCP<Vector<Real> > >::iterator it = gradient_storage_.end();
it--;
(*it)->set(g);
gradvec_storage_.push_back(gv);
hessvec_storage_.push_back(hv.clone());
it = hessvec_storage_.end();
it--;
(*it)->set(hv);
weights_.push_back(weight);
}
Real getValue(SampleGenerator<Real> &sampler) {
// Compute expected value
Real val = RiskMeasure<Real>::val_, ev(0);
sampler.sumAll(&val,&ev,1);
// Compute deviation
Real diff(0), pf0(0), dev(0), one(1);
for ( uint i = 0; i < weights_.size(); i++ ) {
diff = value_storage_[i]-ev;
pf0 = positiveFunction_->evaluate(diff,0);
for ( uint p = 0; p < NumMoments_; p++ ) {
dev0_[p] += std::pow(pf0,order_[p]) * weights_[i];
}
}
sampler.sumAll(&dev0_[0],&des0_[0],NumMoments_);
for ( uint p = 0; p < NumMoments_; p++ ) {
dev += coeff_[p]*std::pow(des0_[p],one/order_[p]);
}
// Return mean plus deviation
return ev + dev;
}
void getGradient(Vector<Real> &g, SampleGenerator<Real> &sampler) {
// Compute expected value
Real val = RiskMeasure<Real>::val_, ev(0);
sampler.sumAll(&val,&ev,1);
// Compute deviation
Real diff(0), pf0(0), pf1(0), c(0), one(1), zero(0);
for ( uint i = 0; i < weights_.size(); i++ ) {
diff = value_storage_[i]-ev;
pf0 = positiveFunction_->evaluate(diff,0);
pf1 = positiveFunction_->evaluate(diff,1);
for ( uint p = 0; p < NumMoments_; p++ ) {
dev0_[p] += weights_[i] * std::pow(pf0,order_[p]);
dev1_[p] += weights_[i] * std::pow(pf0,order_[p]-one) * pf1;
}
}
sampler.sumAll(&dev0_[0],&des0_[0],NumMoments_);
sampler.sumAll(&dev1_[0],&des1_[0],NumMoments_);
for ( uint p = 0; p < NumMoments_; p++ ) {
dev0_[p] = std::pow(des0_[p],one-one/order_[p]);
}
// Compute derivative
for ( uint i = 0; i < weights_.size(); i++ ) {
c = zero;
diff = value_storage_[i]-ev;
pf0 = positiveFunction_->evaluate(diff,0);
pf1 = positiveFunction_->evaluate(diff,1);
for ( uint p = 0; p < NumMoments_; p++ ) {
if ( dev0_[p] > zero ) {
c += coeff_[p]/dev0_[p] * (std::pow(pf0,order_[p]-one)*pf1 - des1_[p]);
}
}
dualVector1_->axpy(weights_[i]*c,*(gradient_storage_[i]));
}
dualVector1_->plus(*(RiskMeasure<Real>::g_));
sampler.sumAll(*dualVector1_,*dualVector2_);
// Set RiskVector
(Teuchos::dyn_cast<RiskVector<Real> >(g)).setVector(*dualVector2_);
}
void getHessVec(Vector<Real> &hv, SampleGenerator<Real> &sampler) {
// Compute expected value
std::vector<Real> myval(2), val(2);
myval[0] = RiskMeasure<Real>::val_;
myval[1] = RiskMeasure<Real>::gv_;
sampler.sumAll(&myval[0],&val[0],2);
Real ev = val[0], egv = val[1];
// Compute deviation
Real diff(0), pf0(0), pf1(0), pf2(0), zero(0), one(1), two(2);
Real cg(0), ch(0), diff1(0), diff2(0), diff3(0);
for ( uint i = 0; i < weights_.size(); i++ ) {
diff = value_storage_[i]-ev;
pf0 = positiveFunction_->evaluate(diff,0);
pf1 = positiveFunction_->evaluate(diff,1);
pf2 = positiveFunction_->evaluate(diff,2);
for ( uint p = 0; p < NumMoments_; p++ ) {
dev0_[p] += weights_[i] * std::pow(pf0,order_[p]);
dev1_[p] += weights_[i] * std::pow(pf0,order_[p]-one) * pf1;
dev2_[p] += weights_[i] * std::pow(pf0,order_[p]-two) * pf1 * pf1;
dev3_[p] += weights_[i] * std::pow(pf0,order_[p]-one) * pf2;
}
}
sampler.sumAll(&dev0_[0],&des0_[0],NumMoments_);
sampler.sumAll(&dev1_[0],&des1_[0],NumMoments_);
sampler.sumAll(&dev2_[0],&des2_[0],NumMoments_);
sampler.sumAll(&dev3_[0],&des3_[0],NumMoments_);
for ( uint p = 0; p < NumMoments_; p++ ) {
devp_[p] = std::pow(des0_[p],two-one/order_[p]);
dev0_[p] = std::pow(des0_[p],one-one/order_[p]);
}
for ( uint i = 0; i < value_storage_.size(); i++ ) {
diff = value_storage_[i]-ev;
pf0 = positiveFunction_->evaluate(diff,0);
pf1 = positiveFunction_->evaluate(diff,1);
pf2 = positiveFunction_->evaluate(diff,2);
for ( uint p = 0; p < NumMoments_; p++ ) {
gvp1_[p] += weights_[i] * (std::pow(pf0,order_[p]-one)*pf1-des1_[p]) *
(gradvec_storage_[i] - egv);
gvp2_[p] += weights_[i] * (std::pow(pf0,order_[p]-two)*pf1*pf1-des2_[p]) *
(gradvec_storage_[i] - egv);
gvp3_[p] += weights_[i] * (std::pow(pf0,order_[p]-one)*pf2-des3_[p]) *
(gradvec_storage_[i] - egv);
}
}
sampler.sumAll(&gvp1_[0],&gvs1_[0],NumMoments_);
sampler.sumAll(&gvp2_[0],&gvs2_[0],NumMoments_);
sampler.sumAll(&gvp3_[0],&gvs3_[0],NumMoments_);
// Compute derivative
for ( uint i = 0; i < weights_.size(); i++ ) {
cg = one;
ch = zero;
diff = value_storage_[i]-ev;
pf0 = positiveFunction_->evaluate(diff,0);
pf1 = positiveFunction_->evaluate(diff,1);
pf2 = positiveFunction_->evaluate(diff,2);
for ( uint p = 0; p < NumMoments_; p++ ) {
if ( dev0_[p] > zero ) {
diff1 = std::pow(pf0,order_[p]-one)*pf1-des1_[p];
diff2 = std::pow(pf0,order_[p]-two)*pf1*pf1*(gradvec_storage_[i]-egv)-gvs2_[p];
diff3 = std::pow(pf0,order_[p]-one)*pf2*(gradvec_storage_[i]-egv)-gvs3_[p];
cg += coeff_[p]*diff1/dev0_[p];
ch += coeff_[p]*(((order_[p]-one)*diff2+diff3)/dev0_[p] -
(order_[p]-one)*gvs1_[p]*diff1/devp_[p]);
}
}
dualVector1_->axpy(weights_[i]*ch,*(gradient_storage_[i]));
dualVector1_->axpy(weights_[i]*cg,*(hessvec_storage_[i]));
}
sampler.sumAll(*dualVector1_,*dualVector2_);
// Fill RiskVector
(Teuchos::dyn_cast<RiskVector<Real> >(hv)).setVector(*dualVector2_);
}
};
}
#endif
|