ROL
ROL_Vector_SimOpt.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef ROL_VECTOR_SIMOPT_HPP
11#define ROL_VECTOR_SIMOPT_HPP
12
13#include "ROL_Vector.hpp"
14
20namespace ROL {
21
22template<class Real>
23class Vector_SimOpt : public Vector<Real> {
24private:
25 ROL::Ptr<Vector<Real> > vec1_;
26 ROL::Ptr<Vector<Real> > vec2_;
27 mutable ROL::Ptr<Vector<Real> > dual_vec1_;
28 mutable ROL::Ptr<Vector<Real> > dual_vec2_;
29 mutable ROL::Ptr<Vector_SimOpt<Real> > dual_vec_;
30
31public:
32 Vector_SimOpt( const ROL::Ptr<Vector<Real> > &vec1, const ROL::Ptr<Vector<Real> > &vec2 )
33 : vec1_(vec1), vec2_(vec2) {
34 dual_vec1_ = (vec1_->dual()).clone();
35 dual_vec2_ = (vec2_->dual()).clone();
36 }
37
38 void plus( const Vector<Real> &x ) {
39 const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
40 dynamic_cast<const Vector<Real>&>(x));
41 vec1_->plus(*(xs.get_1()));
42 vec2_->plus(*(xs.get_2()));
43 }
44
45 void scale( const Real alpha ) {
46 vec1_->scale(alpha);
47 vec2_->scale(alpha);
48 }
49
50 void axpy( const Real alpha, const Vector<Real> &x ) {
51 const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
52 dynamic_cast<const Vector<Real>&>(x));
53 vec1_->axpy(alpha,*(xs.get_1()));
54 vec2_->axpy(alpha,*(xs.get_2()));
55 }
56
57 Real dot( const Vector<Real> &x ) const {
58 const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
59 dynamic_cast<const Vector<Real>&>(x));
60 return vec1_->dot(*(xs.get_1())) + vec2_->dot(*(xs.get_2()));
61 }
62
63 Real norm() const {
64 Real norm1 = vec1_->norm();
65 Real norm2 = vec2_->norm();
66 return sqrt( norm1*norm1 + norm2*norm2 );
67 }
68
69 ROL::Ptr<Vector<Real> > clone() const {
70 return ROL::makePtr<Vector_SimOpt>(vec1_->clone(),vec2_->clone());
71 }
72
73 const Vector<Real> & dual(void) const {
74 dual_vec1_->set(vec1_->dual());
75 dual_vec2_->set(vec2_->dual());
76 dual_vec_ = ROL::makePtr<Vector_SimOpt<Real>>(dual_vec1_,dual_vec2_);
77 return *dual_vec_;
78 }
79
80 Real apply( const Vector<Real> &x ) const {
81 const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(
82 dynamic_cast<const Vector<Real>&>(x));
83 return vec1_->apply(*(xs.get_1())) + vec2_->apply(*(xs.get_2()));
84 }
85
86 ROL::Ptr<Vector<Real> > basis( const int i ) const {
87 int n1 = (vec1_)->dimension();
88 if ( i < n1 ) {
89 ROL::Ptr<Vector<Real> > e1 = (vec1_)->basis(i);
90 ROL::Ptr<Vector<Real> > e2 = (vec2_)->clone(); e2->zero();
91 ROL::Ptr<Vector<Real> > e = ROL::makePtr<Vector_SimOpt<Real>>(e1,e2);
92 return e;
93 }
94 else {
95 ROL::Ptr<Vector<Real> > e1 = (vec1_)->clone(); e1->zero();
96 ROL::Ptr<Vector<Real> > e2 = (vec2_)->basis(i-n1);
97 ROL::Ptr<Vector<Real> > e = ROL::makePtr<Vector_SimOpt<Real>>(e1,e2);
98 return e;
99 }
100 }
101
102 void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
103
104 vec1_->applyUnary(f);
105 vec2_->applyUnary(f);
106
107 }
108
109 void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
110 const Vector_SimOpt<Real> &xs = dynamic_cast<const Vector_SimOpt<Real>&>(x);
111
112 vec1_->applyBinary(f,*xs.get_1());
113 vec2_->applyBinary(f,*xs.get_2());
114
115 }
116
117 Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
118
119 Real result = r.initialValue();
120 r.reduce(vec1_->reduce(r),result);
121 r.reduce(vec2_->reduce(r),result);
122 return result;
123 }
124
125 void setScalar( const Real C ) {
126 vec1_->setScalar(C);
127 vec2_->setScalar(C);
128 }
129
130 void randomize( const Real l=0.0, const Real u=1.0 ) {
131 vec1_->randomize(l,u);
132 vec2_->randomize(l,u);
133 }
134
135
136 int dimension() const {
137 return (vec1_)->dimension() + (vec2_)->dimension();
138 }
139
140 ROL::Ptr<const Vector<Real> > get_1() const {
141 return vec1_;
142 }
143
144 ROL::Ptr<const Vector<Real> > get_2() const {
145 return vec2_;
146 }
147
148 ROL::Ptr<Vector<Real> > get_1() {
149 return vec1_;
150 }
151
152 ROL::Ptr<Vector<Real> > get_2() {
153 return vec2_;
154 }
155
156 void set_1(const Vector<Real>& vec) {
157 vec1_->set(vec);
158 }
159
160 void set_2(const Vector<Real>& vec) {
161 vec2_->set(vec);
162 }
163
164 void print( std::ostream &outStream ) const {
165 outStream << "Sim: ";
166 vec1_->print(outStream);
167 outStream << "Opt: ";
168 vec2_->print(outStream);
169 }
170};
171
172template<template<typename> class V, typename Real, typename P = Ptr<Vector<Real>>>
173inline typename std::enable_if<std::is_base_of<Vector<Real>,V<Real>>::value,P>::type
174make_Vector_SimOpt( const Ptr<V<Real>>& vsim, const Ptr<V<Real>>& vopt ) {
175 return makePtr<Vector_SimOpt<Real>>(vsim,vopt);
176}
177
178} // namespace ROL
179
180#endif
Vector< Real > V
Defines the linear algebra or vector space interface for simulation-based optimization.
ROL::Ptr< const Vector< Real > > get_2() const
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
ROL::Ptr< const Vector< Real > > get_1() const
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
ROL::Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
void setScalar(const Real C)
Set where .
ROL::Ptr< Vector< Real > > dual_vec1_
Vector_SimOpt(const ROL::Ptr< Vector< Real > > &vec1, const ROL::Ptr< Vector< Real > > &vec2)
void print(std::ostream &outStream) const
ROL::Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
ROL::Ptr< Vector< Real > > get_2()
Real norm() const
Returns where .
void scale(const Real alpha)
Compute where .
void set_1(const Vector< Real > &vec)
int dimension() const
Return dimension of the vector space.
ROL::Ptr< Vector< Real > > dual_vec2_
Real reduce(const Elementwise::ReductionOp< Real > &r) const
ROL::Ptr< Vector< Real > > vec2_
void plus(const Vector< Real > &x)
Compute , where .
Real dot(const Vector< Real > &x) const
Compute where .
ROL::Ptr< Vector_SimOpt< Real > > dual_vec_
void set_2(const Vector< Real > &vec)
ROL::Ptr< Vector< Real > > get_1()
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
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< Vector< Real > > vec1_
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
Defines the linear algebra or vector space interface.
ROL::Objective_SerialSimOpt Objective_SimOpt value(const V &u, const V &z, Real &tol) override
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)