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_StdVector.hpp"
48 #include "ROL_BatchManager.hpp"
49 
55 namespace ROL {
56 
57 template <class Real>
59 
60 template <class Real>
62 
63 template <class Real>
64 class ProbabilityVector : public StdVector<Real> {
65  typedef typename std::vector<Real>::size_type uint;
66 private:
68 
69 public:
70  ProbabilityVector(const Teuchos::RCP<std::vector<Real> > &vec)
71  : StdVector<Real>(vec), numMySamples_(vec->size()) {}
72 
73  const Real getProbability(const int i) const {
74  if ( i >= 0 && i < (int)numMySamples_ ) {
75  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
76  return yval[i];
77  }
78  else {
79  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
80  ">>> ERROR (ROL::ProbabilityVector): index out of bounds in getProbability!");
81  }
82  return 0;
83  }
84 
85  void setProbability(const int i, const Real wt) {
86  if ( i >= 0 && i < (int)numMySamples_ ) {
87  std::vector<Real> &yval = *(StdVector<Real>::getVector());
88  yval[i] = wt;
89  }
90  else {
91  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
92  ">>> ERROR (ROL::ProbabilityVector): index out of bounds in setProbability!");
93  }
94  }
95 
96  int getNumMyAtoms(void) const {
97  return (int)numMySamples_;
98  }
99 };
100 
101 template<class Real>
102 class PrimalProbabilityVector : public ProbabilityVector<Real> {
103  typedef typename std::vector<Real>::size_type uint;
104 private:
106  Teuchos::RCP<std::vector<Real> > scale_;
107  Teuchos::RCP<BatchManager<Real> > bman_;
108 
109  mutable Teuchos::RCP<DualProbabilityVector<Real> > dual_vec_;
110 
111 public:
112  PrimalProbabilityVector(const Teuchos::RCP<std::vector<Real> > &vec,
113  const Teuchos::RCP<std::vector<Real> > &scale,
114  const Teuchos::RCP<BatchManager<Real> > &bman)
115  : ProbabilityVector<Real>(vec), numMySamples_(vec->size()),
116  scale_(scale), bman_(bman) {}
117 
118  Real dot(const Vector<Real> &x) const {
119  const PrimalProbabilityVector<Real> &ex = Teuchos::dyn_cast<const PrimalProbabilityVector>(x);
120  const std::vector<Real> &xval = *(ex.getVector());
121  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
122  Real val = 0;
123  for (uint i = 0; i < numMySamples_; i++) {
124  val += xval[i] * (*scale_)[i] * yval[i];
125  }
126  // Global sum
127  Real sum_val = 0;
128  bman_->sumAll(&val,&sum_val,1);
129  return sum_val;
130  }
131 
132  Teuchos::RCP<Vector<Real> > clone(void) const {
133  return Teuchos::rcp(new PrimalProbabilityVector(
134  Teuchos::rcp(new std::vector<Real>(numMySamples_)),scale_,bman_));
135  }
136 
137  const Vector<Real> & dual(void) const {
138  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
139  std::vector<Real> tmp(yval);
140  for (uint i = 0; i < numMySamples_; i++) {
141  tmp[i] *= (*scale_)[i];
142  }
143  dual_vec_ = Teuchos::rcp(new DualProbabilityVector<Real>(
144  Teuchos::rcp(new std::vector<Real>(tmp)),scale_,bman_));
145  return *dual_vec_;
146  }
147 
148  int dimension(void) const {
149  Real dim = (Real)StdVector<Real>::dimension();
150  Real sum = 0.;
151  bman_->sumAll(&dim,&sum,1);
152  return (int)sum;
153  }
154 
155  Real reduce(const Elementwise::ReductionOp<Real> &r) const {
156  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
157  Real result = r.initialValue();
158  for (uint i = 0; i < numMySamples_; i++) {
159  r.reduce(yval[i],result);
160  }
161  // Global sum
162  Real sum = 0.;
163  bman_->reduceAll(&result,&sum,r);
164  return sum;
165  }
166 };
167 
168 template<class Real>
169 class DualProbabilityVector : public ProbabilityVector<Real> {
170  typedef typename std::vector<Real>::size_type uint;
171 private:
173  Teuchos::RCP<std::vector<Real> > scale_;
174  Teuchos::RCP<BatchManager<Real> > bman_;
175 
176  mutable Teuchos::RCP<PrimalProbabilityVector<Real> > dual_vec_;
177 
178 public:
179  DualProbabilityVector(const Teuchos::RCP<std::vector<Real> > &vec,
180  const Teuchos::RCP<std::vector<Real> > &scale,
181  const Teuchos::RCP<BatchManager<Real> > &bman)
182  : ProbabilityVector<Real>(vec), numMySamples_(vec->size()),
183  scale_(scale), bman_(bman) {}
184 
185  Real dot(const Vector<Real> &x) const {
186  const DualProbabilityVector<Real> &ex = Teuchos::dyn_cast<const DualProbabilityVector>(x);
187  const std::vector<Real> &xval = *(ex.getVector());
188  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
189  Real val = 0;
190  for (uint i = 0; i < numMySamples_; i++) {
191  val += xval[i] * yval[i] / (*scale_)[i];
192  }
193  // Global sum
194  Real sum_val = 0;
195  bman_->sumAll(&val,&sum_val,1);
196  return sum_val;
197  }
198 
199  Teuchos::RCP<Vector<Real> > clone(void) const {
200  return Teuchos::rcp(new DualProbabilityVector(
201  Teuchos::rcp(new std::vector<Real>(numMySamples_)),scale_,bman_));
202  }
203 
204  const Vector<Real> & dual(void) const {
205  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
206  std::vector<Real> tmp(yval);
207  for (uint i = 0; i < numMySamples_; i++) {
208  tmp[i] /= (*scale_)[i];
209  }
210  dual_vec_ = Teuchos::rcp(new PrimalProbabilityVector<Real>(
211  Teuchos::rcp(new std::vector<Real>(tmp)),scale_,bman_));
212  return *dual_vec_;
213  }
214 
215  int dimension(void) const {
216  Real dim = (Real)StdVector<Real>::dimension();
217  Real sum = 0.;
218  bman_->sumAll(&dim,&sum,1);
219  return (int)sum;
220  }
221 
222  Real reduce(const Elementwise::ReductionOp<Real> &r) const {
223  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
224  Real result = r.initialValue();
225  for (uint i = 0; i < numMySamples_; i++) {
226  r.reduce(yval[i],result);
227  }
228  // Global sum
229  Real sum = 0.;
230  bman_->reduceAll(&result,&sum,r);
231  return sum;
232  }
233 };
234 
235 } // namespace ROL
236 
237 #endif
Teuchos::RCP< DualProbabilityVector< Real > > dual_vec_
Teuchos::RCP< std::vector< Real > > scale_
std::vector< Real >::size_type uint
void scale(const Real alpha)
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Teuchos::RCP< const std::vector< Element > > getVector() const
Teuchos::RCP< BatchManager< Real > > bman_
const Real getProbability(const int i) const
DualProbabilityVector(const Teuchos::RCP< std::vector< Real > > &vec, const Teuchos::RCP< std::vector< Real > > &scale, const Teuchos::RCP< BatchManager< Real > > &bman)
Teuchos::RCP< 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...
Teuchos::RCP< PrimalProbabilityVector< Real > > dual_vec_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real dot(const Vector< Real > &x) const
Compute where .
std::vector< Real >::size_type uint
Provides the std::vector implementation of the ROL::Vector interface.
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Teuchos::RCP< BatchManager< Real > > bman_
void setProbability(const int i, const Real wt)
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Provides the std::vector implementation of the ROL::Vector interface.
PrimalProbabilityVector(const Teuchos::RCP< std::vector< Real > > &vec, const Teuchos::RCP< std::vector< Real > > &scale, const Teuchos::RCP< BatchManager< Real > > &bman)
int dimension(void) const
Return dimension of the vector space.
std::vector< Real >::size_type uint
int dimension(void) const
Return dimension of the vector space.
Teuchos::RCP< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
Real dot(const Vector< Real > &x) const
Compute where .
ProbabilityVector(const Teuchos::RCP< std::vector< Real > > &vec)
Teuchos::RCP< std::vector< Real > > scale_