ipo interior-point optimisation library

Links

Home ·
Download ·
Documentation ·
Namespaces and #includes ·
Defining functions ·
Example of a function ·
Variables and arrays ·
Objective and constraints ·
Building a model ·
Example ·
Bugs ·

Example of a function

The following example shows how you might define a function that takes as an argument a vector of length two and computes the sum of the squares of the values. Ipo provides functions ipo_function::concrete::SumOfSquares and ipo_function::concrete::QuadraticCombination, so that you don’t need this function as more than an example.

class MyFunction : public ipo_function::Function, 
		   public ipo_function::DerivativesEstimates {
public:
  MyFunction() : size( 2 ){
    functionGradient = gsl::vector( 2 );
    functionHessian = gsl::matrix { 2, 2 };
  }
  double operator()( gsl::vector const& vector ){
    double const& x { vector[0] };
    double const& y { vector[1] };
    return x * x + y * y;
  }
  gsl::vector gradient( gsl::vector const& vector ){
    double const& x { vector[0] };
    double const& y { vector[1] };
    gsl::vector result( 2 );
    result[0] = 2 * x;
    result[1] = 2 * y;
    return result;
  }
  gsl::matrix hessian( gsl::vector const& vector ){
    gsl::matrix result{ 2, 2 };
    result.set_all( 0 );
    result.set( 0, 0, 2 );
    result.set( 1, 1, 2 );
    return result;
  }
  void setVector( gsl::vector const& vector ){
    double const& x { vector[0] };
    double const& y { vector[1] };
    functionValue = x * x + y * y;
    functionGradient[0] = 2 * x;
    functionGradient[1] = 2 * y;
    functionHessian.set_all( 0 );
    functionHessian.set( 0, 0, 2 );
    functionHessian.set( 1, 1, 2 );
  }
};
	


Last modified: Tue 02 Jul 2013 04:56 pm

SourceForge.net Logo