00001 // Copyright (C) 2008 Peter Carbonetto. All Rights Reserved. 00002 // This code is published under the Eclipse Public License. 00003 // 00004 // Author: Peter Carbonetto 00005 // Dept. of Computer Science 00006 // University of British Columbia 00007 // September 18, 2008 00008 00009 #ifndef INCLUDE_CALLBACKFUNCTIONS 00010 #define INCLUDE_CALLBACKFUNCTIONS 00011 00012 #include "mex.h" 00013 #include "iterate.hpp" 00014 #include "sparsematrix.hpp" 00015 #include "matlabfunctionhandle.hpp" 00016 00017 // Class CallbackFunctions. 00018 // ----------------------------------------------------------------- 00019 // An object of this class does two things. First of all, it stores 00020 // handles to MATLAB functions (type HELP FUNCTION_HANDLE in the 00021 // MATLAB console) for all the necessary and optional callback 00022 // functions for IPOPT. Secondly, this class actually provides the 00023 // routines for calling these functions with the necessary inputs and 00024 // outputs. 00025 class CallbackFunctions { 00026 public: 00027 00028 // The constructor must be provided with a single MATLAB array, and 00029 // this MATLAB array must be a structure array in which each field 00030 // is a function handle. 00031 explicit CallbackFunctions (const mxArray* ptr); 00032 00033 // The destructor. 00034 ~CallbackFunctions(); 00035 00036 // These functions return true if the respective callback functions 00037 // are available. 00038 bool constraintFuncIsAvailable() const { return *constraintfunc; }; 00039 bool jacobianFuncIsAvailable () const { return *jacobianfunc; }; 00040 bool hessianFuncIsAvailable () const { return *hessianfunc; }; 00041 bool iterFuncIsAvailable () const { return *iterfunc; }; 00042 00043 // These functions execute the various callback functions with the 00044 // appropriate inputs and outputs. The auxiliary data may be altered 00045 // over the course of executing the callback function. If there is 00046 // no auxiliary data, then simply pass in a null pointer. Here, m is 00047 // the number of constraints. The first function returns the value 00048 // of the objective at x. 00049 double computeObjective (const Iterate& x, const mxArray* auxdata) const; 00050 00051 // This function computes the value of the gradient at x, and 00052 // returns the gradient entries in the array g, which must be of 00053 // length equal to the number of optimization variables. 00054 void computeGradient (const Iterate& x, double* g, 00055 const mxArray* auxdata) const; 00056 00057 // This function computes the response of the vector-valued 00058 // constraint function at x, and stores the result in the array c 00059 // which must be of length m. 00060 void computeConstraints (const Iterate& x, int m, double* c, 00061 const mxArray* auxdata) const; 00062 00063 // This function gets the structure of the sparse m x n Jacobian matrix. 00064 SparseMatrix* getJacobianStructure (int n, int m, 00065 const mxArray* auxdata) const; 00066 00067 // This function gets the structure of the sparse n x n Hessian matrix. 00068 SparseMatrix* getHessianStructure (int n, const mxArray* auxdata) const; 00069 00070 // This function computes the Jacobian of the constraints at x. 00071 void computeJacobian (int m, const Iterate& x, SparseMatrix& J, 00072 const mxArray* auxdata) const; 00073 00074 // This function computes the Hessian of the Lagrangian at x. 00075 void computeHessian (const Iterate& x, double sigma, int m, 00076 const double* lambda, SparseMatrix& H, 00077 const mxArray* auxdata) const; 00078 00079 // Call the intermediate callback function. A return value of false 00080 // tells IPOPT to terminate. 00081 bool iterCallback (int t, double f, const mxArray*& auxdata) const; 00082 00083 protected: 00084 MatlabFunctionHandle* objfunc; // Objective callback function. 00085 MatlabFunctionHandle* gradfunc; // Gradient callback function. 00086 MatlabFunctionHandle* constraintfunc; // Constraint callback function. 00087 MatlabFunctionHandle* jacobianfunc; // Jacobian callback function. 00088 MatlabFunctionHandle* jacstrucfunc; // Jacobian structure function. 00089 MatlabFunctionHandle* hessianfunc; // Hessian callback function. 00090 MatlabFunctionHandle* hesstrucfunc; // Hessian structure function. 00091 MatlabFunctionHandle* iterfunc; // Iterative callback function. 00092 }; 00093 00094 #endif