Пример #1
0
void
hull(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
	dd_PolyhedraPtr P;
	dd_ErrorType err;
	dd_MatrixPtr H,V;
	
	if (nrhs  == 1 && nlhs == 1 && mxIsStruct(prhs[0])) {
		V = FT_get_V_MatrixPtr(prhs[0]);		
		dd_set_global_constants();  /* First, this must be called. */

		P = dd_DDMatrix2Poly(V, &err); /* compute the second representation */
		if (err == dd_NoError) {
			H = dd_CopyInequalities(P);
			plhs[0] = FT_set_H_MatrixPtr(H);
			dd_FreeMatrix(H);
		} else {
    			dd_WriteErrorMessages(stdout,err);
    			mexErrMsgTxt("CDD returned an error, see above(!) for details");
  		}
		dd_FreeMatrix(V);
  		dd_FreePolyhedra(P);
  		return;
	} else {
		mexErrMsgTxt("hull expects a V input struct and produces an H output struct");
	}
}
Пример #2
0
void allfacets(int n_input, int d_input, double *g_input)
/* output facets and incidences */
{
  dd_PolyhedraPtr poly;
  dd_MatrixPtr A=NULL,G=NULL;
  dd_SetFamilyPtr AI=NULL;
  dd_rowrange i,n; 
  dd_colrange j,d;
  dd_ErrorType err;

  n=(dd_rowrange)n_input; d=(dd_colrange)d_input;
  G=dd_CreateMatrix(n,d);
  for (i=0; i<n; i++){
    for (j=0; j<d; j++) dd_set_d(G->matrix[i][j],g_input[i*d+j]);
  }
  G->representation=dd_Generator;
  poly=dd_DDMatrix2Poly(G, &err);
    /* compute the second (inequality) representation */
  if (err==dd_NoError){
    A=dd_CopyInequalities(poly);
    AI=dd_CopyIncidence(poly);

    MLPutFunction(stdlink,"List",2);
    dd_MLWriteMatrix(A);
    dd_MLWriteSetFamily(AI);
  } else {
    dd_MLWriteError(poly);
  }

  dd_FreeMatrix(A);
  dd_FreeMatrix(G);
  dd_FreeSetFamily(AI);
}
Пример #3
0
 bool init()
 {
     V_ = FromEigen(v);
     dd_ErrorType error = dd_NoError;
     /*dd_rowset redset,impl_linset;
     dd_rowindex newpos;
     dd_MatrixCanonicalize(&V_, &impl_linset, &redset, &newpos, &error);
     if(error != dd_NoError)
     {
         std::cout << ("can not reduce matrix") << std::endl;
     }
     fprintf(stdout, "\nRedundant rows: ");
     set_fwrite(stdout, redset);
     fprintf(stdout, "\n");
     set_free(redset);
     set_free(impl_linset);
     free(newpos);
     error = dd_NoError;*/
     H_= dd_DDMatrix2Poly(V_, &error);
     if(error != dd_NoError)
     {
         if(dd_debug)
             std::cout << ("numerical instability in cddlib. ill formed polytope") << std::endl;
     }
     else
     {
         init_= true;
         b_A = dd_CopyInequalities(H_);
         // get equalities and add them as complementary inequality constraints
         long elem;
         std::vector<long> eq_rows;
         for(elem=1;elem<=(long)(b_A->linset[0]);++elem)
         {
             if (set_member(elem,b_A->linset))
                eq_rows.push_back(elem);
         }
         int rowsize = (int)b_A->rowsize;
         A = matrix_t (rowsize + eq_rows.size(), (int)b_A->colsize-1);
         b = vector_t (rowsize + eq_rows.size());
         for(int i=0; i < rowsize; ++i)
         {
             b(i) = (value_type)(*(b_A->matrix[i][0]));
             for(int j=1; j < b_A->colsize; ++j)
             {
                 A(i, j-1) = -(value_type)(*(b_A->matrix[i][j]));
             }
         }
         int i = 0;
         for(std::vector<long int>::const_iterator cit = eq_rows.begin();
             cit != eq_rows.end(); ++cit, ++i)
         {
             b(rowsize + i) = -b((int)(*cit));
             A(rowsize + i) = -A((int)(*cit));
         }
     }
     return init_;
 }
Пример #4
0
void 
file_ine(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
/* Ine file input, V output, similar to extreme */
{
    dd_PolyhedraPtr poly;
    dd_MatrixPtr M;
    dd_ErrorType err;
    char *inputfile;
    FILE *reading=NULL;
    dd_MatrixPtr A, G;
    dd_SetFamilyPtr GI,GA;
    int buflen, status;
    
    dd_set_global_constants();  /* First, this must be called. */
    if (nrhs  == 1 && nlhs <=2 && mxIsChar(prhs[0])) {
        /*  dd_SetInputFile(&reading,inputfile, &err); */
        buflen = mxGetN(prhs[0]) + 1;
        inputfile= mxCalloc(buflen, sizeof(char));
        status = mxGetString(prhs[0], inputfile, buflen);
        if ( (reading = fopen(inputfile,"r") )== NULL) {
            mxErrMsgTxt("Input file not found\n");
            return;
        }
        printf(" Input file opened. \n");    
        M=dd_PolyFile2Matrix(reading, &err);
        
        if (err==dd_NoError) {
            poly=dd_DDMatrix2Poly(M, &err); /* compute the second representation */
            if (err!=dd_NoError) {
                dd_WriteErrorMessages(stdout,err);
                mxErrMsgTxt("CDD internal error\n");
                return;
            }
            A=dd_CopyInequalities(poly);
            G=dd_CopyGenerators(poly);
            GI=dd_CopyInputIncidence(poly);
            GA=dd_CopyAdjacency(poly);
            plhs[0] = FT_set_V_MatrixPtr(G);
            plhs[1] = ZH_set_Vlist(GI,GA);
            dd_FreePolyhedra(poly);
            dd_FreeMatrix(M);
            return;
        }
    }
    else {
        mexErrMsgTxt("file-ine expects an file input");
    }
    return;
}
Пример #5
0
int main(int argc, char *argv[])
{
  dd_PolyhedraPtr poly;
  dd_LPPtr lp;
  dd_MatrixPtr M,A;
  dd_ErrorType err=dd_NoError;
  dd_DataFileType inputfile,outputfile;
  FILE *reading=NULL, *writing;

  dd_set_global_constants();  /* First, this must be called. */

  if (argc>1) strcpy(inputfile,argv[1]);
  if (argc<=1 || !SetInputFile(&reading,argv[1])){
    dd_WriteProgramDescription(stdout);
    dd_SetInputFile(&reading,inputfile, &err);
  }
  if (err==dd_NoError) {
    M=dd_PolyFile2Matrix(reading, &err);
  }
  else {
    printf("Input file not found\n");
    goto _L99;
  }

  if (err!=dd_NoError) goto _L99;

  if (M->objective==dd_LPnone){ /* do representation conversion */
    poly=dd_DDMatrix2Poly2(M, dd_LexMin, &err);
    /* equivalent to poly=dd_DDMatrix2Poly2(M, &err) when the second argument is set to dd_LexMin. */
    if (err!=dd_NoError) goto _L99;

    dd_SetWriteFileName(inputfile, outputfile, 'o', poly->representation);
    SetWriteFile(&writing, outputfile);
    dd_WriteProgramDescription(writing);
    dd_WriteRunningMode(writing, poly);
    switch (poly->representation) {
    case dd_Inequality:
      fprintf(writing, "ext_file: Generators\n");
      A=dd_CopyGenerators(poly);
      dd_WriteMatrix(writing,A);
      dd_FreeMatrix(A);
      break;

    case dd_Generator:
      fprintf(writing, "ine_file: Inequalities\n");
      A=dd_CopyInequalities(poly);
      dd_WriteMatrix(writing,A);
      dd_FreeMatrix(A);
      break;

    default:
      break;
    }
    dd_WriteDDTimes(writing,poly);
    fclose(writing);

    dd_SetWriteFileName(inputfile, outputfile, 'a', poly->representation);
    SetWriteFile(&writing, outputfile);
    dd_WriteAdjacency(writing,poly);
    fclose(writing);

    dd_SetWriteFileName(inputfile, outputfile, 'j', poly->representation);
    SetWriteFile(&writing, outputfile);
    dd_WriteInputAdjacency(writing,poly);
    fclose(writing);

    dd_SetWriteFileName(inputfile, outputfile, 'i', poly->representation);
    SetWriteFile(&writing, outputfile);
    dd_WriteIncidence(writing,poly);
    fclose(writing);

    dd_SetWriteFileName(inputfile, outputfile, 'n', poly->representation);
    SetWriteFile(&writing, outputfile);
    dd_WriteInputIncidence(writing,poly);
    fclose(writing);

    dd_FreeMatrix(M);
    dd_FreePolyhedra(poly);

  } else { /* solve the LP */
    lp=dd_Matrix2LP(M, &err);  if (err!=dd_NoError) goto _L99;
    dd_LPSolve(lp,dd_DualSimplex,&err);  if (err!=dd_NoError) goto _L99;

    dd_SetWriteFileName(inputfile, outputfile, 's', M->representation);
    SetWriteFile(&writing, outputfile);
    dd_WriteLPResult(writing, lp, err);
    fclose(writing);

    dd_FreeMatrix(M);
    dd_FreeLPData(lp);
  }
_L99:
  if (err!=dd_NoError) dd_WriteErrorMessages(stdout,err);
  return 0;
}