Beispiel #1
0
void
ndfs_local_deinit   (run_t *run, wctx_t *ctx)
{
    alg_local_t        *loc = ctx->local;
    if (all_red)
        bitvector_free (&loc->stackbits);
    bitvector_free (&loc->color_map);
    dfs_stack_destroy (loc->stack);
    RTfree (loc);
    (void) run;
}
Beispiel #2
0
void
reach_sat_loop(reach_proc_t reach_proc, vset_t visited,
               bitvector_t *reach_groups, long *eg_count, long *next_count, long *guard_count)
{
    bitvector_t groups[max_sat_levels];
    int empty_groups[max_sat_levels];
    vset_t old_vis = vset_create(domain, -1, NULL);
    vset_t prev_vis[nGrps];

    for (int k = 0; k < max_sat_levels; k++)
        bitvector_create(&groups[k], nGrps);

    initialize_levels(groups, empty_groups, NULL, reach_groups);

    for (int i = 0; i < max_sat_levels; i++)
        prev_vis[i] = save_sat_levels?vset_create(domain, -1, NULL):NULL;

    while (!vset_equal(old_vis, visited)) {
        vset_copy(old_vis, visited);
        for (int k = 0; k < max_sat_levels; k++) {
            if (empty_groups[k]) continue;
            Warning(infoLong, "Saturating level: %d", k);
            reach_proc(visited, prev_vis[k], &groups[k], eg_count, next_count,guard_count);
            check_invariants(visited, -1);
            if (save_sat_levels) vset_copy(prev_vis[k], visited);
        }
    }

    for (int k = 0; k < max_sat_levels; k++)
        bitvector_free(&groups[k]);

    vset_destroy(old_vis);
    if (save_sat_levels)
        for (int i = 0; i < max_sat_levels; i++) vset_destroy(prev_vis[i]);
}
Beispiel #3
0
void model_free(model_t *model)
{
  model->n_params = 0;
  lbfgs_free(model->params);

  if (model->f_restrict != NULL)
    bitvector_free(model->f_restrict);
}
Beispiel #4
0
void cleanup (vector *bases)
{
    uscalar_t i, length;
    length = vector_length (bases);
    for (i = 0; i < length; i++) {
        basis *B;
        vector *T;
        uscalar_t j, len;

        B = vector_get (bases, i);
        cdnfformula_disjunction_free (B->H);
        bitvector_free (B->a);
        T = B->T;
        len = vector_length (T);
        for (j = 0; j < len; j++) {
            bitvector_free (vector_get (T, j));
        }
        vector_free (T);
        free (B);
    }
    vector_free (bases);
}
Beispiel #5
0
void
dm_free (matrix_t *m)
{
    // free memory
    m->rows = 0;
    m->cols = 0;

    // free bits for matrix
    bitvector_free (&(m->bits));

    // free row header
    dm_free_header (&(m->row_perm));

    // free column header
    dm_free_header (&(m->col_perm));

    return;
}
Beispiel #6
0
void rebuild_disjunctions (vector *bases)
{
    uscalar_t i, length;

    length = vector_length (bases);

    for (i = 0; i < length; i++) {
        basis *B;
        bitvector *a_i;
        vector *T_i;
        disjunction *H_i;
        uscalar_t j, T_i_len;
        monomial *m;

        B = (basis *) vector_get (bases, i);
        a_i = B->a;
        T_i = B->T;
        assert (B->H == NULL);
        H_i = cdnfformula_disjunction_unit ();
        T_i_len = vector_length (T_i);

        for (j = 0; j < T_i_len; j++) {
            bitvector *xor, *v;

            v = (bitvector *) vector_get (T_i, j);
            xor = bitvector_xor (v, a_i);
            /* when xor == 0, a conflict has occurred. */
            assert (!bitvector_is_zeros (xor));

            /* compute M_DNF (v_i + a_i) */
            m = cdnfformula_monomial_M_DNF (xor);
            /* compute m (x ^ a_i) */
            m = compute_m_of_x_xor_a (m, a_i);
            vector_add (H_i, m);
            bitvector_free (xor);
        }
        B->H = H_i;
    }
}
// gcc -Wall -O3 -DTEST_BIT_VECTOR  bitvector.c && ./a.out
int main() {
  const unsigned int nbits=40;
  bitvector_t b=bitvector_new(nbits);
  if (sizeof(int)!=4) {
    fprintf(stderr,"Size problem in bitvector.c");
    exit(1);
  }
  bitvector_show(b,nbits); printf("\n");
  bitvector_set(b,0);
  bitvector_set(b,1);
  bitvector_set(b,11);
  bitvector_set(b,38);
  bitvector_show(b,nbits); printf("\n");
  bitvector_clear(b,38);
  bitvector_show(b,nbits); printf("\n");
  bitvector_flip(b,0);
  bitvector_show(b,nbits); printf("\n");
  bitvector_flip(b,0);
  bitvector_show(b,nbits); printf("\n");
  printf("count_slow=%u\n",bitvector_count_slow(b,nbits));
  printf("count=%u\n",bitvector_count(b,nbits));
  bitvector_free(b);
  return 0;
}
Beispiel #8
0
int
main (void)
{

    bitvector_t         b1;
    bitvector_t         b2;

    bitvector_create (&b1, 20);

    user_bitvector_print (&b1);

    bitvector_set (&b1, 4);
    user_bitvector_print (&b1);

    bitvector_copy (&b2, &b1);

    bitvector_unset (&b1, 4);
    user_bitvector_print (&b1);

    user_bitvector_print (&b2);

    // test is_empty
    printf ("is_empty b1? %c (should be t)\n", bitvector_is_empty(&b1)?'t':'f');
    printf ("is_empty b2? %c (should be f)\n", bitvector_is_empty(&b2)?'t':'f');

    // set even/odd bits in b1/b2
    for(int i=0; i<20; ++i)
    {
        if (i%2)
        {
            bitvector_set(&b1,i);
        } else {
            bitvector_set(&b2,i);
        }
    }

    // print before union
    printf ("before union\n");
    user_bitvector_print (&b1);
    user_bitvector_print (&b2);

    // disjoint?
    printf ("b1,b2 are disjoint %c (should be t)\n", bitvector_is_disjoint(&b1, &b2)?'t':'f');

    printf ("union\n");
    bitvector_union(&b1, &b2);

    // disjoint?
    printf ("b1,b2 are disjoint %c (should be f)\n", bitvector_is_disjoint(&b1, &b2)?'t':'f');

    // print after union
    user_bitvector_print (&b1);
    user_bitvector_print (&b2);
    printf ("intersect\n");
    bitvector_intersect(&b1, &b2);

    // disjoint?
    printf ("b1,b2 are disjoint %c (should be f)\n", bitvector_is_disjoint(&b1, &b2)?'t':'f');

    // print after intersection
    user_bitvector_print (&b1);
    user_bitvector_print (&b2);

    printf ("invert b1\n");
    bitvector_invert(&b1);

    // print after inversion
    user_bitvector_print (&b1);

    // disjoint?
    printf ("b1,b2 are disjoint %c (should be t)\n", bitvector_is_disjoint(&b1, &b2)?'t':'f');

    bitvector_free (&b2);
    bitvector_free (&b1);

    matrix_t            m1;
    matrix_t            m2;
    dm_create (&m1, 10, 10);

    print_matrix (&m1);
    printf ("dm_set(4,4)\n");
    dm_set (&m1, 4, 4);
    print_matrix (&m1);
    printf ("dm_unset(4,4)\n");
    dm_unset (&m1, 4, 4);
    print_matrix (&m1);

    printf ("test shift permutation (3,4,5)(6,7)\n");
    printf ("before\n");
    dm_set (&m1, 3, 3);
    dm_set (&m1, 4, 4);
    dm_set (&m1, 5, 5);
    dm_set (&m1, 6, 6);
    dm_set (&m1, 7, 7);
    print_matrix (&m1);

    printf ("after\n");
    // create permutation_group, apply
    permutation_group_t o1;
    dm_create_permutation_group (&o1, 2, NULL);
    dm_add_to_permutation_group (&o1, 3);
    dm_add_to_permutation_group (&o1, 4);
    dm_add_to_permutation_group (&o1, 5);
    dm_close_group (&o1);
    dm_add_to_permutation_group (&o1, 6);
    dm_add_to_permutation_group (&o1, 7);

    dm_permute_cols (&m1, &o1);

    print_matrix (&m1);

    dm_free_permutation_group (&o1);

    printf ("swap cols 6,7\n");
    dm_swap_cols (&m1, 6, 7);
    print_matrix (&m1);

    printf ("swap rows 6,7\n");
    dm_swap_rows (&m1, 6, 7);
    print_matrix (&m1);

    printf ("copy\n");
    dm_create(&m2, dm_nrows(&m1), dm_ncols(&m1));
    dm_copy (&m1, &m2);
    // TODO: needs some more work
    print_matrix (&m2);


    dm_sort_rows (&m1, &min_row_first);
    print_matrix (&m1);

    dm_sort_rows (&m1, &max_row_first);
    print_matrix (&m1);

    dm_print_perm (&(m1.row_perm));

    printf ("to nub rows added & resorted\n");
    dm_set (&m1, 7, 3);
    dm_set (&m1, 8, 4);
    dm_sort_rows (&m1, &max_row_first);
    print_matrix (&m1);

    printf ("flatten \n");
    dm_flatten (&m1);

    print_matrix (&m1);
    dm_print_perm (&(m1.row_perm));

    printf ("nub sorted\n");

    //dm_nub_rows (&m1, &eq_rows, NULL);

    print_matrix (&m1);

    dm_print_perm (&(m1.row_perm));
    /* 
     * printf("again, now to test row order & nub idx\n"); dm_free(&m1);
     * dm_create(&m1, 10, 10); dm_set(&m1, 0,0); dm_set(&m1, 1,0);
     * dm_set(&m1, 2,3); dm_set(&m1, 3,3); print_matrix(&m1);
     * 
     * printf("nub sorted\n");
     * 
     * dm_nub_rows(&m1);
     * 
     * print_matrix(&m1);
     * 
     * dm_print_perm(&(m1.row_perm)); */
    printf ("optimize sorted\n");
    dm_set (&m1, 0, 7);
    dm_set (&m1, 1, 6);
    dm_set (&m1, 3, 9);

    printf ("before\n");
    print_matrix (&m1);
    dm_optimize (&m1);
    printf ("after\n");
    print_matrix (&m1);

    printf ("resorted\n");
    dm_sort_rows (&m1, &max_row_first);
    print_matrix (&m1);

/*
    dm_nub_cols(&m1);
    dm_ungroup_rows(&m1);
    dm_ungroup_cols(&m1);
    print_matrix (&m1);
*/

    // get bitvector from matrix text
    bitvector_create (&b1, 6);
    bitvector_create (&b2, 10);

    printf ("bitvector of row 0\n");
    user_bitvector_print (&b2);

    printf ("bitvector of col 8\n");
    user_bitvector_print (&b1);

    bitvector_free (&b2);
    bitvector_free (&b1);

    printf ("count test\n");
    for (int i = 0; i < dm_nrows (&m1); i++)
        printf ("ones in row %d: %d\n", i, dm_ones_in_row (&m1, i));
    for (int i = 0; i < dm_ncols (&m1); i++)
        printf ("ones in col %d: %d\n", i, dm_ones_in_col (&m1, i));

    printf ("iterator test\n");

    dm_row_iterator_t   mx;
    dm_col_iterator_t   my;

    for (int i = 0; i < dm_nrows (&m1); i++) {
        printf ("iterator row: %d\n", i);
        dm_create_row_iterator (&mx, &m1, i);
        int                 r;
        while ((r = dm_row_next (&mx)) != -1)
            printf (" next: %d\n", r);
        printf ("\n\n");
    }

    for (int i = 0; i < dm_ncols (&m1); i++) {
        printf ("iterator col: %d\n", i);
        dm_create_col_iterator (&my, &m1, i);
        int                 r;
        while ((r = dm_col_next (&my)) != -1)
            printf (" next: %d\n", r);
        printf ("\n\n");
    }

    printf ("projection test\n");
    int                 s0[10];
    int                 src[10];
    int                 prj[2];
    int                 tgt[10];

    // initialize
    for (int i = 0; i < 10; i++) {
        s0[i] = -i;
        src[i] = i;
    }

    // do projection
    int                 prj_n = dm_project_vector (&m1, 0, src, prj);

    // print projection
    printf ("projection:");
    for (int i = 0; i < prj_n; i++) {
        printf (" %d", prj[i]);
    }
    printf ("\n");

    printf ("expansion test\n");

    // do expansion
    int                 exp_n = dm_expand_vector (&m1, 0, s0, prj, tgt);
    (void)exp_n;

    // print expansion
    printf ("expansion:");
    for (int i = 0; i < 10; i++) {
        printf (" %d", tgt[i]);
    }
    printf ("\n");

    // subsumption
    printf ("subsumption test:\n");
    dm_swap_rows (&m1, 0, 3);
    dm_swap_rows (&m1, 1, 2);
    dm_flatten (&m1);
    print_matrix (&m1);
    dm_subsume_rows (&m1, &eq_rows, NULL);
    printf ("after subsumption:\n");
    print_matrix (&m1);
    printf ("\n");

    printf ("after ungrouping:\n");
    dm_ungroup_rows (&m1);
    print_matrix (&m1);
    printf ("\n");

    printf ("column sort test:\n");
    dm_flatten (&m1);
    printf ("max col first:\n");
    dm_sort_cols (&m1, &max_col_first);
    print_matrix (&m1);
    printf ("min col first:\n");
    dm_sort_cols (&m1, &min_col_first);
    print_matrix (&m1);

    printf ("nub columns test:\n");
    dm_set (&m1, 0, 1);
    dm_set (&m1, 3, 1);
    dm_set (&m1, 3, 4);
    dm_set (&m1, 3, 5);
    dm_sort_cols (&m1, &max_col_first);
    // dm_flatten(&m1);
    printf ("max col first:\n");
    print_matrix (&m1);
    //printf ("subsume columns:\n");
    //dm_subsume_cols (&m1, &eq_cols, NULL);
    //dm_subsume_rows (&m1, &eq_rows, NULL);
    print_matrix (&m1);
    printf ("column permutation:\n");
    dm_print_perm (&(m1.col_perm));

    printf ("optimize columns:\n");
    dm_optimize (&m1);
    print_matrix (&m1);

    //printf ("ungroup columns:\n");
    //dm_ungroup_cols (&m1);
    //print_matrix (&m1);


    //printf ("all permutations:\n");
    //dm_set (&m1, 0, 9);
    //dm_nub_cols(&m1, &eq_cols, NULL);
    //print_matrix (&m1);
    //dm_all_perm (&m1);

    dm_free (&m2);
    dm_free (&m1);

    return 0;
}
Beispiel #9
0
boolformula_t *learn_core (void *info,
                           uscalar_t num_vars,
                           membership_t membership, membership_t comembership,
                           equivalence_t equivalence, int mode)
{
    equivalence_result_t *eq_result;
    conjunction *conjecture;
    vector *bases;

    bases = vector_new (0);
    conjecture = get_conjecture (bases);
    boolformula_t* b_conjecture = cdnfformula_to_boolformula (conjecture);
    vector_free (conjecture);
    eq_result = (*equivalence) (info, num_vars, boolformula_copy (b_conjecture));
    if (eq_result->is_equal) {
        //fprintf(stderr,"Number of Variables Used : %d\n", (unsigned int)num_vars);
        free(eq_result);
        return b_conjecture;
    } else {
        boolformula_free(b_conjecture);
    }

#ifdef DEBUG
    fprintf (stderr, "add first basis with ");
    bitvector_print (eq_result->counterexample);
#endif

    assert (bitvector_length (eq_result->counterexample) == num_vars + 1);
    add_basis (bases, eq_result->counterexample);
    bitvector_free (eq_result->counterexample);
    free(eq_result);
    while (true) {
        vector *I;
        bitvector *v;
        uscalar_t j, length;

        conjecture = get_conjecture (bases);
#ifdef DEBUG
        fprintf (stderr, "new conjecture = ");
        boolformula_print (conjecture);
#endif
        boolformula_t* b_conjecture = cdnfformula_to_boolformula (conjecture);
        vector_free (conjecture);
        eq_result = (*equivalence) (info, num_vars, boolformula_copy (b_conjecture));

        if (eq_result->is_equal) {
            //fprintf(stderr,"Number of Variables Used : %d\n", (unsigned int)num_vars);
            cleanup (bases);
            free (eq_result);
            return b_conjecture;
        } else {
            boolformula_free(b_conjecture);
        }

        /* H_i's are still in bases, only free the conjunction */
        assert (bitvector_length (eq_result->counterexample) == num_vars + 1);
        v = eq_result->counterexample;
        I = get_indices_to_modify (bases, v);
        if (vector_length (I) == 0) {
            if(mode==CDNF_Plus3 && (*membership)(info,v)==true) {
#ifdef DEBUG
                fprintf (stderr, "conflict detected on: ");
                bitvector_print (v);
                fprintf (stderr, "num of variables: %d \n",num_vars);
#endif
                refine_bases(info, membership,comembership,bases,mode);
                num_vars++;
            } else {
#ifdef DEBUG
                fprintf (stderr, "add basis: ");
                bitvector_print (v);
#endif
                add_basis (bases, v);
            }
            bitvector_free (v);
            free(eq_result);
            vector_free (I);
            continue;
        }
        free(eq_result);

#ifdef DEBUG
        fprintf (stderr, "fix m_dnf with ");
        bitvector_print (v);
#endif
        length = vector_length (I);
        for (j = 0; j < length; j++) {
            uscalar_t i;
            basis *B;
            bitvector *a_i, *v_i, *xor;
            vector *T_i;
            disjunction *H_i;
            monomial *m;

            i = (uscalar_t) vector_get (I, j);
            B = (basis *) vector_get (bases, i);
            a_i = B->a;
            T_i = B->T;
            H_i = B->H;

            v_i = bitvector_copy (v);
            walk (info, membership, v_i, a_i);
            xor = bitvector_xor (v_i, a_i);
            /* when xor == 0, a conflict has occurred. */
            /* assert (!bitvector_is_zeros (xor)); */
            if (bitvector_is_zeros (xor)) {
#ifdef DEBUG
                fprintf (stderr, "conflict with the basis ");
                bitvector_print (a_i);
#endif
                bitvector_free (xor);
                bitvector_free (v_i);
                if(mode==CDNF_PlusPlus||mode==CDNF_Plus3) {
                    /* increase the number of variables */
#ifdef DEBUG
                    fprintf (stderr, "num of variables: %d \n",num_vars);
#endif
                    refine_bases (info, membership, comembership, bases,mode);
                    num_vars++;
                } else {
                    bitvector_free (v);
                    vector_free (I);
                    return NULL;
                }
                break;
            }
#ifdef DEBUG
            fprintf (stderr, "add support ");
            bitvector_print (v_i);
#endif
            /* store v_i in T_i */
            /* note that the original CDNF algorithm stores xor in S_i */
            vector_add (T_i, v_i);

            /* compute M_DNF (v_i + a_i) */
            m = cdnfformula_monomial_M_DNF (xor);
            /* compute m (x ^ a_i) */
            m = compute_m_of_x_xor_a (m, a_i);
            vector_add (H_i, m);
            bitvector_free (xor);
        }
        bitvector_free (v);
        vector_free (I);
    }
}
Beispiel #10
0
void refine_bases (void *info,
                   membership_t membership, membership_t comembership,
                   vector *bases, int mode)
{
    uscalar_t i, length;

    length = vector_length (bases);
#ifdef DEBUG
    fprintf (stderr, "refine bases, num of DNF = %d",length);
#endif
    for (i = 0; i < length; i++) {
        basis *B;
        bitvector *a_i;
        vector *T_i;
        uscalar_t l, T_i_len, j;
        bool b;

        B = (basis *) vector_get (bases, i);
        /*
         * extends each unsatisfying assignment by FALSE
         */
        a_i = B->a;
        l = bitvector_length (a_i);
        bitvector_resize (a_i, l + 1);
        bitvector_set (a_i, l, false);
        if(mode==CDNF_PlusPlus)
            b = (*comembership) (info, a_i) == true ? false : true;
        else
            b=false;
        bitvector_set (a_i, l, b);
#ifdef DEBUG
        fprintf (stderr, "extends basis: ");
        bitvector_print (a_i);
#endif

#ifdef DEBUG
        fprintf (stderr, "extends support: ");
#endif
        T_i = B->T;
        T_i_len = vector_length (T_i);
        /* except the last basis, all bases have at least one support */
        assert (i == length - 1 || T_i_len > 0);
        for (j = 0; j < T_i_len; j++) {
            bitvector *v;
            bool c;

            /*
             * extends v to v+b if MEM (v+b) = YES
             *           to v+!b if MEM (v+b) = NO
             */
            v = (bitvector *)vector_get (T_i, j);
            assert (bitvector_length (v) == l);
            bitvector_resize (v, l + 1);
            bitvector_set (v, l, b);
            c = (*membership) (info, v) == true ? b : !b;
            if (c != b)
                bitvector_set (v, l, c);
#ifdef DEBUG
            bitvector_print (v);
#endif
        }
        /* clean up disjunction for the current basis */
        cdnfformula_disjunction_free (B->H);
        B->H = NULL;

        /* remove the last basis if it has no support */
        if (T_i_len == 0) {
            assert (i == length - 1);
            bitvector_free (a_i);
            vector_free (T_i);
            free (vector_get (bases, length - 1));
            vector_resize (bases, length - 1);
        }
    }
    /*
     * reconstruct disjunctions for the bases
     */
    rebuild_disjunctions (bases);
}
Beispiel #11
0
static void
initialize_levels(bitvector_t *groups, int *empty_groups, int *back,
                      bitvector_t *reach_groups)
{
    int level[nGrps];

    // groups: i = 0 .. nGrps - 1
    // vars  : j = 0 .. N - 1

    // level[i] = first '+' in row (highest in BDD) of group i
    // recast 0 .. N - 1 down to equal groups 0 .. (N - 1) / sat_granularity
    for (int i = 0; i < nGrps; i++) {
        level[i] = -1;

        for (int j = 0; j < N; j++) {
            if (dm_is_set(GBgetDMInfo(model), i, j)) {
                level[i] = (N - j - 1) / sat_granularity;
                break;
            }
        }

        if (level[i] == -1)
            level[i] = 0;
    }

    for (int i = 0; i < nGrps; i++)
        bitvector_set(&groups[level[i]], i);

    // Limit the bit vectors to the groups we are interested in and establish
    // which saturation levels are not used.
    for (int k = 0; k < max_sat_levels; k++) {
        bitvector_intersect(&groups[k], reach_groups);
        empty_groups[k] = bitvector_is_empty(&groups[k]);
    }

    if (back == NULL)
        return;

    // back[k] = last + in any group of level k
    bitvector_t level_matrix[max_sat_levels];

    for (int k = 0; k < max_sat_levels; k++) {
        bitvector_create(&level_matrix[k], N);
        back[k] = max_sat_levels;
    }

    for (int i = 0; i < nGrps; i++) {
        dm_row_union(&level_matrix[level[i]], GBgetDMInfo(model), i);
    }

    for (int k = 0; k < max_sat_levels; k++) {
        for (int j = 0; j < k; j++) {
            bitvector_t temp;
            int empty;

            bitvector_copy(&temp, &level_matrix[j]);
            bitvector_intersect(&temp, &level_matrix[k]);
            empty = bitvector_is_empty(&temp);
            bitvector_free(&temp);

            if (!empty)
                if (j < back[k]) back[k] = j;
        }

        if (back[k] == max_sat_levels && !bitvector_is_empty(&level_matrix[k]))
            back[k] = k + 1;
    }

    for (int k = 0; k < max_sat_levels; k++)
        bitvector_free(&level_matrix[k]);

}