ROL
ROL_Vector_SimOpt.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_VECTOR_SIMOPT_HPP
45 #define ROL_VECTOR_SIMOPT_HPP
46 
47 #include "ROL_Vector.hpp"
48 
54 namespace ROL {
55 
56 template<class Real>
57 class Vector_SimOpt : public Vector<Real> {
58 private:
59  ROL::Ptr<Vector<Real> > vec1_;
60  ROL::Ptr<Vector<Real> > vec2_;
61  mutable ROL::Ptr<Vector<Real> > dual_vec1_;
62  mutable ROL::Ptr<Vector<Real> > dual_vec2_;
63  mutable ROL::Ptr<Vector_SimOpt<Real> > dual_vec_;
64 
65 public:
66  Vector_SimOpt( const ROL::Ptr<Vector<Real> > &vec1, const ROL::Ptr<Vector<Real> > &vec2 )
67  : vec1_(vec1), vec2_(vec2) {
68  dual_vec1_ = (vec1_->dual()).clone();
69  dual_vec2_ = (vec2_->dual()).clone();
70  }
71 
72  void plus( const Vector<Real> &x ) {
73  const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
74  dynamic_cast<const Vector<Real>&>(x));
75  vec1_->plus(*(xs.get_1()));
76  vec2_->plus(*(xs.get_2()));
77  }
78 
79  void scale( const Real alpha ) {
80  vec1_->scale(alpha);
81  vec2_->scale(alpha);
82  }
83 
84  void axpy( const Real alpha, const Vector<Real> &x ) {
85  const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
86  dynamic_cast<const Vector<Real>&>(x));
87  vec1_->axpy(alpha,*(xs.get_1()));
88  vec2_->axpy(alpha,*(xs.get_2()));
89  }
90 
91  Real dot( const Vector<Real> &x ) const {
92  const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
93  dynamic_cast<const Vector<Real>&>(x));
94  return vec1_->dot(*(xs.get_1())) + vec2_->dot(*(xs.get_2()));
95  }
96 
97  Real norm() const {
98  Real norm1 = vec1_->norm();
99  Real norm2 = vec2_->norm();
100  return sqrt( norm1*norm1 + norm2*norm2 );
101  }
102 
103  ROL::Ptr<Vector<Real> > clone() const {
104  return ROL::makePtr<Vector_SimOpt>(vec1_->clone(),vec2_->clone());
105  }
106 
107  const Vector<Real> & dual(void) const {
108  dual_vec1_->set(vec1_->dual());
109  dual_vec2_->set(vec2_->dual());
110  dual_vec_ = ROL::makePtr<Vector_SimOpt<Real>>(dual_vec1_,dual_vec2_);
111  return *dual_vec_;
112  }
113 
114  Real apply( const Vector<Real> &x ) const {
115  const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
116  dynamic_cast<const Vector<Real>&>(x));
117  return vec1_->apply(*(xs.get_1())) + vec2_->apply(*(xs.get_2()));
118  }
119 
120  ROL::Ptr<Vector<Real> > basis( const int i ) const {
121  int n1 = (vec1_)->dimension();
122  if ( i < n1 ) {
123  ROL::Ptr<Vector<Real> > e1 = (vec1_)->basis(i);
124  ROL::Ptr<Vector<Real> > e2 = (vec2_)->clone(); e2->zero();
125  ROL::Ptr<Vector<Real> > e = ROL::makePtr<Vector_SimOpt<Real>>(e1,e2);
126  return e;
127  }
128  else {
129  ROL::Ptr<Vector<Real> > e1 = (vec1_)->clone(); e1->zero();
130  ROL::Ptr<Vector<Real> > e2 = (vec2_)->basis(i-n1);
131  ROL::Ptr<Vector<Real> > e = ROL::makePtr<Vector_SimOpt<Real>>(e1,e2);
132  return e;
133  }
134  }
135 
136  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
137 
138  vec1_->applyUnary(f);
139  vec2_->applyUnary(f);
140 
141  }
142 
143  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
144  const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(x);
145 
146  vec1_->applyBinary(f,*xs.get_1());
147  vec2_->applyBinary(f,*xs.get_2());
148 
149  }
150 
151  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
152 
153  Real result = r.initialValue();
154  r.reduce(vec1_->reduce(r),result);
155  r.reduce(vec2_->reduce(r),result);
156  return result;
157  }
158 
159  void setScalar( const Real C ) {
160  vec1_->setScalar(C);
161  vec2_->setScalar(C);
162  }
163 
164  void randomize( const Real l=0.0, const Real u=1.0 ) {
165  vec1_->randomize(l,u);
166  vec2_->randomize(l,u);
167  }
168 
169 
170  int dimension() const {
171  return (vec1_)->dimension() + (vec2_)->dimension();
172  }
173 
174  ROL::Ptr<const Vector<Real> > get_1() const {
175  return vec1_;
176  }
177 
178  ROL::Ptr<const Vector<Real> > get_2() const {
179  return vec2_;
180  }
181 
182  ROL::Ptr<Vector<Real> > get_1() {
183  return vec1_;
184  }
185 
186  ROL::Ptr<Vector<Real> > get_2() {
187  return vec2_;
188  }
189 
190  void set_1(const Vector<Real>& vec) {
191  vec1_->set(vec);
192  }
193 
194  void set_2(const Vector<Real>& vec) {
195  vec2_->set(vec);
196  }
197 
198  void print( std::ostream &outStream ) const {
199  outStream << "Sim: ";
200  vec1_->print(outStream);
201  outStream << "Opt: ";
202  vec2_->print(outStream);
203  }
204 };
205 
206 template<template<typename> class V, typename Real, typename P = Ptr<Vector<Real>>>
207 inline typename std::enable_if<std::is_base_of<Vector<Real>,V<Real>>::value,P>::type
208 make_Vector_SimOpt( const Ptr<V<Real>>& vsim, const Ptr<V<Real>>& vopt ) {
209  return makePtr<Vector_SimOpt<Real>>(vsim,vopt);
210 }
211 
212 } // namespace ROL
213 
214 #endif
ROL::Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Real reduce(const Elementwise::ReductionOp< Real > &r) const
ROL::Ptr< const Vector< Real > > get_2() const
Defines the linear algebra or vector space interface for simulation-based optimization.
ROL::Ptr< Vector< Real > > vec2_
void set_1(const Vector< Real > &vec)
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Objective_SimOpt value
std::enable_if< std::is_base_of< Vector< Real >, V< Real > >::value, P >::type make_Vector_SimOpt(const Ptr< V< Real >> &vsim, const Ptr< V< Real >> &vopt)
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Real norm() const
Returns where .
void setScalar(const Real C)
Set where .
ROL::Ptr< Vector_SimOpt< Real > > dual_vec_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Vector< Real > V
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
ROL::Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
void print(std::ostream &outStream) const
void scale(const Real alpha)
Compute where .
ROL::Ptr< Vector< Real > > dual_vec2_
ROL::Ptr< Vector< Real > > vec1_
ROL::Ptr< Vector< Real > > dual_vec1_
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
ROL::Ptr< Vector< Real > > get_2()
void plus(const Vector< Real > &x)
Compute , where .
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Vector_SimOpt(const ROL::Ptr< Vector< Real > > &vec1, const ROL::Ptr< Vector< Real > > &vec2)
ROL::Ptr< Vector< Real > > get_1()
void set_2(const Vector< Real > &vec)
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
int dimension() const
Return dimension of the vector space.
ROL::Ptr< const Vector< Real > > get_1() const