void mnreg_make_clauses( long posct, long negct, long propct, long clength, long ccount ) { clause curr_clause; literal curr_literal; long prop_guess, clause_guess; long totalct, pos_remaining, neg_remaining; long i, j, k; totalct = posct + negct; for( i=0; i<propct; i++ ) { prop_guess = tiset_choose( unexhausted_props ); tiset_delete( unexhausted_props, prop_guess ); pos_remaining = posct; neg_remaining = negct; for( j=0; j<totalct; j++ ) { if( tiset_isempty(unexhausted_clauses) ) for( k=0; k<ccount; k++ ) tiset_adjoin( unexhausted_clauses, k); clause_guess = tiset_choose( unexhausted_clauses ); tiset_delete( unexhausted_clauses, clause_guess ); curr_clause = &(the_formula->clauses[clause_guess]); curr_literal = &(curr_clause->members[curr_clause->length]); curr_literal->p_index = prop_guess; if( pos_remaining && !neg_remaining ) { curr_literal->sign = Pos; pos_remaining--; } else if( neg_remaining && !pos_remaining ) { curr_literal->sign = Neg; neg_remaining--; } else if( random_long(totalct - j) < pos_remaining ) { curr_literal->sign = Pos; pos_remaining--; } else { curr_literal->sign = Neg; neg_remaining--; } curr_clause->length++; } } }
void random_sorted_longs_generate(long num, long range) { long* arr = random_sorted_longs; long i; /* Free it, if needbe. */ if( arr ) { free(arr); } arr = malloc(sizeof(long) * num); /* Generate num random numbers */ for(i = 0; i < num; i++) { arr[i] = random_long() % range; } /* Sort the random numbers */ qsort(arr, num, sizeof(long), compare_longs); /* Save the array for later. */ random_sorted_longs = arr; }
int count_substitutions(tcube *a_cube, int the_part, int *the_individual_var, long *the_dividers, int *the_nb_consts, long *the_block_nb_inst, int *the_substitution, int *the_block_results, table the_table, int the_individual_arity) { int the_error = 0; tliteral *the_cube_lits = NULL; int the_cube_nb_args; targ *the_cube_args = NULL; int i; long j,the_long; long N = 0; long the_internal_N = 0; int the_result; int the_clause_value; int the_positive_value; int the_negative_value; int the_internal_clause_value; int the_internal_positive_value; int the_internal_negative_value; int the_first_lit; int the_block,the_block_sign,the_block_value; long the_sampling_size; long the_internal_long; if (unification_form == EXPLICIT){ the_cube_nb_args = a_cube->nb_exp_args; the_cube_lits = a_cube->exp_lits; the_cube_args = a_cube->exp_args; } else{ the_cube_nb_args = a_cube->nb_arg; the_cube_lits = a_cube->lits; the_cube_args = a_cube->args; } for (i=0;i<the_cube_nb_args;i++) if (the_cube_args[i].flip == VAR) { if (the_type_table[the_cube_args[i].type].global == TRUE) the_nb_consts[i] = the_type_table[the_cube_args[i].type].nb_const[0]; else the_nb_consts[i] = the_type_table[the_cube_args[i].type].nb_const[the_part]; } else the_nb_consts[i] = 1; the_dividers[the_individual_var[the_individual_arity-1]] = 1; for (i=the_individual_arity-2;(i>=0) && (the_error == 0);i--) if ((the_nb_consts[the_individual_var[i+1]] != 0) && (the_dividers[the_individual_var[i+1]] >= LONG_MAX / the_nb_consts[the_individual_var[i+1]])) the_error = 11; else the_dividers[the_individual_var[i]] = the_dividers[the_individual_var[i+1]] * the_nb_consts[the_individual_var[i+1]]; if (the_cube_nb_args > the_individual_arity) { the_dividers[the_individual_var[the_cube_nb_args-1]] = 1; for (i=the_cube_nb_args-2;(i>=the_individual_arity) && (the_error == 0);i--) if ((the_nb_consts[the_individual_var[i+1]] != 0) && (the_dividers[the_individual_var[i+1]] >= LONG_MAX / the_nb_consts[the_individual_var[i+1]])) the_error = 11; else the_dividers[the_individual_var[i]] = the_dividers[the_individual_var[i+1]] * the_nb_consts[the_individual_var[i+1]]; } if (the_error == 0){ if ((the_nb_consts[the_individual_var[0]] != 0) && (the_dividers[the_individual_var[0]] >= LONG_MAX / the_nb_consts[the_individual_var[0]])) the_error = 11; else{ N = the_dividers[the_individual_var[0]] * the_nb_consts[the_individual_var[0]]; if (the_cube_nb_args > the_individual_arity) { if ((the_nb_consts[the_individual_var[the_individual_arity]] != 0) && (the_dividers[the_individual_var[the_individual_arity]] >= LONG_MAX / the_nb_consts[the_individual_var[the_individual_arity]])) the_error = 11; else the_internal_N = the_dividers[the_individual_var[the_individual_arity]] * the_nb_consts[the_individual_var[the_individual_arity]]; } else the_internal_N = 1; } } if (the_error == 0) { if ((counting_bottom_up == TRUE) && (individual_based == FALSE) && ((the_expected_value == NORMAL)||(the_expected_value == NAIVE)) && (use_sampling == FALSE) && (unification_form == IMPLICIT)) { the_error = count_building_substitutions_from_instances(a_cube,the_substitution,the_nb_consts,the_cube_nb_args,the_part); } else { a_cube->nb_inst = 0; a_cube->nb_inst_positive = 0; a_cube->nb_inst_negative = 0; for (i=0;i<a_cube->num_blocks;i++) a_cube->block_nb_inst[i] = 0; if (use_sampling == 1) the_sampling_size = N * the_sampling_ratio; else the_sampling_size = N; a_cube->capital_n = 0; for (j=0;j<the_sampling_size;j++) { if (use_sampling == 1) the_long = random_long(N-1); else the_long = j; if ((use_sampling != 2) || (random_long(N) <= (long)(the_sampling_ratio * (double)N))) { a_cube->capital_n += 1; for (i=0;i<the_individual_arity;i++) the_substitution[the_individual_var[i]] = (the_long / the_dividers[the_individual_var[i]]) % the_nb_consts[the_individual_var[i]]; if ((counting_bottom_up == TRUE) && ((the_expected_value == NORMAL)||(the_expected_value == NAIVE)) && (unification_form == IMPLICIT)) { the_error = count_building_substitutions_from_instances(a_cube,the_substitution,the_nb_consts,the_cube_nb_args,the_part); } else { the_clause_value = 0; the_positive_value = 0; the_negative_value = 0; for (i=0;i<a_cube->num_blocks;i++) the_block_results[i] = 0; for (the_internal_long=0; (the_internal_long<the_internal_N) &&(the_clause_value==0); the_internal_long++) { for (i=the_individual_arity;i<the_cube_nb_args;i++) the_substitution[the_individual_var[i]] = (the_internal_long / the_dividers[the_individual_var[i]]) % the_nb_consts[the_individual_var[i]]; the_internal_clause_value = 1; the_internal_positive_value = 1; the_internal_negative_value = 1; for (the_block=0;(the_block<a_cube->num_blocks)&&(the_error==0) &&((the_expected_value==NAIVE)||(the_expected_value==BACKGROUND) ||(the_expected_value == LINEAR_RESOLUTION) ||(the_expected_value == INTEGRITY_CONSTRAINTS) ||(the_internal_positive_value==1)||(the_internal_negative_value==1));the_block++) { the_block_value = 1; if (unification_form == EXPLICIT) { for (i=0;(i<a_cube->nb_substs)&&(the_block_value==1);i++) if (a_cube->blocks_table[a_cube->nb_lit+i] == the_block) { if (a_cube->substs[i].flip == VAR) the_block_value = (the_substitution[a_cube->substs[i].arg] == the_substitution[a_cube->substs[i].value]); else the_block_value = (the_substitution[a_cube->substs[i].arg] == a_cube->substs[i].value); } the_internal_clause_value = the_internal_clause_value && the_block_value; the_internal_positive_value = the_internal_positive_value && the_block_value; the_internal_negative_value = the_internal_negative_value && the_block_value; } if (individual_based == TRUE) the_first_lit = 1; else the_first_lit = 0; the_block_sign = get_block_sign(a_cube,the_block,the_cube_lits); for (i=the_first_lit;(i<a_cube->nb_lit)&&(the_error==0)&&(the_block_value==1);i++) if (a_cube->blocks_table[i] == the_block) if ((the_expected_value==NAIVE) || (the_expected_value==BACKGROUND) || (the_expected_value == LINEAR_RESOLUTION) || (the_expected_value == INTEGRITY_CONSTRAINTS) || (((the_search_bias == CLASSIFICATION) || (the_search_bias == POSITIVE_CLASSIFICATION) || (the_search_bias == HORN_POSITIVE_CLASSIFICATION)) && (the_internal_positive_value==1)) || (((the_block_sign==POS) && (the_internal_positive_value==1)) || ((the_block_sign==NEG) && (the_internal_negative_value==1)))) { the_error = evaluate_literal(&(the_cube_lits[i]),the_cube_args,the_part,the_substitution, &the_result); if (the_error == 0) the_block_value = the_block_value && the_result; } if (the_error == 0) { the_block_results[the_block] = the_block_results[the_block] || the_block_value; a_cube->block_nb_inst[the_block] += the_block_value; the_internal_clause_value = the_internal_clause_value && the_block_value; if ((the_search_bias == CLASSIFICATION) || (the_search_bias == POSITIVE_CLASSIFICATION) || (the_search_bias == HORN_POSITIVE_CLASSIFICATION)) { if ((a_cube->num_blocks > the_first_lit)&&(the_block == a_cube->blocks_table[the_first_lit]) &&(the_block_sign == NEG)) the_internal_negative_value = the_internal_negative_value && the_block_value; else the_internal_positive_value = the_internal_positive_value && the_block_value; } else if (the_block_sign == POS) the_internal_positive_value = the_internal_positive_value && the_block_value; else the_internal_negative_value = the_internal_negative_value && the_block_value; } } the_clause_value = the_clause_value || the_internal_clause_value; the_positive_value = the_positive_value || the_internal_positive_value; the_negative_value = the_negative_value || the_internal_negative_value; } if (the_clause_value>=1) { a_cube->nb_inst += the_clause_value; a_cube->nb_inst_positive += the_positive_value; a_cube->nb_inst_negative += the_negative_value; } else { if (the_positive_value==1) a_cube->nb_inst_positive += the_positive_value; if (the_negative_value==1) a_cube->nb_inst_negative += the_negative_value; } #if PROLOG increment_literal_cells(a_cube->num_blocks,the_block_results,a_cube,the_table); #endif } } } } } return the_error; }
void test_random_long() { long test_long = random_long(); ok1(test_long >= LONG_MIN && test_long <= LONG_MAX); }