-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforwardSearch.java
More file actions
309 lines (241 loc) · 8 KB
/
Copy pathforwardSearch.java
File metadata and controls
309 lines (241 loc) · 8 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Jack Pharies
CSC 372
A3
forwardSearch
forwardSearch is an attempt at a constarint solving with forward search.
A list holds all the variables, each recursion the weights of the indexes are recalculated.
forwardSearch chooses the variable that is in the most unsolved clauses and its inverse
Trying to emulate a forward search where we can tell the best move forward by being able to solve the clauses with the most literals
will backtrack if we run into conflict at any point
will terminate if we find that we cannot satisfy the equation
From what I can tell, this is a combinations of a greedy local search and a DFS
*/
import java.util.*;
import javax.swing.plaf.TreeUI;
import java.io.*;
public class forwardSearch
{
private ArrayList<clauseNode> unsolved;
private Stack<clauseNode> solved;
private ArrayList<satNode> remaining;
private Stack<satNode> forTree;
private int numOfVars;
private int numOfClauses;
private int globalCount;
/*
Constructor for the forwardSearch
Example: Used to make a forwardSearch search object within a testHub.
*/
public forwardSearch()
{
this.unsolved = new ArrayList<clauseNode>();
this.solved = new Stack<clauseNode>();
this.forTree = new Stack<satNode>();
this.remaining = new ArrayList<satNode>();
this.numOfClauses = 0;
this.numOfVars = 0;
this.globalCount = 0;
}
/*
Method that is called to start the search. Will open the files and run forward.
Example: called within a testhub to start the forward search
*/
public void solve(String file)
{
this.globalCount = 0;
this.solved = new Stack<clauseNode>();
this.unsolved = new ArrayList<clauseNode>();
this.forTree = new Stack<satNode>();
this.remaining = new ArrayList<satNode>();
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)
{
//printNodes();
System.out.println("solved");
}
System.out.println(globalCount);
System.out.println("completed");
}
/*
Starts the recursion for the forward search
Returns: the boolean whether the search succeeded
Example: used to start recursion in solve()
*/
private boolean start()
{
satNode start = new satNode(0, null);
forTree.push(start);
for (int i=0; i < numOfVars; i++)
{
satNode newNode = new satNode(i+1, null);
satNode newNodeMinus = new satNode((i+1)*-1, null);
remaining.add(newNode);
remaining.add(newNodeMinus);
}
boolean solved = this.forward();
if (solved == true)
{
return true;
}
return false;
}
/*
The main recursion method in forward
Returns: the boolean whether the search succeeded
Example: used to recurse
*/
private boolean forward()
{
globalCount++;
recalculate();
satNode currNode = remaining.get(0);
int count = 0;
int pos = 0;
int negPos = 0;
for (int i = 0; i< remaining.size(); i++)
{
if (remaining.get(i).getRepeat() > count && remaining.get(i).getUsed() == false)
{
count = remaining.get(i).getRepeat();
pos = i;
currNode = remaining.get(i);
}
}
int currVal = currNode.getVal();
remaining.get(pos).setUsed(true);
if (count == 0)
{
return false;
}
// find oppisite
satNode negate = remaining.get(0);
for (int i = 0; i< remaining.size(); i++)
{
if (remaining.get(i).getVal() == (currVal * -1))
{
negate = remaining.get(i);
negPos = i;
}
}
remaining.get(negPos).setUsed(true);
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 (this.solved.size() == numOfClauses)
{
//this.printNodes(currNode);
return true;
}
satNode newNode = new satNode(currVal, forTree.peek());
forTree.push(newNode);
boolean solved = forward();
if (solved == true)
{
return true;
}
forTree.pop();
for(int i = 0; i < solvedCounter; i++)
{
clauseNode removed = this.solved.pop();
removed.setSolved();
}
solvedCounter = 0;
// solve the sectiosn that can be solved
for (int i = 0; i < unsolved.size(); i++)
{
if (unsolved.get(i).checkSolved(negate.getVal()) == true)
{
clauseNode node = this.unsolved.get(i);
this.solved.push(node);
solvedCounter++;
}
}
if (this.solved.size() == numOfClauses)
{
//this.printNodes(currNode);
return true;
}
satNode newNodeNegate = new satNode(negate.getVal(), forTree.peek());
forTree.push(newNodeNegate);
solved = forward();
if (solved == true)
{
return true;
}
forTree.pop();
for(int i = 0; i < solvedCounter; i++)
{
clauseNode removed = this.solved.pop();
removed.setSolved();
}
remaining.get(pos).setUsed(false);
remaining.get(negPos).setUsed(false);
return false;
}
/*
used to recalculate the weightes of each variable.
Example: use at begining gof recursion to recalculate the number of clauses each variable is in
*/
private void recalculate()
{
int counter = 0;
for (int i = 0; i < remaining.size(); i++)
{
counter = 0;
for (int j = 0; j < unsolved.size(); j++)
{
if (unsolved.get(j).checkVar(remaining.get(i).getVal()) == true
&& unsolved.get(j).getSolved() == false)
{
counter++;
}
}
remaining.get(i).setRepeat(counter);
}
}
// used to print nodes for debugging -- nots called as of now
private void printNodes()
{
satNode peeked = forTree.peek();
int absolutVal = Math.abs(peeked.getVal());
while(forTree.isEmpty() != true)
{
satNode popped = forTree.pop();
System.out.println(popped.getVal());
}
}
}