示例#1
0
NNF_NODE var2nnf(Var* var, NnfManager* nnf_manager) {
  Lit* plit = sat_pos_literal(var);
  Lit* nlit = sat_neg_literal(var);
  if(sat_implied_literal(plit))      return nnf_literal2node(plit,nnf_manager);
  else if(sat_implied_literal(nlit)) return nnf_literal2node(nlit,nnf_manager);
  else return ONE_NNF_NODE;
}
示例#2
0
文件: sat_api.c 项目: wsw1206/CS264A
BOOLEAN sat_check_subsumed_clause(const Clause* clause) {
	c2dSize size = sat_clause_size(clause);
	for (c2dSize i = 0; i < size; i++) {
		Lit* lit = vector_get(&clause->lits, i);
		if (sat_implied_literal(lit))
			return 1;
	}
	return 0;
}
示例#3
0
文件: sat_api.c 项目: wsw1206/CS264A
//applies unit resolution to the cnf of sat state
//returns 1 if unit resolution succeeds, 0 if it finds a contradiction
BOOLEAN sat_unit_resolution(SatState* sat_state) {
	BOOLEAN more = 1;
	while (more) {
		more = 0;
		for (c2dSize i = vector_size(&sat_state->q); i >= 1; i--) {
			Clause* clause = vector_get(&sat_state->q, i - 1);
			clause->subsumed = sat_check_subsumed_clause(clause);
			if (sat_subsumed_clause(clause)) {
				vector_erase(&sat_state->q, i - 1);
				continue;
			}
			BOOLEAN flag = 0;
			c2dSize pos;
			for (c2dSize j = 0; j < sat_clause_size(clause); j++) {
				Lit* lit = vector_get(&clause->lits, j);
				if (!sat_implied_literal(sat_opp_literal(lit))) {
					flag++;
					pos = j;
					if (flag == 2)
						break;
				}
			}
			if (flag == 0) {
				sat_state->ac = malloc(sizeof(Clause));
				vector_init(&sat_state->ac->lits);
				sat_state->ac->subsumed = 0;
				sat_state->ac->mark = 0;
				if (vector_size(&sat_state->ds) > 0) {
					for (c2dSize j = 0; j < sat_var_count(sat_state); j++) {
						Var* var = vector_get(&sat_state->vars, j);
						var->u = var->level <= 1;
					}
					sat_get_assert_clause(sat_state, sat_state->ac, clause->index - 1, 0,
							vector_size(&sat_state->s) - 1);
				}
				return 0;
			}
			if (flag == 1) {
				Lit* lit = vector_get(&clause->lits, pos);
				lit->implied = 1;
				lit->locate = clause->index - 1;
				lit->var->level = vector_size(&sat_state->ds) + 1;
				vector_push(&sat_state->il, lit);
				vector_push(&sat_state->s, lit);
				clause->subsumed = 1;
				vector_erase(&sat_state->q, i - 1);
				more = 1;
			}
		}
	}
	return 1;
}
示例#4
0
static BOOLEAN evaluate_delta(SatState* sat_state, Lit** pending_list, unsigned long num_pending_lit){
	BOOLEAN fails =0;
#ifdef DEBUG
	printf("Evaluate delta loop: %ld at pending lit %ld \n",num_pending_lit, pending_list[num_pending_lit-1]->sindex );
#endif
	// just for the sake of this function but will be reset if contradiction happens
	for(unsigned long i =0; i < num_pending_lit; i++){
		Lit* pending_lit = pending_list[i];
		pending_lit->decision_level = sat_state->current_decision_level;
		if(pending_lit->sindex <0){
			pending_lit->LitValue = 0;
			pending_lit->LitState = 1;

			Lit* opposite_Lit = sat_literal_var(pending_lit)->posLit;
			opposite_Lit->LitValue = 0;
			opposite_Lit->LitState = 1;
			opposite_Lit->decision_level = pending_lit->decision_level;
		}
		else if(pending_lit->sindex >0){
			pending_lit->LitValue = 1;
			pending_lit->LitState = 1;

			Lit* opposite_Lit = sat_literal_var(pending_lit)->negLit;
			opposite_Lit->LitValue = 1;
			opposite_Lit->LitState = 1;
			opposite_Lit->decision_level = pending_lit->decision_level;
		}
	}


	for(unsigned long i=0; i<sat_state->num_clauses_in_delta; i++){
		Clause* clause = &sat_state->delta[i];
		//check clause
		BOOLEAN contradiction_found = 1;
		for(unsigned long k = 0; k<clause->num_literals_in_clause;k++){
			//check if the literal is asserted or not yet set
			if(sat_is_asserted_literal(clause->literals[k]) || (!sat_implied_literal(clause->literals[k]) )){
				contradiction_found = 0;
				break; //don't check the other literals of the clause
			}
		} // for all literals in a clause

		if(contradiction_found == 1){
			fails = 1;
			sat_state->conflict_clause = clause;
#ifdef DEBUG
			printf("In Evaluate Delta: \t");
			print_clause(clause);
#endif
//			for(unsigned long j = 0; j < num_pending_lit; j++){
//				Lit* pending_lit = pending_list[j];
//				pending_lit->LitValue = 'u';
//				pending_lit->LitState = 0;
//				//SALMA added this
//				pending_lit->decision_level = 0;
//				if(pending_lit->sindex <0){
//					Lit* opposite_Lit = sat_literal_var(pending_lit)->posLit;
//					opposite_Lit->LitValue = 'u';
//					opposite_Lit->LitState = 0;
//					//SALMA added this
//					opposite_Lit->decision_level = 0;
//				}else{
//					Lit* opposite_Lit = sat_literal_var(pending_lit)->negLit;
//					opposite_Lit->LitValue = 'u';
//					opposite_Lit->LitState = 0;
//					//SALMA added this
//					opposite_Lit->decision_level = 0;
//				}
//
//			}
			break;
		}

	}//end of for loop on clauses in delta
#ifdef DEBUG
	printf("Contradiction_found = %d\n",fails);
#endif

	return fails;
}
示例#5
0
static BOOLEAN evaluate_current_assignment(SatState* sat_state, Lit** pending_list, unsigned long num_pending_lit){
#ifdef DEBUG
	printf("Evaluate the current assignment\n");
#endif
	BOOLEAN fails = 0;

	// just for the sake of this function but will be reset if contradiction happens
	for(unsigned long i =0; i<num_pending_lit; i++){
		Lit* pending_lit = pending_list[i];
		if(pending_lit->sindex <0){
			pending_lit->LitValue = 0;
			pending_lit->LitState = 1;
		}
		else if(pending_lit->sindex >0){
			pending_lit->LitValue = 1;
			pending_lit->LitState = 1;
		}
	}

	Lit** decisions = sat_state->decisions;
	//now I have the current decision and the pending list
	// instead of merging the two list in one list just pass by each one
	for(unsigned long i =0; i<sat_state->num_literals_in_decision; i++){
#ifdef DEBUG
		printf("In the decision literals loop: current literal %ld \n", decisions[i]->sindex);
#endif
		Var* corresponding_var = sat_literal_var(decisions[i]);
		unsigned long* decision_clause_list = corresponding_var->list_clause_of_variables;


		for(unsigned long j=0; j<corresponding_var->num_of_clauses_of_variables; j++){

			Clause* clause = sat_index2clause(decision_clause_list[j], sat_state);

			//check clause
			for(unsigned long k = 0; k<clause->num_literals_in_clause;k++){
				//check if the literal is asserted or not yet set
				if(sat_is_asserted_literal(clause->literals[k]) || (!sat_implied_literal(clause->literals[k]) )){
					break; //don't check the other literals of the clause
				}
				else if(k== clause->num_literals_in_clause-1){ // I reached the end of the loop with no break (contradicted clause)
					fails = 1;
					sat_state->conflict_clause = clause;
					// reset the pending list
					for(unsigned long i =0; i<num_pending_lit; i++){
						Lit* pending_lit = pending_list[i];
						pending_lit->LitValue = 'u';
						pending_lit->LitState = 0;
					}
					return fails;
				}
			} // for all literals in a clause
		} // for all clauses for this variable
	}// for all variables in decision list


	//repeat the same for pending list
	for(unsigned long i =0; i< num_pending_lit; i++){
#ifdef DEBUG
		printf("In the pending literals loop: current literal %ld \n", pending_list[i]->sindex);
#endif
		Var* corresponding_var = sat_literal_var(pending_list[i]);
		unsigned long* pending_clause_list = corresponding_var->list_clause_of_variables;

		for(unsigned long j=0; j<corresponding_var->num_of_clauses_of_variables; j++){

			Clause* clause = sat_index2clause(pending_clause_list[j], sat_state);

			//check clause
			for(unsigned long k = 0; k<clause->num_literals_in_clause;k++){
				//check if the literal is asserted or not yet set
				if(sat_is_asserted_literal(clause->literals[k]) || (!sat_implied_literal(clause->literals[k]) )){
					break; //don't check the other literals of the clause
				}
				else if(k== clause->num_literals_in_clause-1){ // I reached the end of the loop with no break (contradicted clause)
					fails = 1;
					sat_state->conflict_clause = clause;
					// reset the pending list
					for(unsigned long i =0; i<num_pending_lit; i++){
						Lit* pending_lit = pending_list[i];
						pending_lit->LitValue = 'u';
						pending_lit->LitState = 0;
					}
					return fails;

				}
			} // for all literals in a clause
		} // for all clauses for this variable
	}// for all variables in pending list


//TODO: check do i have to unset all the literals in the pending list
#ifdef DEBUG
	if(fails == 1){
		printf("Contradiction happens in evaluating clauses in the pending list\n");
	}
#endif

	return fails;
}
示例#6
0
Var* sat_index2var(c2dSize index, const SatState* sat_state) {
  ASSERT_TEST(index>=1 && index<=sat_var_count(sat_state)); //index range test
  return sat_state->variables[index-1];
}
//returns the index of a variable
c2dSize sat_var_index(const Var* var) {
  return var->index;
}
//returns the variable of a literal
Var* sat_literal_var(const Lit* lit) {
  return lit->var;
}
//returns 1 if the variable is instantiated, 0 otherwise
//a variable is instantiated either by decision or implication (by unit resolution)
BOOLEAN sat_instantiated_var(const Var* var) {
  ASSERT_TEST(!sat_implied_literal(var->pos)||!sat_implied_literal(var->neg)) //pos, neg should not be all implied
  return (sat_implied_literal(var->pos)||sat_implied_literal(var->neg));
}
//returns 1 if all the clauses mentioning the variable are subsumed, 0 otherwise
BOOLEAN sat_irrelevant_var(const Var* var) {
  c2dSize i;
  Clause* clause;

  for (i=0; i<sat_var_occurences(var); i++) {
    clause = sat_clause_of_var(i, var);
    if (sat_subsumed_clause(clause))
      continue;
    else
      return 0;
  }
  return 1;
示例#7
0
文件: sat_api.c 项目: wsw1206/CS264A
//returns 1 if the variable is instantiated, 0 otherwise
//a variable is instantiated either by decision or implication (by unit resolution)
BOOLEAN sat_instantiated_var(const Var* var) {
	Lit* plit = sat_pos_literal(var);
	Lit* nlit = sat_neg_literal(var);
	return sat_implied_literal(plit) || sat_implied_literal(nlit);
}
示例#8
0
文件: sat_api.c 项目: Lcch/CS264A
//returns 1 if the variable is instantiated, 0 otherwise
//a variable is instantiated either by decision or implication (by unit resolution)
BOOLEAN sat_instantiated_var(const Var* var) {
  return sat_implied_literal(var->p_literal) | sat_implied_literal(var->n_literal);
}