/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OS/OS/src/OSModelInterfaces/OSMatlabSolver.cpp

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

Generated on Sat Mar 29 22:38:02 2008 by  doxygen 1.5.3