Example #1
0
/*
 * FIXME in lazy version, we need to consider the clauses that would be
 * activated and becoming unsat if the variable is flipped. The dcost computed
 * by the current code is an underestimate
 */
void cost_flip_unfixed_variable(samp_table_t *table, int32_t var, 
		int32_t *dcost) {
	rule_inst_table_t *rule_inst_table = &table->rule_inst_table;
	atom_table_t *atom_table = &table->atom_table;
	samp_literal_t plit, nlit;
	uint32_t i;

	if (assigned_true(atom_table->assignment[var])) {
		nlit = neg_lit(var);
		plit = pos_lit(var);
	} else {
		nlit = pos_lit(var);
		plit = neg_lit(var);
	}

	*dcost = 0;
	samp_clause_t *ptr;
	samp_clause_t *cls;

	/* add the number of the watched clauses that will be flipped */
	for (ptr = rule_inst_table->watched[plit].head;
			ptr != rule_inst_table->watched[plit].tail;
			ptr = next_clause_ptr(ptr)) {
		cls = ptr->link;
		i = 0;
		while (i < cls->num_lits
				&& (cls->disjunct[i] == plit
				|| assigned_false_lit(atom_table->assignment, cls->disjunct[i]))) {
			i++;
		}
		if (i == cls->num_lits) {
			*dcost += 1;
		}
	}

	/* subtract the number of the unsatisfied clauses that can be flipped */
	for (ptr = rule_inst_table->unsat_clauses.head;
			ptr != rule_inst_table->unsat_clauses.tail;
			ptr = next_clause_ptr(ptr)) {
		cls = ptr->link;
		i = 0;
		while (i < cls->num_lits && cls->disjunct[i] != nlit) {
			assert(cls->disjunct[i] != plit);
			i++;
		}
		if (i < cls->num_lits) {
			*dcost -= 1;
		}
	}
}
Example #2
0
samp_clause_t *choose_random_clause(samp_clause_list_t *list) {
	//int32_t clsidx = random_uint(list->length);
	int32_t clsidx = genrand_uint(list->length);
	samp_clause_t *ptr = list->head;
	for (; clsidx > 0; clsidx--) {
		ptr = next_clause_ptr(ptr);
	}
	return ptr->link;
}
Example #3
0
bool valid_clause_list(samp_clause_list_t *list) {
	assert(list->tail->link == NULL);
	samp_clause_t *ptr;
	int32_t length = 0;
	for (ptr = list->head; ptr != list->tail; ptr = next_clause_ptr(ptr)) {
		length++;
	}
	assert(length == list->length);
	return true;
}
Example #4
0
bool valid_clause_list(samp_clause_list_t *list) {
	assert(list->tail->link == NULL);
	samp_clause_t *ptr;
	int32_t length = 0;
	/* When two threads are running,  ptr == list->tail can fail: */
	for (ptr = list->head; ptr != list->tail; ptr = next_clause_ptr(ptr)) {
		length++;
	}
	assert(length == list->length);
	return true;
}
Example #5
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 #6
0
/*
 * Validate all the lists in the clause table
 */
bool valid_rule_inst_table(rule_inst_table_t *rule_inst_table, atom_table_t *atom_table){
	samp_clause_t *ptr;
	samp_clause_t *cls;
	int32_t lit, i;
	assert(rule_inst_table->size >= 0);
	assert(rule_inst_table->num_rule_insts >= 0);
	assert(rule_inst_table->num_rule_insts <= rule_inst_table->size);
	if (rule_inst_table->size < 0) return false;
	if (rule_inst_table->num_rule_insts < 0 
			|| rule_inst_table->num_rule_insts > rule_inst_table->size)
		return false;

	/* check that every clause in the unsat list is unsat */
	valid_clause_list(&rule_inst_table->unsat_clauses);
	for (ptr = rule_inst_table->unsat_clauses.head;
			ptr != rule_inst_table->unsat_clauses.tail;
			ptr = next_clause_ptr(ptr)) {
		cls = ptr->link;
		assert(eval_clause(atom_table->assignment, cls) == -1);
		if (eval_clause(atom_table->assignment, cls) != -1)
			return false;
	}

	///* check negative_or_unit_clauses */
	//valid_clause_list(&rule_inst_table->negative_or_unit_clauses);
	//for (ptr = rule_inst_table->negative_or_unit_clauses.head;
	//		ptr != rule_inst_table->negative_or_unit_clauses.tail;
	//		ptr = next_clause_ptr(ptr)) {
	//	cls = ptr->link;
	//	assert(cls->weight < 0 || cls->numlits == 1);
	//	if (cls->weight >= 0 && cls->numlits != 1) 
	//		return false;
	//}

	///* check dead_negative_or_unit_clauses */
	//valid_clause_list(&rule_inst_table->dead_negative_or_unit_clauses);
	//for (ptr = rule_inst_table->dead_negative_or_unit_clauses.head;
	//		ptr != rule_inst_table->dead_negative_or_unit_clauses.tail;
	//		ptr = next_clause_ptr(ptr)) {
	//	cls = ptr->link;
	//	assert(cls->weight < 0 || cls->numlits == 1);
	//	if (cls->weight >= 0 && cls->numlits != 1) 
	//		return false;
	//}

	/* check that every watched clause is satisfied */
	for (i = 0; i < atom_table->num_vars; i++) {
		lit = pos_lit(i);
		valid_watched_lit(rule_inst_table, lit, atom_table);

		lit = neg_lit(i);
		valid_watched_lit(rule_inst_table, lit, atom_table);
	}

	/* check the sat_clauses to see if the first disjunct is fixed true */
	valid_clause_list(&rule_inst_table->sat_clauses);
	for (ptr = rule_inst_table->sat_clauses.head;
			ptr != rule_inst_table->sat_clauses.tail;
			ptr = next_clause_ptr(ptr)) {
		cls = ptr->link;
		assert(clause_contains_fixed_true_lit(cls, atom_table->assignment));
		if (!clause_contains_fixed_true_lit(cls, atom_table->assignment))
			return false;
	}
	
	/* check the live_clauses */
	valid_clause_list(&rule_inst_table->sat_clauses);

	/* check all the clauses to see if they are properly indexed */
	rule_inst_t *rinst;
	for (i = 0; i < rule_inst_table->num_rule_insts; i++){
		rinst = rule_inst_table->rule_insts[i];
		assert(rinst->num_clauses >= 0);
		if (rinst->num_clauses < 0) 
			return false;
		//for (j = 0; j < clause->numlits; j++){
		//	assert(var_of(clause->disjunct[j]) >= 0);
		//	assert(var_of(clause->disjunct[j]) < atom_table->num_vars);
		//	if (var_of(clause->disjunct[j]) < 0 ||
		//			var_of(clause->disjunct[j]) >= atom_table->num_vars)
		//		return false;
		//}
	}
	return true;
}