ROL
ROL_BVP.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 
49 #ifndef USE_HESSVEC
50 #define USE_HESSVEC 1
51 #endif
52 
53 #ifndef ROL_BVP_HPP
54 #define ROL_BVP_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_Objective.hpp"
58 #include "ROL_BoundConstraint.hpp"
59 
60 namespace ROL {
61 namespace ZOO {
62 
65  template<class Real>
66  class Objective_BVP : public Objective<Real> {
67 
68  typedef std::vector<Real> vector;
69  typedef Vector<Real> V;
70  typedef StdVector<Real> SV;
71 
72  typedef typename vector::size_type uint;
73 
74  private:
75  uint dim_;
76 
77  Teuchos::RCP<const vector> getVector( const V& x ) {
78  using Teuchos::dyn_cast;
79  return dyn_cast<const SV>(x).getVector();
80  }
81 
82  Teuchos::RCP<vector> getVector( V& x ) {
83  using Teuchos::dyn_cast;
84  return dyn_cast<SV>(x).getVector();
85  }
86 
87 
88  public:
89  Objective_BVP(void) : dim_(20) {}
90 
91  Real value( const Vector<Real> &x, Real &tol ) {
92 
93  using Teuchos::RCP;
94  RCP<const vector> ex = getVector(x);
95  Real val = 0.0;
96  Real f = 0.0;
97  Real h = 1.0/((Real)(dim_) + 1.0);
98  for ( uint i = 0; i < dim_; i++ ) {
99  f = 2.0*(*ex)[i] + h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,3.0)/2.0;
100  if ( i < (dim_-1) ) { f -= (*ex)[i+1]; }
101  if ( i > 0 ) { f -= (*ex)[i-1]; }
102  val += f*f;
103  }
104  return val;
105  }
106 
107  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
108 
109  using Teuchos::RCP;
110  RCP<const vector> ex = getVector(x);
111  RCP<vector> eg = getVector(g);
112  g.zero();
113  Real h = 1.0/((Real)(dim_) + 1.0);
114  vector f(dim_,0.0);
115 
116  for ( uint i = 0; i < dim_; i++ ) {
117  f[i] = 2.0*(*ex)[i] + h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,3.0)/2.0;
118  if ( i < (dim_-1) ) { f[i] -= (*ex)[i+1]; }
119  if ( i > 0) { f[i] -= (*ex)[i-1]; }
120  }
121  Real df = 0.0;
122  for ( uint i = 0; i < dim_; i++ ) {
123  df = (2.0 + 3.0*h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,2.0)/2.0)*f[i];
124  if ( i < (dim_-1) ) { df -= f[i+1]; }
125  if ( i > 0 ) { df -= f[i-1]; }
126  (*eg)[i] += 2.0*df;
127  }
128  }
129 #if USE_HESSVEC
130  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
131 
132  using Teuchos::RCP;
133  RCP<const vector> ex = getVector(x);
134  RCP<const vector> ev = getVector(v);
135  RCP<vector> ehv = getVector(hv);
136 
137  hv.zero();
138  Real h = 1.0/((Real)(dim_) + 1.0);
139  Real f = 0.0, df = 0.0, dfn = 0.0, hf = 0.0;
140  for ( uint i = 0; i < dim_; i++ ) {
141  f = 2.0*(*ex)[i] + h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,3.0)/2.0;
142  df = 2.0 + 3.0/2.0 * h*h * std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,2.0);
143  hf = 3.0 * h*h * ((*ex)[i] + (Real)(i+1)*h + 1.0);
144  if ( i < (dim_-2) ) {
145  (*ehv)[i] += 2.0*(*ev)[i+2];
146  }
147  if ( i < (dim_-1) ) {
148  f -= (*ex)[i+1];
149  dfn = 2.0 + 3.0/2.0 * h*h * std::pow((*ex)[i+1] + (Real)(i+2)*h + 1.0,2.0);
150  (*ehv)[i] -= 2.0*(df + dfn)*(*ev)[i+1];
151  (*ehv)[i] += 2.0*(*ev)[i];
152  }
153  if ( i > 0 ) {
154  f -= (*ex)[i-1];
155  dfn = 2.0 + 3.0/2.0 * h*h * std::pow((*ex)[i-1] + (Real)(i)*h + 1.0,2.0);
156  (*ehv)[i] -= 2.0*(df + dfn)*(*ev)[i-1];
157  (*ehv)[i] += 2.0*(*ev)[i];
158  }
159  if ( i > 1 ) {
160  (*ehv)[i] += 2.0*(*ev)[i-2];
161  }
162  (*ehv)[i] += 2.0*(hf*f + df*df)*(*ev)[i];
163  }
164  }
165 #endif
166  };
167 
168  template<class Real>
169  void getBVP( Teuchos::RCP<Objective<Real> > &obj, Teuchos::RCP<BoundConstraint<Real> > &con,
170  Vector<Real> &x0, Vector<Real> &x ) {
171 
172  typedef std::vector<Real> vector;
173  typedef Vector<Real> V;
174  typedef StdVector<Real> SV;
175 
176  typedef typename vector::size_type uint;
177 
178  using Teuchos::RCP;
179  using Teuchos::rcp;
180  using Teuchos::dyn_cast;
181 
182  // Cast Initial Guess and Solution Vectors
183  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
184  RCP<vector> xp = dyn_cast<SV>(x).getVector();
185 
186  uint n = xp->size();
187  // Resize Vectors
188  n = 20;
189  x0p->resize(n);
190  xp->resize(n);
191  // Instantiate Objective Function
192  obj = Teuchos::rcp( new Objective_BVP<Real> );
193 
194  // Instantiate BoundConstraint
195 
196  RCP<vector> l_rcp = rcp( new vector() );
197  RCP<vector> u_rcp = rcp( new vector() );
198 
199  RCP<V> l = rcp( new SV(l_rcp) );
200  RCP<V> u = rcp( new SV(u_rcp) );
201 
202  std::vector<Real> val(n,0.0);
203  val[0] = 0.1*0.2321;
204  val[1] = -0.1*0.4520;
205  val[2] = -0.1*0.6588;
206  val[3] = -0.1*0.8514;
207  val[4] = -0.1*1.0288;
208  val[5] = -0.1*1.1985;
209  val[6] = -0.1*1.3322;
210  val[7] = -0.1*1.4553;
211  val[8] = -0.1*1.5571;
212  val[9] = -0.1*1.6354;
213  val[10] = -0.1*1.6881;
214  val[11] = -0.1*1.7127;
215  val[12] = -0.1*1.7060;
216  val[13] = -0.1*1.6650;
217  val[14] = -0.1*1.5856;
218  val[15] = -0.1*1.4636;
219  val[16] = -0.1*1.2938;
220  val[17] = -0.1*1.0702;
221  val[18] = -0.1*0.7858;
222  val[19] = -0.1*0.4323;
223  for ( uint i = 0; i < n; i++ ) {
224  if ( i%2 == 0 ) {
225  l_rcp->push_back(std::max(-0.2*(Real)(n),val[i]+0.1));
226  u_rcp->push_back(std::min( 0.2*(Real)(n),val[i]+1.1));
227  }
228  else {
229  l_rcp->push_back(-0.2*(Real)(n));
230  u_rcp->push_back( 0.2*(Real)(n));
231  }
232  }
233 
234  con = rcp( new BoundConstraint<Real>(l,u) );
235  // Get Initial Guess
236  Real h = 1.0/((Real)n + 1.0);
237  for ( uint i = 0; i < n; i++ ) {
238  (*x0p)[i] = (Real)(i+1)*h*((Real)(i+1)*h - 1.0);
239  }
240  con->project(x0);
241  // Get Solution
242  (*xp)[0] = 1.2321000000000001e-01;
243  (*xp)[1] = 2.1743122909175336e-01;
244  (*xp)[2] = 2.8625218549543746e-01;
245  (*xp)[3] = 3.3309751851140840e-01;
246  (*xp)[4] = 3.6117201714254760e-01;
247  (*xp)[5] = 3.7342787212179440e-01;
248  (*xp)[6] = 3.7255212003706123e-01;
249  (*xp)[7] = 3.6096984201471016e-01;
250  (*xp)[8] = 3.4085861052124522e-01;
251  (*xp)[9] = 3.1417024791439530e-01;
252  (*xp)[10] = 2.8265678244892922e-01;
253  (*xp)[11] = 2.4789833165179542e-01;
254  (*xp)[12] = 2.1133139591375166e-01;
255  (*xp)[13] = 1.7427666644258599e-01;
256  (*xp)[14] = 1.3796594229036069e-01;
257  (*xp)[15] = 1.0356813245768780e-01;
258  (*xp)[16] = 7.2214621084083663e-02;
259  (*xp)[17] = 4.5024529114833199e-02;
260  (*xp)[18] = 2.3130648161534966e-02;
261  (*xp)[19] = 7.7070870882527927e-03;
262  }
263 
264 
265 }// End ZOO Namespace
266 }// End ROL Namespace
267 
268 #endif
Provides the interface to evaluate objective functions.
vector::size_type uint
Definition: ROL_BVP.hpp:72
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_BVP.hpp:107
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_BVP.hpp:91
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_BVP.hpp:82
StdVector< Real > SV
Definition: ROL_BVP.hpp:70
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_BVP.hpp:77
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Vector< Real > V
Definition: ROL_BVP.hpp:69
Provides the std::vector implementation of the ROL::Vector interface.
void getBVP(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_BVP.hpp:169
The discrete boundary value problem.
Definition: ROL_BVP.hpp:66
Provides the interface to apply upper and lower bound constraints.
std::vector< Real > vector
Definition: ROL_BVP.hpp:68