src/main/ctu_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 testComplementTotalUnimodularity(
  const char* instanceFileName, 
  FileFormat inputFormat,       
  FileFormat outputFormat,      
  bool outputComplements,       
  bool outputComplemented,      
  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 isCTU;
  size_t complementRow = SIZE_MAX;
  size_t complementColumn = SIZE_MAX;
  CMR_CTU_STATISTICS stats;
  CMR_CALL( CMRstatsComplementTotalUnimodularityInit(&stats) );
  CMR_CALL( CMRtestComplementTotalUnimodularity(cmr, matrix, &isCTU, &complementRow, &complementColumn, &stats) );
  fprintf(stderr, "Matrix %scomplement totally unimodular.\n", isCTU ? "IS " : "IS NOT ");
  if (printStats)
    CMR_CALL( CMRstatsComplementTotalUnimodularityPrint(stderr, &stats, NULL) );
  if (complementRow < SIZE_MAX || complementColumn < SIZE_MAX)
  {
    if (outputComplements)
    {
      fprintf(stderr, "\nNon-TU matrix is obtained by");
      if (complementRow < SIZE_MAX)
      {
        fprintf(stderr, " complementing row %lu", complementRow + 1);
        if (complementColumn < SIZE_MAX)
          fprintf(stderr, " and complementing column %lu", complementColumn + 1);
      }
      else
        fprintf(stderr, " complementing column %lu", complementColumn + 1);
      fprintf(stderr, ".\n");
    }
    if (outputComplemented)
    {
      CMR_CHRMAT* complemented = NULL;
      CMR_CALL( CMRcomplementRowColumn(cmr, matrix, complementRow, complementColumn, &complemented) );
      fprintf(stderr, "\nComplemented non-TU matrix with %lu nonzeros:\n", complemented->numNonzeros);
      if (outputFormat == FILEFORMAT_MATRIX_DENSE)
        CMR_CALL( CMRchrmatPrintDense(cmr, complemented, stdout, '0', false) );
      else if (outputFormat == FILEFORMAT_MATRIX_SPARSE)
        CMR_CALL( CMRchrmatPrintSparse(cmr, complemented, stdout) );
      CMR_CALL( CMRchrmatFree(cmr, &complemented) );
    }
  }
  CMR_CALL( CMRchrmatFree(cmr, &matrix) );
  CMR_CALL( CMRfreeEnvironment(&cmr) );
  return CMR_OKAY;
}
static CMR_ERROR testComplementTotalUnimodularity ( const char * instanceFileName , FileFormat inputFormat , FileFormat outputFormat , bool outputComplements , bool outputComplemented , 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 isCTU ; size_t complementRow = SIZE_MAX ; size_t complementColumn = SIZE_MAX ; CMR_CTU_STATISTICS stats ; CMR_CALL ( CMRstatsComplementTotalUnimodularityInit ( & stats ) ) ; CMR_CALL ( CMRtestComplementTotalUnimodularity ( cmr , matrix , & isCTU , & complementRow , & complementColumn , & stats ) ) ; fprintf ( stderr , " \n " , isCTU ? " " : " " ) ; if ( printStats ) CMR_CALL ( CMRstatsComplementTotalUnimodularityPrint ( stderr , & stats , NULL ) ) ; if ( complementRow < SIZE_MAX || complementColumn < SIZE_MAX ) { if ( outputComplements ) { fprintf ( stderr , " \n " ) ; if ( complementRow < SIZE_MAX ) { fprintf ( stderr , " " , complementRow + 1 ) ; if ( complementColumn < SIZE_MAX ) fprintf ( stderr , " " , complementColumn + 1 ) ; } else fprintf ( stderr , " " , complementColumn + 1 ) ; fprintf ( stderr , " \n " ) ; } if ( outputComplemented ) { CMR_CHRMAT * complemented = NULL ; CMR_CALL ( CMRcomplementRowColumn ( cmr , matrix , complementRow , complementColumn , & complemented ) ) ; fprintf ( stderr , " \n \n " , complemented -> numNonzeros ) ; if ( outputFormat == FILEFORMAT_MATRIX_DENSE ) CMR_CALL ( CMRchrmatPrintDense ( cmr , complemented , stdout , ' ' , false ) ) ; else if ( outputFormat == FILEFORMAT_MATRIX_SPARSE ) CMR_CALL ( CMRchrmatPrintSparse ( cmr , complemented , stdout ) ) ; CMR_CALL ( CMRchrmatFree ( cmr , & complemented ) ) ; } } 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 complement operations leading to a non-TU matrix. */ /**< Whether to print the complemented non-TU matrix. */ /**< Whether to print statistics to stderr. */ /* Read matrix. */ /* Actual test. */ /* Cleanup. */