本文整理汇总了C++中Solver::addClause方法的典型用法代码示例。如果您正苦于以下问题:C++ Solver::addClause方法的具体用法?C++ Solver::addClause怎么用?C++ Solver::addClause使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver::addClause方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: extendModelImpossible
/**
@brief Used when a variable was eliminated, but it replaced some other variables
This function will add to solver2 clauses that represent the relationship of
the variables to their replaced cousins. Then, calling solver2.solve() should
take care of everything
*/
void VarReplacer::extendModelImpossible(Solver& solver2) const
{
#ifdef VERBOSE_DEBUG
std::cout << "extendModelImpossible() called" << std::endl;
#endif //VERBOSE_DEBUG
vec<Lit> tmpClause;
uint32_t i = 0;
for (vector<Lit>::const_iterator it = table.begin(); it != table.end(); it++, i++) {
if (it->var() == i) continue;
if (solver.assigns[it->var()] == l_Undef) {
assert(solver.assigns[it->var()] == l_Undef);
assert(solver.assigns[i] == l_Undef);
tmpClause.clear();
tmpClause.push(Lit(it->var(), true));
tmpClause.push(Lit(i, it->sign()));
solver2.addClause(tmpClause);
assert(solver2.ok);
tmpClause.clear();
tmpClause.push(Lit(it->var(), false));
tmpClause.push(Lit(i, it->sign()^true));
solver2.addClause(tmpClause);
assert(solver2.ok);
}
}
}
示例2: getPredecessors
int getPredecessors(Solver& S, int attr_number, int num_var, int curr_depth) {
vec<Lit> lits;
int nBasins = 0;
int i = num_var;
while (i--){
lits.push((global_var.Attractors[attr_number].states[0].c_str()[i]=='1')? Lit(i) : ~Lit(i));
S.addClause(lits);
lits.clear();
}
i = num_var;
while (i--){
lits.push((global_var.Attractors[attr_number].states[0].c_str()[i]=='1')? ~Lit(i+num_var) : Lit(i+num_var));
}
S.addClause(lits);
lits.clear();
while (1){
if (!S.solve()) break;
constrain_state(curr_depth-1, S.model, curr_depth-1, S, num_var);
nBasins++;
}
return nBasins;
}
示例3: constrain_state
/*Make state with index a from the stats_sequance not allowed in future solutions of S in position with index b */
void constrain_state(unsigned a, vec<lbool>& stats_sequance, unsigned b, Solver &S, unsigned number_var ){
vec<Lit> lits;
int a_tmp=a*number_var;
int b_tmp=b*number_var;
ASSERT( a_tmp+number_var <= stats_sequance.size() );
ASSERT( b_tmp+number_var <= S.nVars() );
while(number_var--){
if (stats_sequance[a_tmp] != l_Undef){
#ifdef PRINT_STATE
//printf( "%s", (stats_sequance[a_tmp]==l_True)?"1":"0");
#endif
lits.push((stats_sequance[a_tmp]==l_True)?~Lit( b_tmp ):Lit( b_tmp ));
}else{
//printf("-");
}
a_tmp++;
b_tmp++;
}
S.addClause(lits);
#ifdef PRINT_STATE
//puts(" ");
#endif
}
示例4: add_clauses_with_variable_shift
void add_clauses_with_variable_shift( std::vector< std::vector<Lit> > &clauses2add, Solver &S,int shift){
unsigned i;
vec<Lit> lits;
lits.clear();
i=clauses2add.size();
while(i--){//PRINT(i); /*ckhong: no change in orig_clauses*/
int j=clauses2add[i].size();//PRINT(j);
while(j--){//PRINT(toInt(clauses2add[i][j])); PRINT(toInt(clauses2add[i][j]) + (shift+shift));
lits.push(toLit( toInt(clauses2add[i][j]) + (shift+shift) ));
}
/*
j=lits.size();
while(j--){
PRINT(toInt(lits[j]));
}
*/
S.addClause(lits);
lits.clear();
}
}
示例5: main
int main(int argc, char *argv[])
{
Solver s = Solver();
char c;
std::string in;
int n_vars, n_clauses, var;
cin >> c;
while(c == 'c')
{
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
cin >> c;
}
if(c != 'p')
{
cerr << "Incorrect input format" << endl;
return 1;
}
cin >> in;
if (in != "cnf")
{
cerr << "Format not supported: " << in << endl;
return 1;
}
cin >> n_vars >> n_clauses;
for (int i = 0; i < n_vars; ++i)
{
s.newVar ();
}
lit_vec cl;
while(!cin.eof())
{
cin >> var;
if (var != 0)
{
p_lit l(new lit(var));
cl.push_back(l);
}
else
{
lit_vec add(cl);
s.addClause(add);
cl.clear();
}
}
if (s.solve()) {
cout << "Sat! " << endl
<< "solution : " << s.getModel() << endl;
} else {
cout << "Unsat!" << endl;
}
return 0;
}
示例6: parse_DIMACS_main
static void parse_DIMACS_main(B& in, Solver& S) {
vec<Lit> lits;
for (;;){
skipWhitespace(in);
if (*in == EOF)
break;
else if (*in == 'c' || *in == 'p')
skipLine(in);
else
readClause(in, S, lits),
S.addClause(lits);
}
}
示例7: Lit
/*
* Class: kodkod_engine_satlab_MiniSat
* Method: addClause
* Signature: (J[I)Z
*/
JNIEXPORT jboolean JNICALL Java_kodkod_engine_satlab_MiniSat_addClause
(JNIEnv * env, jobject, jlong solver, jintArray clause) {
jsize length = env->GetArrayLength(clause);
jint* buf = env->GetIntArrayElements(clause, JNI_FALSE);
Solver* solverPtr = ((Solver*)solver);
vec<Lit> lits;
for(int i = 0; i < length; ++i) {
int var = *(buf+i);
lits.push((var > 0) ? Lit(var-1) : ~Lit(-var-1));
}
solverPtr->addClause(lits);
env->ReleaseIntArrayElements(clause, buf, 0);
return solverPtr->okay();
}
示例8: parse_BCNF
static bool parse_BCNF(cchar* filename, Solver& S)
{
FILE* in = fopen(filename, "rb");
if (in == NULL) fprintf(stderr, "ERROR! Could not open file: %s\n", filename), exit(1);
char header[16];
fread(header, 1, 16, in);
if (strncmp(header, "BCNF", 4) != 0) fprintf(stderr, "ERROR! Not a BCNF file: %s\n", filename), exit(1);
if (*(int*)(header+4) != 0x01020304) fprintf(stderr, "ERROR! BCNF file in unsupported byte-order: %s\n", filename), exit(1);
int n_vars = *(int*)(header+ 8);
//int n_clauses = *(int*)(header+12);
int* buf = xmalloc<int>(CHUNK_LIMIT);
int buf_sz;
vec<Lit> c;
for (int i = 0; i < n_vars; i++) S.newVar();
for(;;){
int n = fread(&buf_sz, 4, 1, in);
if (n != 1) break;
assert(buf_sz <= CHUNK_LIMIT);
fread(buf, 4, buf_sz, in);
int* p = buf;
while (*p != -1){
int size = *p++;
c.clear();
c.growTo(size);
for (int i = 0; i < size; i++)
c[i] = toLit(p[i]);
p += size;
S.addClause(c); // Add clause.
if (!S.okay()){
xfree(buf); fclose(in);
return false; }
}
}
xfree(buf);
fclose(in);
S.simplifyDB();
return S.okay();
}
示例9: read_minterm_file_to_solver
void read_minterm_file_to_solver(char * fileName, Solver &S,int *input_array,int input_array_size , Lit output_lit){
char temp[MAX_LENGTH];
FILE *fp;
vec<Lit> lits;
int i;
//printf("%d ",toInt(output_lit));
//getchar();
lits.clear();
if ((fp = fopen(fileName, "r")) == NULL) {
fprintf(stderr, "Couldn't open file: %s\n", fileName);
exit (1);
}
while(!feof(fp)) {
fgets(temp,MAX_LENGTH, fp);
if( temp[0]=='0' || temp[0]=='1' || temp[0]=='-'){
break;
}
}
for(i=0;i<input_array_size;i++){
//PRINT(input_array[i]);
}
while(temp[0]!='.'){
//puts(temp);
for(i=0;i<input_array_size;i++){
if(temp[i]=='-')
continue;
lits.push( (temp[i]=='1') ? ~Lit( input_array[i] - INDEX_OF_FIRST_VARIABLE ) : Lit(input_array[i] - INDEX_OF_FIRST_VARIABLE ) );
//printf("%d:%d ",i,toInt(lits[lits.size()-1]));
}
//puts(" ");
lits.push( output_lit );
S.addClause(lits);
lits.clear();
fgets(temp,MAX_LENGTH, fp);
}
fclose(fp);
}
示例10: parse_DIMACS_main
static void parse_DIMACS_main(B& in, Solver& S) {
vec<Lit> lits;
for (;;){
skipWhitespace(in);
if (*in == EOF)
break;
else if (*in == 'p'){
if (match(in, "p cnf")){
int vars = parseInt(in);
int clauses = parseInt(in);
reportf("| Number of variables: %-12d |\n", vars);
reportf("| Number of clauses: %-12d |\n", clauses);
}else{
reportf("PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
}
} else if (*in == 'c' || *in == 'p')
skipLine(in);
else
readClause(in, S, lits),
S.addClause(lits);
}
}
示例11: moveClauses
void PartHandler::moveClauses(vec<Clause*>& cs, Solver& newSolver, const uint32_t part, PartFinder& partFinder)
{
Clause **i, **j, **end;
for (i = j = cs.getData(), j = i , end = i + cs.size(); i != end; i++) {
if ((**i).learnt() || partFinder.getVarPart((**i)[0].var()) != part) {
*j++ = *i;
continue;
}
solver.detachClause(**i);
#ifdef VERBOSE_DEBUG
std::cout << "clause in this part:"; (**i).plainPrint();
#endif
Clause& c = **i;
vec<Lit> tmp(c.size());
std::copy(c.getData(), c.getDataEnd(), tmp.getData());
newSolver.addClause(tmp, c.getGroup());
//NOTE: we need the CS because otherwise, the addClause could have changed **i, which we need to re-add later!
clausesRemoved.push(*i);
}
cs.shrink(i-j);
}
示例12: fgets
//.........这里部分代码省略.........
/* go to the end of sequense of spaces */
while(temp[i]==' '){
i++;
}
if(temp[i]=='\n'){
goto end_loops;
}
sscanf(&temp[i],"%d",&tmp_1);
if( tmp_1 < 0 || tmp_1 >redunt_number_of_var){
fprintf(stderr,"Wrong format of input file in line %d. The varible %d in string:\n %s exceeds number of declared variables.\n",line_count, tmp_1,temp);
exit (1);
}
if( redunt_var2var[tmp_1] == 0){
fprintf(stderr, "Wrong format of input file in line %d. One of the input was not declared in list of inputs %s\n",line_count, temp );
exit (1);
}
input_array[current_number_of_inputs++]= redunt_var2var[tmp_1];
}
end_loops:
if(current_number_of_inputs!=number_of_inputs){
fprintf(stderr, "Wrong format of input file in line %d. Declared number of inputs is not equal to actual number of input %s\n",line_count, temp );
exit (1);
}
if(number_of_inputs==0){
fgets(temp,MAX_LENGTH, fp);
if( temp[0]=='1'){
lits.push( Lit( current_var - INDEX_OF_FIRST_VARIABLE ) );
S.addClause(lits);
lits.clear();
}else{
if( temp[0]!='0'){
printf("Node %d assumed to be constant 0\n",current_var);
}
lits.push( ~Lit( current_var - INDEX_OF_FIRST_VARIABLE ) );
S.addClause(lits);
lits.clear();
}
goto next_vertex;
}
/*--------------- Reading in the function of the node ---------------*/
current_number_of_inputs=0;
while(!feof(fp)){
fgets(temp,MAX_LENGTH, fp);
if( temp[0]=='0' || temp[0]=='1' || temp[0]=='-'){
for(i=0;i<number_of_inputs;i++){
if(temp[i]=='-')
continue;
//PRINT(input_array[i]);
lits.push( (temp[i]=='1') ? ~Lit( number_of_var + input_array[i] - INDEX_OF_FIRST_VARIABLE) : Lit(number_of_var + input_array[i] - INDEX_OF_FIRST_VARIABLE) );
if(temp[i]!='1' && temp[i]!='0'){
fprintf(stderr, "Wrong format of input file in line %d. Wrong line:%s\n",line_count, temp );
exit (1);
}
}
示例13: main
int main(int argc, char** argv)
{
Solver S;
Solver T;
vec<Lit> lits;
vec<Lit> lits2;
vec<Lit> lits3;
vec<Lit> lits4;
vec<Lit> lits5;
vec<Lit> lits6;
vec<Lit> lits0;
lits.clear();
lits2.clear();
T.newVar();
T.newVar();
T.newVar();
T.newVar();
T.newVar();
T.newVar();
T.newVar();
T.newVar();
S.newVar();
S.newVar();
S.newVar();
S.newVar();
S.newVar();
S.newVar();
S.newVar();
S.newVar();
lits0.clear();
lits0.push(Lit(0));
unsigned int i;
int parsed_lit = -1;
lits.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = 1;
lits.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
S.addClause(lits);
T.addClause(lits);
parsed_lit = 2;
printf("lalla1\n");
lits3.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = 3;
lits3.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -4;
lits3.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -2;
lits4.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = 5;
lits4.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -6;
lits4.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = 3;
lits5.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -5;
lits5.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = 10;
lits5.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -3;
lits6.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -7;
lits6.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -10;
lits6.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
lits6.push( (parsed_lit > 0)
? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = -100;
lits2.push( (parsed_lit > 0) ? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
parsed_lit = 100;
lits2.push( (parsed_lit > 0) ? Lit(abs(parsed_lit)-1) : ~Lit(abs(parsed_lit)-1) );
S.addClause(lits2);
T.addClause(lits2);
printf("lalla2\n");
S.addClause(lits3);
T.addClause(lits3);
printf("lalla3\n");
printf("Snvas %i \n", S.nVars());
S.addClause(lits4);
T.addClause(lits4);
printf("lalla4\n");
S.addClause(lits5);
T.addClause(lits5);
printf("lalla5\n");
S.addClause(lits6);
T.addClause(lits6);
printf("lallala\n");
bool ret = S.solve();
//.........这里部分代码省略.........
示例14: getPredecessors
//int getPredecessors(Solver& S, const char *state, int num_var, int curr_depth) {
int getPredecessors( std::vector< std::vector<Lit> > &orig_clauses, const char *state, int num_var, int curr_depth) {
vec<Lit> lits;
//global_var.basins.push_back(state);
int number_of_predecessors = 1;
/*
if (curr_depth == 12){
//printf("\n");
return number_of_predecessors;
}*/
Solver S;
int i = num_var;
while (i--){
S.newVar(); S.newVar();
}
add_clauses_with_variable_shift(orig_clauses, S, 0);
//char state_[] = "000001000000";
i = num_var;
while (i--){
lits.push((state[i]=='1')? Lit(i) : ~Lit(i));
S.addClause(lits);
lits.clear();
}
i = num_var;
while (i--){
lits.push((state[i]=='1')? ~Lit(i+num_var) : Lit(i+num_var));
}
S.addClause(lits);
lits.clear();
int nDirectPredecessors = 0;
while (1){
if (!S.solve()) break;
int a_tmp=num_var;
int b_tmp=0;
int counter=num_var;
char tState[num_var];
while(counter--){
if(S.model[a_tmp] != l_Undef){
//printf("%s", (S.model[a_tmp]==l_True)? "1":"0");
sprintf(tState+b_tmp, "%s", (S.model[a_tmp]==l_True)? "1":"0");
} else{
//printf("-");
sprintf(tState+b_tmp, "-");
}
a_tmp++;
b_tmp++;
}
//printf("%s <- %s, ", state, tState);
//PRINT(curr_depth);
number_of_predecessors = number_of_predecessors + getPredecessors(orig_clauses, tState, num_var, curr_depth + 1);
//constrain_state(curr_depth, S.model, curr_depth, S, num_var);
i = num_var;
while (i--){
if (strcmp(tState, "-")) {
lits.push((tState[i]=='1')? ~Lit(i+num_var) : Lit(i+num_var));
}
else {
}
}
S.addClause(lits);
lits.clear();
nDirectPredecessors++;
}
/*
if (nDirectPredecessors > MAX_NUMBER_OF_HUBS) {
global_var.hubs.push_back(state);
}*/
return number_of_predecessors;
}
示例15: formulate
void formulate(int p, int n, Solver& s) {
_p = p; _n = n;
for (int i = 0; i < N*(p+n); ++i)
s.newVar();
for (int i = 0; i < N; ++i)
for (int j = 0; j < i; ++j) {
if (intersect(i, j)) {
for (int r = 1; r <= p; ++r) {
Lit a = vn(i, r);
Lit b = vn(j, r);
s.addBinary(~a, ~b);
}
for (int r = -1; r >= -n; --r) {
Lit a = vn(i, r);
Lit b = vn(j, r);
s.addBinary(~a, ~b);
}
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i == j) continue;
if (!contain(i, j)) continue;
for (int pj = 1; pj <= p-1; ++pj) {
for (int pi = pj+1; pi <= p; ++pi) {
Lit a = vn(i, pi);
Lit b = vn(j, pj);
s.addBinary(~a, ~b);
}
}
for (int pj = -1; pj >= -n+1; --pj) {
for (int pi = pj-1; pi >= -n; --pi) {
Lit a = vn(i, pi);
Lit b = vn(j, pj);
s.addBinary(~a, ~b);
}
}
}
}
// Exact one for each color
/*vec<Lit>* c;
for (int i = 0; i < N; ++i) {
c = new vec<Lit>;
c->growTo(n+p);
for (int pi = 1; pi <= p; ++pi)
(*c)[i-1] = vn(i, pi);
for (int pi = -1; pi >= -n; --pi)
(*c)[p-pi-1] = vn(i, pi);
s.addClause(*c);
for (int j = 0; j < n+p; ++j)
for (int k = 0; k < j; ++k) {
s.addBinary(~vnalt(i, j), ~vnalt(i, k));
}
}*/
for (int i = 0; i < N; ++i) {
vec<Lit> c;
c.growTo(n+p);
for (int j = 0; j < n+p; ++j)
c[j] = vnalt(i, j);
s.addClause(c);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < n+p; ++j)
for (int k = 0; k < j; ++k) {
s.addBinary(~vnalt(i, j), ~vnalt(i, k));
}
}
}