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

Generated on Tue Sep 30 03:01:24 2008 by  doxygen 1.4.7