-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDPLLService.java
More file actions
228 lines (184 loc) · 9 KB
/
DPLLService.java
File metadata and controls
228 lines (184 loc) · 9 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
package com.mobcom.solver;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;
import java.util.*;
/*
if C is consistent set of unit clauses, return true if C contains empty clause, return false
as long as there is a unit clause {l} in C:
C := unit-propagate(l, C)
as long as there are pure literals l in C:
C := pure-literal-assign(l, C):
l := choose-literal(C)
return DPLL(C [ {l}) || DPLL(C [ {¬l})
*/
public class DPLLService {
public static final int NOT_FOUND = 0;
public boolean solveWithDPLL(CNF cnfContent){
//There are no clauses
if(cnfContent.getClauses().size()==0){
return true;
}
//There are empty clauses.
if(cnfContent.getClauses().stream().filter(clause->clause.size()==0).count()!=0){
return false;
}
//Do unit propagation as much as possible.
//If the given clauses has a unit clause, program will not proceed beyond this point until no more unit clause left.
int result = findUnitClause(cnfContent);
while(result!=NOT_FOUND){
//unit propagation in this case
doPropagation(cnfContent, result);
cnfContent.getSelectedVariables().add(result);
//There are no clauses
if(cnfContent.getClauses().size()==0){
return true;
}
//There are empty clauses.
if(cnfContent.getClauses().stream().filter(clause->clause.size()==0).count()!=0){
return false;
}
result = findUnitClause(cnfContent);
}
result = hasPureLiteral(cnfContent);
while(result != NOT_FOUND){
//elimination cannot create a unit clause, so the program should not go back to top.
eliminatePureLiteral(cnfContent,result);
cnfContent.getSelectedVariables().add(result);
if(cnfContent.getClauses().size()==0){
return true;
}
result = hasPureLiteral(cnfContent);
}
int selectedLiteral = chooseLiteral(cnfContent);
CNF cnfContentTrueBranch = new CNF(cnfContent);
CNF cnfContentComplementBranch = new CNF(cnfContent);
cnfContentTrueBranch.getClauses().add(new LinkedHashSet<>(Arrays.asList(selectedLiteral)));
cnfContentComplementBranch.getClauses().add(new LinkedHashSet<>(Arrays.asList(-selectedLiteral)));
//could be an optimization
doPropagation(cnfContentTrueBranch,selectedLiteral);
doPropagation(cnfContentComplementBranch,-selectedLiteral);
if(solveWithDPLL(cnfContentTrueBranch)){
//set itself
cnfContent.getSelectedVariables().add(selectedLiteral);
cnfContent.getSelectedVariables().addAll(cnfContentTrueBranch.getSelectedVariables());
return true;
}else{
//negation
//set
boolean isSatisfiable = solveWithDPLL(cnfContentComplementBranch);
if(isSatisfiable){
cnfContent.getSelectedVariables().addAll(cnfContentComplementBranch.getSelectedVariables());
cnfContent.getSelectedVariables().add(-selectedLiteral);
}
return isSatisfiable;
}
}
private int chooseLiteral(CNF cnfContent){
int selectedLiteral;
Optional<LinkedHashSet<Integer>> selectedClause = cnfContent.getClauses().stream().filter(clause->clause.size()==2).findFirst();
if(selectedClause.isPresent()){
Object[] literals = (Object[])selectedClause.get().toArray();
if(cnfContent.getVariablesAndOccurrences().count(literals[0])<cnfContent.getVariablesAndOccurrences().count(literals[1])){
selectedLiteral = (Integer)literals[0];
}else{
selectedLiteral = (Integer)literals[1];
}
}else{
selectedLiteral = Multisets.copyHighestCountFirst(cnfContent.getVariablesAndOccurrences()).entrySet().iterator().next().getElement();
}
return selectedLiteral;
}
private int findUnitClause(CNF cnfContent){
Optional<LinkedHashSet<Integer>> unitClause = cnfContent.getClauses().stream().filter(clause->clause.size()==1).findFirst();
if(unitClause.isPresent()){
return unitClause.get().iterator().next();
}else{
return NOT_FOUND;
}
}
private void doPropagation(CNF cnfContent, int literal){
int length = cnfContent.getClauses().size();
Multiset<Integer> variablesAndOccurrences = cnfContent.getVariablesAndOccurrences();
for (int i = length-1; i >= 0; i--) {
LinkedHashSet<Integer> clause = cnfContent.getClauses().get(i);
if(clause.contains(literal)){
//remove clause itself
Iterator<Integer> itr = clause.iterator();
while(itr.hasNext()){
variablesAndOccurrences.remove(itr.next());
}
cnfContent.getClauses().remove(i);
}else if(clause.contains(-literal)){
//remove negation
variablesAndOccurrences.remove(-literal);
//cnfContent.getClauses().get(i).remove(-literal);
clause.remove(-literal);
cnfContent.getClauses().set(i,clause);
}
}
cnfContent.setVariablesAndOccurrences(variablesAndOccurrences);
}
private int hasPureLiteral(CNF cnfContent){
Multiset<Integer> variablesAndOccurences = cnfContent.getVariablesAndOccurrences();
Iterator<Integer> itr = variablesAndOccurences.iterator();
boolean found = false;
Integer pureLiteral = 0;
while(itr.hasNext() && pureLiteral==0){
int value = itr.next();
if(variablesAndOccurences.count(value)>0 && variablesAndOccurences.count(-value)==0){
pureLiteral = value;
}else if(variablesAndOccurences.count(-value)>0 && variablesAndOccurences.count(value)==0){
pureLiteral = -value;
}
}
return pureLiteral;
}
private void eliminatePureLiteral(CNF cnfContent, int literal){
int length = cnfContent.getClauses().size();
Multiset<Integer> variablesAndOccurrences = cnfContent.getVariablesAndOccurrences();
for (int i = length-1; i >= 0; i--) {
LinkedHashSet<Integer> clause = cnfContent.getClauses().get(i);
if(clause.contains(literal)){
//remove clause
Iterator<Integer> itr = clause.iterator();
while(itr.hasNext()){
variablesAndOccurrences.remove(itr.next());
}
cnfContent.getClauses().remove(i);
}
}
cnfContent.setVariablesAndOccurrences(variablesAndOccurrences);
}
public void computeOccurrences(CNF cnfContent){
Multiset<Integer> variablesAndOccurences = cnfContent.getVariablesAndOccurrences();
cnfContent.getClauses().stream().forEach(clause -> {
Iterator<Integer> itr = clause.iterator();
while(itr.hasNext()){
variablesAndOccurences.add(itr.next());
}
});
cnfContent.setVariablesAndOccurrences(variablesAndOccurences);
}
}
//literal can be chosen in different ways. problem 4 and 5 cannot be solved by working on highest count first.
//randomly choosing is also not an option for these problems. Therefore these approaches are commented oute.
//highestCountFirst is not an optimization.
//if(App.iteration>10) {
//ImmutableMultiset<Integer> sortedSet = Multisets.copyHighestCountFirst(cnfContent.getVariablesAndOccurrences());
//Iterator<Multiset.Entry<Integer>> itr = sortedSet.entrySet().iterator();
//selectedLiteral = itr.next().getElement();
//selectedLiteral = Multisets.copyHighestCountFirst(cnfContent.getVariablesAndOccurrences()).entrySet().iterator().next().getElement();
//}else {
/* ImmutableList<Integer> literals = Multisets.copyHighestCountFirst(cnfContent.getVariablesAndOccurrences()).elementSet().asList();
selectedLiteral = literals.get(literals.size() - 1);
if(cnfContent.getVariablesAndOccurrences().count(literals.get(literals.size()-1)) == cnfContent.getVariablesAndOccurrences().count(literals.get(literals.size()-2))){
Optional<LinkedHashSet<Integer>> selectedClause =cnfContent.getClauses().stream().filter(clause->clause.size()==2).findFirst();
if(selectedClause.isPresent()){
selectedLiteral = selectedClause.get().iterator().next();
}
}*/
//}
//ImmutableList<Integer> literals = Multisets.copyHighestCountFirst(cnfContent.getVariablesAndOccurrences()).elementSet().asList();
//selectedLiteral = literals.get(literals.size() / 2);
//int randomNum = new Random().nextInt(cnfContent.getVariablesAndOccurrences().elementSet().size());
//selectedLiteral = (Integer) cnfContent.getVariablesAndOccurrences().elementSet().toArray()[randomNum];