00001 
00002 
00003 
00004 
00005 
00006 
00007 
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 
00029 
00030 class MatlabProgram : public TNLP {
00031 public:
00032     
00033   
00034   
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   
00049   virtual ~MatlabProgram();
00050 
00051   
00052   
00053   char* geterrormsg() const;
00054 
00055   
00056   
00057   int getnumiterations() const { return numiter; };
00058   
00059   
00060   virtual bool get_nlp_info (int& numVariables, int& numConstraints, 
00061                              int& sizeOfJ, int& sizeOfH, 
00062                              IndexStyleEnum& indexStyle);
00063   
00064   
00065   virtual bool get_bounds_info (int numVariables, double* lbptr, 
00066                                 double* ubptr, int numConstraints, 
00067                                 double* clbptr, double* cubptr);
00068     
00069   
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   
00077   virtual bool eval_f (int numVariables, const double* variables, 
00078                        bool ignoreThis, double& objective);
00079     
00080   
00081   virtual bool eval_grad_f (int numVariables, const double* variables, 
00082                             bool ignoreThis, double* gradient);
00083     
00084   
00085   virtual bool eval_g (int numVariables, const double* variables, 
00086                        bool ignoreThis, int numConstraints, 
00087                        double* constraints);
00088     
00089   
00090   
00091   
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   
00098   
00099   
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   
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   
00116   
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;           
00129   const ArrayOfMatrices& lb;           
00130   const ArrayOfMatrices& ub;           
00131   const Matrix&          constraintlb; 
00132   const Matrix&          constraintub; 
00133   const mxArray*         auxData;      
00134                                        
00135   ArrayOfMatrices&       xsol;         
00136   ArrayOfMatrices*       x;            
00137                                        
00138                                        
00139   Multipliers*           initialMultipliers; 
00140                                        
00141   Multipliers*           multipliers;  
00142                                        
00143                                        
00144   Array<double>*         lambda;       
00145                                        
00146                                        
00147                                        
00148   int                    numiter;      
00149                                        
00150 
00151   
00152   mxArray** prhs;
00153   mxArray*  lambdarhs;
00154 
00155   
00156   bool useQuasiNewton; 
00157 
00158   
00159   
00160   
00161   SparseMatrixStructure* JacobianStructure;
00162   SparseMatrixStructure* HessianStructure;
00163   
00164   
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   
00173   
00174   MatlabProgram            (const MatlabProgram& source);
00175   MatlabProgram& operator= (const MatlabProgram& source) { return *this; };
00176 
00177 private:
00178   
00179   
00180   double computeObjective (const ArrayOfMatrices& x);
00181 
00182   
00183   
00184   void computeGradient (const ArrayOfMatrices& x, ArrayOfMatrices& grad);
00185   
00186   
00187   
00188   
00189   void computeConstraints (const ArrayOfMatrices& x, Array<double>& g);
00190 
00191   
00192   
00193   
00194   
00195   void computeJacobian (const ArrayOfMatrices& x, double* Jacobian);
00196   
00197   
00198   
00199   void computeHessian (const ArrayOfMatrices& x, 
00200                        const Array<double>& lambda,
00201                        double sigma, double* Hessian);
00202 
00203   
00204   
00205   
00206   
00207   
00208   
00209   
00210   
00211   
00212   
00213   mxArray* callMatlabJacobianRoutine (const ArrayOfMatrices& x,
00214                                       bool returnStructureOnly = true);
00215 
00216   
00217   
00218   
00219   
00220   
00221   
00222   
00223   
00224   
00225   
00226   mxArray* callMatlabHessianRoutine  (const ArrayOfMatrices& x,
00227                                       const Array<double>& lambda, 
00228                                       bool returnStructureOnly = true,
00229                                       double sigma = 0);
00230 };
00231 
00232 #endif