-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver2.java
More file actions
269 lines (232 loc) · 9.99 KB
/
Copy pathsolver2.java
File metadata and controls
269 lines (232 loc) · 9.99 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
import java.util.BitSet;
import java.util.Random;
public class solver2 {
// Max runtime
private final static int T = 10000000;
static int numVars;
static int numClauses;
static int[] clauseCosts;
static int[] currentCosts;
static int[] literals;
static int[] values;
static BitSet vars;
public static void main(String[] args) {
Random random = new Random();
// Read in wcard file
CapstoneFileReader reader = new CapstoneFileReader();
reader.InitializeClauses("test3.wcard", false);
// Get data from reader
numVars = reader.getNumVars();
numClauses = reader.getNumClauses();
clauseCosts = reader.getCosts();
currentCosts = new int[numClauses]; // initialise current cost list
literals = reader.getLiterals();
values = reader.getValues();
// Boolean variables
vars = new BitSet(numVars);
// Set up boolean assignment
for (int i=0; i < numVars; i++)
{
// Initiate boolean assignment randomly
if (random.nextInt(2) == 1)
{
vars.set(i);
}
}
// Set up float for each clause
int[] clauseFloats = new int[numClauses];
// Set up make and break scores for each variable
long[] makeScores = new long[numVars];
long[] breakScores = new long[numVars];
// Define flattened 2d array to link variables to clauses
int[] clauses = new int[numVars*numClauses];
// Link variables to clauses
for (int i=0; i < numClauses; i++)
{
for (int j=0; j < numVars; j++)
{
clauses[j*numClauses + i] = (literals[numVars*i+j] == 0) ? 0 : 1;
}
}
// Calculate floats and total cost of initial state
long curTotalCost = 0;
for (int i=0; i < numClauses; i++)
{
clauseFloats[i] = checkFloat(i);
curTotalCost += calcCurrentClauseCost(i);
}
System.out.println("Initial cost: "+String.valueOf(curTotalCost));
// Calculate make and break scores for each variable:
long bestBreakScore = Long.MAX_VALUE;
long bestMakeScore = 0;
int bestFlip = -1;
int sign = 1;
for (int v=0; v < numVars; v++) // iterate through each variable
{
// Initialise make and break scores
breakScores[v] = 0;
makeScores[v] = 0;
for (int c=0; c < numClauses; c++) // check each clause per variable
{
if (clauses[v*numClauses+c] == 1) // only consider clauses that contain the current variable
{
sign = (literals[c*numVars+v] < 0) ? -1 : 1; // check sign of literal
if (clauseFloats[c]==0) // check if break score will increase
{
if (vars.get(v) && sign==1) {breakScores[v] = breakScores[v] + clauseCosts[c];} // increase breakscore by cost of clause
else if (!vars.get(v) && sign==-1) {breakScores[v] = breakScores[v] + clauseCosts[c];}
}
else if (clauseFloats[c]==-1) // check if make score will increase
{
if (!vars.get(v) && sign==1) {makeScores[v] = makeScores[v] + clauseCosts[c];} // increase makescore by cost of clause
else if (vars.get(v) && sign==-1) {makeScores[v] = makeScores[v] + clauseCosts[c];}
}
}
}
// Keep track of best break score
if (breakScores[v] < bestBreakScore) // best is minimum
{
bestBreakScore = breakScores[v];
bestFlip = v; // our heuristic prioritises break score
}
else if (breakScores[v] == bestBreakScore) // tiebreaker
{
if (makeScores[v] > bestMakeScore) // best is maximum
{
bestMakeScore = makeScores[v];
bestFlip = v;
}
}
// Keep track of best make score
if (makeScores[v] > bestMakeScore) // best is maximum
{
bestMakeScore = makeScores[v];
}
}
long bestCost = Long.MAX_VALUE;
BitSet bestAssignment = new BitSet(numVars);
int t = 0;
final double RANDOM_CHANCE = 0.01;
while (true)
{
// Algorithm:
// flip:
// a) variable with highest breakscore
// b) if tied, variable with highest makescore
// c) with small chance, random flip
// recalculate float for each clause
// recalculate break and make scores for each variable
t++;
// Small chance for random flip:
if (random.nextDouble() < RANDOM_CHANCE) {bestFlip = random.nextInt(numVars);}
// Otherwise take greedy flip
else {vars.flip(bestFlip);}
// Recalculate floats
for (int i=0; i < numClauses; i++)
{
clauseFloats[i] = checkFloat(i);
}
// Calculate make and break scores for each variable:
bestBreakScore = Long.MAX_VALUE;
bestMakeScore = 0;
bestFlip = -1;
for (int v=0; v < numVars; v++) // iterate through each variable
{
// Initialise make and break scores
breakScores[v] = Long.MAX_VALUE;
makeScores[v] = 0;
for (int c=0; c < numClauses; c++) // check each clause per variable
{
if (clauses[v*numClauses+c] == 1) // only consider clauses that contain the current variable
{
sign = (literals[c*numVars+v] < 0) ? -1 : 1; // check sign of literal
if (clauseFloats[c]==0) // check if break score will increase
{
if (vars.get(v) && sign==1) {breakScores[v] = breakScores[v] + clauseCosts[c];} // increase breakscore by cost of clause
else if (!vars.get(v) && sign==-1) {breakScores[v] = breakScores[v] + clauseCosts[c];}
}
else if (clauseFloats[c]==-1) // check if make score will increase
{
if (!vars.get(v) && sign==1) {makeScores[v] = makeScores[v] + clauseCosts[c];} // increase makescore by cost of clause
else if (vars.get(v) && sign==-1) {makeScores[v] = makeScores[v] + clauseCosts[c];}
}
}
}
// Keep track of best break score
if (breakScores[v] < bestBreakScore) // best is minimum
{
bestBreakScore = breakScores[v];
bestFlip = v; // our heuristic prioritises break score
}
else if (breakScores[v] == bestBreakScore) // tiebreaker
{
if (makeScores[v] > bestMakeScore) // best is maximum
{
bestMakeScore = makeScores[v];
bestFlip = v;
}
}
// Keep track of best make score
if (makeScores[v] > bestMakeScore) // best is maximum
{
bestMakeScore = makeScores[v];
}
}
// Calculate current cost
curTotalCost = 0;
for (int c=0; c < numClauses; c++)
{
curTotalCost += calcCurrentClauseCost(c);
}
if (curTotalCost < bestCost) // keep track of best cost and assignment
{
bestCost = curTotalCost;
bestAssignment = (BitSet) vars.clone();
}
// Display total cost periodically to check progress
if (t % 100000 == 0)
{
System.out.println("Best cost at time " + String.valueOf(t) + ": " + String.valueOf(bestCost));
}
if (t > T) {break;} // if time is up, end run
}
System.out.println("Final Best Cost: " + String.valueOf(bestCost));
// Create string output
//String output = "(";
//for (int k=0; k < numVars-1; k++)
//{
// output = output + ((bestAssignment.get(k)) ? "1" : "0") + ", ";
//}
//output = output + ((bestAssignment.get(numVars-1)) ? "1" : "0") + ")";
//System.out.println("Corresponding Assignment: " + output);
}
public static int checkFloat(int clauseToCheck)
{
int sum = 0;
// Loop over each variable that could occur in clause (all vars)
for (int i=0; i < numVars; i++)
{
// If a positive literal is mentioned, check if it is set, and if so, add to total value on LHS of expression
if (literals[numVars*clauseToCheck+i] > 0)
{
sum = sum + ((vars.get(i)) ? 1 : 0);
}
// If a negative literal is mentioned, check if it is NOT set, and if not, add to total value on LHS of expression
else if (literals[numVars*clauseToCheck+i] < 0)
{
sum = sum + ((vars.get(i)) ? 0 : 1);
}
// If the literal is 0, don't consider it (no "else" needed)
}
return (sum - values[clauseToCheck]); //return the float, i.e. sum - cost of clause (as we require sum to be >= cost for clause to be SAT)
}
public static boolean checkSAT(int clauseToCheck)
{
return (checkFloat(clauseToCheck) >= 0); //return whether the float is >= 0
}
public static int calcCurrentClauseCost(int clause)
{
// If clause is satisfied, cost is 0, otherwise return associated cost
return (checkSAT(clause)) ? 0 : clauseCosts[clause];
}
}