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

Generated on Fri May 16 15:54:14 2008 by  doxygen 1.4.7