-
Notifications
You must be signed in to change notification settings - Fork 1
/
SATInstance.cpp
199 lines (182 loc) · 5.08 KB
/
SATInstance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/////////////////
// OS Includes
#include <fstream>
#include <iomanip>
//////////////
// Includes
#include "Clause.h"
#include "Random.h"
#include "SATInstance.h"
#include "SATSolver.h"
/////////////
// Defines
/////////////////////////////
// Static data initialization
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Public Methods
SATInstance::~SATInstance()
{
vDestroy();
}
void SATInstance::vOutputDimacs(ostream& xUseMe_) const
{
xUseMe_ << "p cnf " << iVariableCount << ' ' << _iClauseCount << '\n';
int i;
for (i=0; i<_iClauseCount; i++) {
Clause* pPrintMe = _aClause[i];
if (!pPrintMe->bLearned()) {
for (int j=0; j < pPrintMe->iVariableCount(); j++) {
if (pPrintMe->iIsNegated(j))
xUseMe_ << "-";
xUseMe_ << (int) (pPrintMe->eConstrainedVariable(j)+1) << " ";
}
xUseMe_ << " 0\n";
}
xUseMe_ << flush;
}
xUseMe_ << "c NOTE: The following clauses are derived (non-essential):\n";
for (i=0; i<_iClauseCount; i++) {
Clause* pPrintMe = _aClause[i];
if (pPrintMe->bLearned()) {
for (int j=0; j < pPrintMe->iVariableCount(); j++) {
if (pPrintMe->iIsNegated(j))
xUseMe_ << "-";
xUseMe_ << (int) (pPrintMe->eConstrainedVariable(j)+1) << " ";
}
xUseMe_ << " 0\n";
}
xUseMe_ << flush;
}
}
VariableSet* SATInstance::pReadPrimaryVariables(const char* aFileName_)
{
// Primary variable file format: list of (any kind of) whitespace separated variable numbers.
// Call only after bReadDimacs.
// The caller of this method is responsible for deleting the returned VariableSet.
ifstream xInputFile(aFileName_);
VariableSet* return_me = pReadPrimaryVariables(xInputFile);
xInputFile.close();
return return_me;
}
VariableSet* SATInstance::pReadPrimaryVariables(istream& xInputFile)
{
char buffer[5000];
VariableID eVariableID;
VariableSet* return_me = new VariableSet(iVariableCount);
while (1) {
xInputFile >> eVariableID;
if (xInputFile.eof()) {
return return_me;
}
if (eVariableID < 1 || eVariableID > iVariableCount) {
xOutputStream << "\nError: Primary variable file contains unexpected variable id." << endl;
return 0;
}
else {
return_me->vAddVariable(eVariableID-1);
}
}
}
boolean SATInstance::bReadDimacs(const char* aFileName_)
{
ifstream xInputFile(aFileName_);
boolean return_me = bReadDimacs(xInputFile);
xInputFile.close();
return return_me;
}
boolean SATInstance::bReadDimacs(istream& xInputFile)
{
xOutputStream << "c Reading instance..." << flush;
char buffer[5000];
VariableID eMaxVar = 0;
int iClauseCount;
char cCheck;
while (1) {
xInputFile >> cCheck;
if (cCheck == 'c') {
xInputFile.getline(buffer, 5000);
continue;
}
else if (cCheck == 'p') {
xInputFile >> setw(5000) >> buffer;
xInputFile >> eMaxVar;
xInputFile >> iClauseCount;
break;
}
else {
xOutputStream << "\nError: File not in DIMACS format?\n";
return 0;
}
}
VariableSet xPositiveVariables(eMaxVar);
VariableSet xNegativeVariables(eMaxVar);
iVariableCount = eMaxVar;
int iWorkCount = 0;
while (1) {
if (xInputFile.eof()) {
xOutputStream << "\nError: Unexpected end of file.\n";
return 0;
}
xInputFile >> cCheck;
if (cCheck == 'c') {
xInputFile.getline(buffer, 5000);
continue;
}
else xInputFile.putback(cCheck);
xPositiveVariables.vClear();
xNegativeVariables.vClear();
boolean bIgnoreMe = 0;
do {
if (xInputFile.eof()) {
xOutputStream << "\nError: Unexpected end of file.\n";
return 0;
}
VariableID eVar;
xInputFile >> eVar;
if (eVar == 0)
break;
if (eVar > eMaxVar){
xOutputStream << "\nError: some variable is numbered larger than the maximum.\n";
return 0;
}
if (eVar < 0) {
if (xPositiveVariables.bHasVariable(0-(eVar+1))) {
bIgnoreMe = 1;// Tautology
}
else {
xNegativeVariables.vAddVariable(0-(eVar+1));
}
}
else {
if (xNegativeVariables.bHasVariable(eVar-1)) {
bIgnoreMe = 1;// Tautology
}
else {
xPositiveVariables.vAddVariable(eVar-1);
}
}
} while (1);
if (!bIgnoreMe) {
if (xNegativeVariables.iCount() + xPositiveVariables.iCount() == 0) {
xOutputStream << "\nError: encountered a 0 length clause \n" << endl ;
return 0;
}
// Silly up-casting needed to keep xlC compiler happy
Clause* pNewConstraint = new Clause((VariableList&)xPositiveVariables,
(VariableList&)xNegativeVariables,
1);
pNewConstraint->vSortVariableList();
vAddClause(pNewConstraint);
iWorkCount++;
}
else iClauseCount--;
if (iWorkCount == iClauseCount)
break;
}
xOutputStream << "..done." << endl;
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Protected Methods
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private Methods