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 "ipopt/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& lb;
00129 const ArrayOfMatrices& ub;
00130 const Matrix& constraintlb;
00131 const Matrix& constraintub;
00132 const mxArray* auxData;
00133
00134 ArrayOfMatrices& xsol;
00135 ArrayOfMatrices* x;
00136
00137
00138 Multipliers* initialMultipliers;
00139
00140 Multipliers* multipliers;
00141
00142
00143 Array<double>* lambda;
00144
00145
00146
00147 int numiter;
00148
00149
00150
00151 mxArray** prhs;
00152 mxArray* lambdarhs;
00153
00154
00155 bool useQuasiNewton;
00156
00157
00158
00159
00160 SparseMatrixStructure* JacobianStructure;
00161 SparseMatrixStructure* HessianStructure;
00162
00163
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
00172
00173 MatlabProgram (const MatlabProgram& source);
00174 MatlabProgram& operator= (const MatlabProgram& source) { return *this; };
00175
00176 private:
00177
00178
00179 double computeObjective (const ArrayOfMatrices& x);
00180
00181
00182
00183 void computeGradient (const ArrayOfMatrices& x, ArrayOfMatrices& grad);
00184
00185
00186
00187
00188 void computeConstraints (const ArrayOfMatrices& x, Array<double>& g);
00189
00190
00191
00192
00193
00194 void computeJacobian (const ArrayOfMatrices& x, double* Jacobian);
00195
00196
00197
00198 void computeHessian (const ArrayOfMatrices& x,
00199 const Array<double>& lambda,
00200 double sigma, double* Hessian);
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 mxArray* callMatlabJacobianRoutine (const ArrayOfMatrices& x,
00213 bool returnStructureOnly = true);
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 mxArray* callMatlabHessianRoutine (const ArrayOfMatrices& x,
00226 const Array<double>& lambda,
00227 bool returnStructureOnly = true,
00228 double sigma = 0);
00229 };
00230
00231 #endif