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 
92 template<class Real>
93 class PrimalProbabilityVector : public ProbabilityVector<Real> {
95 private:
96  ROL::Ptr<std::vector<Real> > scale_;
97  mutable ROL::Ptr<DualProbabilityVector<Real> > dual_vec_;
98  mutable bool isDualInitialized_;
99 
100 public:
101  PrimalProbabilityVector(const ROL::Ptr<std::vector<Real> > &vec,
102  const ROL::Ptr<BatchManager<Real> > &bman,
103  const ROL::Ptr<std::vector<Real> > &scale)
104  : ProbabilityVector<Real>(vec,bman), scale_(scale),
105  isDualInitialized_(false) {}
106 
107  Real dot(const Vector<Real> &x) const {
108  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
109  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
110  uint numMySamples = static_cast<uint>(yval.size());
111  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
112  "Error: Vectors must have the same dimension." );
113  Real val(0), sum_val(0);
114  for (uint i = 0; i < numMySamples; i++) {
115  val += xval[i] * (*scale_)[i] * yval[i];
116  }
117  // Global sum
118  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
119  return sum_val;
120  }
121 
122  ROL::Ptr<Vector<Real> > clone(void) const {
123  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
124  return ROL::makePtr<PrimalProbabilityVector>(
125  ROL::makePtr<std::vector<Real>>(numMySamples),
127  }
128 
129  const Vector<Real> & dual(void) const {
130  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
131  if ( !isDualInitialized_ ) {
132  dual_vec_ = ROL::makePtr<DualProbabilityVector<Real>>(
133  ROL::makePtr<std::vector<Real>>(numMySamples),
135  isDualInitialized_ = true;
136  }
137  for (uint i = 0; i < numMySamples; ++i) {
138  (*(dual_vec_->getVector()))[i]
139  = (*scale_)[i]*(*StdVector<Real>::getVector())[i];
140  }
141  return *dual_vec_;
142  }
143 };
144 
145 template<class Real>
146 class DualProbabilityVector : public ProbabilityVector<Real> {
148 private:
149  ROL::Ptr<std::vector<Real> > scale_;
150  mutable ROL::Ptr<PrimalProbabilityVector<Real> > primal_vec_;
151  mutable bool isDualInitialized_;
152 
153 public:
154  DualProbabilityVector(const ROL::Ptr<std::vector<Real> > &vec,
155  const ROL::Ptr<BatchManager<Real> > &bman,
156  const ROL::Ptr<std::vector<Real> > &scale)
157  : ProbabilityVector<Real>(vec,bman), scale_(scale),
158  isDualInitialized_(false) {}
159 
160  Real dot(const Vector<Real> &x) const {
161  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
162  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
163  uint numMySamples = static_cast<uint>(yval.size());
164  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
165  "Error: Vectors must have the same dimension." );
166  Real val(0), sum_val(0);
167  for (uint i = 0; i < numMySamples; i++) {
168  val += xval[i] * yval[i] / (*scale_)[i];
169  }
170  // Global sum
171  BatchStdVector<Real>::getBatchManager()->sumAll(&val,&sum_val,1);
172  return sum_val;
173  }
174 
175  ROL::Ptr<Vector<Real> > clone(void) const {
176  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
177  return ROL::makePtr<DualProbabilityVector>(
178  ROL::makePtr<std::vector<Real>>(numMySamples),
180  }
181 
182  const Vector<Real> & dual(void) const {
183  uint numMySamples = static_cast<uint>(StdVector<Real>::getVector()->size());
184  if ( !isDualInitialized_ ) {
185  primal_vec_ = ROL::makePtr<PrimalProbabilityVector<Real>>(
186  ROL::makePtr<std::vector<Real>>(numMySamples),
188  isDualInitialized_ = true;
189  }
190  for (uint i = 0; i < numMySamples; i++) {
191  (*(primal_vec_->getVector()))[i]
192  = (*StdVector<Real>::getVector())[i]/(*scale_)[i];
193  }
194  return *primal_vec_;
195  }
196 };
197 
198 } // namespace ROL
199 
200 #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_
const ROL::Ptr< BatchManager< Real > > getBatchManager(void) const
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...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
ProbabilityVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman)
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.
std::vector< Real >::size_type uint
std::vector< Real >::size_type uint
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Ptr< std::vector< Real > > scale_