-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDPLL.java
More file actions
271 lines (213 loc) · 6.59 KB
/
Copy pathDPLL.java
File metadata and controls
271 lines (213 loc) · 6.59 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Jack Pharies
CSC 372
A3
DPLL
DPLL search object. When called upon, DPLL opens a predetermined file and solves the SAT inside.
Will be called in A3_SAT.
SAT is formatted in DIMACS form.
Will go file by file and solve the SATS.
*/
import java.util.*;
import javax.swing.plaf.TreeUI;
import java.io.*;
public class DPLL {
private ArrayList<clauseNode> unsolved;
private Stack<clauseNode> solved;
private Stack<satNode> dpllTree;
private int recent;
private int numOfVars;
private int numOfClauses;
private int currClauseNum;
private int currLitNum;
private int globalCount;
/*
Constructor for the DPLL
Example: Used to make a DPLL search object within a testHub.
*/
public DPLL()
{
this.unsolved = new ArrayList<clauseNode>();
this.solved = new Stack<clauseNode>();
this.dpllTree = new Stack<satNode>();
this.recent = 0;
this.currClauseNum = 0;
this.currLitNum = 0;
this.globalCount = 0;
}
/*
Method that is called to start the search. Will open the files and run DPLL.
Example: called within a testhub to start the DPLL search
*/
public void solve(String file)
{
this.globalCount = 0;
this.solved = new Stack<clauseNode>();
this.unsolved = new ArrayList<clauseNode>();
InputStream input = getClass().getResourceAsStream(file);
Scanner reader = new Scanner(input);
while (reader.hasNextLine())
{
String line = reader.nextLine();
String[] clause = line.split(" ");
if (clause[0].equals("p") == true)
{
this.numOfVars = Integer.valueOf(clause[2]);
this.numOfClauses = Integer.valueOf(clause[3]);
}
if (clause[0].equals("c") != true && clause[0].equals("p") != true)
{
ArrayList<Integer> intList = new ArrayList<Integer>();
for (int i = 0; i < 3; i++)
{
int number = Integer.valueOf(clause[i]);
intList.add(number);
}
clauseNode node = new clauseNode(intList);
unsolved.add(node);
}
}
// System.out.println(numOfClauses);
reader.close();
boolean solved = start();
if (solved == true)
{
System.out.println("solved");
}
System.out.println(globalCount);
System.out.println("completed");
}
/*
Starts the recursion for the DPLL search
Returns: the boolean whether the search succeeded
Example: used to start recursion in solve()
*/
private boolean start()
{
satNode newNode = new satNode(-1, null);
boolean solved = dpll(newNode);
if (solved == true)
{
return true;
}
newNode.changeCharge();
solved = dpll(newNode);
if (solved == true)
{
return true;
}
return false;
}
/*
The main recursion method in DPLL
Returns: the boolean whether the search succeeded
Example: used to recurse
*/
private boolean dpll(satNode currNode)
{
globalCount++;
int currVal = currNode.getVal();
int solvedCounter = 0;
// solve the sectiosn that can be solved
for (int i = 0; i < unsolved.size(); i++)
{
if (unsolved.get(i).checkSolved(currVal) == true)
{
clauseNode node = this.unsolved.get(i);
this.solved.push(node);
solvedCounter++;
}
}
// if every clause is T w.r.t. model, then return T
if (this.solved.size() == numOfClauses)
{
//this.printNodes(currNode);
return true;
}
// if some clause is F, then return F
for (int i = 0; i < unsolved.size(); i++)
{
if (unsolved.get(i).checkHighest(currVal) == true && unsolved.get(i).getSolved() == false)
{
for(int j = 0; j < solvedCounter; j++)
{
clauseNode removed = this.solved.pop();
removed.setSolved();
}
return false;
}
}
boolean pure = pureSymbol(currVal);
if (pure == true)
{
//this.printNodes(currNode);
return true;
}
int absCurrVal = Math.abs(currVal);
if (absCurrVal + 1 <= numOfVars)
{
absCurrVal++;
absCurrVal = absCurrVal * -1;
satNode newNode = new satNode(absCurrVal, currNode);
boolean solved = dpll(newNode);
if (solved == true)
{
return true;
}
newNode.changeCharge();
solved = dpll(newNode);
if (solved == true)
{
return true;
}
}
for(int i = 0; i < solvedCounter; i++)
{
clauseNode removed = this.solved.pop();
removed.setSolved();
}
return false;
}
// used to print nodes for debugging -- nots called as of now
private void printNodes(satNode node)
{
int absolutVal = Math.abs(node.getVal());
if (absolutVal < numOfVars)
{
for(int i = numOfVars; i > absolutVal; i--)
{
System.out.println(i);
}
}
while(node.getParent() != null)
{
System.out.println(node.getVal());
node = node.getParent();
}
System.out.println(node.getVal());
}
/*
Pure-Symbol-Heuristic that looks at all unsolved clauses and sees if val
is the same literal (positive or negated) in all of them. If so the rest of the
clauses can be solved.
Params: int val that is the litera that is being checked
Return: boolean whether the val is in all remaining clauses
Example: used to stop the DFS early and solve the SAT problem
*/
private boolean pureSymbol(int val)
{
int count = 0;
for (int i = 0; i < unsolved.size(); i++)
{
if (unsolved.get(i).checkVar(val) == true && unsolved.get(i).getSolved() == false)
{
count++;
}
}
if (count == unsolved.size())
{
return true;
}
return false;
}
}