/home/coin/SVN-release/Ipopt-3.3.1/Ipopt/contrib/MatlabInterface/src/matlabprogram.h

Go to the documentation of this file.
00001 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved.
00002 // This code is published under the Common Public License.
00003 //
00004 // Author: Peter Carbonetto
00005 //         Dept. of Computer Science
00006 //         University of British Columbia
00007 //         May 19, 2007
00008 
00009 #ifndef INCLUDE_MATLABPROGRAM
00010 #define INCLUDE_MATLABPROGRAM
00011 
00012 #include "array.h"
00013 #include "matlabexception.h"
00014 #include "matlabmatrix.h"
00015 #include "sparsematrix.h"
00016 #include "arrayofmatrices.h"
00017 #include "ipopt/IpTNLP.hpp"
00018 #include "mex.h"
00019 
00020 using Ipopt::TNLP;
00021 using Ipopt::SolverReturn;
00022 using Ipopt::AlgorithmMode;
00023 using Ipopt::IpoptData;
00024 using Ipopt::IpoptCalculatedQuantities;
00025 
00026 // Class MatlabProgram
00027 // -----------------------------------------------------------------
00028 class MatlabProgram : public TNLP {
00029 public:
00030     
00031   // The constructor. The input "error" will be set to the appropriate
00032   // exception object if the IPOPT solver terminates abnormally.
00033   MatlabProgram (const ArrayOfMatrices& x0, const ArrayOfMatrices& lb,
00034                  const ArrayOfMatrices& ub, const Matrix& constraintlb,
00035                  const Matrix& constraintub, const char* objFunc, 
00036                  const char* gradFunc, const char* constraintFunc, 
00037                  const char* jacobianFunc, const char* hessianFunc,
00038                  const char* iterFunc, const mxArray* auxData, 
00039                  ArrayOfMatrices& xsol, bool useQuasiNewton);
00040     
00041   // The destructor.
00042   virtual ~MatlabProgram();
00043 
00044   // Returns the error generated. If no error was generated, returns a
00045   // null pointer.
00046   char* geterrormsg() const;
00047   
00048   // Method to return some info about the nonlinear program.
00049   virtual bool get_nlp_info (int& numVariables, int& numConstraints, 
00050                              int& sizeOfJ, int& sizeOfH, 
00051                              IndexStyleEnum& indexStyle);
00052   
00053   // Return the bounds for the problem.
00054   virtual bool get_bounds_info (int numVariables, double* lbptr, 
00055                                 double* ubptr, int numConstraints, 
00056                                 double* clbptr, double* cubptr);
00057     
00058   // Return the starting point for the algorithm.
00059   virtual bool get_starting_point (int numVariables, bool initializeVars, 
00060                                    double* variables, 
00061                                    bool initializez, double* zl, 
00062                                    double* zu, int numConstraints, 
00063                                    bool initializeLambda, double* lambda);
00064     
00065   // Compute the value of the objective.
00066   virtual bool eval_f (int numVariables, const double* variables, 
00067                        bool ignoreThis, double& objective);
00068     
00069   // Compute the gradient of the objective.
00070   virtual bool eval_grad_f (int numVariables, const double* variables, 
00071                             bool ignoreThis, double* gradient);
00072     
00073   // Evaluate the constraint residuals.
00074   virtual bool eval_g (int numVariables, const double* variables, 
00075                        bool ignoreThis, int numConstraints, 
00076                        double* constraints);
00077     
00078   // This method either returns: 1.) The structure of the Jacobian
00079   // (if "Jacobian" is zero), or 2.) The values of the Jacobian (if
00080   // "Jacobian" is not zero).
00081   virtual bool eval_jac_g (int numVariables, const double* variables, 
00082                            bool ignoreThis, int numConstraints, 
00083                            int sizeOfJ, int* rows, int *cols, 
00084                            double* Jacobian);
00085     
00086   // This method either returns: 1.) the structure of the Hessian of
00087   // the Lagrangian (if "Hessian" is zero), or 2.) the values of the
00088   // Hessian of the Lagrangian (if "Hesson" is not zero).
00089   virtual bool eval_h (int numVariables, const double* variables, 
00090                        bool ignoreThis, double sigma, 
00091                        int numConstraints, const double* multipliers, 
00092                        bool ignoreThisToo, int sizeOfH, int* rows, 
00093                        int* cols, double* Hessian);
00094 
00095   // This method is called when the algorithm is complete.
00096   virtual void finalize_solution (SolverReturn status, int numVariables, 
00097                                   const double* variables, const double* zl, 
00098                                   const double* zu, int numConstraints, 
00099                                   const double* constraints, 
00100                                   const double* lambda, double objective,
00101                                   const IpoptData* ip_data,
00102                                   IpoptCalculatedQuantities* ip_cq);
00103 
00104   // Intermediate callback method. It is called once per iteration
00105   // of the IPOPT algorithm.
00106   virtual bool intermediate_callback (AlgorithmMode mode,
00107                                       int iteration, double objective,
00108                                       double inf_pr, double inf_du,
00109                                       double mu, double d_norm,
00110                                       double regularization_ize,
00111                                       double alpha_du, double alpha_pr,
00112                                       int ls_trials,
00113                                       const IpoptData* ip_data,
00114                                       IpoptCalculatedQuantities* ip_cq);
00115 
00116 protected:
00117   const ArrayOfMatrices& lb;           // Lower bounds on the variables.
00118   const ArrayOfMatrices& ub;           // Upper bounds on the variables.
00119   const Matrix&          constraintlb; // Lower bounds on the constraints.
00120   const Matrix&          constraintub; // Upper bounds on the constraints.
00121   const mxArray*         auxData;      // Auxiliary data passed to the 
00122                                        // Matlab callback routines.
00123   ArrayOfMatrices&       xsol;         // Storage for the solution.
00124   ArrayOfMatrices*       x;            // Current value of the variables 
00125                                        // that's passed to the Matlab 
00126                                        // callback routines.
00127   Array<double>*         lambda;       // Current value of the Lagrange 
00128                                        // multipliers that's passed to the
00129                                        // Matlab callback routine for
00130                                        // computing the Hessian.
00131   
00132   // Array of inputs to a Matlab callback function.
00133   mxArray** prhs;
00134   mxArray*  lambdarhs;
00135 
00136   // If true, we don't need to compute the Hessian of the Lagrangian.
00137   bool useQuasiNewton; 
00138 
00139   // These next two members store information about the structure of
00140   // the sparse Matlab matrix for the Jacobian of the constraints
00141   // and the Hessian of the Lagragian.
00142   SparseMatrixStructure* JacobianStructure;
00143   SparseMatrixStructure* HessianStructure;
00144   
00145   // The following members specify the Matlab callback routines.
00146   const char* objFunc;        // Computes the objective function.
00147   const char* gradFunc;       // Computes the gradient of the objective.
00148   const char* constraintFunc; // Computes the constraints.
00149   const char* jacobianFunc;   // Computes the Jacobian of the constraints.
00150   const char* hessianFunc;    // Computes the Hessian of the Lagrangian.
00151   const char* iterFunc;       // The iterative callback function.
00152 
00153   // The copy constructor and copy assignment operator are kept
00154   // private so that they are not used.
00155   MatlabProgram            (const MatlabProgram& source);
00156   MatlabProgram& operator= (const MatlabProgram& source) { return *this; };
00157 
00158 private:
00159   
00160   // Return the value of the objective function at the point "x".
00161   double computeObjective (const ArrayOfMatrices& x);
00162 
00163   // Compute the first derivatives of the objective function at the
00164   // point "x" and return their values in "grad".
00165   void computeGradient (const ArrayOfMatrices& x, ArrayOfMatrices& grad);
00166   
00167   // Compute the value of each constraint function at the point "x"
00168   // and return the result in second input argument, which is an
00169   // array of length equal to the number of constraints.
00170   void computeConstraints (const ArrayOfMatrices& x, Array<double>& g);
00171 
00172   // Compute the derivatives of the Jacobian of the constraints at
00173   // the point "x". For this function to work correctly, the Matlab
00174   // routine must return the expected sparse structure of the
00175   // Jacobian matrix.
00176   void computeJacobian (const ArrayOfMatrices& x, double* Jacobian);
00177   
00178   // Compute the derivatives of the Hessian of the Lagrangian at the
00179   // point "x". 
00180   void computeHessian (const ArrayOfMatrices& x, 
00181                        const Array<double>& lambda,
00182                        double sigma, double* Hessian);
00183 
00184   // Call the Matlab routine for computing the Jacobian. The input
00185   // arguments to the Matlab routine are the current values of the
00186   // optimization variables. The result must be a sparse
00187   // matrix. Additionally, there is a second input argument passed
00188   // to the Matlab routine. If it is true (1), then we ignore the
00189   // actual values entered into the sparse matrix, as long as every
00190   // entry that might possibly be a value other than zero at any
00191   // time be returned as a non-zero value. It is up to the
00192   // programmer to properly deallocate the return value at a later
00193   // time by calling mxDestroyArray().
00194   mxArray* callMatlabJacobianRoutine (const ArrayOfMatrices& x,
00195                                       bool returnStructureOnly = true);
00196 
00197   // Call the Matlab routine for computing the Hessian. The input
00198   // arguments to the Matlab routine are the current values of the
00199   // optimization variables. The result must be a sparse, lower
00200   // triangular matrix. Additionally, there is a second input
00201   // argument passed to the Matlab routine. If it is true (1), then
00202   // we ignore the actual values entered into the sparse matrix, as
00203   // long as every entry that might possibly be a value other than
00204   // zero at any time be returned as a non-zero value. It is up to
00205   // the programmer to properly deallocate the return value at a
00206   // later time by calling mxDestroyArray().
00207   mxArray* callMatlabHessianRoutine  (const ArrayOfMatrices& x,
00208                                       const Array<double>& lambda, 
00209                                       bool returnStructureOnly = true,
00210                                       double sigma = 0);
00211 };
00212 
00213 #endif

Generated on Thu May 15 22:25:42 2008 by  doxygen 1.4.7