/home/coin/SVN-release/OS-2.4.2/OS/applications/matlab/OSMatlabSolverMex.cpp

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

Generated on Wed Nov 30 03:04:14 2011 by  doxygen 1.4.7