ROL
ROL_BVP.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 
15 #ifndef USE_HESSVEC
16 #define USE_HESSVEC 1
17 #endif
18 
19 #ifndef ROL_BVP_HPP
20 #define ROL_BVP_HPP
21 
22 #include "ROL_ScaledStdVector.hpp"
23 #include "ROL_TestProblem.hpp"
24 #include "ROL_Bounds.hpp"
25 
26 namespace ROL {
27 namespace ZOO {
28 
31  template<class Real>
32  class Objective_BVP : public Objective<Real> {
33 
35 
36  private:
38 
39  public:
40  Objective_BVP(void) : dim_(20) {}
41 
42  Real value( const Vector<Real> &x, Real &tol ) {
43  Ptr<const std::vector<Real> > ex
44  = dynamic_cast<const StdVector<Real>&>(x).getVector();
45 
46  Real val = 0.0;
47  Real f = 0.0;
48  Real h = 1.0/((Real)(dim_) + 1.0);
49  for ( uint i = 0; i < dim_; i++ ) {
50  f = 2.0*(*ex)[i] + h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,3.0)/2.0;
51  if ( i < (dim_-1) ) { f -= (*ex)[i+1]; }
52  if ( i > 0 ) { f -= (*ex)[i-1]; }
53  val += f*f;
54  }
55  return val;
56  }
57 
58  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
59  Ptr<std::vector<Real> > eg
60  = dynamic_cast<StdVector<Real>&>(g).getVector();
61  Ptr<const std::vector<Real> > ex
62  = dynamic_cast<const StdVector<Real>&>(x).getVector();
63 
64  g.zero();
65  Real h = 1.0/((Real)(dim_) + 1.0);
66  std::vector<Real> f(dim_,0.0);
67 
68  for ( uint i = 0; i < dim_; i++ ) {
69  f[i] = 2.0*(*ex)[i] + h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,3.0)/2.0;
70  if ( i < (dim_-1) ) { f[i] -= (*ex)[i+1]; }
71  if ( i > 0) { f[i] -= (*ex)[i-1]; }
72  }
73  Real df = 0.0;
74  for ( uint i = 0; i < dim_; i++ ) {
75  df = (2.0 + 3.0*h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,2.0)/2.0)*f[i];
76  if ( i < (dim_-1) ) { df -= f[i+1]; }
77  if ( i > 0 ) { df -= f[i-1]; }
78  (*eg)[i] += 2.0*df;
79  }
80  }
81 #if USE_HESSVEC
82  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
83  Ptr<std::vector<Real> > ehv
84  = dynamic_cast<StdVector<Real>&>(hv).getVector();
85  Ptr<const std::vector<Real> > ev
86  = dynamic_cast<const StdVector<Real>&>(v).getVector();
87  Ptr<const std::vector<Real> > ex
88  = dynamic_cast<const StdVector<Real>&>(x).getVector();
89 
90  hv.zero();
91  Real h = 1.0/((Real)(dim_) + 1.0);
92  Real f = 0.0, df = 0.0, dfn = 0.0, hf = 0.0;
93  for ( uint i = 0; i < dim_; i++ ) {
94  f = 2.0*(*ex)[i] + h*h*std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,3.0)/2.0;
95  df = 2.0 + 3.0/2.0 * h*h * std::pow((*ex)[i] + (Real)(i+1)*h + 1.0,2.0);
96  hf = 3.0 * h*h * ((*ex)[i] + (Real)(i+1)*h + 1.0);
97  if ( i < (dim_-2) ) {
98  (*ehv)[i] += 2.0*(*ev)[i+2];
99  }
100  if ( i < (dim_-1) ) {
101  f -= (*ex)[i+1];
102  dfn = 2.0 + 3.0/2.0 * h*h * std::pow((*ex)[i+1] + (Real)(i+2)*h + 1.0,2.0);
103  (*ehv)[i] -= 2.0*(df + dfn)*(*ev)[i+1];
104  (*ehv)[i] += 2.0*(*ev)[i];
105  }
106  if ( i > 0 ) {
107  f -= (*ex)[i-1];
108  dfn = 2.0 + 3.0/2.0 * h*h * std::pow((*ex)[i-1] + (Real)(i)*h + 1.0,2.0);
109  (*ehv)[i] -= 2.0*(df + dfn)*(*ev)[i-1];
110  (*ehv)[i] += 2.0*(*ev)[i];
111  }
112  if ( i > 1 ) {
113  (*ehv)[i] += 2.0*(*ev)[i-2];
114  }
115  (*ehv)[i] += 2.0*(hf*f + df*df)*(*ev)[i];
116  }
117  }
118 #endif
119  };
120 
121  template<class Real>
122  class getBVP : public TestProblem<Real> {
123  private:
124  int n_;
125  Ptr<std::vector<Real> > scale_;
126 
127  public:
128  getBVP(void) {
129  n_ = 20;
130  scale_ = makePtr<std::vector<Real>>(n_,0.0);
131  (*scale_)[0] = 1.e2;
132  (*scale_)[1] = 1.e2;
133  (*scale_)[2] = 1.e2;
134  (*scale_)[3] = 1.e2;
135  (*scale_)[4] = 1.e2;
136  (*scale_)[5] = 1.e2;
137  (*scale_)[6] = 1.e2;
138  (*scale_)[7] = 1.e2;
139  (*scale_)[8] = 1.e2;
140  (*scale_)[9] = 1.e2;
141  (*scale_)[10] = 1.e2;
142  (*scale_)[11] = 1.e2;
143  (*scale_)[12] = 1.e2;
144  (*scale_)[13] = 1.e2;
145  (*scale_)[14] = 1.e2;
146  (*scale_)[15] = 1.e2;
147  (*scale_)[16] = 1.e4;
148  (*scale_)[17] = 1.e4;
149  (*scale_)[18] = 1.e4;
150  (*scale_)[19] = 1.e6;
151  }
152 
153  Ptr<Objective<Real>> getObjective(void) const {
154  return makePtr<Objective_BVP<Real>>();
155  }
156 
157  Ptr<Vector<Real>> getInitialGuess(void) const {
158  // Get Initial Guess
159  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n_,0.0);
160  Real h = 1.0/((Real)n_ + 1.0);
161  for ( int i = 0; i < n_; i++ ) {
162  (*x0p)[i] = (Real)(i+1)*h*((Real)(i+1)*h - 1.0);
163  }
164  return makePtr<PrimalScaledStdVector<Real>>(x0p,scale_);
165  }
166 
167  Ptr<Vector<Real>> getSolution(const int i = 0) const {
168  // Get Solution
169  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n_,0.0);
170  (*xp)[0] = 1.2321000000000001e-01;
171  (*xp)[1] = 2.1743122909175336e-01;
172  (*xp)[2] = 2.8625218549543746e-01;
173  (*xp)[3] = 3.3309751851140840e-01;
174  (*xp)[4] = 3.6117201714254760e-01;
175  (*xp)[5] = 3.7342787212179440e-01;
176  (*xp)[6] = 3.7255212003706123e-01;
177  (*xp)[7] = 3.6096984201471016e-01;
178  (*xp)[8] = 3.4085861052124522e-01;
179  (*xp)[9] = 3.1417024791439530e-01;
180  (*xp)[10] = 2.8265678244892922e-01;
181  (*xp)[11] = 2.4789833165179542e-01;
182  (*xp)[12] = 2.1133139591375166e-01;
183  (*xp)[13] = 1.7427666644258599e-01;
184  (*xp)[14] = 1.3796594229036069e-01;
185  (*xp)[15] = 1.0356813245768780e-01;
186  (*xp)[16] = 7.2214621084083663e-02;
187  (*xp)[17] = 4.5024529114833199e-02;
188  (*xp)[18] = 2.3130648161534966e-02;
189  (*xp)[19] = 7.7070870882527927e-03;
190  return makePtr<PrimalScaledStdVector<Real>>(xp,scale_);
191  }
192 
193  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
194  // Instantiate BoundConstraint
195  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>();
196  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>();
197  std::vector<Real> val(n_,0.0);
198  val[0] = 0.1*0.2321;
199  val[1] = -0.1*0.4520;
200  val[2] = -0.1*0.6588;
201  val[3] = -0.1*0.8514;
202  val[4] = -0.1*1.0288;
203  val[5] = -0.1*1.1985;
204  val[6] = -0.1*1.3322;
205  val[7] = -0.1*1.4553;
206  val[8] = -0.1*1.5571;
207  val[9] = -0.1*1.6354;
208  val[10] = -0.1*1.6881;
209  val[11] = -0.1*1.7127;
210  val[12] = -0.1*1.7060;
211  val[13] = -0.1*1.6650;
212  val[14] = -0.1*1.5856;
213  val[15] = -0.1*1.4636;
214  val[16] = -0.1*1.2938;
215  val[17] = -0.1*1.0702;
216  val[18] = -0.1*0.7858;
217  val[19] = -0.1*0.4323;
218  for ( int i = 0; i < n_; i++ ) {
219  if ( i%2 == 0 ) {
220  lp->push_back(std::max(-0.2*(Real)(n_),val[i]+0.1));
221  up->push_back(std::min( 0.2*(Real)(n_),val[i]+1.1));
222  }
223  else {
224  lp->push_back(-0.2*(Real)(n_));
225  up->push_back( 0.2*(Real)(n_));
226  }
227  }
228  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
229  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
230  return makePtr<Bounds<Real>>(l,u);
231  }
232  };
233 
234 }// End ZOO Namespace
235 }// End ROL Namespace
236 
237 #endif
Provides the interface to evaluate objective functions.
Ptr< std::vector< Real > > scale_
Definition: ROL_BVP.hpp:125
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_BVP.hpp:58
typename PV< Real >::size_type size_type
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_BVP.hpp:42
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_BVP.hpp:157
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_BVP.hpp:167
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:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
std::vector< Real >::size_type uint
Definition: ROL_BVP.hpp:34
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_BVP.hpp:153
Contains definitions of test objective functions.
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_BVP.hpp:193
The discrete boundary value problem.
Definition: ROL_BVP.hpp:32