コード例 #1
0
void
adj_extreme(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
	dd_PolyhedraPtr P;
	dd_ErrorType err;
	dd_MatrixPtr H,V;
	dd_SetFamilyPtr A;
	
	if (nrhs  == 1 && nlhs == 2 && mxIsStruct(prhs[0])) {
		dd_set_global_constants();  /* First, this must be called. */
		H = FT_get_H_MatrixPtr(prhs[0]);		

		P = dd_DDMatrix2Poly(H, &err); /* compute the second representation */
		if (err == dd_NoError) {
			V = dd_CopyGenerators(P);
			A = dd_CopyAdjacency(P);
			plhs[0] = FT_set_V_MatrixPtr(V);
			plhs[1] = FT_set_SetFamilyPtr(A);
			dd_FreeMatrix(V);
			dd_FreeSetFamily(A);
		} else {
    			dd_WriteErrorMessages(stdout,err);
    			mexErrMsgTxt("CDD returned an error, see above(!) for details");    			
  		}
		dd_FreeMatrix(H);
  		dd_FreePolyhedra(P);
  		return;
	} else {
		mexErrMsgTxt("adj_extreme expects an H input struct and produces a V output struct and the adjacency struct");
	}
}
コード例 #2
0
void
adjacency(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
	dd_PolyhedraPtr P;
	dd_ErrorType err;
	dd_MatrixPtr V;
	dd_SetFamilyPtr A;
	
		
	if (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) {
			A = dd_CopyInputAdjacency(P);
			plhs[0] = FT_set_SetFamilyPtr(A);
			dd_FreeSetFamily(A);
		} else {
    			dd_WriteErrorMessages(stdout,err);
    			mexErrMsgTxt("CDD returned an error, see above(!) for details");
  		}
		dd_FreeMatrix(V);
  		dd_FreePolyhedra(P);
	}
}
コード例 #3
0
void allvertices(int m_input, int d_input, double *a_input)
/* output vertices and incidences */
{
  dd_PolyhedraPtr poly;
  dd_MatrixPtr A=NULL,G=NULL;
  dd_SetFamilyPtr GI=NULL;
  dd_rowrange i,m; 
  dd_colrange j,d;
  dd_ErrorType err;

  m=(dd_rowrange)m_input; d=(dd_colrange)d_input;
  A=dd_CreateMatrix(m,d);
  for (i=0; i<m; i++){
    for (j=0; j<d; j++) dd_set_d(A->matrix[i][j],a_input[i*d+j]);
  }
  A->representation=dd_Inequality;
  poly=dd_DDMatrix2Poly(A, &err);
    /* compute the second (generator) representation */
  if (err==dd_NoError) {
    G=dd_CopyGenerators(poly);
    GI=dd_CopyIncidence(poly);

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

  dd_FreeMatrix(A);
  dd_FreeMatrix(G);
  dd_FreeSetFamily(GI);
}
コード例 #4
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");
	}
}
コード例 #5
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);
}
コード例 #6
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_;
 }
コード例 #7
0
ファイル: pplacer_cdd.c プロジェクト: boegel/pplacer
/*
 * Find the extreme vertices between lower_bound and upper_bound of the convex
 * hull of ineqs.
 *
 * ineqs: row-major order matrix of total length nrow*ncols
 * nrows: Number of rows in matrix
 * ncols: Number of columns in matrix
 * lower_bound: lower bound of solution space on x-axis
 * upper_bound: upper bound of solution space on y-axis
 * output size: Set to size of result
 *
 * Returns array representing 3 by (output_size/3) matrix, where each row is:
 *   x_i, y_i, l_i
 * Where x_i, y_i are the coordinates of the vertex, and l_i is the index of
 * the input inequality that constrains the solution space *to the right* of
 * vertex i.
 */
double *extreme_vertices(const double *ineqs, const size_t nrows,
                         const size_t ncols, float lower_bound,
                         float upper_bound, /*OUT*/ size_t * output_size)
{

  /* Preconditions */
  assert(ineqs != NULL);
  assert(ncols == 3);
  assert(output_size != NULL);

  /* Check for approx equal lower and upper bound */
  if (abs(lower_bound - upper_bound) < EPS)
    return NULL;

  assert(lower_bound <= upper_bound);

  /*
   * Initialize library
   * TODO: Do we want to do this on every call?
   */
  dd_set_global_constants();
  dd_ErrorType err;
  dd_MatrixPtr generators;
  dd_MatrixPtr m =
      init_ineq_doubles(ineqs, nrows, ncols, lower_bound, upper_bound);
  dd_SetFamilyPtr incidence;

  /* Outputs */
  dd_PolyhedraPtr poly = dd_DDMatrix2Poly(m, &err);
  if (err != dd_NoError) {
    return NULL;
  }

  /* Get generators */
  generators = dd_CopyGenerators(poly);

  /* Get incidence */
  incidence = dd_CopyIncidence(poly);

  double *result =
      list_extreme_vertices(generators, incidence, nrows, m->rowsize - 1,
          output_size);

  dd_FreeMatrix(m);
  dd_FreeMatrix(generators);
  dd_FreePolyhedra(poly);
  dd_FreeSetFamily(incidence);
  dd_free_global_constants();

  return result;
}
コード例 #8
0
ファイル: cddmex.c プロジェクト: zhihan/checkmate
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;
}
コード例 #9
0
ファイル: cddproj.c プロジェクト: adsnaider/Robotics-Project
dd_MatrixPtr dd_BlockElimination(dd_MatrixPtr M, dd_colset delset, dd_ErrorType *error)
/* Eliminate the variables (columns) delset by
   the Block Elimination with dd_DoubleDescription algorithm.

   Given (where y is to be eliminated):
   c1 + A1 x + B1 y >= 0
   c2 + A2 x + B2 y =  0

   1. First construct the dual system:  z1^T B1 + z2^T B2 = 0, z1 >= 0.
   2. Compute the generators of the dual.
   3. Then take the linear combination of the original system with each generator.
   4. Remove redundant inequalies.

*/
{
  dd_MatrixPtr Mdual=NULL, Mproj=NULL, Gdual=NULL;
  dd_rowrange i,h,m,mproj,mdual,linsize;
  dd_colrange j,k,d,dproj,ddual,delsize;
  dd_colindex delindex;
  mytype temp,prod;
  dd_PolyhedraPtr dualpoly;
  dd_ErrorType err=dd_NoError;
  dd_boolean localdebug=dd_FALSE;

  *error=dd_NoError;
  m= M->rowsize;
  d= M->colsize;
  delindex=(long*)calloc(d+1,sizeof(long));
  dd_init(temp);
  dd_init(prod);

  k=0; delsize=0;
  for (j=1; j<=d; j++){
    if (set_member(j, delset)){
      k++;  delsize++;
      delindex[k]=j;  /* stores the kth deletion column index */
    }
  }
  if (localdebug) dd_WriteMatrix(stdout, M);

  linsize=set_card(M->linset);
  ddual=m+1;
  mdual=delsize + m - linsize;  /* #equalitions + dimension of z1 */

  /* setup the dual matrix */
  Mdual=dd_CreateMatrix(mdual, ddual);
  Mdual->representation=dd_Inequality;
  for (i = 1; i <= delsize; i++){
    set_addelem(Mdual->linset,i);  /* equality */
    for (j = 1; j <= m; j++) {
      dd_set(Mdual->matrix[i-1][j], M->matrix[j-1][delindex[i]-1]);
    }
  } 

  k=0;
  for (i = 1; i <= m; i++){
    if (!set_member(i, M->linset)){
      /* set nonnegativity for the dual variable associated with
         each non-linearity inequality. */
      k++;
      dd_set(Mdual->matrix[delsize+k-1][i], dd_one);  
    }
  } 
  
  /* 2. Compute the generators of the dual system. */
  dualpoly=dd_DDMatrix2Poly(Mdual, &err);
  Gdual=dd_CopyGenerators(dualpoly);

  /* 3. Take the linear combination of the original system with each generator.  */
  dproj=d-delsize;
  mproj=Gdual->rowsize;
  Mproj=dd_CreateMatrix(mproj, dproj);
  Mproj->representation=dd_Inequality;
  set_copy(Mproj->linset, Gdual->linset);

  for (i=1; i<=mproj; i++){
    k=0;
    for (j=1; j<=d; j++){
      if (!set_member(j, delset)){
        k++;  /* new index of the variable x_j  */
        dd_set(prod, dd_purezero);
        for (h = 1; h <= m; h++){
          dd_mul(temp,M->matrix[h-1][j-1],Gdual->matrix[i-1][h]); 
          dd_add(prod,prod,temp);
        }
        dd_set(Mproj->matrix[i-1][k-1],prod);
      }
    }
  }
  if (localdebug) printf("Size of the projection system: %ld x %ld\n", mproj, dproj);
  
  dd_FreePolyhedra(dualpoly);
  free(delindex);
  dd_clear(temp);
  dd_clear(prod);
  dd_FreeMatrix(Mdual);
  dd_FreeMatrix(Gdual);
  return Mproj;
}