src/main/camion_main.c
/**
 * \brief Tests matrix from a file for total unimodularity.
 */
\brief Tests matrix from a file for total unimodularity.
\ brief Tests matrix from a file for total unimodularity .
static
CMR_ERROR testCamionSigned(
  const char* instanceFileName, 
  FileFormat inputFormat,       
  FileFormat outputFormat,      
  bool outputSubmatrixElements, 
  bool outputSubmatrix,         
  bool printStats               
)
{
  clock_t readClock = clock();
  FILE* instanceFile = strcmp(instanceFileName, "-") ? fopen(instanceFileName, "r") : stdin;
  if (!instanceFile)
    return CMR_ERROR_INPUT;
  CMR* cmr = NULL;
  CMR_CALL( CMRcreateEnvironment(&cmr) );
  CMR_CHRMAT* matrix = NULL;
  if (inputFormat == FILEFORMAT_MATRIX_DENSE)
    CMR_CALL( CMRchrmatCreateFromDenseStream(cmr, instanceFile, &matrix) );
  else if (inputFormat == FILEFORMAT_MATRIX_SPARSE)
    CMR_CALL( CMRchrmatCreateFromSparseStream(cmr, instanceFile, &matrix) );
  if (instanceFile != stdin)
    fclose(instanceFile);
  fprintf(stderr, "Read %lux%lu matrix with %lu nonzeros in %f seconds.\n", matrix->numRows, matrix->numColumns,
    matrix->numNonzeros, (clock() - readClock) * 1.0 / CLOCKS_PER_SEC);
  bool isCamion;
  CMR_SUBMAT* submatrix = NULL;
  CMR_CAMION_STATISTICS stats;
  CMR_CALL( CMRstatsCamionInit(&stats) );
  CMR_CALL( CMRtestCamionSigned(cmr, matrix, &isCamion,
    (outputFormat || outputSubmatrixElements) ? &submatrix : NULL, printStats ? &stats : NULL) );
  fprintf(stderr, "Matrix %sCamion-signed.\n", isCamion ? "IS " : "IS NOT ");
  if (printStats)
    CMR_CALL( CMRstatsCamionPrint(stderr, &stats, NULL) );
  if (submatrix)
  {
    if (outputSubmatrixElements)
    {
      fprintf(stderr, "\nNon-camion submatrix consists of these elements:\n");
      printf("%ld rows:", submatrix->numRows);
      for (size_t r = 0; r < submatrix->numRows; ++r)
        printf(" %ld", submatrix->rows[r]+1);
      printf("\n%ld columns: ", submatrix->numColumns);
      for (size_t c = 0; c < submatrix->numColumns; ++c)
        printf(" %ld", submatrix->columns[c]+1);
      printf("\n");
    }
    if (outputSubmatrix)
    {
      CMR_CHRMAT* violatorMatrix = NULL;
      CMR_CALL( CMRchrmatZoomSubmat(cmr, matrix, submatrix, &violatorMatrix) );
      fprintf(stderr, "\nExtracted %lux%lu non-camion submatrix with %lu nonzeros.\n", violatorMatrix->numRows,
        violatorMatrix->numColumns, violatorMatrix->numNonzeros);
      if (outputFormat == FILEFORMAT_MATRIX_DENSE)
        CMR_CALL( CMRchrmatPrintDense(cmr, violatorMatrix, stdout, '0', false) );
      else if (outputFormat == FILEFORMAT_MATRIX_SPARSE)
        CMR_CALL( CMRchrmatPrintSparse(cmr, violatorMatrix, stdout) );
      CMR_CALL( CMRchrmatFree(cmr, &violatorMatrix) );
    }
    CMR_CALL( CMRsubmatFree(cmr, &submatrix) );
  }
  CMR_CALL( CMRchrmatFree(cmr, &matrix) );
  CMR_CALL( CMRfreeEnvironment(&cmr) );
  return CMR_OKAY;
}
static CMR_ERROR testCamionSigned ( const char * instanceFileName , FileFormat inputFormat , FileFormat outputFormat , bool outputSubmatrixElements , bool outputSubmatrix , bool printStats ) { clock_t readClock = clock ( ) ; FILE * instanceFile = strcmp ( instanceFileName , " " ) ? fopen ( instanceFileName , " " ) : stdin ; if ( ! instanceFile ) return CMR_ERROR_INPUT ; CMR * cmr = NULL ; CMR_CALL ( CMRcreateEnvironment ( & cmr ) ) ; CMR_CHRMAT * matrix = NULL ; if ( inputFormat == FILEFORMAT_MATRIX_DENSE ) CMR_CALL ( CMRchrmatCreateFromDenseStream ( cmr , instanceFile , & matrix ) ) ; else if ( inputFormat == FILEFORMAT_MATRIX_SPARSE ) CMR_CALL ( CMRchrmatCreateFromSparseStream ( cmr , instanceFile , & matrix ) ) ; if ( instanceFile != stdin ) fclose ( instanceFile ) ; fprintf ( stderr , " \n " , matrix -> numRows , matrix -> numColumns , matrix -> numNonzeros , ( clock ( ) - readClock ) * 1.0 / CLOCKS_PER_SEC ) ; bool isCamion ; CMR_SUBMAT * submatrix = NULL ; CMR_CAMION_STATISTICS stats ; CMR_CALL ( CMRstatsCamionInit ( & stats ) ) ; CMR_CALL ( CMRtestCamionSigned ( cmr , matrix , & isCamion , ( outputFormat || outputSubmatrixElements ) ? & submatrix : NULL , printStats ? & stats : NULL ) ) ; fprintf ( stderr , " \n " , isCamion ? " " : " " ) ; if ( printStats ) CMR_CALL ( CMRstatsCamionPrint ( stderr , & stats , NULL ) ) ; if ( submatrix ) { if ( outputSubmatrixElements ) { fprintf ( stderr , " \n \n " ) ; printf ( " " , submatrix -> numRows ) ; for ( size_t r = 0 ; r < submatrix -> numRows ; ++ r ) printf ( " " , submatrix -> rows [ r ] + 1 ) ; printf ( " \n " , submatrix -> numColumns ) ; for ( size_t c = 0 ; c < submatrix -> numColumns ; ++ c ) printf ( " " , submatrix -> columns [ c ] + 1 ) ; printf ( " \n " ) ; } if ( outputSubmatrix ) { CMR_CHRMAT * violatorMatrix = NULL ; CMR_CALL ( CMRchrmatZoomSubmat ( cmr , matrix , submatrix , & violatorMatrix ) ) ; fprintf ( stderr , " \n \n " , violatorMatrix -> numRows , violatorMatrix -> numColumns , violatorMatrix -> numNonzeros ) ; if ( outputFormat == FILEFORMAT_MATRIX_DENSE ) CMR_CALL ( CMRchrmatPrintDense ( cmr , violatorMatrix , stdout , ' ' , false ) ) ; else if ( outputFormat == FILEFORMAT_MATRIX_SPARSE ) CMR_CALL ( CMRchrmatPrintSparse ( cmr , violatorMatrix , stdout ) ) ; CMR_CALL ( CMRchrmatFree ( cmr , & violatorMatrix ) ) ; } CMR_CALL ( CMRsubmatFree ( cmr , & submatrix ) ) ; } CMR_CALL ( CMRchrmatFree ( cmr , & matrix ) ) ; CMR_CALL ( CMRfreeEnvironment ( & cmr ) ) ; return CMR_OKAY ; }
\brief Tests matrix from a file for total unimodularity.
\ brief Tests matrix from a file for total unimodularity .
/**< File name containing the input matrix (may be `-' for stdin). */ /**< Format of the input matrix. */ /**< Format of the output submatrix. */ /**< Whether to print the elements of a non-camion submatrix. */ /**< Whether to print a non-camion submatrix. */ /**< Whether to print statistics to stderr. */ /* Read matrix. */ /* Actual test. */ /* Cleanup. */