ROL
ROL_ProbabilityVector.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_PROBABILITYVECTOR_H
11 #define ROL_PROBABILITYVECTOR_H
12 
13 #include "ROL_BatchStdVector.hpp"
14 
20 namespace ROL {
21 
22 template <class Real>
24 
25 template <class Real>
27 
28 template <class Real>
29 class ProbabilityVector : public BatchStdVector<Real> {
31 public:
32  ProbabilityVector(const ROL::Ptr<std::vector<Real>> &vec,
33  const ROL::Ptr<BatchManager<Real>> &bman)
34  : BatchStdVector<Real>(vec,bman) {}
35 
36  const Real getProbability(const int i) const {
37  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
38  int numMySamples = static_cast<int>(yval.size());
39  ROL_TEST_FOR_EXCEPTION((i < 0 || i >= numMySamples), std::invalid_argument,
40  ">>> ERROR (ROL::ProbabilityVector): index out of bounds in getProbability!");
41  return yval[i];
42  }
43 
44  void setProbability(const int i, const Real wt) {
45  std::vector<Real> &yval = *(StdVector<Real>::getVector());
46  int numMySamples = static_cast<int>(yval.size());
47  ROL_TEST_FOR_EXCEPTION((i < 0 || i >= numMySamples), std::invalid_argument,
48  ">>> ERROR (ROL::ProbabilityVector): index out of bounds in setProbability!");
49  yval[i] = wt;
50  }
51 
52  int getNumMyAtoms(void) const {
53  int numMySamples = static_cast<int>(StdVector<Real>::getVector()->size());
54  return numMySamples;
55  }
56 
57  ROL::Ptr<Vector<Real> > clone(void) const {
58  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
59  uint numMySamples = yval.size();
60  return ROL::makePtr<ProbabilityVector>(
61  ROL::makePtr<std::vector<Real>>(numMySamples),BatchStdVector<Real>::getBatchManager());
62  }
63 };
64 
65 template<class Real>
66 class PrimalProbabilityVector : public ProbabilityVector<Real> {
68 private:
69  ROL::Ptr<std::vector<Real> > scale_;
70  mutable ROL::Ptr<DualProbabilityVector<Real> > dual_vec_;
71  mutable bool isDualInitialized_;
72 
73 public:
74  PrimalProbabilityVector(const ROL::Ptr<std::vector<Real> > &vec,
75  const ROL::Ptr<BatchManager<Real> > &bman,
76  const ROL::Ptr<std::vector<Real> > &scale)
77  : ProbabilityVector<Real>(vec,bman), scale_(scale),
78  isDualInitialized_(false) {}
79 
80  Real dot(const Vector<Real> &x) const {
81  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
82  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
83  uint numMySamples = static_cast<uint>(yval.size());
84  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
85  "Error: Vectors must have the same dimension." );
86  Real val(0), sum_val(0);
87  for (uint i = 0; i < numMySamples; i++) {
88  val += xval[i] * (*scale_)[i] * yval[i];
89  }
90  // Global sum
91  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
92  return sum_val;
93  }
94 
95  ROL::Ptr<Vector<Real> > clone(void) const {
96  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
97  return ROL::makePtr<PrimalProbabilityVector>(
98  ROL::makePtr<std::vector<Real>>(numMySamples),
100  }
101 
102  const Vector<Real> & dual(void) const {
103  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
104  if ( !isDualInitialized_ ) {
105  dual_vec_ = ROL::makePtr<DualProbabilityVector<Real>>(
106  ROL::makePtr<std::vector<Real>>(numMySamples),
108  isDualInitialized_ = true;
109  }
110  for (uint i = 0; i < numMySamples; ++i) {
111  (*(dual_vec_->getVector()))[i]
112  = (*scale_)[i]*(*StdVector<Real>::getVector())[i];
113  }
114  return *dual_vec_;
115  }
116 };
117 
118 template<class Real>
119 class DualProbabilityVector : public ProbabilityVector<Real> {
121 private:
122  ROL::Ptr<std::vector<Real> > scale_;
123  mutable ROL::Ptr<PrimalProbabilityVector<Real> > primal_vec_;
124  mutable bool isDualInitialized_;
125 
126 public:
127  DualProbabilityVector(const ROL::Ptr<std::vector<Real> > &vec,
128  const ROL::Ptr<BatchManager<Real> > &bman,
129  const ROL::Ptr<std::vector<Real> > &scale)
130  : ProbabilityVector<Real>(vec,bman), scale_(scale),
131  isDualInitialized_(false) {}
132 
133  Real dot(const Vector<Real> &x) const {
134  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
135  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
136  uint numMySamples = static_cast<uint>(yval.size());
137  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
138  "Error: Vectors must have the same dimension." );
139  Real val(0), sum_val(0);
140  for (uint i = 0; i < numMySamples; i++) {
141  val += xval[i] * yval[i] / (*scale_)[i];
142  }
143  // Global sum
144  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
145  return sum_val;
146  }
147 
148  ROL::Ptr<Vector<Real> > clone(void) const {
149  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
150  return ROL::makePtr<DualProbabilityVector>(
151  ROL::makePtr<std::vector<Real>>(numMySamples),
153  }
154 
155  const Vector<Real> & dual(void) const {
156  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
157  if ( !isDualInitialized_ ) {
158  primal_vec_ = ROL::makePtr<PrimalProbabilityVector<Real>>(
159  ROL::makePtr<std::vector<Real>>(numMySamples),
161  isDualInitialized_ = true;
162  }
163  for (uint i = 0; i < numMySamples; i++) {
164  (*(primal_vec_->getVector()))[i]
165  = (*StdVector<Real>::getVector())[i]/(*scale_)[i];
166  }
167  return *primal_vec_;
168  }
169 };
170 
171 } // namespace ROL
172 
173 #endif
std::vector< Real >::size_type uint
typename PV< Real >::size_type size_type
void scale(const Real alpha)
Compute where .
PrimalProbabilityVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const ROL::Ptr< std::vector< Real > > &scale)
const Real getProbability(const int i) const
ROL::Ptr< DualProbabilityVector< Real > > dual_vec_
Provides the std::vector implementation of the ROL::Vector interface.
Ptr< const std::vector< Element > > getVector() const
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
ProbabilityVector(const ROL::Ptr< std::vector< Real >> &vec, const ROL::Ptr< BatchManager< Real >> &bman)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Ptr< std::vector< Real > > scale_
std::vector< Real >::size_type uint
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
DualProbabilityVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const ROL::Ptr< std::vector< Real > > &scale)
void setProbability(const int i, const Real wt)
ROL::Ptr< PrimalProbabilityVector< Real > > primal_vec_
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
Provides the std::vector implementation of the ROL::Vector interface.
const Ptr< BatchManager< Real > > getBatchManager(void) const
std::vector< Real >::size_type uint
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
std::vector< Real >::size_type uint
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Ptr< std::vector< Real > > scale_