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
48
54namespace ROL {
55
56template <class Real>
57class PrimalProbabilityVector;
58
59template <class Real>
60class DualProbabilityVector;
61
62template <class Real>
63class ProbabilityVector : public BatchStdVector<Real> {
64 typedef typename std::vector<Real>::size_type uint;
65public:
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
99template<class Real>
101 typedef typename std::vector<Real>::size_type uint;
102private:
103 ROL::Ptr<std::vector<Real> > scale_;
104 mutable ROL::Ptr<DualProbabilityVector<Real> > dual_vec_;
105 mutable bool isDualInitialized_;
106
107public:
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
152template<class Real>
154 typedef typename std::vector<Real>::size_type uint;
155private:
156 ROL::Ptr<std::vector<Real> > scale_;
157 mutable ROL::Ptr<PrimalProbabilityVector<Real> > primal_vec_;
158 mutable bool isDualInitialized_;
159
160public:
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
Provides the std::vector implementation of the ROL::Vector interface.
const Ptr< BatchManager< Real > > getBatchManager(void) const
ROL::Ptr< std::vector< Real > > scale_
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
std::vector< Real >::size_type uint
ROL::Ptr< PrimalProbabilityVector< Real > > primal_vec_
Real dot(const Vector< Real > &x) const
Compute where .
DualProbabilityVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const ROL::Ptr< std::vector< Real > > &scale)
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Ptr< DualProbabilityVector< Real > > dual_vec_
std::vector< Real >::size_type uint
PrimalProbabilityVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman, const ROL::Ptr< std::vector< Real > > &scale)
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
ROL::Ptr< std::vector< Real > > scale_
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
Provides the std::vector implementation of the ROL::Vector interface.
ProbabilityVector(const ROL::Ptr< std::vector< Real > > &vec, const ROL::Ptr< BatchManager< Real > > &bman)
void setProbability(const int i, const Real wt)
ROL::Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
std::vector< Real >::size_type uint
const Real getProbability(const int i) const
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void scale(const Real alpha)
Compute where .
Ptr< const std::vector< Element > > getVector() const
Defines the linear algebra or vector space interface.