ROL
ROL_PD_RandVarFunctional.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_PD_RANDVARFUNCTIONAL_HPP
11 #define ROL_PD_RANDVARFUNCTIONAL_HPP
12 
14 
15 namespace ROL {
16 
17 template<class Real>
20 private:
21  Real pen_;
22  int update_;
23  bool setData_;
24 
25  Ptr<ScalarController<Real>> values_;
26  Ptr<ScalarController<Real>> multipliers_;
27  Ptr<ScalarController<Real>> multipliers_new_;
28 
29 protected:
30  // Set value data at current parameter
31  void setValue(const Real val, const std::vector<Real> &pt) {
32  values_->set(val, pt);
33  }
34 
35  // Get multiplier at current parameter
36  void getMultiplier(Real &lam, const std::vector<Real> &pt) const {
37  multipliers_->get(lam, pt);
38  }
39 
40  void setMultiplier(Real &lam, const std::vector<Real> &pt) {
41  multipliers_new_->set(lam, pt);
42  }
43 
44  // Get penalty parameter
45  Real getPenaltyParameter(void) const {
46  return pen_;
47  }
48 
49  // Smooth plus function approximation
50  Real ppf(const Real x, const Real t, const Real r, const int deriv = 0) const {
51  const Real zero(0), half(0.5), one(1), arg(r*x+t);
52  Real val(0);
53  if ( arg < zero ) {
54  val = (deriv==0 ? -half*t*t/r : zero);
55  }
56  else if ( zero <= arg && arg <= one ) {
57  val = (deriv==0 ? half*r*x*x+t*x
58  : (deriv==1 ? arg : r));
59  }
60  else {
61  val = (deriv==0 ? (arg-half*(t*t+one))/r
62  : (deriv==1 ? one : zero));
63  }
64  return val;
65  }
66 
67 public:
69  : RandVarFunctional<Real>(), pen_(1.0), update_(0), setData_(true) {
70  values_ = makePtr<ScalarController<Real>>();
71  multipliers_ = makePtr<ScalarController<Real>>();
72  multipliers_new_ = makePtr<ScalarController<Real>>();
73  }
74 
75  void setData(SampleGenerator<Real> &sampler, const Real pen, const Real lam = 0.0) {
76  if (setData_) {
77  pen_ = pen;
78  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
79  multipliers_->set(lam, sampler.getMyPoint(i));
80  }
81  setData_ = false;
82  }
83  }
84 
85  virtual Real computeDual(SampleGenerator<Real> &sampler) {
86  const Real zero(0), one(1);
87  Real val(0), lold(0), lnew(0), mdiff(0), gdiff(0);
88  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
89  values_->get(val, sampler.getMyPoint(i));
90  multipliers_->get(lold, sampler.getMyPoint(i));
91  if (update_ == 0) {
92  //lnew = ppf(val, lold, pen_, 1);
93  lnew = std::min(one, std::max(zero, pen_*val+lold));
94  }
95  else {
96  lnew = (val < zero ? zero : one);
97  }
98  mdiff += sampler.getMyWeight(i) * std::pow(lnew-lold,2);
99  multipliers_new_->set(lnew, sampler.getMyPoint(i));
100  }
101  sampler.sumAll(&mdiff,&gdiff,1);
102  gdiff = std::sqrt(gdiff);
103  return gdiff;
104  }
105 
107  Real lam(0);
108  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
109  multipliers_new_->get(lam, sampler.getMyPoint(i));
110  multipliers_->set(lam, sampler.getMyPoint(i));
111  }
112  }
113 
114  void updatePenalty(const Real pen) {
115  pen_ = pen;
116  }
117 
118  virtual void setStorage(const Ptr<ScalarController<Real>> &value_storage,
119  const Ptr<VectorController<Real>> &gradient_storage) {
120  RandVarFunctional<Real>::setStorage(value_storage,gradient_storage);
121  }
122 
123  virtual void setHessVecStorage(const Ptr<ScalarController<Real>> &gradvec_storage,
124  const Ptr<VectorController<Real>> &hessvec_storage) {
125  RandVarFunctional<Real>::setHessVecStorage(gradvec_storage,hessvec_storage);
126  }
127 
128  virtual void initialize(const Vector<Real> &x) {
130  }
131 };
132 
133 }
134 
135 #endif
std::vector< Real >::size_type uint
typename PV< Real >::size_type size_type
virtual void setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
Ptr< ScalarController< Real > > multipliers_
Real ppf(const Real x, const Real t, const Real r, const int deriv=0) const
void setMultiplier(Real &lam, const std::vector< Real > &pt)
virtual std::vector< Real > getMyPoint(const int i) const
virtual Real getMyWeight(const int i) const
virtual void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual int numMySamples(void) const
void updateDual(SampleGenerator< Real > &sampler)
void sumAll(Real *input, Real *output, int dim) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Ptr< ScalarController< Real > > multipliers_new_
virtual Real computeDual(SampleGenerator< Real > &sampler)
virtual void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
void setData(SampleGenerator< Real > &sampler, const Real pen, const Real lam=0.0)
void getMultiplier(Real &lam, const std::vector< Real > &pt) const
Ptr< ScalarController< Real > > values_
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
void setValue(const Real val, const std::vector< Real > &pt)
virtual void setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.