- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/coin/SVN-release/Ipopt-3.7.1/Ipopt/contrib/MatlabInterface-Old/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& x0;           // The initial point.
    00129   const ArrayOfMatrices& lb;           // Lower bounds on the variables.
    00130   const ArrayOfMatrices& ub;           // Upper bounds on the variables.
    00131   const Matrix&          constraintlb; // Lower bounds on the constraints.
    00132   const Matrix&          constraintub; // Upper bounds on the constraints.
    00133   const mxArray*         auxData;      // Auxiliary data passed to the
    00134                                        // Matlab callback routines.
    00135   ArrayOfMatrices&       xsol;         // Storage for the solution.
    00136   ArrayOfMatrices*       x;            // Current value of the variables
    00137                                        // that's passed to the Matlab
    00138                                        // callback routines.
    00139   Multipliers*           initialMultipliers; // The initial point of the
    00140                                        // Lagrange multipliers.
    00141   Multipliers*           multipliers;  // This is used to store the
    00142                                        // value of the Lagrangne
    00143                                        // multipliers at the solution.
    00144   Array<double>*         lambda;       // Current value of the Lagrange
    00145                                        // multipliers that's passed to the
    00146                                        // Matlab callback routine for
    00147                                        // computing the Hessian.
    00148   int                    numiter;      // Keeps track of the number of
    00149                                        // IPOPT iterations.
    00150
    00151   // Array of inputs to a Matlab callback function.
    00152   mxArray** prhs;
    00153   mxArray*  lambdarhs;
    00154
    00155   // If true, we don't need to compute the Hessian of the Lagrangian.
    00156   bool useQuasiNewton;
    00157
    00158   // These next two members store information about the structure of
    00159   // the sparse Matlab matrix for the Jacobian of the constraints
    00160   // and the Hessian of the Lagragian.
    00161   SparseMatrixStructure* JacobianStructure;
    00162   SparseMatrixStructure* HessianStructure;
    00163
    00164   // The following members specify the Matlab callback routines.
    00165   const MatlabFunctionHandle& objFunc;
    00166   const MatlabFunctionHandle& gradFunc;
    00167   const MatlabFunctionHandle& constraintFunc;
    00168   const MatlabFunctionHandle& jacobianFunc;
    00169   const MatlabFunctionHandle& hessianFunc;
    00170   const MatlabFunctionHandle& iterFunc;
    00171
    00172   // The copy constructor and copy assignment operator are kept
    00173   // private so that they are not used.
    00174   MatlabProgram            (const MatlabProgram& source);
    00175   MatlabProgram& operator= (const MatlabProgram& source) { return *this; };
    00176
    00177 private:
    00178
    00179   // Return the value of the objective function at the point "x".
    00180   double computeObjective (const ArrayOfMatrices& x);
    00181
    00182   // Compute the first derivatives of the objective function at the
    00183   // point "x" and return their values in "grad".
    00184   void computeGradient (const ArrayOfMatrices& x, ArrayOfMatrices& grad);
    00185
    00186   // Compute the value of each constraint function at the point "x"
    00187   // and return the result in second input argument, which is an
    00188   // array of length equal to the number of constraints.
    00189   void computeConstraints (const ArrayOfMatrices& x, Array<double>& g);
    00190
    00191   // Compute the derivatives of the Jacobian of the constraints at
    00192   // the point "x". For this function to work correctly, the Matlab
    00193   // routine must return the expected sparse structure of the
    00194   // Jacobian matrix.
    00195   void computeJacobian (const ArrayOfMatrices& x, double* Jacobian);
    00196
    00197   // Compute the derivatives of the Hessian of the Lagrangian at the
    00198   // point "x".
    00199   void computeHessian (const ArrayOfMatrices& x,
    00200                        const Array<double>& lambda,
    00201                        double sigma, double* Hessian);
    00202
    00203   // Call the Matlab routine for computing the Jacobian. The input
    00204   // arguments to the Matlab routine are the current values of the
    00205   // optimization variables. The result must be a sparse
    00206   // matrix. Additionally, there is a second input argument passed
    00207   // to the Matlab routine. If it is true (1), then we ignore the
    00208   // actual values entered into the sparse matrix, as long as every
    00209   // entry that might possibly be a value other than zero at any
    00210   // time be returned as a non-zero value. It is up to the
    00211   // programmer to properly deallocate the return value at a later
    00212   // time by calling mxDestroyArray().
    00213   mxArray* callMatlabJacobianRoutine (const ArrayOfMatrices& x,
    00214                                       bool returnStructureOnly = true);
    00215
    00216   // Call the Matlab routine for computing the Hessian. The input
    00217   // arguments to the Matlab routine are the current values of the
    00218   // optimization variables. The result must be a sparse, lower
    00219   // triangular matrix. Additionally, there is a second input
    00220   // argument passed to the Matlab routine. If it is true (1), then
    00221   // we ignore the actual values entered into the sparse matrix, as
    00222   // long as every entry that might possibly be a value other than
    00223   // zero at any time be returned as a non-zero value. It is up to
    00224   // the programmer to properly deallocate the return value at a
    00225   // later time by calling mxDestroyArray().
    00226   mxArray* callMatlabHessianRoutine  (const ArrayOfMatrices& x,
    00227                                       const Array<double>& lambda,
    00228                                       bool returnStructureOnly = true,
    00229                                       double sigma = 0);
    00230 };
    00231
    00232 #endif