SlideShare a Scribd company logo
1 of 21
Download to read offline
First C++ code written... I think it's quite efficient with all the test set included in path data.
The simplest data set looks like:
-1 -3 0 -1 -5 0 -3 -5 0 -2 -4 0 -2 -6 0 -4 -6 0 1 2 0 3 4 0 5 6 0
used vector saving clauses and map saving assignments.
the main recursive function looks like following:
/*
1. If the set of clauses is empty, it returns true
2. Otherwise, if the set of clauses contains an empty clause, it returns false
3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively
4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself
recursively
5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively
*/
Find the attached zip folder. I have referred this code example in my project. It will solve your
problem.
Implement following code :
clause.cpp
#include
#include "clause.h"
using namespace std;
using namespace Sat;
void Clause::readClause(istream& is)
{
static int lit = 0;
if (lit != 0) addLiteral(lit);
while (!is.eof()) {
is >> lit;
if (lit == 0) break;
addLiteral(lit);
}
is >> lit;
}
void Clause::dumpClause() const
{
int i;
cout << "(";
for (i = 0; i < numLits() - 1; ++i) {
cout << getLit(i) << ", ";
}
if (numLits() != 0) {
cout << getLit(i);
}
cout << ")" << endl;
}
Clause_impl.cpp
#include
#include
#include "clause.h"
#include "clause_impl.h"
using namespace std;
using namespace Sat;
//size_t ClauseImpl::clause_num = 0;
// Add a literal to the clause
void ClauseImpl::addLiteral(int lit){
add_lits(lit);
}
// Get the largest variable in the clause
int ClauseImpl::maxVar() const{
int max = -1;
for (int i = 0; i < lits.size(); ++ i) {
if(max < abs(lits[i]))
max = abs(lits[i]);
}
return max;
}
// Get the number of variables in the clause
int ClauseImpl::numLits() const{
return lits.size();
}
// Get the i^th literal in the clause
int ClauseImpl::getLit(int i) const{
return lits.at(i) ;
}
vector ClauseImpl::get_lits() const {return lits;}
void ClauseImpl::add_lits(int lit) {
lits.push_back(lit);
}
bool ClauseImpl::hasLit(int a) const {
for (int i = 0; i < lits.size(); ++ i) {
if (lits[i] == a) {
return true;
}
}
return false;
}
debug.cpp
#include
#include "debug.h"
using namespace std;
namespace DebugUtil {
ostream& operator<<(ostream& os, const Exception& e)
{
return os << e.toString();
}
void assertError(const string& file, int line,
const string& cond)
{
ostringstream ss;
ss << "in " << file << ":" << line << " (" << cond << ") ";
throw Exception(ss.str());
}
} // end of namespace DebugUtil
formula.cpp
#include "debug.h"
#include "clause.h"
#include "formula.h"
using namespace std;
using namespace Sat;
void Formula::readClauses(istream& is)
{
while (!is.eof()) {
Clause& c = addClause();
c.readClause(is);
int tmp = c.maxVar();
if (tmp > maxVar) maxVar = tmp;
}
}
void Formula::dumpClauses()
{
for (int i = 0; i < numClauses(); ++i) {
getClause(i).dumpClause();
}
}
solver.cpp
#include "solver.h"
using namespace std;
using namespace Sat;
bool Solver::solve(FormulaImpl &f) {
//vector p = findPure(f);
//pureLit(f,p);
//int uni = getUnitProp(f.getClauses());
//unitProp(f, uni);
//return true;
return splitting (f);
}
/*
1. If the set of clauses is empty, it returns true
2. Otherwise, if the set of clauses contains an empty clause, it returns false
3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively
4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself
recursively
5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively
*/
bool Solver::splitting (FormulaImpl &f) {
FormulaImpl c (f);
pair temp;
// condition 1
if (c.getClauses().size() == 0)
return true;
// condition 2
for (int i = 0; i < c.getClauses().size(); ++ i) {
if (c.getClause(i).numLits() == 0)
return false;
}
int uni = getUnitProp (c.getClauses());
// condition 3
if (uni != 0) {
temp = make_pair(uni, true);
assgn.insert(temp);
return splitting (unitProp(c, uni));
}
// condition 4
else {
vector p = getPure(c);
if (p.size() != 0) {
for(int i = 0; i < p.size(); ++ i) {
temp = make_pair(p[i], true);
assgn.insert(temp);
}
return splitting (pureLit(c, p));
}
// condition 5
else {
int firstLit = c.getClause(0).getLit(0);
temp = make_pair(firstLit, true);
assgn.insert (temp);
if (splitting (unitProp(c, firstLit)) == true)
return true;
else {
assgn.erase(firstLit);
temp = make_pair(firstLit, false);
assgn.insert (temp);
return splitting (unitProp(f, -firstLit));
}
}
}
}
/*
a literal and a list of clauses;
returns a new list of clauses resulting from applying the pure literal rule with the given literal.
*/
FormulaImpl& Solver::unitProp(FormulaImpl &f, int uni) {
vector lits;
while (uni != 0) {
pair temp;
temp = make_pair(uni, true);
assgn.insert(temp);
vector::iterator it;
for (it = f.clauses.begin(); it != f.clauses.end();) {
bool flag = false;
vector::iterator jt;
for (jt = it->lits.begin(); jt != it->lits.end(); ) {
if ((*jt) == -uni) {
jt = it->lits.erase(jt);
break;
}
else if ((*jt) == uni){
flag = true;
break;
} else {
++ jt;
}
}
if(flag == false)
++ it;
else {
//it->clearLits();
it = f.clauses.erase(it);
}
}
uni = getUnitProp (f.getClauses());
}
//f.setClauses (f.clauses);
return f;
}
int Solver::getUnitProp (vector& c) const {
for (int i = 0; i < c.size(); ++i) {
if(c[i].get_lits().size() == 1) {
return c[i].getLit(0);
}
}
return 0;
}
/*
if it creates a clause that contains both a variable and its negation, filter the clause out
*/
FormulaImpl& Solver::pureLit(FormulaImpl &f, vector p) {
vector::iterator it;
for (int i = 0; i < p.size(); ++ i) {
for (it = f.clauses.begin(); it != f.clauses.end();) {
if (it->hasLit(p[i])) {
pair temp;
temp = make_pair(p[i], true);
assgn.insert(temp);
it = f.clauses.erase(it);
} else {
++ it;
}
}
}
return f;
}
vector Solver::getPure (FormulaImpl &f) {
map litTable;
vector p;
vector::iterator it;
for (it = f.clauses.begin(); it != f.clauses.end(); ++it) {
vector::iterator jt;
for (jt = it->lits.begin(); jt != it->lits.end(); ++jt) {
if (litTable.find(abs(*jt))==litTable.end()) {
if (*jt < 0)
litTable.insert(pair(abs(*jt), -1));
else
litTable.insert(pair(abs(*jt), 1));
}
else {
if (litTable.find(abs(*jt))->second == -1 && *jt > 0) {
litTable.erase(*jt);
litTable.insert(pair(abs(*jt), 0));
} else if (litTable.find(abs(*jt))->second == 1 && *jt < 0) {
litTable.erase(abs(*jt));
litTable.insert(pair(abs(*jt),0));
}
}
}
}
map::iterator kt;
int lit;
for(kt = litTable.begin(); kt != litTable.end(); ++kt) {
if (kt->second != 0) {
lit = (kt->first) * (kt->second);
p.push_back(lit);
}
}
return p;
}
formula_imp.cpp
#include "debug.h"
#include "clause.h"
#include "clause_impl.h"
#include "formula.h"
#include "formula_impl.h"
using namespace std;
using namespace Sat;
// Add a new Clause to the Formula
// Return a reference to the new clause
Clause& FormulaImpl::addClause() {
ClauseImpl c;
clauses.push_back(c);
return clauses.back();
}
// Get the number of clauses
int FormulaImpl::numClauses() const {
return clauses.size();
}
// Get the i^th clause in the formula
Clause& FormulaImpl::getClause(int i) {
return clauses.at(i);
}
sat.cpp
#include
#include "debug.h"
#include "formula_impl.h"
#include "solver.h"
using namespace std;
using namespace Sat;
int main(int argc, char **argv)
{
if (argc != 2) {
cout << "Expected filename" << endl;
return 0;
}
istream* is = new ifstream(*(argv+1));
if (!(*is)) {
cout << "File not found" << endl;
return 0;
}
FormulaImpl f;
f.readClauses(*is);
Assert(f.numClauses() > 0);
f.dumpClauses();
Solver sat;
if (sat.solve(f)) {
cout << "Satisfiable" << endl;
}
else {
cout << "Unsatisfiable" << endl;
}
return 0;
}
This will help. Please import necessary files. I am not able to upload zip files.
Try this sample. If you find any difficulty please let me know.
Solution
First C++ code written... I think it's quite efficient with all the test set included in path data.
The simplest data set looks like:
-1 -3 0 -1 -5 0 -3 -5 0 -2 -4 0 -2 -6 0 -4 -6 0 1 2 0 3 4 0 5 6 0
used vector saving clauses and map saving assignments.
the main recursive function looks like following:
/*
1. If the set of clauses is empty, it returns true
2. Otherwise, if the set of clauses contains an empty clause, it returns false
3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively
4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself
recursively
5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively
*/
Find the attached zip folder. I have referred this code example in my project. It will solve your
problem.
Implement following code :
clause.cpp
#include
#include "clause.h"
using namespace std;
using namespace Sat;
void Clause::readClause(istream& is)
{
static int lit = 0;
if (lit != 0) addLiteral(lit);
while (!is.eof()) {
is >> lit;
if (lit == 0) break;
addLiteral(lit);
}
is >> lit;
}
void Clause::dumpClause() const
{
int i;
cout << "(";
for (i = 0; i < numLits() - 1; ++i) {
cout << getLit(i) << ", ";
}
if (numLits() != 0) {
cout << getLit(i);
}
cout << ")" << endl;
}
Clause_impl.cpp
#include
#include
#include "clause.h"
#include "clause_impl.h"
using namespace std;
using namespace Sat;
//size_t ClauseImpl::clause_num = 0;
// Add a literal to the clause
void ClauseImpl::addLiteral(int lit){
add_lits(lit);
}
// Get the largest variable in the clause
int ClauseImpl::maxVar() const{
int max = -1;
for (int i = 0; i < lits.size(); ++ i) {
if(max < abs(lits[i]))
max = abs(lits[i]);
}
return max;
}
// Get the number of variables in the clause
int ClauseImpl::numLits() const{
return lits.size();
}
// Get the i^th literal in the clause
int ClauseImpl::getLit(int i) const{
return lits.at(i) ;
}
vector ClauseImpl::get_lits() const {return lits;}
void ClauseImpl::add_lits(int lit) {
lits.push_back(lit);
}
bool ClauseImpl::hasLit(int a) const {
for (int i = 0; i < lits.size(); ++ i) {
if (lits[i] == a) {
return true;
}
}
return false;
}
debug.cpp
#include
#include "debug.h"
using namespace std;
namespace DebugUtil {
ostream& operator<<(ostream& os, const Exception& e)
{
return os << e.toString();
}
void assertError(const string& file, int line,
const string& cond)
{
ostringstream ss;
ss << "in " << file << ":" << line << " (" << cond << ") ";
throw Exception(ss.str());
}
} // end of namespace DebugUtil
formula.cpp
#include "debug.h"
#include "clause.h"
#include "formula.h"
using namespace std;
using namespace Sat;
void Formula::readClauses(istream& is)
{
while (!is.eof()) {
Clause& c = addClause();
c.readClause(is);
int tmp = c.maxVar();
if (tmp > maxVar) maxVar = tmp;
}
}
void Formula::dumpClauses()
{
for (int i = 0; i < numClauses(); ++i) {
getClause(i).dumpClause();
}
}
solver.cpp
#include "solver.h"
using namespace std;
using namespace Sat;
bool Solver::solve(FormulaImpl &f) {
//vector p = findPure(f);
//pureLit(f,p);
//int uni = getUnitProp(f.getClauses());
//unitProp(f, uni);
//return true;
return splitting (f);
}
/*
1. If the set of clauses is empty, it returns true
2. Otherwise, if the set of clauses contains an empty clause, it returns false
3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively
4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself
recursively
5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively
*/
bool Solver::splitting (FormulaImpl &f) {
FormulaImpl c (f);
pair temp;
// condition 1
if (c.getClauses().size() == 0)
return true;
// condition 2
for (int i = 0; i < c.getClauses().size(); ++ i) {
if (c.getClause(i).numLits() == 0)
return false;
}
int uni = getUnitProp (c.getClauses());
// condition 3
if (uni != 0) {
temp = make_pair(uni, true);
assgn.insert(temp);
return splitting (unitProp(c, uni));
}
// condition 4
else {
vector p = getPure(c);
if (p.size() != 0) {
for(int i = 0; i < p.size(); ++ i) {
temp = make_pair(p[i], true);
assgn.insert(temp);
}
return splitting (pureLit(c, p));
}
// condition 5
else {
int firstLit = c.getClause(0).getLit(0);
temp = make_pair(firstLit, true);
assgn.insert (temp);
if (splitting (unitProp(c, firstLit)) == true)
return true;
else {
assgn.erase(firstLit);
temp = make_pair(firstLit, false);
assgn.insert (temp);
return splitting (unitProp(f, -firstLit));
}
}
}
}
/*
a literal and a list of clauses;
returns a new list of clauses resulting from applying the pure literal rule with the given literal.
*/
FormulaImpl& Solver::unitProp(FormulaImpl &f, int uni) {
vector lits;
while (uni != 0) {
pair temp;
temp = make_pair(uni, true);
assgn.insert(temp);
vector::iterator it;
for (it = f.clauses.begin(); it != f.clauses.end();) {
bool flag = false;
vector::iterator jt;
for (jt = it->lits.begin(); jt != it->lits.end(); ) {
if ((*jt) == -uni) {
jt = it->lits.erase(jt);
break;
}
else if ((*jt) == uni){
flag = true;
break;
} else {
++ jt;
}
}
if(flag == false)
++ it;
else {
//it->clearLits();
it = f.clauses.erase(it);
}
}
uni = getUnitProp (f.getClauses());
}
//f.setClauses (f.clauses);
return f;
}
int Solver::getUnitProp (vector& c) const {
for (int i = 0; i < c.size(); ++i) {
if(c[i].get_lits().size() == 1) {
return c[i].getLit(0);
}
}
return 0;
}
/*
if it creates a clause that contains both a variable and its negation, filter the clause out
*/
FormulaImpl& Solver::pureLit(FormulaImpl &f, vector p) {
vector::iterator it;
for (int i = 0; i < p.size(); ++ i) {
for (it = f.clauses.begin(); it != f.clauses.end();) {
if (it->hasLit(p[i])) {
pair temp;
temp = make_pair(p[i], true);
assgn.insert(temp);
it = f.clauses.erase(it);
} else {
++ it;
}
}
}
return f;
}
vector Solver::getPure (FormulaImpl &f) {
map litTable;
vector p;
vector::iterator it;
for (it = f.clauses.begin(); it != f.clauses.end(); ++it) {
vector::iterator jt;
for (jt = it->lits.begin(); jt != it->lits.end(); ++jt) {
if (litTable.find(abs(*jt))==litTable.end()) {
if (*jt < 0)
litTable.insert(pair(abs(*jt), -1));
else
litTable.insert(pair(abs(*jt), 1));
}
else {
if (litTable.find(abs(*jt))->second == -1 && *jt > 0) {
litTable.erase(*jt);
litTable.insert(pair(abs(*jt), 0));
} else if (litTable.find(abs(*jt))->second == 1 && *jt < 0) {
litTable.erase(abs(*jt));
litTable.insert(pair(abs(*jt),0));
}
}
}
}
map::iterator kt;
int lit;
for(kt = litTable.begin(); kt != litTable.end(); ++kt) {
if (kt->second != 0) {
lit = (kt->first) * (kt->second);
p.push_back(lit);
}
}
return p;
}
formula_imp.cpp
#include "debug.h"
#include "clause.h"
#include "clause_impl.h"
#include "formula.h"
#include "formula_impl.h"
using namespace std;
using namespace Sat;
// Add a new Clause to the Formula
// Return a reference to the new clause
Clause& FormulaImpl::addClause() {
ClauseImpl c;
clauses.push_back(c);
return clauses.back();
}
// Get the number of clauses
int FormulaImpl::numClauses() const {
return clauses.size();
}
// Get the i^th clause in the formula
Clause& FormulaImpl::getClause(int i) {
return clauses.at(i);
}
sat.cpp
#include
#include "debug.h"
#include "formula_impl.h"
#include "solver.h"
using namespace std;
using namespace Sat;
int main(int argc, char **argv)
{
if (argc != 2) {
cout << "Expected filename" << endl;
return 0;
}
istream* is = new ifstream(*(argv+1));
if (!(*is)) {
cout << "File not found" << endl;
return 0;
}
FormulaImpl f;
f.readClauses(*is);
Assert(f.numClauses() > 0);
f.dumpClauses();
Solver sat;
if (sat.solve(f)) {
cout << "Satisfiable" << endl;
}
else {
cout << "Unsatisfiable" << endl;
}
return 0;
}
This will help. Please import necessary files. I am not able to upload zip files.
Try this sample. If you find any difficulty please let me know.

More Related Content

Similar to First C++ code written... I think its quite efficient with all the.pdf

C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfandreaplotner1
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Bdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infiniteBdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infiniteGiordano Scalzo
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3Dillon Lee
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2docSrikanth
 
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdfListings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdfRAJATCHUGH12
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorNeeraj Kaushik
 
Toonz code leaves much to be desired
Toonz code leaves much to be desiredToonz code leaves much to be desired
Toonz code leaves much to be desiredPVS-Studio
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfcharanjit1717
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITEgor Bogatov
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 

Similar to First C++ code written... I think its quite efficient with all the.pdf (20)

C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Bdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infiniteBdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infinite
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdfListings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression Calculator
 
Toonz code leaves much to be desired
Toonz code leaves much to be desiredToonz code leaves much to be desired
Toonz code leaves much to be desired
 
Thread
ThreadThread
Thread
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
03. Week 03.pptx
03. Week 03.pptx03. Week 03.pptx
03. Week 03.pptx
 
Namespaces
NamespacesNamespaces
Namespaces
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJIT
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 

More from ankit482504

57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf
57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf
57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdfankit482504
 
9The von Neumann equation for time evolution10Solution9T.pdf
9The von Neumann equation for time evolution10Solution9T.pdf9The von Neumann equation for time evolution10Solution9T.pdf
9The von Neumann equation for time evolution10Solution9T.pdfankit482504
 
27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf
27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf
27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdfankit482504
 
Nature of the reports produced. Financial accounting reports tend to.pdf
  Nature of the reports produced. Financial accounting reports tend to.pdf  Nature of the reports produced. Financial accounting reports tend to.pdf
Nature of the reports produced. Financial accounting reports tend to.pdfankit482504
 
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdfankit482504
 
Step1 Fe is oxidised ; its oxidation state change.pdf
                     Step1 Fe is oxidised ; its oxidation state change.pdf                     Step1 Fe is oxidised ; its oxidation state change.pdf
Step1 Fe is oxidised ; its oxidation state change.pdfankit482504
 
The process of performing the procedure known as a Tracheostomy is c.pdf
The process of performing the procedure known as a Tracheostomy is c.pdfThe process of performing the procedure known as a Tracheostomy is c.pdf
The process of performing the procedure known as a Tracheostomy is c.pdfankit482504
 
The Nixon shockwas a series of economic measures undertaken by Unite.pdf
The Nixon shockwas a series of economic measures undertaken by Unite.pdfThe Nixon shockwas a series of economic measures undertaken by Unite.pdf
The Nixon shockwas a series of economic measures undertaken by Unite.pdfankit482504
 
The IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdf
The IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdfThe IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdf
The IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdfankit482504
 
sumOfSquaresimport java.util.;public class Arraylistoperation.pdf
sumOfSquaresimport java.util.;public class Arraylistoperation.pdfsumOfSquaresimport java.util.;public class Arraylistoperation.pdf
sumOfSquaresimport java.util.;public class Arraylistoperation.pdfankit482504
 
Secant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdf
Secant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdfSecant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdf
Secant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdfankit482504
 
Ro is the reproduction number. It gives the average number of people.pdf
Ro is the reproduction number. It gives the average number of people.pdfRo is the reproduction number. It gives the average number of people.pdf
Ro is the reproduction number. It gives the average number of people.pdfankit482504
 
the salt bridge acts as a porous wall between the.pdf
                     the salt bridge acts as a porous wall between the.pdf                     the salt bridge acts as a porous wall between the.pdf
the salt bridge acts as a porous wall between the.pdfankit482504
 
metl ion can be Cobalt .pdf
                     metl ion can be Cobalt                           .pdf                     metl ion can be Cobalt                           .pdf
metl ion can be Cobalt .pdfankit482504
 
solution or homogeneous mixture Solution .pdf
                     solution or homogeneous mixture Solution     .pdf                     solution or homogeneous mixture Solution     .pdf
solution or homogeneous mixture Solution .pdfankit482504
 
Look at the Periodic Table. Elements are more me.pdf
                     Look at the Periodic Table.  Elements are more me.pdf                     Look at the Periodic Table.  Elements are more me.pdf
Look at the Periodic Table. Elements are more me.pdfankit482504
 
Kcl RbNO3 NaCl .pdf
                     Kcl RbNO3 NaCl                                   .pdf                     Kcl RbNO3 NaCl                                   .pdf
Kcl RbNO3 NaCl .pdfankit482504
 
HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf
                     HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf                     HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf
HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdfankit482504
 
CCl4 because it has for bond pairs and 0 lone pai.pdf
                     CCl4 because it has for bond pairs and 0 lone pai.pdf                     CCl4 because it has for bond pairs and 0 lone pai.pdf
CCl4 because it has for bond pairs and 0 lone pai.pdfankit482504
 
C) each nitrogen has one nonbinding electron pair.pdf
                     C) each nitrogen has one nonbinding electron pair.pdf                     C) each nitrogen has one nonbinding electron pair.pdf
C) each nitrogen has one nonbinding electron pair.pdfankit482504
 

More from ankit482504 (20)

57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf
57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf
57. Howard Florey 58. Albert Schatz 59. Ernst Boris Chain - .pdf
 
9The von Neumann equation for time evolution10Solution9T.pdf
9The von Neumann equation for time evolution10Solution9T.pdf9The von Neumann equation for time evolution10Solution9T.pdf
9The von Neumann equation for time evolution10Solution9T.pdf
 
27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf
27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf
27. Microtubules- made of Tubulidentata and present in cytoplasm28.pdf
 
Nature of the reports produced. Financial accounting reports tend to.pdf
  Nature of the reports produced. Financial accounting reports tend to.pdf  Nature of the reports produced. Financial accounting reports tend to.pdf
Nature of the reports produced. Financial accounting reports tend to.pdf
 
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
 
Step1 Fe is oxidised ; its oxidation state change.pdf
                     Step1 Fe is oxidised ; its oxidation state change.pdf                     Step1 Fe is oxidised ; its oxidation state change.pdf
Step1 Fe is oxidised ; its oxidation state change.pdf
 
The process of performing the procedure known as a Tracheostomy is c.pdf
The process of performing the procedure known as a Tracheostomy is c.pdfThe process of performing the procedure known as a Tracheostomy is c.pdf
The process of performing the procedure known as a Tracheostomy is c.pdf
 
The Nixon shockwas a series of economic measures undertaken by Unite.pdf
The Nixon shockwas a series of economic measures undertaken by Unite.pdfThe Nixon shockwas a series of economic measures undertaken by Unite.pdf
The Nixon shockwas a series of economic measures undertaken by Unite.pdf
 
The IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdf
The IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdfThe IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdf
The IUPAC names of the above compounds areZ-4,5-dichloro pent-4-e.pdf
 
sumOfSquaresimport java.util.;public class Arraylistoperation.pdf
sumOfSquaresimport java.util.;public class Arraylistoperation.pdfsumOfSquaresimport java.util.;public class Arraylistoperation.pdf
sumOfSquaresimport java.util.;public class Arraylistoperation.pdf
 
Secant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdf
Secant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdfSecant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdf
Secant is 1cosineRangeof cosine is(-1,1)Secant function cannot .pdf
 
Ro is the reproduction number. It gives the average number of people.pdf
Ro is the reproduction number. It gives the average number of people.pdfRo is the reproduction number. It gives the average number of people.pdf
Ro is the reproduction number. It gives the average number of people.pdf
 
the salt bridge acts as a porous wall between the.pdf
                     the salt bridge acts as a porous wall between the.pdf                     the salt bridge acts as a porous wall between the.pdf
the salt bridge acts as a porous wall between the.pdf
 
metl ion can be Cobalt .pdf
                     metl ion can be Cobalt                           .pdf                     metl ion can be Cobalt                           .pdf
metl ion can be Cobalt .pdf
 
solution or homogeneous mixture Solution .pdf
                     solution or homogeneous mixture Solution     .pdf                     solution or homogeneous mixture Solution     .pdf
solution or homogeneous mixture Solution .pdf
 
Look at the Periodic Table. Elements are more me.pdf
                     Look at the Periodic Table.  Elements are more me.pdf                     Look at the Periodic Table.  Elements are more me.pdf
Look at the Periodic Table. Elements are more me.pdf
 
Kcl RbNO3 NaCl .pdf
                     Kcl RbNO3 NaCl                                   .pdf                     Kcl RbNO3 NaCl                                   .pdf
Kcl RbNO3 NaCl .pdf
 
HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf
                     HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf                     HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf
HOAc + NAOH = OAc- + H2O 0.1 0.03 0.07 0 0.03 pH .pdf
 
CCl4 because it has for bond pairs and 0 lone pai.pdf
                     CCl4 because it has for bond pairs and 0 lone pai.pdf                     CCl4 because it has for bond pairs and 0 lone pai.pdf
CCl4 because it has for bond pairs and 0 lone pai.pdf
 
C) each nitrogen has one nonbinding electron pair.pdf
                     C) each nitrogen has one nonbinding electron pair.pdf                     C) each nitrogen has one nonbinding electron pair.pdf
C) each nitrogen has one nonbinding electron pair.pdf
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

First C++ code written... I think its quite efficient with all the.pdf

  • 1. First C++ code written... I think it's quite efficient with all the test set included in path data. The simplest data set looks like: -1 -3 0 -1 -5 0 -3 -5 0 -2 -4 0 -2 -6 0 -4 -6 0 1 2 0 3 4 0 5 6 0 used vector saving clauses and map saving assignments. the main recursive function looks like following: /* 1. If the set of clauses is empty, it returns true 2. Otherwise, if the set of clauses contains an empty clause, it returns false 3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively 4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself recursively 5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively */ Find the attached zip folder. I have referred this code example in my project. It will solve your problem. Implement following code : clause.cpp #include #include "clause.h" using namespace std; using namespace Sat; void Clause::readClause(istream& is) { static int lit = 0; if (lit != 0) addLiteral(lit); while (!is.eof()) { is >> lit; if (lit == 0) break; addLiteral(lit); } is >> lit;
  • 2. } void Clause::dumpClause() const { int i; cout << "("; for (i = 0; i < numLits() - 1; ++i) { cout << getLit(i) << ", "; } if (numLits() != 0) { cout << getLit(i); } cout << ")" << endl; } Clause_impl.cpp #include #include #include "clause.h" #include "clause_impl.h" using namespace std; using namespace Sat; //size_t ClauseImpl::clause_num = 0; // Add a literal to the clause void ClauseImpl::addLiteral(int lit){ add_lits(lit); } // Get the largest variable in the clause int ClauseImpl::maxVar() const{ int max = -1; for (int i = 0; i < lits.size(); ++ i) { if(max < abs(lits[i])) max = abs(lits[i]); } return max; }
  • 3. // Get the number of variables in the clause int ClauseImpl::numLits() const{ return lits.size(); } // Get the i^th literal in the clause int ClauseImpl::getLit(int i) const{ return lits.at(i) ; } vector ClauseImpl::get_lits() const {return lits;} void ClauseImpl::add_lits(int lit) { lits.push_back(lit); } bool ClauseImpl::hasLit(int a) const { for (int i = 0; i < lits.size(); ++ i) { if (lits[i] == a) { return true; } } return false; } debug.cpp #include #include "debug.h" using namespace std; namespace DebugUtil { ostream& operator<<(ostream& os, const Exception& e) { return os << e.toString(); } void assertError(const string& file, int line, const string& cond) { ostringstream ss;
  • 4. ss << "in " << file << ":" << line << " (" << cond << ") "; throw Exception(ss.str()); } } // end of namespace DebugUtil formula.cpp #include "debug.h" #include "clause.h" #include "formula.h" using namespace std; using namespace Sat; void Formula::readClauses(istream& is) { while (!is.eof()) { Clause& c = addClause(); c.readClause(is); int tmp = c.maxVar(); if (tmp > maxVar) maxVar = tmp; } } void Formula::dumpClauses() { for (int i = 0; i < numClauses(); ++i) { getClause(i).dumpClause(); } } solver.cpp #include "solver.h" using namespace std; using namespace Sat; bool Solver::solve(FormulaImpl &f) { //vector p = findPure(f); //pureLit(f,p);
  • 5. //int uni = getUnitProp(f.getClauses()); //unitProp(f, uni); //return true; return splitting (f); } /* 1. If the set of clauses is empty, it returns true 2. Otherwise, if the set of clauses contains an empty clause, it returns false 3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively 4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself recursively 5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively */ bool Solver::splitting (FormulaImpl &f) { FormulaImpl c (f); pair temp; // condition 1 if (c.getClauses().size() == 0) return true; // condition 2 for (int i = 0; i < c.getClauses().size(); ++ i) { if (c.getClause(i).numLits() == 0) return false; } int uni = getUnitProp (c.getClauses()); // condition 3 if (uni != 0) { temp = make_pair(uni, true); assgn.insert(temp); return splitting (unitProp(c, uni)); } // condition 4 else { vector p = getPure(c); if (p.size() != 0) { for(int i = 0; i < p.size(); ++ i) {
  • 6. temp = make_pair(p[i], true); assgn.insert(temp); } return splitting (pureLit(c, p)); } // condition 5 else { int firstLit = c.getClause(0).getLit(0); temp = make_pair(firstLit, true); assgn.insert (temp); if (splitting (unitProp(c, firstLit)) == true) return true; else { assgn.erase(firstLit); temp = make_pair(firstLit, false); assgn.insert (temp); return splitting (unitProp(f, -firstLit)); } } } } /* a literal and a list of clauses; returns a new list of clauses resulting from applying the pure literal rule with the given literal. */ FormulaImpl& Solver::unitProp(FormulaImpl &f, int uni) { vector lits; while (uni != 0) { pair temp; temp = make_pair(uni, true); assgn.insert(temp); vector::iterator it; for (it = f.clauses.begin(); it != f.clauses.end();) { bool flag = false;
  • 7. vector::iterator jt; for (jt = it->lits.begin(); jt != it->lits.end(); ) { if ((*jt) == -uni) { jt = it->lits.erase(jt); break; } else if ((*jt) == uni){ flag = true; break; } else { ++ jt; } } if(flag == false) ++ it; else { //it->clearLits(); it = f.clauses.erase(it); } } uni = getUnitProp (f.getClauses()); } //f.setClauses (f.clauses); return f; } int Solver::getUnitProp (vector& c) const { for (int i = 0; i < c.size(); ++i) { if(c[i].get_lits().size() == 1) { return c[i].getLit(0); } } return 0; } /* if it creates a clause that contains both a variable and its negation, filter the clause out */
  • 8. FormulaImpl& Solver::pureLit(FormulaImpl &f, vector p) { vector::iterator it; for (int i = 0; i < p.size(); ++ i) { for (it = f.clauses.begin(); it != f.clauses.end();) { if (it->hasLit(p[i])) { pair temp; temp = make_pair(p[i], true); assgn.insert(temp); it = f.clauses.erase(it); } else { ++ it; } } } return f; } vector Solver::getPure (FormulaImpl &f) { map litTable; vector p; vector::iterator it; for (it = f.clauses.begin(); it != f.clauses.end(); ++it) { vector::iterator jt; for (jt = it->lits.begin(); jt != it->lits.end(); ++jt) { if (litTable.find(abs(*jt))==litTable.end()) { if (*jt < 0) litTable.insert(pair(abs(*jt), -1)); else litTable.insert(pair(abs(*jt), 1)); } else { if (litTable.find(abs(*jt))->second == -1 && *jt > 0) { litTable.erase(*jt); litTable.insert(pair(abs(*jt), 0)); } else if (litTable.find(abs(*jt))->second == 1 && *jt < 0) { litTable.erase(abs(*jt)); litTable.insert(pair(abs(*jt),0));
  • 9. } } } } map::iterator kt; int lit; for(kt = litTable.begin(); kt != litTable.end(); ++kt) { if (kt->second != 0) { lit = (kt->first) * (kt->second); p.push_back(lit); } } return p; } formula_imp.cpp #include "debug.h" #include "clause.h" #include "clause_impl.h" #include "formula.h" #include "formula_impl.h" using namespace std; using namespace Sat; // Add a new Clause to the Formula // Return a reference to the new clause Clause& FormulaImpl::addClause() { ClauseImpl c; clauses.push_back(c); return clauses.back(); } // Get the number of clauses int FormulaImpl::numClauses() const { return clauses.size(); } // Get the i^th clause in the formula
  • 10. Clause& FormulaImpl::getClause(int i) { return clauses.at(i); } sat.cpp #include #include "debug.h" #include "formula_impl.h" #include "solver.h" using namespace std; using namespace Sat; int main(int argc, char **argv) { if (argc != 2) { cout << "Expected filename" << endl; return 0; } istream* is = new ifstream(*(argv+1)); if (!(*is)) { cout << "File not found" << endl; return 0; } FormulaImpl f; f.readClauses(*is); Assert(f.numClauses() > 0); f.dumpClauses(); Solver sat; if (sat.solve(f)) { cout << "Satisfiable" << endl; } else { cout << "Unsatisfiable" << endl; }
  • 11. return 0; } This will help. Please import necessary files. I am not able to upload zip files. Try this sample. If you find any difficulty please let me know. Solution First C++ code written... I think it's quite efficient with all the test set included in path data. The simplest data set looks like: -1 -3 0 -1 -5 0 -3 -5 0 -2 -4 0 -2 -6 0 -4 -6 0 1 2 0 3 4 0 5 6 0 used vector saving clauses and map saving assignments. the main recursive function looks like following: /* 1. If the set of clauses is empty, it returns true 2. Otherwise, if the set of clauses contains an empty clause, it returns false 3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively 4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself recursively 5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively */ Find the attached zip folder. I have referred this code example in my project. It will solve your problem. Implement following code : clause.cpp #include #include "clause.h" using namespace std; using namespace Sat; void Clause::readClause(istream& is) { static int lit = 0; if (lit != 0) addLiteral(lit);
  • 12. while (!is.eof()) { is >> lit; if (lit == 0) break; addLiteral(lit); } is >> lit; } void Clause::dumpClause() const { int i; cout << "("; for (i = 0; i < numLits() - 1; ++i) { cout << getLit(i) << ", "; } if (numLits() != 0) { cout << getLit(i); } cout << ")" << endl; } Clause_impl.cpp #include #include #include "clause.h" #include "clause_impl.h" using namespace std; using namespace Sat; //size_t ClauseImpl::clause_num = 0; // Add a literal to the clause void ClauseImpl::addLiteral(int lit){ add_lits(lit); } // Get the largest variable in the clause int ClauseImpl::maxVar() const{ int max = -1;
  • 13. for (int i = 0; i < lits.size(); ++ i) { if(max < abs(lits[i])) max = abs(lits[i]); } return max; } // Get the number of variables in the clause int ClauseImpl::numLits() const{ return lits.size(); } // Get the i^th literal in the clause int ClauseImpl::getLit(int i) const{ return lits.at(i) ; } vector ClauseImpl::get_lits() const {return lits;} void ClauseImpl::add_lits(int lit) { lits.push_back(lit); } bool ClauseImpl::hasLit(int a) const { for (int i = 0; i < lits.size(); ++ i) { if (lits[i] == a) { return true; } } return false; } debug.cpp #include #include "debug.h" using namespace std; namespace DebugUtil { ostream& operator<<(ostream& os, const Exception& e) { return os << e.toString();
  • 14. } void assertError(const string& file, int line, const string& cond) { ostringstream ss; ss << "in " << file << ":" << line << " (" << cond << ") "; throw Exception(ss.str()); } } // end of namespace DebugUtil formula.cpp #include "debug.h" #include "clause.h" #include "formula.h" using namespace std; using namespace Sat; void Formula::readClauses(istream& is) { while (!is.eof()) { Clause& c = addClause(); c.readClause(is); int tmp = c.maxVar(); if (tmp > maxVar) maxVar = tmp; } } void Formula::dumpClauses() { for (int i = 0; i < numClauses(); ++i) { getClause(i).dumpClause(); } } solver.cpp #include "solver.h"
  • 15. using namespace std; using namespace Sat; bool Solver::solve(FormulaImpl &f) { //vector p = findPure(f); //pureLit(f,p); //int uni = getUnitProp(f.getClauses()); //unitProp(f, uni); //return true; return splitting (f); } /* 1. If the set of clauses is empty, it returns true 2. Otherwise, if the set of clauses contains an empty clause, it returns false 3. Otherwise, if there is a unit clause, it applies unit propagation and then calls itself recursively 4. Otherwise, if there is a pure literal, it applies the pure literal rule and then calls itself recursively 5. Otherwise, it applies either the resolution rule or the splitting rule and calls itself recursively */ bool Solver::splitting (FormulaImpl &f) { FormulaImpl c (f); pair temp; // condition 1 if (c.getClauses().size() == 0) return true; // condition 2 for (int i = 0; i < c.getClauses().size(); ++ i) { if (c.getClause(i).numLits() == 0) return false; } int uni = getUnitProp (c.getClauses()); // condition 3 if (uni != 0) { temp = make_pair(uni, true); assgn.insert(temp); return splitting (unitProp(c, uni));
  • 16. } // condition 4 else { vector p = getPure(c); if (p.size() != 0) { for(int i = 0; i < p.size(); ++ i) { temp = make_pair(p[i], true); assgn.insert(temp); } return splitting (pureLit(c, p)); } // condition 5 else { int firstLit = c.getClause(0).getLit(0); temp = make_pair(firstLit, true); assgn.insert (temp); if (splitting (unitProp(c, firstLit)) == true) return true; else { assgn.erase(firstLit); temp = make_pair(firstLit, false); assgn.insert (temp); return splitting (unitProp(f, -firstLit)); } } } } /* a literal and a list of clauses; returns a new list of clauses resulting from applying the pure literal rule with the given literal. */ FormulaImpl& Solver::unitProp(FormulaImpl &f, int uni) { vector lits; while (uni != 0) { pair temp;
  • 17. temp = make_pair(uni, true); assgn.insert(temp); vector::iterator it; for (it = f.clauses.begin(); it != f.clauses.end();) { bool flag = false; vector::iterator jt; for (jt = it->lits.begin(); jt != it->lits.end(); ) { if ((*jt) == -uni) { jt = it->lits.erase(jt); break; } else if ((*jt) == uni){ flag = true; break; } else { ++ jt; } } if(flag == false) ++ it; else { //it->clearLits(); it = f.clauses.erase(it); } } uni = getUnitProp (f.getClauses()); } //f.setClauses (f.clauses); return f; } int Solver::getUnitProp (vector& c) const { for (int i = 0; i < c.size(); ++i) { if(c[i].get_lits().size() == 1) { return c[i].getLit(0); }
  • 18. } return 0; } /* if it creates a clause that contains both a variable and its negation, filter the clause out */ FormulaImpl& Solver::pureLit(FormulaImpl &f, vector p) { vector::iterator it; for (int i = 0; i < p.size(); ++ i) { for (it = f.clauses.begin(); it != f.clauses.end();) { if (it->hasLit(p[i])) { pair temp; temp = make_pair(p[i], true); assgn.insert(temp); it = f.clauses.erase(it); } else { ++ it; } } } return f; } vector Solver::getPure (FormulaImpl &f) { map litTable; vector p; vector::iterator it; for (it = f.clauses.begin(); it != f.clauses.end(); ++it) { vector::iterator jt; for (jt = it->lits.begin(); jt != it->lits.end(); ++jt) { if (litTable.find(abs(*jt))==litTable.end()) { if (*jt < 0) litTable.insert(pair(abs(*jt), -1)); else litTable.insert(pair(abs(*jt), 1)); } else {
  • 19. if (litTable.find(abs(*jt))->second == -1 && *jt > 0) { litTable.erase(*jt); litTable.insert(pair(abs(*jt), 0)); } else if (litTable.find(abs(*jt))->second == 1 && *jt < 0) { litTable.erase(abs(*jt)); litTable.insert(pair(abs(*jt),0)); } } } } map::iterator kt; int lit; for(kt = litTable.begin(); kt != litTable.end(); ++kt) { if (kt->second != 0) { lit = (kt->first) * (kt->second); p.push_back(lit); } } return p; } formula_imp.cpp #include "debug.h" #include "clause.h" #include "clause_impl.h" #include "formula.h" #include "formula_impl.h" using namespace std; using namespace Sat; // Add a new Clause to the Formula // Return a reference to the new clause Clause& FormulaImpl::addClause() { ClauseImpl c; clauses.push_back(c); return clauses.back();
  • 20. } // Get the number of clauses int FormulaImpl::numClauses() const { return clauses.size(); } // Get the i^th clause in the formula Clause& FormulaImpl::getClause(int i) { return clauses.at(i); } sat.cpp #include #include "debug.h" #include "formula_impl.h" #include "solver.h" using namespace std; using namespace Sat; int main(int argc, char **argv) { if (argc != 2) { cout << "Expected filename" << endl; return 0; } istream* is = new ifstream(*(argv+1)); if (!(*is)) { cout << "File not found" << endl; return 0; } FormulaImpl f; f.readClauses(*is); Assert(f.numClauses() > 0); f.dumpClauses(); Solver sat; if (sat.solve(f)) {
  • 21. cout << "Satisfiable" << endl; } else { cout << "Unsatisfiable" << endl; } return 0; } This will help. Please import necessary files. I am not able to upload zip files. Try this sample. If you find any difficulty please let me know.