/home/coin/SVN-release/OS-2.1.1/OS/examples/matlab/OSMatlabSolver.cpp

Go to the documentation of this file.
00001 /* $Id: OSMatlabSolver.cpp 2831 2009-07-21 07:34:44Z kmartin $ */
00002 
00003 #include <iostream>
00004 #include <string>
00005 #include "mex.h"
00006 
00007 
00008 
00009 // OS includes
00010 #include "OSDataStructures.h"
00011 //#include "OSParameters.h"
00012 #include "OSMatlabSolver.h"
00013 
00014 #ifdef COIN_HAS_ASL
00015 #include "OSnl2osil.h"
00016 #endif
00017 
00018 
00019 /* If you are using a compiler that equates NaN to be zero, you must
00020  * compile this example using the flag  -DNAN_EQUALS_ZERO. For example:
00021  *
00022  *     mex -DNAN_EQUALS_ZERO fulltosparse.c
00023  *
00024  * This will correctly define the IsNonZero macro for your C compiler.
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      // Check for proper number of input and output arguments
00058      mexPrintf("BEGIN PROCESSING DATA\n");
00059      if (nrhs != 15) {
00060           mexErrMsgTxt("Fifteen input arguments are required.");
00061      }
00062      
00063      
00064      //
00065      // get number of variables and number of constraints
00066      matlabModel->numVar =  (int) mxGetScalar( prhs[ 0]) ;
00067      //printf("variable numVar = %i\n",  matlabModel->numVar );
00068      matlabModel->numCon = (int) mxGetScalar( prhs[ 1]);
00069      //
00070      // get the constraint matrix
00071      // check the data type
00072      if (!(mxIsDouble(prhs[2 ]))){
00073           mexErrMsgTxt("Constraint matrix A must be of type double.");
00074      }
00075      // check the dimension
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      // get the rest of the model
00081      //
00082      // both bl and bu should equal the number of rows
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           // convert Matlab -infinity to OS -infinity
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           // convert Matlab infinity to OS infinity
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           // convert Matlab -infinity to OS -infinity
00120           for(i = 0;  i < matlabModel->numVar;  i++){
00121               //printf("variable lb = %f\n",  matlabModel->vl[i] );
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           // convert Matlab infinity to OS infinity
00133           for(i = 0;  i < matlabModel->numVar;  i++){
00134               //printf("variable ub = %f\n",  matlabModel->vu[i] );
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      //printf("Objective Function Type = %d\n", matlabModel->objType);
00145      //
00146      // get the variable types, this is character data
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      // get the quadratic terms
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           //printf("NUMBER OF Q TERMS =  %d \n", numQTerms);
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                     //printf(" Q COMP = %f\n", pr[ k]) ;
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      // get the name of the solver
00201       matlabModel->sSolverName = mxArrayToString( prhs[ 13]);    
00202       printf("WE ARE USING SOLVER %s\n", &matlabModel->sSolverName[0]);
00203      //
00204      // get the name of the solver service
00205       matlabModel->sAgentAddress = mxArrayToString( prhs[ 14]);    
00206       printf("WE ARE USING AGENT %s\n", &matlabModel->sAgentAddress[0]);
00207      //
00208      // create the OSInstance
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     //char *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      sparseMat = new SparseMatrix();
00233     /* Declare variable */
00234      mxArray *plhs;
00235      //mwSize m,n;
00236      // mwSize nzmax;
00237      //mwIndex *irs, *jcs, j, k;
00238      int m,n;
00239      int nzmax;
00240      int *irs, *jcs, j, k;
00241      int  cmplx;
00242      double *pr, *pi, *si, *sr;
00243     /* Get the size and pointers to input data */
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     /* Copy nonzeros */
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           //printf("Column start = %d\n", jcs[ j]);
00277           for(km = jcs[ j]; km < jcs[ j+1]; km++ ){
00278                // printf("row index= %d\n",  irs[ km]);
00279                //  printf("nonzero value = %f\n",   sr[ km]);
00280           }
00281      }
00282      // now fill in a sparse matrix data structure
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 }

Generated on Mon May 3 03:05:22 2010 by  doxygen 1.4.7