예제 #1
0
static void tst6() {
    params_ref      ps;
    reslimit        rlim;
    nlsat::solver s(rlim, ps);
    anum_manager & am     = s.am();
    nlsat::pmanager & pm  = s.pm();
    nlsat::assignment as(am);
    nlsat::explain& ex    = s.get_explain();
    nlsat::var x0, x1, x2, a, b, c, d;
    a  = s.mk_var(false);
    b  = s.mk_var(false);
    c  = s.mk_var(false);
    d  = s.mk_var(false);
    x0 = s.mk_var(false);
    x1 = s.mk_var(false);
    x2 = s.mk_var(false);

    polynomial_ref p1(pm), p2(pm), p3(pm), p4(pm), p5(pm);
    polynomial_ref _x0(pm), _x1(pm), _x2(pm);
    polynomial_ref _a(pm), _b(pm), _c(pm), _d(pm);
    _x0 = pm.mk_polynomial(x0);
    _x1 = pm.mk_polynomial(x1);
    _x2 = pm.mk_polynomial(x2);
    _a  = pm.mk_polynomial(a);
    _b  = pm.mk_polynomial(b);
    _c  = pm.mk_polynomial(c);
    _d  = pm.mk_polynomial(d);

    p1 = (_a*(_x0^2)) + _x2 + 2;
    p2 = (_b*_x1) - (2*_x2) - _x0 + 8;
    nlsat::scoped_literal_vector lits(s);
    lits.push_back(mk_gt(s, p1));
    lits.push_back(mk_gt(s, p2));
    lits.push_back(mk_gt(s, (_c*_x0) + _x2 + 1));
    lits.push_back(mk_gt(s, (_d*_x0) - _x1 + 5*_x2));

    scoped_anum zero(am), one(am), two(am);
    am.set(zero, 0);
    am.set(one,  1);
    am.set(two,  2);
    as.set(0, one);
    as.set(1, one);
    as.set(2, two);
    as.set(3, two);
    as.set(4, two);
    as.set(5, one);
    as.set(6, one);
    s.set_rvalues(as);


    project(s, ex, x0, 2, lits.c_ptr());
    project(s, ex, x1, 3, lits.c_ptr());
    project(s, ex, x2, 3, lits.c_ptr());
    project(s, ex, x2, 2, lits.c_ptr());
    project(s, ex, x2, 4, lits.c_ptr());
    project(s, ex, x2, 3, lits.c_ptr()+1);


}
예제 #2
0
static void tst7() {
    params_ref      ps;
    reslimit        rlim;
    nlsat::solver s(rlim, ps);
    anum_manager & am     = s.am();
    nlsat::pmanager & pm  = s.pm();
    nlsat::var x0, x1, x2, a, b, c, d;
    a  = s.mk_var(false);
    b  = s.mk_var(false);
    c  = s.mk_var(false);
    d  = s.mk_var(false);
    x0 = s.mk_var(false);
    x1 = s.mk_var(false);
    x2 = s.mk_var(false);
    polynomial_ref p1(pm), p2(pm), p3(pm), p4(pm), p5(pm);
    polynomial_ref _x0(pm), _x1(pm), _x2(pm);
    polynomial_ref _a(pm), _b(pm), _c(pm), _d(pm);
    _x0 = pm.mk_polynomial(x0);
    _x1 = pm.mk_polynomial(x1);
    _x2 = pm.mk_polynomial(x2);
    _a  = pm.mk_polynomial(a);
    _b  = pm.mk_polynomial(b);
    _c  = pm.mk_polynomial(c);
    _d  = pm.mk_polynomial(d);

    p1 = _x0 + _x1;
    p2 = _x2 - _x0;
    p3 = (-1*_x0) - _x1;

    nlsat::scoped_literal_vector lits(s);
    lits.push_back(mk_gt(s, p1));
    lits.push_back(mk_gt(s, p2));
    lits.push_back(mk_gt(s, p3));

    nlsat::literal_vector litsv(lits.size(), lits.c_ptr());
    lbool res = s.check(litsv);
    SASSERT(res == l_false);
    for (unsigned i = 0; i < litsv.size(); ++i) {
        s.display(std::cout, litsv[i]);
        std::cout << " ";
    }
    std::cout << "\n";

    litsv.reset();
    litsv.append(2, lits.c_ptr());
    res = s.check(litsv);
    SASSERT(res == l_true);
    s.display(std::cout);
    s.am().display(std::cout, s.value(x0));
    std::cout << "\n";
    s.am().display(std::cout, s.value(x1));
    std::cout << "\n";
    s.am().display(std::cout, s.value(x2));
    std::cout << "\n";

}
예제 #3
0
void TestEval::apply01() {

	const ExprSymbol& x1 = ExprSymbol::new_("x1");
	const ExprSymbol& x2 = ExprSymbol::new_("x2");

	Function f1(x1,x1,"f1");

	Function f2(x2,f1(x2));

	IntervalVector _x2(1,Interval(2,2));
	check(f2.eval(_x2), Interval(2,2));
	CPPUNIT_ASSERT((f2.eval(_x2)).is_superset(Interval(2,2)));
}
예제 #4
0
static void tst8() {
    params_ref      ps;
    reslimit        rlim;
    nlsat::solver s(rlim, ps);
    anum_manager & am     = s.am();
    nlsat::pmanager & pm  = s.pm();
    nlsat::assignment as(am);
    nlsat::explain& ex    = s.get_explain();
    nlsat::var x0, x1, x2, a, b, c, d;
    a  = s.mk_var(false);
    b  = s.mk_var(false);
    c  = s.mk_var(false);
    d  = s.mk_var(false);
    x0 = s.mk_var(false);
    x1 = s.mk_var(false);
    x2 = s.mk_var(false);

    polynomial_ref p1(pm), p2(pm), p3(pm), p4(pm), p5(pm);
    polynomial_ref _x0(pm), _x1(pm), _x2(pm);
    polynomial_ref _a(pm), _b(pm), _c(pm), _d(pm);
    _x0 = pm.mk_polynomial(x0);
    _x1 = pm.mk_polynomial(x1);
    _x2 = pm.mk_polynomial(x2);
    _a  = pm.mk_polynomial(a);
    _b  = pm.mk_polynomial(b);
    _c  = pm.mk_polynomial(c);
    _d  = pm.mk_polynomial(d);

    scoped_anum zero(am), one(am), two(am), six(am);
    am.set(zero, 0);
    am.set(one,  1);
    am.set(two,  2);
    am.set(six,  6);
    as.set(0, two); // a
    as.set(1, one); // b
    as.set(2, six); // c
    as.set(3, zero); // d
    as.set(4, zero); // x0
    as.set(5, zero); // x1
    as.set(6, two); // x2
    s.set_rvalues(as);

    nlsat::scoped_literal_vector lits(s);
    lits.push_back(mk_eq(s, (_a*_x2*_x2) - (_b*_x2) - _c));
    project(s, ex, x2, 1, lits.c_ptr());
}
예제 #5
0
static void *parallel64_t(void *input) {
	int i;
	threads64_t *in = (threads64_t *) input;
	const int start = in->thread * in->size / NUM_THREADS;
	const int stop  = in->thread + 1 == NUM_THREADS ? in->size : (in->thread + 1) * in->size / NUM_THREADS;
	const int curr  = in->thread * HIST_SIZE;
	uint32_t *A     = &in->histA[curr];
	uint32_t *B     = &in->histB[curr];
	uint32_t *C     = &in->histC[curr];
	uint32_t *D     = &in->histD[curr];
	uint32_t *E     = &in->histE[curr];
	uint32_t *F     = &in->histF[curr];
	for (i=start;i<stop;++i) {
		const uint64_t pos = (in->array[i] = in->position(in->array[i]));
		A[_x1(pos)]++;
		B[_x2(pos)]++;
		C[_x3(pos)]++;
		D[_x4(pos)]++;
		E[_x5(pos)]++;
		F[_x6(pos)]++;
	}
	pthread_exit(NULL);
}
예제 #6
0
/* unconditional Monte Carlo simulation for discrete tests. */
SEXP mcarlo_mean(SEXP x, SEXP y, SEXP lx, SEXP ly, SEXP length, SEXP samples,
    SEXP test) {

double *fact = NULL, *res = NULL, observed = 0;
int *n = NULL, *ncolt = NULL, *nrowt = NULL, *workspace = NULL;
int *num = INTEGER(length), *nr = INTEGER(lx), *nc = INTEGER(ly);
int *xx = INTEGER(x), *yy = INTEGER(y), *B = INTEGER(samples);
int i = 0, k = 0, npermuts = 0;
SEXP result;

  /* allocate and initialize the result. */
  PROTECT(result = allocVector(REALSXP, 3));
  res = REAL(result);
  res[0] = res[1] = res[2] = 0; // initial test score / mean score / nb permutations

  /* allocate and compute the factorials needed by rcont2. */
  allocfact(*num);

  /* allocate and initialize the workspace for rcont2. */
  workspace = alloc1dcont(*nc);

  /* initialize the contingency table. */
  n = alloc1dcont(*nr * (*nc));

  /* initialize the marginal frequencies. */
  nrowt = alloc1dcont(*nr);
  ncolt = alloc1dcont(*nc);

  /* compute the joint frequency of x and y. */
  for (k = 0; k < *num; k++)
    n[CMC(xx[k] - 1, yy[k] - 1, *nr)]++;

  /* compute the marginals. */
  for (i = 0; i < *nr; i++)
    for (k = 0; k < *nc; k++) {

      nrowt[i] += n[CMC(i, k, *nr)];
      ncolt[k] += n[CMC(i, k, *nr)];

    }/*FOR*/

  /* initialize the random number generator. */
  GetRNGstate();
  
  /* pick up the observed value of the test statistic, then generate a set of
     random contingency tables (given row and column totals) and adds their
     test scores to compute the mean.*/
  switch(INT(test)) {

    case MUTUAL_INFORMATION:
      observed = 2 * _mi(n, nrowt, ncolt, nr, nc, num);
      
      for (k = 0; k < *B; k++) {
        rcont2(nr, nc, nrowt, ncolt, num, fact, workspace, n);
        res[1] += 2 * _mi(n, nrowt, ncolt, nr, nc, num);
        npermuts++;
      }

      break;

    case PEARSON_X2:
      observed = _x2(n, nrowt, ncolt, nr, nc, num);
      
      for (k = 0; k < *B; k++) {
        rcont2(nr, nc, nrowt, ncolt, num, fact, workspace, n);
        res[1] += _x2(n, nrowt, ncolt, nr, nc, num);
        npermuts++;
      }

      break;

  }/*SWITCH*/

  PutRNGstate();

  /* save the observed and mean values of the statistic,
     and the number of permutations performed. */
  res[0] = observed;
  res[1] /= *B; // mean
  res[2] = npermuts;

  UNPROTECT(1);

  return result;

}/*MCARLO_MEAN*/
예제 #7
0
/* unconditional Monte Carlo simulation for discrete tests. */
SEXP mcarlo(SEXP x, SEXP y, SEXP lx, SEXP ly, SEXP length, SEXP samples,
    SEXP test, SEXP alpha) {

double *fact = NULL, *res = NULL, observed = 0;
int *n = NULL, *ncolt = NULL, *nrowt = NULL, *workspace = NULL;
int *num = INTEGER(length), *nr = INTEGER(lx), *nc = INTEGER(ly);
int *xx = INTEGER(x), *yy = INTEGER(y), *B = INTEGER(samples);
int i = 0, k = 0, npermuts = 0, enough = ceil(NUM(alpha) * (*B)) + 1;
SEXP result;

  /* allocate and initialize the result. */
  PROTECT(result = allocVector(REALSXP, 3));
  res = REAL(result);
  res[0] = res[1] = res[2] = 0; // initial test score / p-value / nb permutations

  /* allocate and compute the factorials needed by rcont2. */
  allocfact(*num);

  /* allocate and initialize the workspace for rcont2. */
  workspace = alloc1dcont(*nc);

  /* initialize the contingency table. */
  n = alloc1dcont(*nr * (*nc));

  /* initialize the marginal frequencies. */
  nrowt = alloc1dcont(*nr);
  ncolt = alloc1dcont(*nc);

  /* compute the joint frequency of x and y. */
  for (k = 0; k < *num; k++)
    n[CMC(xx[k] - 1, yy[k] - 1, *nr)]++;

  /* compute the marginals. */
  for (i = 0; i < *nr; i++)
    for (k = 0; k < *nc; k++) {

      nrowt[i] += n[CMC(i, k, *nr)];
      ncolt[k] += n[CMC(i, k, *nr)];

    }/*FOR*/

  /* initialize the random number generator. */
  GetRNGstate();

  /* pick up the observed value of the test statistic, then generate a set of
     random contingency tables (given row and column totals) and check how many
     tests are greater than the original one.*/
  switch(INT(test)) {

    case MUTUAL_INFORMATION:
      observed = _mi(n, nrowt, ncolt, nr, nc, num);

      for (k = 0; k < *B; k++) {

        rcont2(nr, nc, nrowt, ncolt, num, fact, workspace, n);

        if (_mi(n, nrowt, ncolt, nr, nc, num) > observed) {

          sequential_counter_check(res[1]);

        }/*THEN*/

        npermuts++;

      }/*FOR*/

      observed = 2 * observed;

      break;

    case PEARSON_X2:
      observed = _x2(n, nrowt, ncolt, nr, nc, num);

      for (k = 0; k < *B; k++) {

        rcont2(nr, nc, nrowt, ncolt, num, fact, workspace, n);

        if (_x2(n, nrowt, ncolt, nr, nc, num) > observed) {

          sequential_counter_check(res[1]);

        }/*THEN*/

        npermuts++;

      }/*FOR*/

      break;

  }/*SWITCH*/

  PutRNGstate();

  /* save the observed value of the statistic, the corresponding
     p-value, and the number of permutations performed. */
  res[0] = observed;
  res[1] /= (*B);
  res[2] = npermuts;

  UNPROTECT(1);

  return result;

}/*MCARLO*/