Beispiel #1
0
void createExpression(array_t * expression, YAP_Term t)
/* returns the expression as an array_t of terms (cubes) starting from the prolog lists of terms
each term is an array_t of factors obtained from a prolog list of factors
each factor is a couple (index of variable, index of value) obtained from a prolog list containing 
two integers
*/
{
     	YAP_Term  termTerm,factorTerm;
	factor f;	
	int i,j;
	array_t * term;

	i=0;
	while(YAP_IsPairTerm(t))
	{
		term=array_alloc(factor,0);
		termTerm=YAP_HeadOfTerm(t);
		j=0;
		while(YAP_IsPairTerm(termTerm))
		{
			factorTerm=YAP_HeadOfTerm(termTerm);
			f.var=YAP_IntOfTerm(YAP_HeadOfTerm(factorTerm));
			f.value=YAP_IntOfTerm(YAP_HeadOfTerm(YAP_TailOfTerm(factorTerm)));
			array_insert(factor,term,j,f);
			termTerm=YAP_TailOfTerm(termTerm);
			j++;
		}
		array_insert(array_t *,expression,i,term);
		t=YAP_TailOfTerm(t);
		i++;
	}
}
Beispiel #2
0
void createVars(array_t * vars, YAP_Term t,DdManager * mgr, array_t * bVar2mVar,int create_dot,  char inames[1000][20])
/* adds the boolean variables to the BDD and returns
an array_t containing them (array_t is defined in the util library of glu)
returns also the names of the variables to be used to save the ADD in dot format
 */
{
     	YAP_Term  varTerm,probTerm;
	int varIndex,nVal,i,b;
	variable v;	
	char numberVar[10],numberBit[10];
	double p;
	b=0;

	while(YAP_IsPairTerm(t))
	{
		varTerm=YAP_HeadOfTerm(t);
		varIndex=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));

      		varTerm=YAP_TailOfTerm(varTerm);
		nVal=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
		varTerm=YAP_TailOfTerm(varTerm);
      		probTerm=YAP_HeadOfTerm(varTerm);
		v.nVal=nVal;
		v.nBit=(int)ceil(log(nVal)/log(2));
		v.probabilities=array_alloc(double,0);
		v.booleanVars=array_alloc(DdNode *,0);
		for (i=0;i<nVal;i++)
		{
			if (create_dot)
			{
				strcpy(inames[b+i],"X");
				sprintf(numberVar,"%d",varIndex);
				strcat(inames[b+i],numberVar);
				strcat(inames[b+i],"_");
				sprintf(numberBit,"%d",i);
				strcat(inames[b+i],numberBit);
			}
			p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
			array_insert(double,v.probabilities,i,p);
			probTerm=YAP_TailOfTerm(probTerm);
			array_insert(DdNode *,v.booleanVars,i,Cudd_bddIthVar(mgr,b+i));
			array_insert(int,bVar2mVar,b+i,varIndex);
		}
		Cudd_MakeTreeNode(mgr,b,nVal,MTR_FIXED);
		b=b+nVal;
		array_insert(variable,vars,varIndex,v);
		t=YAP_TailOfTerm(t);
	}
}
Beispiel #3
0
static YAP_Bool point_list_to_sequence (YAP_Term term,
                                          unsigned int size,
                                          sequence_t *sequence)
{
  YAP_Float x, y;
  unsigned int n;
  YAP_Term head;

  *sequence = GEOSCoordSeq_create (size, 2);
  if (*sequence == NULL)
    return (FALSE);
  for (n = 0; YAP_IsPairTerm (term) != FALSE; n ++)
    {
      assert (n < size);
      head = YAP_HeadOfTerm (term);
      if ((Yap_IsNumberTerm (YAP_ArgOfTerm (1, head), &x) == FALSE)
          || (Yap_IsNumberTerm (YAP_ArgOfTerm (2, head), &y) == FALSE)
          || (GEOSCoordSeq_setX (*sequence, n, x) == 0)
          || (GEOSCoordSeq_setY (*sequence, n, y) == 0))
        {
          GEOSCoordSeq_destroy (*sequence);
          return (FALSE);
        }
      term = YAP_TailOfTerm (term);
    }
  assert (n == size);
  assert (YAP_IsAtomTerm (term) != FALSE);
  assert (strcmp (YAP_AtomName (YAP_AtomOfTerm (term)), "[]") == 0);
  return (TRUE);
}
Beispiel #4
0
void setQuery(YAP_Term t1, int32_t **res)
{
    int32_t *query = (int32_t *)malloc(MAXARG * sizeof(int32_t));
    int32_t x, y = 0, *itr;
    predicate *ptr = NULL;
    if(YAP_IsPairTerm(t1))
    {
        while(YAP_IsPairTerm(t1))
        {
            ptr = (predicate *)YAP_IntOfTerm(YAP_HeadOfTerm(t1));
            query[y] = ptr->name;
            itr = ptr->address_host_table;
            x = 2;
            while(itr[x] != 0)
                x++;
            query[y+1] = itr[x+1];
            t1 = YAP_TailOfTerm(t1);
            y+=2;
        }
    }
    else
    {
        ptr = (predicate *)YAP_IntOfTerm(t1);
        query[y] = ptr->name;
        itr = ptr->address_host_table;
        x = 2;
        while(itr[x] != 0)
            x++;
        query[y+1] = itr[x+1];
        y += 2;
    }
    query[y] = -1;
    query[y+1] = -1;
    *res = query;
}
Beispiel #5
0
static YAP_Bool init(void) {
  int j, i;
  YAP_Term arg1, arg2, list;

  ex = 0;
  cycle = 0;
  arg1 = YAP_ARG1;
  arg2 = YAP_ARG2;
  nRules = YAP_IntOfTerm(arg1);

  vars_ex = NULL;
  nVars_ex = NULL;
  eta = (double ***)malloc(nRules * sizeof(double **));
  eta_temp = (double ***)malloc(nRules * sizeof(double **));
  rules = (int *)malloc(nRules * sizeof(int));
  arrayprob = (double **)malloc(nRules * sizeof(double *));
  probs_ex = NULL;
  bVar2mVar_ex = NULL;
  boolVars_ex = NULL;
  mgr_ex = NULL;
  nodes_probs_ex = NULL;
  list = arg2;
  for (j = 0; j < nRules; j++) {
    rules[j] = YAP_IntOfTerm(YAP_HeadOfTerm(list));
    list = YAP_TailOfTerm(list);
    eta[j] = (double **)malloc((rules[j] - 1) * sizeof(double *));
    eta_temp[j] = (double **)malloc((rules[j] - 1) * sizeof(double *));
    arrayprob[j] = (double *)malloc((rules[j] - 1) * sizeof(double));
    for (i = 0; i < rules[j] - 1; i++) {
      eta[j][i] = (double *)malloc(2 * sizeof(double));
      eta_temp[j][i] = (double *)malloc(2 * sizeof(double));
    }
  }
  return 1;
}
Beispiel #6
0
static int
p_create_cell_matrix_and_copy1(void)
{
  int rows, cols;
  mxArray *mat;
  YAP_Term tl = YAP_ARG3;

  rows = YAP_IntOfTerm(YAP_ARG1);
  cols = YAP_IntOfTerm(YAP_ARG2);
  if (!(mat = mxCreateCellMatrix(rows, cols)))
    return FALSE;
    while (YAP_IsPairTerm(tl)) {
    YAP_Term th = YAP_HeadOfTerm(tl);
    int off = MAT_ACCESS(YAP_IntOfTerm(YAP_ArgOfTerm(1,th))-1,
			 YAP_IntOfTerm(YAP_ArgOfTerm(2,th))-1,
			 rows,cols);
    mxArray *mat2 = get_array(YAP_ArgOfTerm(3,th));
    mxSetCell(mat,off, mat2);
    tl = YAP_TailOfTerm(tl);
  }
  if (YAP_IsAtomTerm(YAP_ARG4)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
  }
  return YAP_Unify(YAP_ARG4,address2term(mat));
}
Beispiel #7
0
static mxArray*
get_array(YAP_Term ti)
{
  if (YAP_IsIntTerm(ti)) {
    return mxCreateDoubleScalar(YAP_IntOfTerm(ti));
  } else if (YAP_IsFloatTerm(ti)) {
    return mxCreateDoubleScalar(YAP_FloatOfTerm(ti));
  } else if (YAP_IsAtomTerm(ti)) {
    return matlab_getvar(ti);    
  } else if (YAP_IsPairTerm(ti)) {
    YAP_Term tv = YAP_HeadOfTerm(ti);
    YAP_Term tf = YAP_TailOfTerm(ti);
    const mxArray *mout;

    if (!YAP_IsAtomTerm(tv)) {
      char s[BUFSIZE];
      if (!YAP_StringToBuffer(ti, s, BUFSIZE))
	return FALSE;
      return mxCreateString(s);
    }
    mout = matlab_getvar(tv);
    if (!mout)
      return FALSE;
    if (YAP_IsIntTerm(tf)) {
      return mxGetFieldByNumber(mout, 0, YAP_IntOfTerm(tf));
    } else if (YAP_IsAtomTerm(tf))  {
      const char *s=YAP_AtomName(YAP_AtomOfTerm(tf));
      return mxGetField(mout, 0, s);
    } else {
      return NULL;
    }
  } else {
    return (mxArray *)YAP_IntOfTerm(YAP_ArgOfTerm(1,ti));
  }
}
Beispiel #8
0
static int
load_facts( void ) {

    int32_t nrows = YAP_IntOfTerm(YAP_ARG1);
    int32_t ncols = YAP_IntOfTerm(YAP_ARG2), i = 0;
    YAP_Term t3 = YAP_ARG3;
    int32_t *mat = (int32_t *)malloc(sizeof(int32_t)*nrows*ncols);
    int32_t pname = YAP_AtomToInt(YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3))));
    predicate *pred;

    while(YAP_IsPairTerm(t3)) {
        int32_t j = 0;
        YAP_Term th = YAP_HeadOfTerm(t3);

        for (j = 0; j < ncols; j++) {
            YAP_Term ta = YAP_ArgOfTerm(j+1, th);
            if (YAP_IsAtomTerm(ta)) {
                mat[i*ncols+j] = YAP_AtomToInt(YAP_AtomOfTerm(ta));
            } else {
                mat[i*ncols+j] = YAP_IntOfTerm(ta);
            }
        }
        t3 = YAP_TailOfTerm( t3 );
        i++;
    }
    if (YAP_IsVarTerm( YAP_ARG4)) {
        // new
        pred = (predicate *)malloc(sizeof(predicate));
    } else {
        pred = (predicate *)YAP_IntOfTerm(YAP_ARG4);
        if (pred->address_host_table)
            free( pred->address_host_table );
    }
    pred->name = pname;
    pred->num_rows = nrows;
    pred->num_columns = ncols;
    pred->is_fact = TRUE;
    pred->address_host_table =  mat;
    Cuda_NewFacts(pred);
    if (YAP_IsVarTerm( YAP_ARG4)) {
        return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred));
    } else {
        return TRUE;
    }
}
Beispiel #9
0
static int
p_call_matlab(void)
{
  YAP_Term tlength = YAP_ARG2,
    tl = YAP_ARG3,
    tname = YAP_ARG1,
    tolength = YAP_ARG4,
    tout = YAP_ARG5;
  int i = 0;
  mxArray *inps[50], *outs[50];
  const char *name;
  int olength = YAP_IntOfTerm(tolength);

  if (!YAP_IsAtomTerm(tname))
    return FALSE;
  name = YAP_AtomName(YAP_AtomOfTerm(tname));
  if (!YAP_IsIntTerm(tlength))
    return FALSE;
  while (YAP_IsPairTerm(tl)) {
    inps[i] = get_array(YAP_HeadOfTerm(tl));
    i++;
    tl = YAP_TailOfTerm(tl);
  }
  if (mexCallMATLAB(olength, outs, i, inps, name))
    return FALSE;
  /* output arguments */
  if (YAP_IsPairTerm(tout)) {
    for (i=0; i<olength; i++) {
      YAP_Term ti = YAP_HeadOfTerm(tout);
      if (YAP_IsAtomTerm(ti)) {
	return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(ti)), outs[i]);
      } else {
	return YAP_Unify(ti,address2term(outs[i]));
      }
    }    
  } else {
    YAP_Term to = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
    for (i=olength; i>0; i--) {
      to = YAP_MkPairTerm(address2term(outs[i-1]),to);
    }    
  }
  return TRUE;
}
Beispiel #10
0
expr createExpression(YAP_Term t)
/* returns the expression as an array_t of terms (cubes) starting from the prolog lists of terms
each term is an array_t of factors obtained from a prolog list of factors
each factor is a couple (index of variable, index of value) obtained from a prolog list containing
two integers
*/
{
    YAP_Term  termTerm,factorTerm;
    factor f;
    int i,j;
    term term1;
    expr expression;

    expression.terms=NULL;
    i=0;
    while(YAP_IsPairTerm(t))
    {
        term1.factors=NULL;
        termTerm=YAP_HeadOfTerm(t);
        j=0;
        while(YAP_IsPairTerm(termTerm))
        {
            factorTerm=YAP_HeadOfTerm(termTerm);
            f.var=YAP_IntOfTerm(YAP_HeadOfTerm(factorTerm));
            f.value=YAP_IntOfTerm(YAP_HeadOfTerm(YAP_TailOfTerm(factorTerm)));
            term1.factors=(factor *)realloc(term1.factors,(j+1)* sizeof(factor));
            term1.factors[j]=f;
            termTerm=YAP_TailOfTerm(termTerm);
            j++;
        }
        term1.nFact=j;
        expression.terms=(term *)realloc(expression.terms,(i+1)* sizeof(term));
        expression.terms[i]=term1;
        t=YAP_TailOfTerm(t);
        i++;
    }
    expression.nTerms=i;
    return(expression);
}
Beispiel #11
0
static YAP_Bool list_to_geometry (YAP_Term term,
                                    unsigned int minimum,
                                    procedure_to_geometry_t procedure,
                                    geometry_type_t geometry_type,
                                    geometry_t *geometry)
{
  geometry_t *p;
  unsigned int size;
  unsigned int v, n;
  YAP_Term head;

  assert (geometry != NULL);
  term = YAP_ArgOfTerm (1, term);
  if ((is_list_get_size (term, &size) == FALSE) || (size < minimum))
    return (FALSE);

  p = (geometry_t *) malloc (sizeof (geometry_t) * size);
  if (p == NULL)
    {
      warning ("%s: list_to_geometry: not enough memory", __FILE__);
      return (FALSE);
    }
  memset (p, 0, sizeof (geometry_t) * size);
  for (n = 0; YAP_IsPairTerm (term) != FALSE; n ++)
    {
      assert (n < size);
      head = YAP_HeadOfTerm (term);
      if (procedure (head, &p[n]) == FALSE)
        {
          for (v = 0; v < n; v ++)
            GEOSGeom_destroy (p[v]);
          free (p);
          return (FALSE);
        }
      term = YAP_TailOfTerm (term);
    }
  assert (n == size);
  assert (YAP_IsAtomTerm (term) != FALSE);
  assert (strcmp (YAP_AtomName (YAP_AtomOfTerm (term)), "[]") == 0);
  if (geometry_type == GEOS_POLYGON)
    *geometry = GEOSGeom_createPolygon (p[0], p + 1, size - 1);
  else
    *geometry = GEOSGeom_createCollection (geometry_type, p, size);
  memset (p, 0, sizeof (geometry_t) * size);
  free (p);

  if (*geometry == NULL)
    return (FALSE);
  return (TRUE);
}
Beispiel #12
0
static rect_t RectOfTerm (Term term)
{
  YAP_Term tmp;
  rect_t rect;
  int i;
  
  if (!YAP_IsPairTerm(term))
    return (RectInit());
  
  for (i = 0; YAP_IsPairTerm(term) && i < 4; i++)
    {
      tmp = YAP_HeadOfTerm (term);
      if (!YAP_IsNumberTerm(tmp,&(rect.coords[i])))
        return (RectInit());
      term = YAP_TailOfTerm (term);
    }

  return (rect);
}
Beispiel #13
0
static YAP_Bool add_var(void) {
  YAP_Term arg1, arg2, arg3, arg4, out, probTerm, probTerm_temp;
  variable *v;
  int i;
  DdNode *node;
  double p, p0;

  arg1 = YAP_ARG1;
  arg2 = YAP_ARG2;
  arg3 = YAP_ARG3;
  arg4 = YAP_ARG4;
  nVars_ex[ex] = nVars_ex[ex] + 1;
  vars_ex[ex] =
      (variable *)realloc(vars_ex[ex], nVars_ex[ex] * sizeof(variable));

  v = &vars_ex[ex][nVars_ex[ex] - 1];
  v->nVal = YAP_IntOfTerm(arg1);
  v->nRule = YAP_IntOfTerm(arg3);
  v->firstBoolVar = boolVars_ex[ex];
  probs_ex[ex] = (double *)realloc(
      probs_ex[ex], (((boolVars_ex[ex] + v->nVal - 1) * sizeof(double))));
  bVar2mVar_ex[ex] = (int *)realloc(
      bVar2mVar_ex[ex], ((boolVars_ex[ex] + v->nVal - 1) * sizeof(int)));
  probTerm = arg2;
  p0 = 1;
  for (i = 0; i < v->nVal - 1; i++) {
    node = Cudd_bddIthVar(mgr_ex[ex], boolVars_ex[ex] + i);
    p = YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
    bVar2mVar_ex[ex][boolVars_ex[ex] + i] = nVars_ex[ex] - 1;
    probs_ex[ex][boolVars_ex[ex] + i] = p / p0;
    probTerm_temp = YAP_TailOfTerm(probTerm);
    probTerm = probTerm_temp;
    p0 = p0 * (1 - p / p0);
  }
  boolVars_ex[ex] = boolVars_ex[ex] + v->nVal - 1;
  rules[v->nRule] = v->nVal;
  out = YAP_MkIntTerm((YAP_Int)nVars_ex[ex] - 1);
  return YAP_Unify(out, arg4);
}
Beispiel #14
0
static int
p_set_float_array(void)
{
  int rows, cols, i = 0, j = 0;
  double *input;
  mxArray *mat;
  YAP_Term tl = YAP_ARG3;

  rows = YAP_IntOfTerm(YAP_ARG1);
  cols = YAP_IntOfTerm(YAP_ARG2);
  if (!(mat = mxCreateDoubleMatrix(rows, cols, mxREAL)))
    return FALSE;
  input = mxGetPr(mat);
  /* copy ints to matrix. */
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      YAP_Term th;

      if (!YAP_IsPairTerm(tl)) {
	return FALSE;
      }
      th = YAP_HeadOfTerm(tl);
      if (YAP_IsIntTerm(th)) {
	input[MAT_ACCESS(i,j,rows,cols)] = YAP_IntOfTerm(th);
      } else if (YAP_IsFloatTerm(th)) {
	input[MAT_ACCESS(i,j,rows,cols)] = YAP_FloatOfTerm(th);
      } else {
	/* ERROR */
	return FALSE;
      }
      tl = YAP_TailOfTerm(tl);
    }
  }
  if (YAP_IsAtomTerm(YAP_ARG4)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
  }
  return YAP_Unify(YAP_ARG4,address2term(mat));
}
Beispiel #15
0
static int
p_set_float_vector(void)
{
  mwSize len[1];
  int i = 0;
  double *input;
  mxArray *mat;
  YAP_Term tl = YAP_ARG2;

  len[0] = YAP_IntOfTerm(YAP_ARG1);
  if (!(mat = mxCreateNumericArray(1,len, mxDOUBLE_CLASS, mxREAL)))
    return FALSE;
  input = mxGetPr(mat);
  /* copy ints to matrix. */
  for (i = 0; i < len[0]; i++) {
    YAP_Term th;

    if (!YAP_IsPairTerm(tl)) {
      return FALSE;
    }
    th = YAP_HeadOfTerm(tl);
    if (YAP_IsIntTerm(th)) {
      input[i] = YAP_IntOfTerm(th);
    } else if (YAP_IsFloatTerm(th)) {
      input[i] = YAP_FloatOfTerm(th);
    } else {
      /* ERROR */
      return FALSE;
    }
    tl = YAP_TailOfTerm(tl);
  }
  if (YAP_IsAtomTerm(YAP_ARG3)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), mat);
  }
  return YAP_Unify(YAP_ARG3,address2term(mat));
}
Beispiel #16
0
static int
p_set_int_array(void)
{
  int rows, cols, i = 0, j = 0;
  YAP_Int *input;
  mxArray *mat;
  YAP_Term tl = YAP_ARG4;

  mat = matlab_getvar(YAP_ARG1);
  rows = YAP_IntOfTerm(YAP_ARG2);
  cols = YAP_IntOfTerm(YAP_ARG3);
  input = (YAP_Int *)mxGetPr(mat);
  /* copy ints to matrix. */
  for (i = 0; i < rows*cols; i++) {
    YAP_Term th;

    if (!YAP_IsPairTerm(tl)) {
      return FALSE;
    }
    th = YAP_HeadOfTerm(tl);
    if (!YAP_IsIntTerm(th)) {
      /* ERROR */
      return FALSE;
    }
    input[MAT_ACCESS(i++,j,rows,cols)] = YAP_IntOfTerm(th);
    if (i == rows) {
      i = 0;
      j++;
    }
    tl = YAP_TailOfTerm(tl);
  }
  if (YAP_IsAtomTerm(YAP_ARG4)) {
    return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat);
  }
  return YAP_Unify(YAP_ARG4,address2term(mat));
}
Beispiel #17
0
static int
load_rule( void ) {
    // maximum of 2K symbols per rule, should be enough for ILP
    int32_t vec[2048], *ptr = vec, *nvec, neg[2048];
    // qK different variables;
    YAP_Term vars[1024];
    int32_t nvars = 0, x;
    int32_t ngoals = YAP_IntOfTerm(YAP_ARG1);   /* gives the number of goals */
    int32_t ncols = YAP_IntOfTerm(YAP_ARG2);
    YAP_Term t3 = YAP_ARG3;
    YAP_Atom name = YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3)));
    int32_t pname = YAP_AtomToInt(name);

    const char *strname = YAP_AtomName(name);
    predicate *pred;
    int32_t cont = 0;
    memset(neg, 0x0, 2048 * sizeof(int32_t));

    while(YAP_IsPairTerm(t3)) {
        int32_t j = 0, m;
        YAP_Term th = YAP_HeadOfTerm(t3);
        YAP_Functor f = YAP_FunctorOfTerm( th );
        int32_t n = YAP_ArityOfFunctor( f );
        YAP_Atom at = YAP_NameOfFunctor( f );

        if (at == AtomEq)
            *ptr++ = SBG_EQ;
        else if (at == AtomGt)
            *ptr++ = SBG_GT;
        else if (at == AtomLt)
            *ptr++ = SBG_LT;
        else if (at == AtomGe)
            *ptr++ = SBG_GE;
        else if (at == AtomLe)
            *ptr++ = SBG_LE;
        else if (at == AtomDf)
            *ptr++ = SBG_DF;
        else if (at == AtomNt)
        {
            neg[cont] = 1;
            cont++;
        }
        else
        {
            *ptr++ = YAP_AtomToInt( at );
            cont++;
        }

        for (j = 0; j < n; j++) {
            YAP_Term ta = YAP_ArgOfTerm(j+1, th);

            if (YAP_IsVarTerm(ta)) {
                int32_t k;
                for (k = 0; k< nvars; k++) {
                    if (vars[k] == ta) {
                        *ptr++ = k+1;
                        break;
                    }
                }
                if (k == nvars) {
                    vars[k] = ta;
                    *ptr++ = k+1;
                    nvars++;
                }
            } else if (YAP_IsAtomTerm(ta))  {
                *ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta));
            } else if (YAP_IsApplTerm(ta))  {
                f = YAP_FunctorOfTerm( ta );
                at = YAP_NameOfFunctor( f );
                m = YAP_ArityOfFunctor( f );
                *ptr++ = YAP_AtomToInt( at );

                for (x = 0; x < m; x++) {
                    YAP_Term ta2 = YAP_ArgOfTerm(x+1, ta);

                    if (YAP_IsVarTerm(ta2)) {
                        int32_t k;
                        for (k = 0; k < nvars; k++) {
                            if (vars[k] == ta2) {
                                *ptr++ = k+1;
                                break;
                            }
                        }
                        if (k == nvars) {
                            vars[k] = ta2;
                            *ptr++ = k+1;
                            nvars++;
                        }
                    } else if (YAP_IsAtomTerm(ta2))  {
                        *ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta));
                    } else {
                        *ptr++ = -YAP_IntOfTerm(ta);
                    }
                }
            } else {
                *ptr++ = -YAP_IntOfTerm(ta);
            }
        }
        *ptr++ = 0;
        t3 = YAP_TailOfTerm( t3 );
    }
    if (YAP_IsVarTerm( YAP_ARG4)) {
        // new
        pred = (predicate *)malloc(sizeof(predicate));
    } else {
        pred = (predicate *)YAP_IntOfTerm(YAP_ARG4);
        if (pred->address_host_table)
            free( pred->address_host_table );
    }
    pred->name = pname;
    pred->num_rows = ngoals;
    pred->num_columns = ncols;
    pred->is_fact = FALSE;
    x = (strlen(strname) + 1) * sizeof(char);
    pred->predname = (char *)malloc(x);
    memcpy(pred->predname, strname, x);
    nvec = (int32_t *)malloc(sizeof(int32_t)*(ptr-vec));
    memcpy(nvec, vec, sizeof(int32_t)*(ptr-vec));
    pred->address_host_table =  nvec;
    pred->negatives = (int32_t *)malloc(sizeof(int32_t) * cont);
    memcpy(pred->negatives, neg, sizeof(int32_t) * cont);
    Cuda_NewRule( pred );
    return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred));
}
Beispiel #18
0
static YAP_Bool Q(void) {
  YAP_Term arg1, arg2, arg3, arg4, out, out1, term, nodesTerm, ruleTerm, tail,
      pair, compoundTerm;
  DdNode *node1, **nodes_ex;
  int r, lenNodes, i;
  double p1, p0, **eta_rule, CLL;

  arg1 = YAP_ARG1;
  arg2 = YAP_ARG2;
  arg3 = YAP_ARG3;
  arg4 = YAP_ARG4;

  nodesTerm = arg1;
  lenNodes = YAP_IntOfTerm(arg2);

  nodes_ex = (DdNode **)malloc(lenNodes * sizeof(DdNode *));
  example_prob = (double *)malloc(lenNodes * sizeof(double));

  for (i = 0; i < lenNodes; i++) {
    pair = YAP_HeadOfTerm(nodesTerm);
    node1 = (DdNode *)YAP_IntOfTerm(YAP_HeadOfTerm(pair));
    nodes_ex[i] = node1;
    pair = YAP_TailOfTerm(pair);
    example_prob[i] = YAP_FloatOfTerm(YAP_HeadOfTerm(pair));
    nodesTerm = YAP_TailOfTerm(nodesTerm);
  }

  for (r = 0; r < nRules; r++) {
    for (i = 0; i < rules[r] - 1; i++) {
      eta_rule = eta[r];
      eta_rule[i][0] = 0;
      eta_rule[i][1] = 0;
    }
  }
  CLL = Expectation(nodes_ex, lenNodes);
  out = YAP_TermNil();

  for (r = 0; r < nRules; r++) {
    tail = YAP_TermNil();
    eta_rule = eta[r];
    for (i = 0; i < rules[r] - 1; i++) {
      p0 = eta_rule[i][0];
      p1 = eta_rule[i][1];
      term = YAP_MkPairTerm(YAP_MkFloatTerm(p0),
                            YAP_MkPairTerm(YAP_MkFloatTerm(p1), YAP_TermNil()));
      tail = YAP_MkPairTerm(term, tail);
    }

    ruleTerm = YAP_MkIntTerm(r);
    compoundTerm =
        YAP_MkPairTerm(ruleTerm, YAP_MkPairTerm(tail, YAP_TermNil()));
    out = YAP_MkPairTerm(compoundTerm, out);
  }

  free(nodes_ex);
  free(example_prob);

  out1 = YAP_MkFloatTerm(CLL);
  YAP_Unify(out1, arg4);
  return (YAP_Unify(out, arg3));
}
Beispiel #19
0
static YAP_Bool EM(void) {
  YAP_Term arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, out1, out2, out3,
      nodesTerm, ruleTerm, tail, pair, compoundTerm;
  DdNode *node1, **nodes_ex;
  int r, lenNodes, i, iter;
  long iter1;
  double CLL0 = -2.2 * pow(10, 10); //-inf
  double CLL1 = -1.7 * pow(10, 8);  //+inf
  double p, p0, **eta_rule, ea, er;
  double ratio, diff;

  arg1 = YAP_ARG1;
  arg2 = YAP_ARG2;
  arg3 = YAP_ARG3;
  arg4 = YAP_ARG4;
  arg5 = YAP_ARG5;
  arg6 = YAP_ARG6;
  arg7 = YAP_ARG7;
  arg8 = YAP_ARG8;

  nodesTerm = arg1;
  ea = YAP_FloatOfTerm(arg2);
  er = YAP_FloatOfTerm(arg3);
  lenNodes = YAP_IntOfTerm(arg4);
  iter = YAP_IntOfTerm(arg5);

  nodes_ex = (DdNode **)malloc(lenNodes * sizeof(DdNode *));
  nodes_probs_ex = (double *)malloc(lenNodes * sizeof(double));
  example_prob = (double *)malloc(lenNodes * sizeof(double));

  for (i = 0; i < lenNodes; i++) {
    pair = YAP_HeadOfTerm(nodesTerm);
    node1 = (DdNode *)YAP_IntOfTerm(YAP_HeadOfTerm(pair));
    nodes_ex[i] = node1;
    pair = YAP_TailOfTerm(pair);
    example_prob[i] = YAP_FloatOfTerm(YAP_HeadOfTerm(pair));
    nodesTerm = YAP_TailOfTerm(nodesTerm);
  }
  diff = CLL1 - CLL0;
  ratio = diff / fabs(CLL0);
  if (iter == -1)
    iter1 = 2147000000;
  else
    iter1 = iter;

  while ((diff > ea) && (ratio > er) && (cycle < iter1)) {
    cycle++;
    for (r = 0; r < nRules; r++) {
      for (i = 0; i < rules[r] - 1; i++) {
        eta_rule = eta[r];
        eta_rule[i][0] = 0;
        eta_rule[i][1] = 0;
      }
    }
    CLL0 = CLL1;
    CLL1 = Expectation(nodes_ex, lenNodes);
    Maximization();
    diff = CLL1 - CLL0;
    ratio = diff / fabs(CLL0);
  }
  out2 = YAP_TermNil();
  for (r = 0; r < nRules; r++) {
    tail = YAP_TermNil();
    p0 = 1;
    for (i = 0; i < rules[r] - 1; i++) {
      p = arrayprob[r][i] * p0;
      tail = YAP_MkPairTerm(YAP_MkFloatTerm(p), tail);
      p0 = p0 * (1 - arrayprob[r][i]);
    }
    tail = YAP_MkPairTerm(YAP_MkFloatTerm(p0), tail);
    ruleTerm = YAP_MkIntTerm(r);
    compoundTerm =
        YAP_MkPairTerm(ruleTerm, YAP_MkPairTerm(tail, YAP_TermNil()));
    out2 = YAP_MkPairTerm(compoundTerm, out2);
  }
  out3 = YAP_TermNil();
  for (i = 0; i < lenNodes; i++) {
    out3 = YAP_MkPairTerm(YAP_MkFloatTerm(nodes_probs_ex[i]), out3);
  }
  YAP_Unify(out3, arg8);

  out1 = YAP_MkFloatTerm(CLL1);
  YAP_Unify(out1, arg6);
  free(nodes_ex);
  free(example_prob);
  free(nodes_probs_ex);

  return (YAP_Unify(out2, arg7));
}
Beispiel #20
0
variables  createVars(YAP_Term t,DdManager * mgr, int create_dot,  char inames[1000][20])
/* adds the boolean variables to the BDD and returns
the array vars containing them
returns also the names of the variables to be used to save the ADD in dot format
 */
{
    YAP_Term  varTerm,probTerm;
    int varIndex,nVal,i,b;
    variable v;
    char numberVar[10],numberBit[10];
    double p;
    variables  vars;

    vars.varar=NULL;
    vars.bVar2mVar=NULL;

    b=0;
    vars.nVar=0;
    varIndex=0;
    while(YAP_IsPairTerm(t))
    {
        varTerm=YAP_HeadOfTerm(t);
        varIndex=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
        varTerm=YAP_TailOfTerm(varTerm);
        nVal=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
        varTerm=YAP_TailOfTerm(varTerm);
        probTerm=YAP_HeadOfTerm(varTerm);
        v.nVal=nVal;
        v.nBit=(int)ceil(log(nVal)/log(2));
        v.probabilities=(double *) malloc(nVal* sizeof(double));
        v.booleanVars=(DdNode * *) malloc(v.nBit* sizeof(DdNode *));
        for (i=0; i<nVal; i++)
        {
            p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
            v.probabilities[i]=p;
            probTerm=YAP_TailOfTerm(probTerm);
        }
        for (i=0; i<v.nBit; i++)
        {
            if (create_dot)
            {
                strcpy(inames[b+i],"X");
                sprintf(numberVar,"%d",varIndex);
                strcat(inames[b+i],numberVar);
                strcat(inames[b+i],"_");
                sprintf(numberBit,"%d",i);
                strcat(inames[b+i],numberBit);
            }
            v.booleanVars[i]=Cudd_bddIthVar(mgr,b+i);
            vars.bVar2mVar=(int *)realloc(vars.bVar2mVar,(b+i+1)*sizeof(int));
            vars.bVar2mVar[b+i]=varIndex;
        }
        Cudd_MakeTreeNode(mgr,b,v.nBit,MTR_FIXED);
        b=b+v.nBit;
        vars.varar=(variable *) realloc(vars.varar,(varIndex+1)* sizeof(variable));
        vars.varar[varIndex]=v;
        t=YAP_TailOfTerm(t);
    }
    vars.nVar=varIndex+1;
    vars.nBVar=b;
    return vars;
}