ROL
ROL_ProbabilityVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_PROBABILITYVECTOR_H
45 #define ROL_PROBABILITYVECTOR_H
46 
47 #include "ROL_BatchStdVector.hpp"
48 
54 namespace ROL {
55 
56 template <class Real>
58 
59 template <class Real>
61 
62 template <class Real>
63 class ProbabilityVector : public BatchStdVector<Real> {
65 public:
66  ProbabilityVector(const ROL::Ptr<std::vector<Real>> &vec,
67  const ROL::Ptr<BatchManager<Real>> &bman)
68  : BatchStdVector<Real>(vec,bman) {}
69 
70  const Real getProbability(const int i) const {
71  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
72  int numMySamples = static_cast<int>(yval.size());
73  ROL_TEST_FOR_EXCEPTION((i < 0 || i >= numMySamples), std::invalid_argument,
74  ">>> ERROR (ROL::ProbabilityVector): index out of bounds in getProbability!");
75  return yval[i];
76  }
77 
78  void setProbability(const int i, const Real wt) {
79  std::vector<Real> &yval = *(StdVector<Real>::getVector());
80  int numMySamples = static_cast<int>(yval.size());
81  ROL_TEST_FOR_EXCEPTION((i < 0 || i >= numMySamples), std::invalid_argument,
82  ">>> ERROR (ROL::ProbabilityVector): index out of bounds in setProbability!");
83  yval[i] = wt;
84  }
85 
86  int getNumMyAtoms(void) const {
87  int numMySamples = static_cast<int>(StdVector<Real>::getVector()->size());
88  return numMySamples;
89  }
90 
91  ROL::Ptr<Vector<Real> > clone(void) const {
92  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
93  uint numMySamples = yval.size();
94  return ROL::makePtr<ProbabilityVector>(
95  ROL::makePtr<std::vector<Real>>(numMySamples),BatchStdVector<Real>::getBatchManager());
96  }
97 };
98 
99 template<class Real>
100 class PrimalProbabilityVector : public ProbabilityVector<Real> {
102 private:
103  ROL::Ptr<std::vector<Real> > scale_;
104  mutable ROL::Ptr<DualProbabilityVector<Real> > dual_vec_;
105  mutable bool isDualInitialized_;
106 
107 public:
108  PrimalProbabilityVector(const ROL::Ptr<std::vector<Real> > &vec,
109  const ROL::Ptr<BatchManager<Real> > &bman,
110  const ROL::Ptr<std::vector<Real> > &scale)
111  : ProbabilityVector<Real>(vec,bman), scale_(scale),
112  isDualInitialized_(false) {}
113 
114  Real dot(const Vector<Real> &x) const {
115  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
116  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
117  uint numMySamples = static_cast<uint>(yval.size());
118  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
119  "Error: Vectors must have the same dimension." );
120  Real val(0), sum_val(0);
121  for (uint i = 0; i < numMySamples; i++) {
122  val += xval[i] * (*scale_)[i] * yval[i];
123  }
124  // Global sum
125  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
126  return sum_val;
127  }
128 
129  ROL::Ptr<Vector<Real> > clone(void) const {
130  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
131  return ROL::makePtr<PrimalProbabilityVector>(
132  ROL::makePtr<std::vector<Real>>(numMySamples),
134  }
135 
136  const Vector<Real> & dual(void) const {
137  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
138  if ( !isDualInitialized_ ) {
139  dual_vec_ = ROL::makePtr<DualProbabilityVector<Real>>(
140  ROL::makePtr<std::vector<Real>>(numMySamples),
142  isDualInitialized_ = true;
143  }
144  for (uint i = 0; i < numMySamples; ++i) {
145  (*(dual_vec_->getVector()))[i]
146  = (*scale_)[i]*(*StdVector<Real>::getVector())[i];
147  }
148  return *dual_vec_;
149  }
150 };
151 
152 template<class Real>
153 class DualProbabilityVector : public ProbabilityVector<Real> {
155 private:
156  ROL::Ptr<std::vector<Real> > scale_;
157  mutable ROL::Ptr<PrimalProbabilityVector<Real> > primal_vec_;
158  mutable bool isDualInitialized_;
159 
160 public:
161  DualProbabilityVector(const ROL::Ptr<std::vector<Real> > &vec,
162  const ROL::Ptr<BatchManager<Real> > &bman,
163  const ROL::Ptr<std::vector<Real> > &scale)
164  : ProbabilityVector<Real>(vec,bman), scale_(scale),
165  isDualInitialized_(false) {}
166 
167  Real dot(const Vector<Real> &x) const {
168  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
169  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
170  uint numMySamples = static_cast<uint>(yval.size());
171  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
172  "Error: Vectors must have the same dimension." );
173  Real val(0), sum_val(0);
174  for (uint i = 0; i < numMySamples; i++) {
175  val += xval[i] * yval[i] / (*scale_)[i];
176  }
177  // Global sum
178  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
179  return sum_val;
180  }
181 
182  ROL::Ptr<Vector<Real> > clone(void) const {
183  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
184  return ROL::makePtr<DualProbabilityVector>(
185  ROL::makePtr<std::vector<Real>>(numMySamples),
187  }
188 
189  const Vector<Real> & dual(void) const {
190  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
191  if ( !isDualInitialized_ ) {
192  primal_vec_ = ROL::makePtr<PrimalProbabilityVector<Real>>(
193  ROL::makePtr<std::vector<Real>>(numMySamples),
195  isDualInitialized_ = true;
196  }
197  for (uint i = 0; i < numMySamples; i++) {
198  (*(primal_vec_->getVector()))[i]
199  = (*StdVector<Real>::getVector())[i]/(*scale_)[i];
200  }
201  return *primal_vec_;
202  }
203 };
204 
205 } // namespace ROL
206 
207 #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:80
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_