00001
00002
00003 #include <iostream>
00004 #include <string>
00005 #include "mex.h"
00006
00007
00008
00009
00010 #include "OSDataStructures.h"
00011
00012 #include "OSMatlabSolver.h"
00013
00014 #ifdef COIN_HAS_ASL
00015 #include "OSnl2osil.h"
00016 #endif
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if defined(NAN_EQUALS_ZERO)
00028 #define IsNonZero(d) ((d)!=0.0 || mxIsNaN(d))
00029 #else
00030 #define IsNonZero(d) ((d)!=0.0)
00031 #endif
00032
00033 using namespace std;
00034 using std::cout;
00035 using std::endl;
00036
00037
00038 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
00039
00050 int i;
00051 double *pr;
00052 OSMatlab *matlabModel = new OSMatlab();
00053 string sTest = "";
00054 char *buf;
00055 SparseMatrix* getConstraintMatrix( const mxArray *prhs);
00056
00057
00058 mexPrintf("BEGIN PROCESSING DATA\n");
00059 if (nrhs != 15) {
00060 mexErrMsgTxt("Fifteen input arguments are required.");
00061 }
00062
00063
00064
00065
00066 matlabModel->numVar = (int) mxGetScalar( prhs[ 0]) ;
00067
00068 matlabModel->numCon = (int) mxGetScalar( prhs[ 1]);
00069
00070
00071
00072 if (!(mxIsDouble(prhs[2 ]))){
00073 mexErrMsgTxt("Constraint matrix A must be of type double.");
00074 }
00075
00076 if ( (mxGetN(prhs[ 2]) != matlabModel->numVar) || (mxGetM(prhs[2]) != matlabModel->numCon) ){
00077 mexErrMsgTxt(" Constraint matrix A must have number of rows equal to number of constraints and columns equal number of variables \n");
00078 }
00079 matlabModel->sparseMat = getConstraintMatrix( prhs[2]) ;
00080
00081
00082
00083
00084 if( !mxIsEmpty( prhs[ 3]) ){
00085 if (mxGetN(prhs[3]) != matlabModel->numCon ){
00086 mexErrMsgTxt(" Vector BL size must equal the number of rows\n");
00087 }
00088 matlabModel->bl = mxGetPr( prhs[ 3]);
00089
00090 for(i = 0; i < matlabModel->numCon; i++){
00091 if( mxIsInf( -(matlabModel->bl[i]) ) ) matlabModel->bl[ i] = -OSDBL_MAX;
00092 }
00093 }
00094
00095
00096 if( !mxIsEmpty( prhs[ 4]) ){
00097 if (mxGetN(prhs[4]) != matlabModel->numCon ){
00098 mexErrMsgTxt(" Vector BU size must equal the number of rows\n");
00099 }
00100 matlabModel->bu = mxGetPr( prhs[ 4]);
00101
00102 for(i = 0; i < matlabModel->numCon; i++){
00103 if( mxIsInf( matlabModel->bu[i]) ) matlabModel->bu[ i] = OSDBL_MAX;
00104 }
00105 }
00106
00107
00108 if (mxGetN(prhs[5]) != matlabModel->numVar ){
00109 mexErrMsgTxt(" Vector OBJ size must equal the number of variables\n");
00110 }
00111 matlabModel->obj = mxGetPr( prhs[ 5]);
00112
00113
00114 if( !mxIsEmpty( prhs[ 6]) ){
00115 if (mxGetN(prhs[6]) != matlabModel->numVar ){
00116 mexErrMsgTxt(" Vector VL size must equal the number of variables\n");
00117 }
00118 matlabModel->vl = mxGetPr( prhs[ 6]);
00119
00120 for(i = 0; i < matlabModel->numVar; i++){
00121
00122 if( mxIsInf( -(matlabModel->vl[i]) ) ) matlabModel->vl[ i] = -OSDBL_MAX;
00123 }
00124 }
00125
00126
00127 if( !mxIsEmpty( prhs[ 7]) ){
00128 if (mxGetN(prhs[7]) != matlabModel->numVar ){
00129 mexErrMsgTxt(" Vector VU size must equal the number of variables\n");
00130 }
00131 matlabModel->vu = mxGetPr( prhs[ 7]);
00132
00133 for(i = 0; i < matlabModel->numVar; i++){
00134
00135 if( mxIsInf( matlabModel->vu[i]) ) matlabModel->vu[ i] = OSDBL_MAX;
00136 }
00137 }
00138
00139
00140 if ( (mxGetScalar( prhs[ 8]) != 0) && (mxGetScalar( prhs[ 8]) != 1)){
00141 mexErrMsgTxt(" The objective type must be either 1 (max) or 0 (min)\n");
00142 }
00143 mxGetScalar( prhs[ 8]) > 0 ? matlabModel->objType = 1 : matlabModel->objType = 0;
00144
00145
00146
00147 if(!mxIsChar( prhs[ 9])){
00148 mexErrMsgTxt(" Vector VarType must be a character array\n");
00149 }
00150 if (mxGetN(prhs[ 9]) != matlabModel->numVar ){
00151 mexErrMsgTxt(" Vector VarType size must equal the number of variables\n");
00152 }
00153 buf = mxArrayToString( prhs[ 9]);
00154 matlabModel->varType = buf;
00155
00156
00157
00158
00159 int j;
00160 int k = 0;
00161 if( !mxIsEmpty( prhs[ 10]) ){
00162 if( mxGetM( prhs[ 10]) != 4) mexErrMsgTxt(" Vector Q Must have 4 rows\n");
00163 int numQTerms = mxGetN( prhs[ 10]);
00164
00165 matlabModel->numQTerms = numQTerms;
00166 matlabModel->qRows = new int[ numQTerms];
00167 matlabModel->qIndex1 = new int[ numQTerms];
00168 matlabModel->qIndex2 = new int[ numQTerms];
00169 matlabModel->qVal = new double[ numQTerms];
00170 pr= mxGetPr( prhs[ 10]);
00171 for(i = 0; i < numQTerms; i++){
00172 for(j = 0; j <= 3; j++){
00173
00174 switch( j){
00175 case 0:
00176 matlabModel->qRows[ i] = (int) pr[ k];
00177 break;
00178 case 1:
00179 matlabModel->qIndex1[ i] = (int) pr[ k];
00180 break;
00181 case 2:
00182 matlabModel->qIndex2[ i] = (int) pr[ k];
00183 break;
00184 case 3:
00185 matlabModel->qVal[ i] = pr[ k];
00186 break;
00187 }
00188 k++;
00189 }
00190 }
00191 }
00192 if( !mxIsEmpty( prhs[ 11]) ){
00193 matlabModel->instanceName = mxArrayToString( prhs[ 11]);
00194 }
00195
00196 buf = mxArrayToString( prhs[ 12]);
00197 const char *password = "chicagoesmuyFRIO";
00198 if( strcmp(buf, password) != 0) mexErrMsgTxt(" Incorrect Password\n");
00199
00200
00201 matlabModel->sSolverName = mxArrayToString( prhs[ 13]);
00202 printf("WE ARE USING SOLVER %s\n", &matlabModel->sSolverName[0]);
00203
00204
00205 matlabModel->sAgentAddress = mxArrayToString( prhs[ 14]);
00206 printf("WE ARE USING AGENT %s\n", &matlabModel->sAgentAddress[0]);
00207
00208
00209 mexPrintf("CREATE THE INSTANCE \n");
00210 matlabModel->createOSInstance();
00211 mexPrintf("CALL THE REMOTE SERVER \n");
00212 sTest = matlabModel->solve();
00213 std::string osil = matlabModel->osil;
00214 char *ch = &osil[0];
00215 printf("HERE IS THE INSTANCE %s\n", ch);
00216 mexPrintf("DONE WITH THE REMOTE CALL \n");
00217 mexPrintf("HERE IS THE SOLUTION \n");
00218 mexPrintf(&sTest[0] );
00219
00220
00221
00222
00223 char *str[ 1];
00224
00225 str[ 0] = &sTest[0] ;
00226 plhs[0]= mxCreateCharMatrixFromStrings( 1, (const char **)str);
00227 delete matlabModel;
00228 return ;
00229 }
00230 SparseMatrix* getConstraintMatrix( const mxArray *prhs){
00231 SparseMatrix *sparseMat = NULL;
00232 sparseMat = new SparseMatrix();
00233
00234 mxArray *plhs;
00235
00236
00237
00238 int m,n;
00239 int nzmax;
00240 int *irs, *jcs, j, k;
00241 int cmplx;
00242 double *pr, *pi, *si, *sr;
00243
00244 m = mxGetM( prhs);
00245 n = mxGetN( prhs);
00246 pr = mxGetPr( prhs);
00247 pi = mxGetPi( prhs);
00248 cmplx = (pi==NULL ? 0 : 1);
00249 nzmax = n*m;
00250 plhs = mxCreateSparse(m, n, nzmax, (mxComplexity)cmplx);
00251 sr = mxGetPr( plhs);
00252 si = mxGetPi( plhs);
00253 irs = mxGetIr( plhs);
00254 jcs = mxGetJc( plhs);
00255
00256 k = 0;
00257 for (j=0; (j<n); j++) {
00258 int i;
00259 jcs[j] = k;
00260 for (i=0; (i<m ); i++) {
00261 if (IsNonZero(pr[i]) || (cmplx && IsNonZero(pi[i]))) {
00262 sr[k] = pr[i];
00263 if (cmplx){
00264 si[k]=pi[i];
00265 }
00266 irs[k] = i;
00267 k++;
00268 }
00269 }
00270 pr += m;
00271 pi += m;
00272 }
00273 jcs[ n] = k;
00274 int km;
00275 for(j = 0; j < n; j++){
00276
00277 for(km = jcs[ j]; km < jcs[ j+1]; km++ ){
00278
00279
00280 }
00281 }
00282
00283 sparseMat->bDeleteArrays = false;
00284 sparseMat->isColumnMajor = true;
00285 sparseMat->startSize = n + 1;
00286 sparseMat->valueSize = jcs[ n];
00287 sparseMat->starts = jcs;
00288 sparseMat->indexes = irs;
00289 sparseMat->values = sr;
00290 return sparseMat;
00291 }