-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeesawSearchCountBased.java
167 lines (137 loc) · 5.18 KB
/
SeesawSearchCountBased.java
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
//******************************************************************************
//
// File: SeesawSearchCountBased.java
//
//******************************************************************************
import java.util.Random;
/**
* Class SeesawSearchCountBased is used to perform the Count based Seesaw
* Search for solving the MAX-SAT problem.
*
* @author Harshad Paradkar
*
*/
public class SeesawSearchCountBased {
/**
* This method performs the Count based Seesaw Search to solve the
* MAX-SAT problem for the given parameters.
*
* @param parameters The parameters for the MAX-SAT problem that needs
* to be solved.
* @param maxTries Number of times to perform the search, after which
* the Seesaw Search gives up.
* @param seed The seed value, to initialize the PRNG.
* @return An object of type Solution.
* @throws Exception Any exception that might occur during execution.
*/
public static Solution solveMAXSAT(MAXSATParameters parameters,
int maxTries, long seed) throws Exception {
Random random;
if(seed == 0) {
// If the calling method does not want to provide a seed value.
random = new Random();
}else {
// Use the seed value provided by the calling method.
random = new Random(seed);
}
// To hold the maximum satisfied clauses in any particular iteration.
int temp = 0;
// Array to hold the truth value counts for each variable.
VariableCount variableTruthValueCounts[] = null;
int noOfIterations = 0, maxSatisfiedClausesCount = 0;
Clause[] maxSatisfiedClauses = null;
long startTime = System.currentTimeMillis();
// Start of Count based Seesaw Search
for(int i = 0; i < maxTries; i++) {
noOfIterations++;
temp = 0;
variableTruthValueCounts = new VariableCount[parameters.noOfVariables + 1];
/******************************************************************
* Optimization phase
*
* Make every clause evaluate to TRUE. If a clause is not TRUE,
* randomly choose a variable in that clause, and flip it.
* This will create inconsistencies in the value of variables
* across the clauses, but the Optimization phase does not care
* about it.
*
* Update the count of value for the variables in each clause, to
* indicate how many times a variable was TRUE, and FALSE.
*****************************************************************/
for(int j = 0; j < parameters.clauses.length; j++) {
// Get a clause to make it TRUE, if it's not already.
Clause clause = parameters.clauses[j];
Literal[] literals = clause.getLiterals();
if(!clause.isClauseSatisfied()) {
// If clause is not satisfied, make it evaluate to TRUE
int literalIndex = random.nextInt(literals.length);
Variable variable = literals[literalIndex].variable;
variable.value = !variable.value;
}
// Record the values count of each variable in this clause
for(int k = 0; k < literals.length; k++) {
Variable variable = literals[k].variable;
VariableCount variableCount
= variableTruthValueCounts[variable.name];
if(variableCount == null) {
variableCount = new VariableCount();
variableCount.variable = variable;
variableTruthValueCounts[variable.name] = variableCount;
}
if(variable.value) {
variableCount.trueCount += 1;
}else {
variableCount.falseCount += 1;
}
}
}// Optimization phase.
/******************************************************************
* Constraining phase
*
* Make every variable value consistent across all the clauses by
* using a greedy approach.
*
* Greedy approach idea: From the optimization phase, if a
* particular boolean variable value occurs more number of times
* than the other value, then it must be set to that value.
*****************************************************************/
for(int j = 1; j < variableTruthValueCounts.length; j++) {
if(variableTruthValueCounts[j].trueCount >=
variableTruthValueCounts[j].falseCount) {
variableTruthValueCounts[j].variable.value = true;
}else {
variableTruthValueCounts[j].variable.value = false;
}
}// Constraining phase
// Count the number of satisfied clauses
for(int j = 0; j < parameters.noOfClauses; j++) {
Clause clause = parameters.clauses[j];
if(clause.isClauseSatisfied()) {
temp++;
}
}
// Save this state if a better solution was found
if(temp > maxSatisfiedClausesCount) {
maxSatisfiedClausesCount = temp;
maxSatisfiedClauses = Utilities
.getDeepCopiedClauses(parameters.clauses,
parameters.noOfVariables);
if(temp == parameters.noOfClauses) {
/* if all the clauses evaluated to true, then no need to
* search further.
*/
break;
}
}
}// max-tries
long endTime = System.currentTimeMillis();
// Save time elapsed in seconds
float difference = (endTime - startTime) / 1000.0f;
Solution solution = new Solution(parameters.noOfVariables);
solution.setNoOfIterations(noOfIterations);
solution.setNoOfSatisfiedClauses(maxSatisfiedClausesCount);
solution.setTime(difference);
solution.setClauses(maxSatisfiedClauses);
return solution;
}
}