callbackfunctions.hpp
Go to the documentation of this file.
1 // Copyright (C) 2008 Peter Carbonetto. All Rights Reserved.
2 // This code is published under the Eclipse Public License.
3 //
4 // Author: Peter Carbonetto
5 // Dept. of Computer Science
6 // University of British Columbia
7 // September 18, 2008
8 
9 #ifndef INCLUDE_CALLBACKFUNCTIONS
10 #define INCLUDE_CALLBACKFUNCTIONS
11 
12 #include "mex.h"
13 #include "iterate.hpp"
14 #include "sparsematrix.hpp"
15 #include "matlabfunctionhandle.hpp"
16 
17 // Class CallbackFunctions.
18 // -----------------------------------------------------------------
19 // An object of this class does two things. First of all, it stores
20 // handles to MATLAB functions (type HELP FUNCTION_HANDLE in the
21 // MATLAB console) for all the necessary and optional callback
22 // functions for IPOPT. Secondly, this class actually provides the
23 // routines for calling these functions with the necessary inputs and
24 // outputs.
26 public:
27 
28  // The constructor must be provided with a single MATLAB array, and
29  // this MATLAB array must be a structure array in which each field
30  // is a function handle.
31  explicit CallbackFunctions (const mxArray* ptr);
32 
33  // The destructor.
35 
36  // These functions return true if the respective callback functions
37  // are available.
38  bool constraintFuncIsAvailable() const { return *constraintfunc; };
39  bool jacobianFuncIsAvailable () const { return *jacobianfunc; };
40  bool hessianFuncIsAvailable () const { return *hessianfunc; };
41  bool iterFuncIsAvailable () const { return *iterfunc; };
42 
43  // These functions execute the various callback functions with the
44  // appropriate inputs and outputs. The auxiliary data may be altered
45  // over the course of executing the callback function. If there is
46  // no auxiliary data, then simply pass in a null pointer. Here, m is
47  // the number of constraints. The first function returns the value
48  // of the objective at x.
49  double computeObjective (const Iterate& x, const mxArray* auxdata) const;
50 
51  // This function computes the value of the gradient at x, and
52  // returns the gradient entries in the array g, which must be of
53  // length equal to the number of optimization variables.
54  void computeGradient (const Iterate& x, double* g,
55  const mxArray* auxdata) const;
56 
57  // This function computes the response of the vector-valued
58  // constraint function at x, and stores the result in the array c
59  // which must be of length m.
60  void computeConstraints (const Iterate& x, int m, double* c,
61  const mxArray* auxdata) const;
62 
63  // This function gets the structure of the sparse m x n Jacobian matrix.
64  SparseMatrix* getJacobianStructure (int n, int m,
65  const mxArray* auxdata) const;
66 
67  // This function gets the structure of the sparse n x n Hessian matrix.
68  SparseMatrix* getHessianStructure (int n, const mxArray* auxdata) const;
69 
70  // This function computes the Jacobian of the constraints at x.
71  void computeJacobian (int m, const Iterate& x, SparseMatrix& J,
72  const mxArray* auxdata) const;
73 
74  // This function computes the Hessian of the Lagrangian at x.
75  void computeHessian (const Iterate& x, double sigma, int m,
76  const double* lambda, SparseMatrix& H,
77  const mxArray* auxdata) const;
78 
79  // Call the intermediate callback function. A return value of false
80  // tells IPOPT to terminate.
81  bool iterCallback (int t, double f, const mxArray*& auxdata) const;
82 
83 protected:
84  MatlabFunctionHandle* objfunc; // Objective callback function.
85  MatlabFunctionHandle* gradfunc; // Gradient callback function.
86  MatlabFunctionHandle* constraintfunc; // Constraint callback function.
87  MatlabFunctionHandle* jacobianfunc; // Jacobian callback function.
88  MatlabFunctionHandle* jacstrucfunc; // Jacobian structure function.
89  MatlabFunctionHandle* hessianfunc; // Hessian callback function.
90  MatlabFunctionHandle* hesstrucfunc; // Hessian structure function.
91  MatlabFunctionHandle* iterfunc; // Iterative callback function.
92 };
93 
94 #endif