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