Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Function.cc
Go to the documentation of this file.
1 /*
2  * $Id: Function.cc 139 2013-06-29 15:11:33Z jdl3 $
3  * Copyright (C) 2011, 2012, 2013 John D Lamb
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include<config.h>
22 #endif
23 
24 #include"Function.hpp"
25 
26 using namespace ipo_function;
27 
28 Function::Function( size_t const size ) : FunctionBase { size }{}
29 
30 gsl::vector
31 Function::gradient( gsl::vector const& vector ){
32  // This is where all the work is done.
33  double const H { 2 * h };
34  // Find SIZE
35  const size_t SIZE { vector.size() };
36  // Set up gradient
37  auto gradient = gsl::vector( SIZE );
38  // Fill diffVector
39  for( size_t i { 0 }; i < SIZE; ++i ){
40  double tmp { vector.get( i ) };
41  const_cast<gsl::vector&>( vector ).set( i, tmp + h );
42  double const u { operator()( vector ) };
43  const_cast<gsl::vector&>( vector ).set( i, tmp - h );
44  double const l { operator()( vector ) };
45  const_cast<gsl::vector&>( vector ).set( i, tmp );
46  //std::cout << "(" << l << "," << u << "): " << ((u - l) / H) << std::endl;
47  // vector returned to original value.
48  gradient.set( i, (u - l) / H );
49  }
50  return gradient;
51 }
52 
53 gsl::matrix
54 Function::hessian( gsl::vector const& vector ){
55  // This is where all the work is done.
56  double const H { 4 * h2 * h2 };
57  //std::cout << "h = " << h << std::endl;
58  //std::cout << "H = " << H << std::endl;
59  // Find SIZE
60  const size_t SIZE { vector.size() };
61  // Set up Hessian
62  auto hessian = gsl::matrix( SIZE, SIZE );
63  for( size_t i { 0 }; i < SIZE; ++i ){
64  // diagonal elements
65  double const c { operator()( vector ) };
66  double tmpi { vector.get( i ) };
67  const_cast<gsl::vector&>( vector ).set( i, tmpi + 2 * h2 );
68  double const a { operator()( vector ) };
69  const_cast<gsl::vector&>( vector ).set( i, tmpi - 2 * h2 );
70  double const d { operator()( vector ) };
71  const_cast<gsl::vector&>( vector ).set( i, tmpi );
72  hessian.set( i, i, (a - 2 * c + d) / H );
73  for( size_t j { i + 1 }; j < SIZE; ++j ){
74  // off diagonal elements
75  double tmpj { vector.get( j ) };
76  const_cast<gsl::vector&>( vector ).set( i, tmpi + h2 );
77  const_cast<gsl::vector&>( vector ).set( j, tmpj + h2 );
78  double const a { operator()( vector ) };
79  const_cast<gsl::vector&>( vector ).set( j, tmpj - h2 );
80  double const b { operator()( vector ) };
81  const_cast<gsl::vector&>( vector ).set( i, tmpi - h2 );
82  double const d { operator()( vector ) };
83  const_cast<gsl::vector&>( vector ).set( j, tmpj + h2 );
84  double const c { operator()( vector ) };
85  const_cast<gsl::vector&>( vector ).set( i, tmpi );
86  const_cast<gsl::vector&>( vector ).set( j, tmpj );
87  //std::cout << "a = " << a << std::endl;
88  //std::cout << "b = " << b << std::endl;
89  //std::cout << "c = " << c << std::endl;
90  //std::cout << "d = " << d << std::endl;
91  double const entry = (a - b - c + d) / H;
92  hessian.set( i, j, entry );
93  hessian.set( j, i, entry );
94  }
95  }
96  // DEBUG
97  // std::cout << "Hessian (estimated by central differences)" << std::endl;
98  // for( auto row : hessian ){
99  // for( auto entry : row )
100  // std::cout << entry << " ";
101  // std::cout << std::endl;
102  // }
103  // std::cout << "-----------------------" << std::endl;
104  // END DEBUG
105  return hessian;
106 }
107 
108 std::tuple<gsl::vector,gsl::matrix>
109 Function::derivatives( gsl::vector const& vector ){
110  return std::make_tuple( gradient( vector ), hessian( vector ) );
111 }
virtual std::tuple< gsl::vector, gsl::matrix > derivatives(gsl::vector const &vector)
Compute or estimate derivative values.
Definition: Function.cc:109
double h
Value used for default gradient estimates.
Definition: Function.hpp:82
virtual double operator()(gsl::vector const &vector)=0
Compute a function value.
virtual gsl::vector gradient(gsl::vector const &vector)
Compute or estimate a gradient value.
Definition: Function.cc:31
double h2
Value used for default Hessian estimates.
Definition: Function.hpp:87
Namespace for functions that can be used by ipo::Objective and ipo::Constraint.
Function(size_t const size=0)
Constructor.
Definition: Function.cc:28
virtual gsl::matrix hessian(gsl::vector const &vector)
Compute or estimate a Hessian value.
Definition: Function.cc:54