Ejemplo n.º 1
0
//--------------------------------------------------------------------------
// test import methods
void
CoinLpIOUnitTest(const std::string& lpDir)
{
    // Test default constructor
    {
        CoinLpIO m;
        assert( m.rowsense_ == NULL );
        assert( m.rhs_ == NULL );
        assert( m.rowrange_ == NULL );
        assert( m.matrixByRow_ == NULL );
        assert( m.matrixByColumn_ == NULL );
        assert( m.integerType_ == NULL);
        assert( m.fileName_ == NULL );
        assert( !strcmp( m.problemName_ , ""));
        assert( m.objName_ == NULL);
    }
    {
        CoinRelFltEq eq;
        CoinLpIO m;
        std::string fn = lpDir + "exmip1.lp";
        m.readLp(fn.c_str());
        assert( !strcmp( m.problemName_ , ""));
        assert( !strcmp( m.objName_ , "OBJ"));
        // Test language and re-use
        m.newLanguage(CoinMessages::it);
        m.messageHandler()->setPrefix(false);
        m.readLp(fn.c_str());
        // Test copy constructor and assignment operator
        {
            CoinLpIO lhs;
            {
                CoinLpIO im(m);
                CoinLpIO imC1(im);
                assert( imC1.getNumCols() == im.getNumCols() );
                assert( imC1.getNumRows() == im.getNumRows() );

                for (int i = 0; i < im.numberHash_[0]; i++) {
                    // check the row name
                    assert(!strcmp(im.names_[0][i], imC1.names_[0][i]));
                }

                for (int i = 0; i < im.numberHash_[1]; i++) {
                    // check the column name
                    assert(!strcmp(im.names_[1][i], imC1.names_[1][i]));
                }

                for (int i = 0; i < im.maxHash_[0]; i++) {
                    // check hash value for row name
                    assert(im.hash_[0][i].next  == imC1.hash_[0][i].next);
                    assert(im.hash_[0][i].index == imC1.hash_[0][i].index);
                }

                for (int i = 0; i < im.maxHash_[1]; i++) {
                    // check hash value for column name
                    assert(im.hash_[1][i].next == imC1.hash_[1][i].next);
                    assert(im.hash_[1][i].index == imC1.hash_[1][i].index);
                }

                CoinLpIO imC2(im);
                assert( imC2.getNumCols() == im.getNumCols() );
                assert( imC2.getNumRows() == im.getNumRows() );
                lhs = imC2;
                assert( lhs.getNumCols() == imC2.getNumCols() );
                assert( lhs.getNumRows() == imC2.getNumRows() );

                for (int i = 0; i < imC2.numberHash_[0]; i++) {
                    // check the row name
                    assert(!strcmp(lhs.names_[0][i], imC2.names_[0][i]));
                }

                for (int i = 0; i < imC2.numberHash_[1]; i++) {
                    // check the column name
                    assert(!strcmp(lhs.names_[1][i], imC2.names_[1][i]));
                }

                for (int i = 0; i < imC2.maxHash_[0]; i++) {
                    // check hash value for row name
                    assert(lhs.hash_[0][i].next  == imC2.hash_[0][i].next);
                    assert(lhs.hash_[0][i].index == imC2.hash_[0][i].index);
                }

                for (int i = 0; i < im.maxHash_[1]; i++) {
                    // check hash value for column name
                    assert(lhs.hash_[1][i].next == imC2.hash_[1][i].next);
                    assert(lhs.hash_[1][i].index == imC2.hash_[1][i].index);
                }
            }
            // Test that lhs has correct values even though rhs has gone out of scope
            assert( lhs.getNumCols() == m.getNumCols() );
            assert( lhs.getNumRows() == m.getNumRows() );
        }
        {
            CoinLpIO dumSi(m);
            int nc = dumSi.getNumCols();
            int nr = dumSi.getNumRows();
            const double* cl = dumSi.getColLower();
            const double* cu = dumSi.getColUpper();
            const double* rl = dumSi.getRowLower();
            const double* ru = dumSi.getRowUpper();
            assert( nc == 10 );
            assert( nr == 5 );
            assert( eq(cl[0], 2.5) );
            assert( eq(cl[1], 0.5) );
            assert( eq(cu[1], 4) );
            assert( eq(cu[2], 4.3) );
            assert( eq(rl[0], 2.5) );
            assert( eq(rl[4], 15.0) );
            assert( eq(ru[1], 2.1) );
            assert( eq(ru[4], 15.0) );
            assert( !eq(cl[3], 1.2345) );
            assert( !eq(cu[4], 10.2345) );
            assert( eq( dumSi.getObjCoefficients()[0],  1.0) );
            assert( eq( dumSi.getObjCoefficients()[1],  2.0) );
            assert( eq( dumSi.getObjCoefficients()[2],  -1.0) );
            assert( eq( dumSi.getObjCoefficients()[3],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[4],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[5],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[6],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[7],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[8],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[9],  0.0) );
            dumSi.writeLp("CoinLpIoTest.lp", true);
        }
        // Read just written file
        {
            CoinLpIO dumSi;
            dumSi.readLp("CoinLpIoTest.lp");
            int nc = dumSi.getNumCols();
            int nr = dumSi.getNumRows();
            const double* cl = dumSi.getColLower();
            const double* cu = dumSi.getColUpper();
            const double* rl = dumSi.getRowLower();
            const double* ru = dumSi.getRowUpper();
            assert( nc == 10 );
            assert( nr == 5 );
            assert( eq(cl[0], 2.5) );
            assert( eq(cl[1], 0.5) );
            assert( eq(cu[1], 4) );
            assert( eq(cu[2], 4.3) );
            assert( eq(rl[0], 2.5) );
            assert( eq(rl[4], 15.0) );
            assert( eq(ru[1], 2.1) );
            assert( eq(ru[4], 15.0) );
            assert( !eq(cl[3], 1.2345) );
            assert( !eq(cu[4], 10.2345) );
            assert( eq( dumSi.getObjCoefficients()[0],  1.0) );
            assert( eq( dumSi.getObjCoefficients()[1],  2.0) );
            assert( eq( dumSi.getObjCoefficients()[2], -1.0) );
            assert( eq( dumSi.getObjCoefficients()[3],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[4],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[5],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[6],  0.0) );
            assert( eq( dumSi.getObjCoefficients()[7],  0.0) );
        }
        // Test matrixByRow method
        {
            const CoinLpIO si(m);
            const CoinPackedMatrix* smP = si.getMatrixByRow();
            CoinRelFltEq eq;
            const double* ev = smP->getElements();
            assert( eq(ev[0],   3.0) );
            assert( eq(ev[1],   1.0) );
            assert( eq(ev[2],  -2.0) );
            assert( eq(ev[3],  -1.0) );
            assert( eq(ev[4],  -1.0) );
            assert( eq(ev[5],   2.0) );
            assert( eq(ev[6],   1.1) );
            assert( eq(ev[7],   1.0) );
            assert( eq(ev[8],   1.0) );
            assert( eq(ev[9],   2.8) );
            assert( eq(ev[10], -1.2) );
            assert( eq(ev[11], -1.0 ) );
            assert( eq(ev[12],  5.6) );
            assert( eq(ev[13],  1.0) );
            assert( eq(ev[14],  1.9) );
            assert( eq(ev[15], -1.0) );
            const CoinBigIndex* mi = smP->getVectorStarts();
            assert( mi[0] == 0 );
            assert( mi[1] == 5 );
            assert( mi[2] == 7 );
            assert( mi[3] == 9 );
            assert( mi[4] == 12 );
            assert( mi[5] == 16 );
            const int* ei = smP->getIndices();
            assert( ei[0]  ==  0 );
            assert( ei[1]  ==  3 );
            assert( ei[2]  ==  4 );
            assert( ei[3]  ==  1 );
            assert( ei[4]  ==  2 );
            assert( ei[5]  ==  3 );
            assert( ei[6]  ==  5 );
            assert( ei[7]  ==  5 );
            assert( ei[8]  ==  6 );
            assert( ei[9]  ==  4 );
            assert( ei[10] ==  7 );
            assert( ei[11] ==  8 );
            assert( ei[12] ==  0 );
            assert( ei[13] ==  1 );
            assert( ei[14] ==  2 );
            assert( ei[15] ==  9 );
            assert( smP->getMajorDim() == 5 );
            assert( smP->getNumElements() == 16 );
        }
        // Test matrixByCol method
        {
            const CoinLpIO si(m);
            const CoinPackedMatrix* smP = si.getMatrixByCol();
            CoinRelFltEq eq;
            const double* ev = smP->getElements();
            assert( eq(ev[0],   3.0) );
            assert( eq(ev[1],   5.6) );
            assert( eq(ev[2],   -1.0) );
            assert( eq(ev[3],   1.0) );
            assert( eq(ev[4],   -1.0) );
            assert( eq(ev[5],   1.9) );
            assert( eq(ev[6],   1.0) );
            assert( eq(ev[7],   2.0) );
            assert( eq(ev[8],   -2.0) );
            assert( eq(ev[9],   2.8) );
            assert( eq(ev[10],  1.1) );
            assert( eq(ev[11],  1.0) );
            assert( eq(ev[12],  1.0) );
            assert( eq(ev[13],  -1.2) );
            assert( eq(ev[14],  -1.0) );
            assert( eq(ev[15],  -1.0) );
            const CoinBigIndex* mi = smP->getVectorStarts();
            assert( mi[0] == 0 );
            assert( mi[1] == 2 );
            assert( mi[2] == 4 );
            assert( mi[3] == 6 );
            assert( mi[4] == 8 );
            assert( mi[5] == 10 );
            assert( mi[6] == 12 );
            assert( mi[7] == 13 );
            assert( mi[8] == 14 );
            assert( mi[9] == 15 );
            assert( mi[10] == 16 );
            const int* ei = smP->getIndices();
            assert( ei[0]  ==  0 );
            assert( ei[1]  ==  4 );
            assert( ei[2]  ==  0 );
            assert( ei[3]  ==  4 );
            assert( ei[4]  ==  0  );
            assert( ei[5]  ==  4 );
            assert( ei[6]  ==  0 );
            assert( ei[7]  ==  1 );
            assert( ei[8]  ==  0 );
            assert( ei[9]  ==  3 );
            assert( ei[10] ==  1 );
            assert( ei[11] ==  2 );
            assert( ei[12] ==  2 );
            assert( ei[13] ==  3 );
            assert( ei[14] ==  3 );
            assert( ei[15] ==  4 );
            assert( smP->getMajorDim() == 10 );
            assert( smP->getNumElements() == 16 );
            assert( smP->getSizeVectorStarts() == 11 );
            assert( smP->getMinorDim() == 5 );
        }
        //--------------
        // Test rowsense, rhs, rowrange, matrixByRow
        {
            CoinLpIO lhs;
            {
                assert( lhs.rowrange_ == NULL );
                assert( lhs.rowsense_ == NULL );
                assert( lhs.rhs_ == NULL );
                assert( lhs.matrixByColumn_ == NULL );
                CoinLpIO siC1(m);
                assert( siC1.rowrange_ != NULL );
                assert( siC1.rowsense_ != NULL );
                assert( siC1.rhs_ != NULL );
                assert( siC1.matrixByRow_ != NULL );
                const char*    siC1rs  = siC1.getRowSense();
                assert( siC1rs[0] == 'G' );
                assert( siC1rs[1] == 'L' );
                assert( siC1rs[2] == 'E' );
                assert( siC1rs[3] == 'E' );
                assert( siC1rs[4] == 'E' );
                const double* siC1rhs = siC1.getRightHandSide();
                assert( eq(siC1rhs[0], 2.5) );
                assert( eq(siC1rhs[1], 2.1) );
                assert( eq(siC1rhs[2], 4.0) );
                assert( eq(siC1rhs[3], 1.8) );
                assert( eq(siC1rhs[4], 15.) );
                const double* siC1rr  = siC1.getRowRange();
                assert( eq(siC1rr[0], 0.0) );
                assert( eq(siC1rr[1], 0.0) );
                assert( eq(siC1rr[2], 0.0) );
                assert( eq(siC1rr[3], 0.0) );
                assert( eq(siC1rr[4], 0.0) );
                const CoinPackedMatrix* siC1mbr = siC1.getMatrixByRow();
                assert( siC1mbr != NULL );
                const double* ev = siC1mbr->getElements();
                assert( eq(ev[0],   3.0) );
                assert( eq(ev[1],   1.0) );
                assert( eq(ev[2],  -2.0) );
                assert( eq(ev[3],  -1.0) );
                assert( eq(ev[4],  -1.0) );
                assert( eq(ev[5],   2.0) );
                assert( eq(ev[6],   1.1) );
                assert( eq(ev[7],   1.0) );
                assert( eq(ev[8],   1.0) );
                assert( eq(ev[9],   2.8) );
                assert( eq(ev[10], -1.2) );
                assert( eq(ev[11], -1.0) );
                assert( eq(ev[12],  5.6) );
                assert( eq(ev[13],  1.0) );
                assert( eq(ev[14],  1.9) );
                assert( eq(ev[15], -1.0) );
                const CoinBigIndex* mi = siC1mbr->getVectorStarts();
                assert( mi[0] == 0 );
                assert( mi[1] == 5 );
                assert( mi[2] == 7 );
                assert( mi[3] == 9 );
                assert( mi[4] == 12 );
                assert( mi[5] == 16 );
                const int* ei = siC1mbr->getIndices();
                assert( ei[0]  ==  0 );
                assert( ei[1]  ==  3 );
                assert( ei[2]  ==  4 );
                assert( ei[3]  ==  1 );
                assert( ei[4]  ==  2 );
                assert( ei[5]  ==  3 );
                assert( ei[6]  ==  5 );
                assert( ei[7]  ==  5 );
                assert( ei[8]  ==  6 );
                assert( ei[9]  ==  4 );
                assert( ei[10] ==  7 );
                assert( ei[11] ==  8 );
                assert( ei[12] ==  0 );
                assert( ei[13] ==  1 );
                assert( ei[14] ==  2 );
                assert( ei[15] ==  9 );
                assert( siC1mbr->getMajorDim() == 5 );
                assert( siC1mbr->getNumElements() == 16 );
                assert( siC1rs  == siC1.getRowSense() );
                assert( siC1rhs == siC1.getRightHandSide() );
                assert( siC1rr  == siC1.getRowRange() );
            }
        }
    }
}
Ejemplo n.º 2
0
// retrieve all missing values of LP/MILP
extern "C" int read_lp_file_mp(char * fname)
{
  int * filename_addr;  // char - Input

  int nb_constr;
  int nb_obj_var;
  int nb_val_constr_mat;
  int index_start;

  char ** RowNames = NULL;
  char ** ColNames = NULL;
  char * filename  = NULL;
  double * obj_var_is_int = NULL;

  static char * ListLabels [] = {"plist","constr_mat","constr_sense","obj_coeff",
				 "rhs","lhs","bounds_lower","bounds_upper","obj_var_is_int",
				 "pb_name","col_name","row_name"};

  CoinLpIO           LpReader;
  static SciSparse * ConstrMat = NULL;
  int i, j;
  SciErr _SciErr;

#ifdef DEBUG
  DBGPRINTF("DEBUG: number of parameters = %d\n", Rhs);
#endif

  _SciErr = getVarAddressFromPosition(pvApiCtx, 1, &filename_addr); SCICOINOR_ERROR;
  getAllocatedSingleString(pvApiCtx, filename_addr, &filename);

  ifstream infile;
  infile.open(filename,ifstream::in);
  if (!infile.is_open())
    {
      sciprint("read_lp_file_mp: error while opening file %s\n", filename);
      freeAllocatedSingleString(filename);
      return 0;
    }
  infile.close();

  LpReader.readLp(filename);

  // retrieve number of constraints
  nb_constr = LpReader.getNumRows();
  
  // retrieve number of objective variables
  nb_obj_var = LpReader.getNumCols();
  
  // retrieve number of non-zero elements in constraint matrix
  nb_val_constr_mat = LpReader.getNumElements();

  ///////////////////////////////////////////
  // Create of the output scilab variables //
  ///////////////////////////////////////////

  // First, allocate a new sparse matrix
  ConstrMat      = (SciSparse *)MALLOC(1*sizeof(SciSparse));

  ConstrMat->n    = nb_obj_var;
  ConstrMat->m    = nb_constr;
  ConstrMat->it   = 0;
  ConstrMat->nel  = nb_val_constr_mat;
  ConstrMat->mnel = (int *)MALLOC(nb_constr*sizeof(int));
  ConstrMat->icol = (int *)MALLOC(nb_val_constr_mat*sizeof(int));
  ConstrMat->R    = (double *)MALLOC(nb_val_constr_mat*sizeof(double));

  if ((ConstrMat==(SciSparse *)0) || (ConstrMat->mnel==NULL) || (ConstrMat->R==NULL))
    {
      Scierror(999, "%s: error while allocating the sparse\n",fname);
      return 0;
    }
  
  // Copy the sense of constraintes
  // 'L' - smaller than - <=
  // 'E' - equality     - =
  // 'G' - greater than - >=
  // 'R' - Range        - <= + >=
  // 'N' - Free         - no constraints

  // Copy Row name and column names
  RowNames = (char **)MALLOC(sizeof(char *) * LpReader.getNumRows());
  for(i=0;i<LpReader.getNumRows();i++)
    {
      RowNames[i] = (char *)MALLOC(sizeof(char) * (strlen(LpReader.rowName(i))+1));
      strncpy(RowNames[i],LpReader.rowName(i),strlen(LpReader.rowName(i))+1);
    }

  ColNames = (char **)MALLOC(sizeof(char *) * LpReader.getNumCols());
  for(i=0;i<LpReader.getNumCols();i++)
    {
      ColNames[i] = (char *)MALLOC(sizeof(char) * (strlen(LpReader.columnName(i))+1));
      strncpy(ColNames[i],LpReader.columnName(i),strlen(LpReader.columnName(i))+1);
    }

#ifdef DEBUG
  sciprint("constr_sense = %s\n", LpReader.getRowSense());
#endif
  
  // retrieve column specific data (values, bounds and type)
  obj_var_is_int = (double *)MALLOC(nb_obj_var*sizeof(double));

  for (i=0; i<nb_obj_var; i++) 
    {
      // set to TRUE if objective variable is integer or binary  
      if (LpReader.isInteger(i)) *(obj_var_is_int+i) = 1;
      else                       *(obj_var_is_int+i) = 0;
    }
      
  // the constraint matrix
  for (i=0; i<LpReader.getMatrixByRow()->getSizeVectorLengths(); i++) 
    {
      ConstrMat->mnel[i] = LpReader.getMatrixByRow()->getVectorLengths()[i];
      index_start = LpReader.getMatrixByRow()->getVectorStarts()[i];
#ifdef DEBUG
      DBGPRINTF("mat[%d] : mnel = %d index_start = %d\n",i,ConstrMat->mnel[i],index_start);
#endif
      for(j=0;j<ConstrMat->mnel[i];j++)
	{
	  ConstrMat->icol[index_start+j] = LpReader.getMatrixByRow()->getIndices()[index_start+j] + 1;
	  ConstrMat->R[index_start+j]    = LpReader.getMatrixByRow()->getElements()[index_start+j];
#ifdef DEBUG
	  DBGPRINTF("mat[%d][%d] : icol = %d R = %f\n",i,j,ConstrMat->icol[index_start+j],ConstrMat->R[index_start+j]);
#endif
	}
    }

#ifdef DEBUG
  DBGPRINTF("DEBUG: nb_constr  = %d\n", nb_constr);
  DBGPRINTF("DEBUG: nb_obj_var = %d\n", nb_obj_var);
  DBGPRINTF("DEBUG: nb_val_constr_mat = %d\n", nb_val_constr_mat);

  DBGPRINTF("DEBUG: constrmat_it  = %d\n", constrmat_it);
  DBGPRINTF("DEBUG: constrmat_m   = %d\n", constrmat_m);
  DBGPRINTF("DEBUG: constrmat_n   = %d\n", constrmat_n);
  DBGPRINTF("DEBUG: constrmat_nel = %d\n", constrmat_nel);

  DBGPRINTF("DEBUG: ConstrMat->it  = %d\n", ConstrMat->it);
  DBGPRINTF("DEBUG: ConstrMat->m   = %d\n", ConstrMat->m);
  DBGPRINTF("DEBUG: ConstrMat->n   = %d\n", ConstrMat->n);
  DBGPRINTF("DEBUG: ConstrMat->nel = %d\n", ConstrMat->nel);
  DBGPRINTF("Exiting ...\n");
#endif

  // Now create the mlist of type plist
  int * list_addr = NULL;
  char * tmp_char = NULL;

  _SciErr = createMList(pvApiCtx, Rhs+1, 12, &list_addr); SCICOINOR_ERROR;
  _SciErr = createMatrixOfStringInList(pvApiCtx, Rhs+1, list_addr, 1, 1, 12, ListLabels); SCICOINOR_ERROR;
  _SciErr = createSparseMatrixInList(pvApiCtx, Rhs+1, list_addr, 2, 
				     ConstrMat->m, 
				     ConstrMat->n, 
				     ConstrMat->nel,  
				     ConstrMat->mnel, 
				     ConstrMat->icol,
				     ConstrMat->R); SCICOINOR_ERROR;
  tmp_char = (char *)LpReader.getRowSense();
  _SciErr = createMatrixOfStringInList(pvApiCtx, Rhs+1, list_addr, 3, 1, 1, &tmp_char); SCICOINOR_ERROR;
  _SciErr = createMatrixOfDoubleInList(pvApiCtx, Rhs+1, list_addr, 4, 1, nb_obj_var, (double *)LpReader.getObjCoefficients()); SCICOINOR_ERROR;
  _SciErr = createMatrixOfDoubleInList(pvApiCtx, Rhs+1, list_addr, 5, 1, nb_constr,  (double *)LpReader.getRowUpper()); SCICOINOR_ERROR;
  _SciErr = createMatrixOfDoubleInList(pvApiCtx, Rhs+1, list_addr, 6, 1, nb_constr,  (double *)LpReader.getRowLower()); SCICOINOR_ERROR;
  _SciErr = createMatrixOfDoubleInList(pvApiCtx, Rhs+1, list_addr, 7, 1, nb_obj_var, (double *)LpReader.getColLower()); SCICOINOR_ERROR;
  _SciErr = createMatrixOfDoubleInList(pvApiCtx, Rhs+1, list_addr, 8, 1, nb_obj_var, (double *)LpReader.getColUpper()); SCICOINOR_ERROR;
  _SciErr = createMatrixOfDoubleInList(pvApiCtx, Rhs+1, list_addr, 9, 1, nb_obj_var, obj_var_is_int); SCICOINOR_ERROR;
  tmp_char = (char *)LpReader.getProblemName();
  _SciErr = createMatrixOfStringInList(pvApiCtx, Rhs+1, list_addr, 10, 1, 1, &tmp_char); SCICOINOR_ERROR;
  _SciErr = createMatrixOfStringInList(pvApiCtx, Rhs+1, list_addr, 11, 1, nb_obj_var, ColNames); SCICOINOR_ERROR;
  _SciErr = createMatrixOfStringInList(pvApiCtx, Rhs+1, list_addr, 12, 1, nb_constr,  RowNames); SCICOINOR_ERROR;

  LhsVar(1) = Rhs+1;

  if (ConstrMat->mnel) FREE(ConstrMat->mnel);
  if (ConstrMat->icol) FREE(ConstrMat->icol);
  if (ConstrMat->R)    FREE(ConstrMat->R);
  if (ConstrMat)       FREE(ConstrMat);

  freeArrayOfString(RowNames,LpReader.getNumRows());
  freeArrayOfString(ColNames,LpReader.getNumCols());

  freeAllocatedSingleString(filename);
  FREE(obj_var_is_int);

  return 0;
}