Example #1
0
int32_t choose_clause_var(samp_table_t *table, samp_clause_t *clause,
		double rvar_probability) {
	rule_inst_table_t *rule_inst_table = &table->rule_inst_table;
	atom_table_t *atom_table = &table->atom_table;
	integer_stack_t *clause_var_stack = &table->clause_var_stack;
	uint32_t i, sidx, vidx;

	if (clause_var_stack->size == 0) {
		init_integer_stack(clause_var_stack, 0);
	} else {
		clear_integer_stack(clause_var_stack);
	}

	double choice = choose();
	if (choice < rvar_probability) { /* flip a random unfixed variable */
		for (i = 0; i < clause->num_lits; i++) {
			if (unfixed_tval(atom_table->assignment[var_of(clause->disjunct[i])]))
				push_integer_stack(i, clause_var_stack);
		} 
		if (unfixed_tval(rule_inst_table->assignment[clause->rule_index]))
			push_integer_stack(clause->num_lits, clause_var_stack);
		/* all unfixed vars are now in clause_var_stack */
	} else {
		int32_t dcost = INT32_MAX, vcost = 0;
		for (i = 0; i < clause->num_lits; i++) {
			if (unfixed_tval(atom_table->assignment[var_of(clause->disjunct[i])])) {
				cost_flip_unfixed_variable(table, var_of(clause->disjunct[i]), &vcost);
				if (dcost >= vcost) {
					if (dcost > vcost) {
						dcost = vcost;
						clear_integer_stack(clause_var_stack);
					}
					push_integer_stack(i, clause_var_stack);
				}
			}
		}
		if (unfixed_tval(rule_inst_table->assignment[clause->rule_index])) {
			cost_flip_unfixed_rule(table, clause->rule_index, &vcost);
			if (dcost > vcost) {
				dcost = vcost;
				clear_integer_stack(clause_var_stack);
				push_integer_stack(clause->num_lits, clause_var_stack);
			}
		}
	}
	//sidx = random_uint(length_integer_stack(clause_var_stack));
	sidx = genrand_uint(length_integer_stack(clause_var_stack));
	vidx = nth_integer_stack(sidx, clause_var_stack);

	return vidx;
}
Example #2
0
/*
 * Print literal l in DIMACS format
 * - l is 2 * v + s with 0 <= v < nvars and s=0 or s=1
 * - 0 is not allowed as a variable in DIMACS so we
 *   convert variable v to DIMACS var (v+1)
 */
void dimacs_print_literal(FILE *f, literal_t l) {
  bvar_t v;

  v = var_of(l);
  if (is_neg(l)) fputc('-', f);
  fprintf(f, "%"PRId32, v+1);
}
Example #3
0
void print_egraph_conflict(FILE *f, egraph_t *egraph, ivector_t *expl_vector) {
  void *atom;
  uint32_t i, n;
  literal_t l;
  bvar_t v;

  n = expl_vector->size;
  for (i=0; i<n; i++) {
    l = expl_vector->data[i];
    v = var_of(l);
    if (bvar_has_atom(egraph->core, v)) {
      atom = bvar_atom(egraph->core, v);
      switch (atom_tag(atom)) {
      case EGRAPH_ATM_TAG:
	if (is_neg(l)) fputs("(not ", f);
	print_eterm(f, egraph, ((atom_t *) untag_atom(atom))->eterm);
	if (is_neg(l)) fputc(')', f);
	break;

      default:
	print_literal(f, l);
	break;
      }
    } else {
      print_literal(f, l);
    }
    if (i+1 < n) {
      fputc(' ', f);
    }
  }
  fflush(f);
}
Example #4
0
static char *_try_directory(QSP_ARG_DECL  const char *dir,const char* progname)
{
	FILE *fp;
	static char filename[MAXPATHLEN];

	if( *dir ){
		strcpy(filename,dir);
		strcat(filename,DIR_DELIM);
	} else strcpy(filename,"");

	strcat(filename,progname);
	strcat(filename,".scr");

	fp=fopen(filename,"r");

	if( fp!=NULL ) {
		redir(fp, filename );
		if( *dir ){
			// We should only set the variable here if
			// it doesn't already exist - vars defined
			// in the environment are reserved!
			Variable *vp;
			vp = var_of(STARTUP_DIRNAME);
			if( vp == NULL ){
				assign_var(STARTUP_DIRNAME,dir);
			}
		}
		return(filename);
	} else {
		return(NULL);
	}
}
Example #5
0
/*
 * Pushes a clause to a list depending on its evaluation
 *
 * If it is fixed to be satisfied, push to sat_clauses
 * If it is satisfied, push to the corresponding watched list
 * If it is unsatisified, push to unsat_clauses
 */
void insert_live_clause(samp_clause_t *clause, samp_table_t *table) {
	rule_inst_table_t *rule_inst_table = &table->rule_inst_table;
	atom_table_t *atom_table = &table->atom_table;
	int32_t i, fixable;
	samp_literal_t lit;

	/* See if the clause is fixed-unit propagating */
	fixable = get_fixable_literal(atom_table->assignment, 
			rule_inst_table->assignment, clause);
	if (fixable == -2) { 
		/* fixed unsat */
		mcsat_err("There is a fixed unsat clause, no model exists.");
		return;
	}
	else if (fixable == -1) { /* more than one unfixed lits */
		i = get_true_literal(atom_table->assignment,
				rule_inst_table->assignment, clause);
		if (i < 0) { 
			/* currently unsat, put back to the unsat list */
			clause_list_insert_head(clause, 
					&rule_inst_table->unsat_clauses);
		} else if (i == clause->num_lits) {
			/* currently sat, put to the rule_watched list */
			clause_list_insert_head(clause, 
					&rule_inst_table->rule_watched[clause->rule_index]);
		} else {
			/* currently sat, put to the watched list */
			clause_list_insert_head(clause, 
					&rule_inst_table->watched[clause->disjunct[i]]);
		}
	} else { 
		/* fix a soft rule to unsat, because one of its clauses is fixed unsat */
		if (fixable == clause->num_lits) {
			rule_inst_t *rinst = rule_inst_table->rule_insts[clause->rule_index];
			if (assigned_true(rule_inst_table->assignment[clause->rule_index])) {
				rule_inst_table->unsat_weight += rinst->weight;
			} else {
				rule_inst_table->sat_weight   += rinst->weight;
                        }
			if (get_verbosity_level() >= 3) {
				printf("[insert_live_clause] Fix rule %d: \n", clause->rule_index);
				print_rule_instance(rinst, table);
				printf(" to false\n");
			}
			rule_inst_table->assignment[clause->rule_index] = v_up_false;
		} else {
			/* fixed sat if we fix the 'fixable' literal */
			lit = clause->disjunct[fixable];
			if (unfixed_tval(atom_table->assignment[var_of(lit)])) {
				/* unit propagation */
				fix_lit_true(table, lit);
			} else {
				// already fixed sat
			}
			assert(assigned_fixed_true_lit(atom_table->assignment, lit));
		}
		clause_list_insert_head(clause, &rule_inst_table->sat_clauses);
	}
}
Example #6
0
void window_sys_init(SINGLE_QSP_ARG_DECL)
{
	char s[8];
	Variable *vp;

	if( window_sys_inited ) return;

#ifdef QUIP_DEBUG
	xdebug = add_debug_module(QSP_ARG  "xsupp");
#endif /* QUIP_DEBUG */

	add_event_func(QSP_ARG  i_loop);
	set_discard_func( discard_events );

	window_sys_inited=1;

	if( current_dop == NO_DISP_OBJ ){
		current_dop = default_x_display(SINGLE_QSP_ARG);
		if( current_dop == NO_DISP_OBJ ){
			NWARN("Couldn't open default display!?");
			return;
		}
	}
	// Make sure DISPLAY_WIDTH and DISPLAY_HEIGHT are set...
	// If these have been set in the environment, leave be.
	vp = var_of(QSP_ARG  "DISPLAY_WIDTH");
	if( vp == NULL ){
		sprintf(s,"%d",current_dop->do_width);
		ASSIGN_RESERVED_VAR("DISPLAY_WIDTH",s);
	}
	vp = var_of(QSP_ARG  "DISPLAY_HEIGHT");
	if( vp == NULL ){
		sprintf(s,"%d",current_dop->do_height);
		ASSIGN_RESERVED_VAR("DISPLAY_HEIGHT",s);
	}

	//window_sys_inited=1;
}
Example #7
0
static bool valid_watched_lit(rule_inst_table_t *rule_inst_table, samp_literal_t lit,
		atom_table_t *atom_table) {
	valid_clause_list(&rule_inst_table->watched[lit]);

	bool lit_true = (is_pos(lit) && assigned_true(atom_table->assignment[var_of(lit)]))
	             || (is_neg(lit) && assigned_false(atom_table->assignment[var_of(lit)]));
	assert(is_empty_clause_list(&rule_inst_table->watched[lit]) || lit_true);
	if (!is_empty_clause_list(&rule_inst_table->watched[lit]) && !lit_true) {
		return false;
	}

	samp_clause_t *ptr;
	samp_clause_t *cls;
	for (ptr = rule_inst_table->watched[lit].head;
			ptr != rule_inst_table->watched[lit].tail;
			ptr = next_clause_ptr(ptr)) {
		cls = ptr->link;
		assert(clause_contains_lit(cls, lit));
		if (!clause_contains_lit(cls, lit))
			return false;
	}
	return true;
}
Example #8
0
/*
 * Pseudo literal s: print the literal mapped to s
 */
static void print_pseudo_literal(FILE *f, remap_table_t *table, literal_t s) {
  if (s != null_literal) {
    s = remap_table_find(table, s);
  }
  if (s == null_literal) {
    fputs("?", f);
  } else if (s == true_literal) {
    fputs("t", f);
  } else if (s == false_literal) {
    fputs("f", f);
  } else {
    if (is_neg(s)) fputc('~', f);
    fprintf(f, "p!%"PRId32, var_of(s));
  }
}
Example #9
0
/*
 * Print assignment
 */
void print_boolean_assignment(FILE *f, smt_core_t *core) {
  prop_stack_t *stack;
  uint32_t i, n;
  literal_t l;

  stack = &core->stack;
  n = stack->top;
  for (i=0; i<n; i++) {
    l = stack->lit[i];
    fputc(' ', f);
    if (is_pos(l)) fputc(' ', f);
    print_literal(f, l);
    fprintf(f, " level = %"PRIu32"\n", core->level[var_of(l)]);
  }
}
Example #10
0
void print_egraph_atom_of_literal(FILE *f, egraph_t *egraph, literal_t l) {
  void *atom;
  bvar_t v;

  v = var_of(l);
  assert(bvar_has_atom(egraph->core, v));
  atom = bvar_atom(egraph->core, v);
  assert(atom_tag(atom) == EGRAPH_ATM_TAG);
  if (is_neg(l)) {
    fputs("(not ", f);
  }
  print_eterm(f, egraph, ((atom_t *) untag_atom(atom))->eterm);
  if (is_neg(l)) {
    fputc(')', f);
  }
}
Example #11
0
/*
 * Literal
 */
void print_literal(FILE *f, literal_t l) {
  if (l < 0) {
    if (l == null_literal) {
      //      fputs("null_literal", f);
      fputs("nil", f);
    } else {
      fprintf(f, "LIT%"PRId32, l);
    }
  } else if (l == true_literal) {
    fputs("tt", f);
  } else if (l == false_literal) {
    fputs("ff", f);
  } else {
    if (is_neg(l)) fputc('~', f);
    fprintf(f, "p!%"PRId32, var_of(l));
  }
}
Example #12
0
void print_bv_solver_atom_of_literal(FILE *f, bv_solver_t *solver, literal_t l) {
  void *atm;
  bvar_t v;
  int32_t id;

  v = var_of(l);
  assert(bvar_has_atom(solver->core, v));
  atm = bvar_atom(solver->core, v);
  assert(atom_tag(atm) == BV_ATM_TAG);
  id = bvatom_tagged_ptr2idx(atm);

  if (is_neg(l)) {
    fputs("(not ", f);
  }
  print_bv_solver_atom(f, solver, id);
  if (is_neg(l)) {
    fputc(')', f);
  }
}
Example #13
0
void print_simplex_atom_of_literal(FILE *f, simplex_solver_t *solver, literal_t l) {
  void *atm;
  arith_atom_t *a;
  bvar_t v;
  int32_t i;

  v = var_of(l);
  assert(bvar_has_atom(solver->core, v));
  atm = bvar_atom(solver->core, v);
  assert(atom_tag(atm) == ARITH_ATM_TAG);
  i = arithatom_tagged_ptr2idx(atm);
  a = arith_atom(&solver->atbl, i);
  assert(a->boolvar == v);

  if (is_neg(l)) {
    fputs("(not ", f);
  }
  print_arith_atom(f, &solver->vtbl, a);
  if (is_neg(l)) {
    fputc(')', f);
  }
}
Example #14
0
int default_stim(QSP_ARG_DECL  Trial_Class *tc_p,int val,Staircase *stc_p)
{
	char stim_str[256], *s;
	int coin=0;	// initialize to quiet compiler, but not necessary!?
	int rsp;
	//struct var *vp;
	Variable *vp;
	float *xv_p;

	if( is_fc ){
		coin=(int)rn(1);
		sprintf(stim_str,"%d",coin);
		assign_var("coin",stim_str);
	}

	assert( CLASS_XVAL_OBJ(tc_p) != NULL );
	xv_p = indexed_data( CLASS_XVAL_OBJ(tc_p), val );
	sprintf(stim_str,"%f",*xv_p);

	/* clip trailing zeros if there is a decimal point */
	s=stim_str;
	while( *s ){
		if( *s == '.' ){
			s=stim_str+strlen(stim_str)-1;
			while( *s == '0' ) {
				*s=0;
				s--;
			}
			/*
			 * if ONLY 0's after the decimal pt.,
			 * remove the pt too!
			 */
			if( *s == '.' ){
				*s=0;
				s--;
			}
		}
		s++;
	}

	assign_var("xval",stim_str);
	sprintf(stim_str,"%d",val);
	assign_var("val",stim_str);
	sprintf(stim_str,"%d",CLASS_INDEX(tc_p));
	assign_var("class",stim_str);

	assert( tc_p != NULL );

	//sprintf(msg_str,"Text \"%s\"",(char *)(tc_p->cl_data));
	//PUSH_INPUT_FILE(msg_str);

	//interpret_text_fragment(QSP_ARG tc_p->cl_data);		/* use chew_text??? */
	chew_text(CLASS_CMD(tc_p), "(stimulus text)");
	vp=var_of("response_string");
	if( vp != NULL )
		rsp = collect_response(VAR_VALUE(vp));
	else {
		static int warned=0;

		if( !warned ){
			warn("default_stim:  script variable $response_string not defined");
			warned=1;
		}
		rsp = collect_response("Enter response: ");
	}

	if( is_fc ){
		/* stimulus routine may have changed value of coin */
		vp=var_of("coin");
		if( vp == NULL )
			warn("variable \"coin\" not set!!!");
		else {
			if( sscanf(VAR_VALUE(vp),"%d",&coin) != 1 )
			warn("error scanning integer from variable \"coin\"\n");
		}

		/*
		if( coin ){
			if( rsp == YES ) rsp = NO;
			else if( rsp == NO ) rsp = YES;
		}
		*/
		assert( stc_p != NULL );
        
        // analyzer complains coin is a garbage value??? BUG?
		if( coin ){
			SET_STAIR_CRCT_RSP(stc_p,NO);
		} else {
			SET_STAIR_CRCT_RSP(stc_p,YES);
		}
		if( verbose ){
			if( rsp == STAIR_CRCT_RSP(stc_p) )
				advise("correct");
			else
				advise("incorrect");
		}
	}
	return(rsp);
}
Example #15
0
/* MaxWalkSAT */
void mw_sat(samp_table_t *table, int32_t num_trials, double rvar_probability,
		int32_t max_flips, uint32_t timeout) {
	rule_inst_table_t *rule_inst_table = &table->rule_inst_table;
	atom_table_t *atom_table = &table->atom_table;
	int32_t i, j;
	time_t fintime;

	if (timeout != 0) {
		fintime = time(NULL) + timeout;
	}

	double best_cost = DBL_MAX;
	for (i = 0; i < num_trials; i++) {
		init_random_assignment(table);
		//scan_rule_instances(table);
		for (j = 0; j < rule_inst_table->num_rule_insts; j++) {
			if (rule_inst_table->rule_insts[j]->weight == DBL_MAX) 
				rule_inst_table->assignment[j] = v_up_true;
			else
				rule_inst_table->assignment[j] = v_true;
		}
		init_live_clauses(table);
		scan_live_clauses(table);

		if (get_verbosity_level() >= 3) {
			printf("\n[mw_sat] Trial %d: \n", i);
			print_live_clauses(table);
		}

		for (j = 0; j < max_flips; j++) {

			if (timeout != 0 && time(NULL) >= fintime) {
				printf("Timeout after %"PRIu32" samples\n", i);
				break;
			}

			if (rule_inst_table->unsat_clauses.length == 0 
					&& rule_inst_table->unsat_soft_rules.nelems == 0) {
				return;
			}
			int32_t c = genrand_uint(rule_inst_table->unsat_clauses.length
					+ rule_inst_table->unsat_soft_rules.nelems);

			if (c < rule_inst_table->unsat_clauses.length) { /* picked a hard clause */
				samp_clause_t *clause = choose_random_clause(&rule_inst_table->unsat_clauses);
				int32_t litidx = choose_clause_var(table, clause, rvar_probability);
				
				if (litidx == clause->num_lits) {
					if (get_verbosity_level() >= 3) {
						printf("[flip_rule_to_unsat] Rule %d: ", clause->rule_index);
						print_rule_instance(rule_inst_table->rule_insts[clause->rule_index], table);
						printf("\n");
					}
					/* picked the rule auxiliary variable, i.e., rinst T -> F */
					hmap_get(&rule_inst_table->unsat_soft_rules, clause->rule_index);
					rule_inst_table->assignment[clause->rule_index] = v_false;
					rule_inst_t *rinst = rule_inst_table->rule_insts[clause->rule_index];
					rule_inst_table->unsat_weight += rinst->weight;
                                        /* Is this really going to do the right thing?? */
					rule_inst_table->sat_weight   -= rinst->weight;
					/* move unsat_clauses to live_clause_list and rescan */
					clause_list_concat(&rule_inst_table->unsat_clauses, 
							&rule_inst_table->live_clauses);
					scan_live_clauses(table);
				} else { 
					flip_unfixed_variable(table, var_of(clause->disjunct[litidx]));
				}
			} else {
				/* picked a soft rule auxiliary variable, i.e., rinst F -> T */
				int32_t rule_index = hmap_remove_random(&rule_inst_table->unsat_soft_rules);
				rule_inst_table->assignment[rule_index] = v_true;
				rule_inst_t *rinst = rule_inst_table->rule_insts[rule_index];
				rule_inst_table->unsat_weight -= rinst->weight;
				rule_inst_table->sat_weight   += rinst->weight;
				if (get_verbosity_level() >= 3) {
					printf("[flip_rule_to_sat] Rule %d: ", rule_index);
					print_rule_instance(rinst, table);
					printf("\n");
				}
				/* move watch[~r] to live_clause_list and rescan */
				clause_list_concat(&rule_inst_table->rule_watched[rule_index],
						&rule_inst_table->live_clauses);
				scan_live_clauses(table);
			}

			if (get_verbosity_level() >= 1) {
				printf("[mw_sat] Flip %d: # unsat hard = %d, # unsat soft = %d, " 
						"weight of unsat soft = %f; weight of sat soft = %f\n", j,
						rule_inst_table->unsat_clauses.length,
						rule_inst_table->unsat_soft_rules.nelems,
                                                rule_inst_table->unsat_weight,
                                                rule_inst_table->sat_weight
                                       );
			}
			if (get_verbosity_level() >= 3) {
				print_live_clauses(table);
			}

			if (best_cost > rule_inst_table->unsat_weight) {
				best_cost = rule_inst_table->unsat_weight;
				copy_assignment_array(atom_table);
			}
		}
	}
	restore_assignment_array(atom_table);
}
Example #16
0
/*
 * In unit propagation, fix a literal's value to true
 */
static int32_t inline fix_lit_true(samp_table_t *table, int32_t lit) {
	return update_atom_tval(var_of(lit), 
			sign_of_lit(lit) ? v_up_false : v_up_true, 
			table);
}