-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDCL.cs
321 lines (267 loc) · 10.4 KB
/
CDCL.cs
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
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace SAT_Solver {
public class CDCL : SATSolver {
/// <summary>Branch order is the predefined order in which literals are branched on. [index] is the order, [value] is the literal index.</summary>
private List<Literal> BranchOrder;
private Trail trail;
private Conflict latestConflict;
private string heuristic;
private List<Clause> learnedClauses;
private double[] vsids;
private const double vsidsDecay = 0.01;
public CDCL(string dimacs, bool debug, string heuristic, int timeout = 0) {
this.debug = debug;
this.heuristic = heuristic;
this.timeout = timeout;
ParseDimacs(dimacs);
SetBranchOrder();
Initialize();
}
private void SetBranchOrder() {
BranchOrder = ClauseReferences.Select((clause, variable) => new { variable, clause }).OrderByDescending(x => x.clause.Count).Select(x => new Literal(x.variable, false)).ToList();
if (heuristic == "reverse") {
BranchOrder.Reverse();
}
if (debug) {
for (int i = 0; i < BranchOrder.Count; i++) {
var lit = BranchOrder[i];
Console.WriteLine($"{i}: {lit.index + 1}\t\t{lit.index + 1}'s clauses: {ClauseReferences[lit.index].Count}");
}
}
}
private void Initialize() {
level = 0;
branch = new Stack<Literal>();
trail = new Trail();
learnedClauses = new List<Clause>();
vsids = new double[literals.Count];
}
public override SolverResult Run() {
if (Running)
throw new InvalidOperationException("Solver is already running");
Running = true;
totalwatch = Stopwatch.StartNew();
InitialUnitPropagation(out Conflict conflict);
if (conflict != null) {
Running = false;
return SolverResult.Fail(0);
}
if (AllAssigned) {
Running = false;
return SolverResult.Success(0, literals);
}
if (debug)
Console.WriteLine();
while (true) {
if (debug && latestConflict != null) {
Console.WriteLine("- Conflict");
Console.WriteLine(trail.DebugMessage);
}
iterations++;
TrailNode node = Travel();
UnitPropagation(node, out conflict);
if (debug) {
StateDebug();
}
if (conflict == null && AllAssigned) {
Running = false;
return SolverResult.Success(iterations, literals);
}
if (conflict != null && level == 0) {
Running = false;
return SolverResult.Fail(iterations);
}
if (conflict != null)
latestConflict = conflict;
if (timeout != 0 && totalwatch.ElapsedMilliseconds > timeout) {
Running = false;
return SolverResult.Timeout(iterations, (int) totalwatch.ElapsedMilliseconds);
}
}
}
private void StateDebug() {
Console.WriteLine($"i{iterations} L{level}: {string.Join(" ", trail.Where(n => n.IsDecision).Select(n => n.Literal.ToString()))}");
Console.WriteLine($"Trail: {string.Join(" ", trail)}");
if (learnedClauses.Count > 0)
Console.WriteLine($"{string.Join("\n", learnedClauses.Select(x => x.ToString()))}");
Console.WriteLine();
}
private void AssignLiteral(Literal literal) {
if (literals[literal.index].HasValue)
throw new ArgumentException("The given literal is already assigned");
literals[literal.index] = literal;
assignCount++;
}
private void UnassignLiteral(Literal literal) {
if (!literals[literal.index].HasValue)
throw new ArgumentException("The given literal is already unassigned");
literals[literal.index] = null;
assignCount--;
}
private TrailNode Travel() {
if (latestConflict != null)
return Backtrack();
return Decision();
}
private TrailNode Decision(Literal? force = null) {
level++;
Literal literal = force ?? SelectLiteral();
AssignLiteral(literal);
var node = new TrailNode(level, literal);
trail.Add(node);
return node;
}
private TrailNode Backtrack() {
var conflict = latestConflict;
latestConflict = null;
AddClause(conflict.Clause);
RevertToLevel(conflict.Level);
ApplyVSIDS(conflict.Clause);
var empty = FindEmpty(conflict.Clause);
if (debug) {
Console.WriteLine($"Revert to level {level} | Learned: {conflict.Clause} | Empty: {string.Join(" ", empty)}");
StateDebug();
}
if (empty.Count == 1) {
AssignLiteral(empty[0]);
var node = new TrailNode(level, empty[0], conflict.Clause);
trail.Add(node);
return node;
} else {
return Decision();
}
}
private void RevertToLevel(int targetLevel) {
for (int i = trail.Count - 1; i >= 0; i--) {
if (trail[i].Level > targetLevel) {
UnassignLiteral(trail[i].Literal);
trail.Remove(i);
}
}
level = targetLevel;
}
private List<Literal> FindEmpty(Clause clause) {
List<Literal> list = new List<Literal>();
foreach (var literal in clause) {
if (!literals[literal.index].HasValue) {
list.Add(literal);
}
}
return list;
}
private void ApplyVSIDS(Clause clause) {
for (int i = 0; i < vsids.Length; i++) {
vsids[i] *= 1 - vsidsDecay;
}
foreach (var lit in clause) {
vsids[lit.index] += 1;
}
}
private void InitialUnitPropagation(out Conflict conflict) {
conflict = null;
foreach (var clause in Clauses) {
if (clause.Count == 1) {
Literal literal = clause[0];
if (!literals[literal.index].HasValue) {
AssignLiteral(literal);
var node = new TrailNode(level, literal, clause);
trail.Add(node);
UnitPropagation(node, out conflict);
if (conflict != null)
return;
}
}
}
}
private void UnitPropagation(TrailNode node, out Conflict conflict) {
conflict = null;
foreach (var clause in ClauseReferences[node.Literal.index]) {
if (TryPropagate(node, clause, out bool error) is Literal empty) {
AssignLiteral(empty);
var newNode = new TrailNode(level, empty, clause);
trail.Add(newNode);
UnitPropagation(newNode, out conflict);
if (conflict != null)
return;
}
if (error) {
var learnedClause = trail.GetUIP(clause, out int targetLevel);
conflict = new Conflict(targetLevel, learnedClause);
return;
}
}
}
private Literal? TryPropagate(TrailNode node, Clause clause, out bool conflict) {
int assignedValues = 0;
int falseValues = 0;
Literal empty = default;
conflict = false;
foreach (var lit in clause) {
if (literals[lit.index].HasValue) {
if (literals[lit.index].Value.value != lit.value)
falseValues++;
assignedValues++;
} else {
empty = lit;
}
}
if (assignedValues != falseValues) {
return null;
}
if (assignedValues == clause.Count - 1) {
return empty;
}
if (falseValues == clause.Count) {
conflict = true;
return null;
}
return null;
}
private void AddClause(Clause clause) {
learnedClauses.Add(clause);
Clauses.Add(clause);
foreach (var literal in clause) {
ClauseReferences[literal.index].Add(clause);
}
}
private Literal SelectLiteral() {
if (heuristic == "random")
return RandomPick();
if (heuristic == "vsids")
return VSIDS();
return UseBranchOrder();
}
#region heuristics
private Literal UseBranchOrder() {
for (int i = 0; i < LiteralCount; i++) {
var index = BranchOrder[i].index;
if (!literals[index].HasValue) {
return new Literal(index, false);
}
}
throw new Exception("Branchable literal not found");
}
private Literal RandomPick() {
var rnd = new Random();
while (true) {
var val = rnd.Next(LiteralCount);
if (!literals[val].HasValue) {
return new Literal(val, false);
}
}
}
private Literal VSIDS() {
foreach (var item in vsids.Select((value, index) => new { index, value }).OrderByDescending(x => x.value)) {
if (!literals[item.index].HasValue) {
return new Literal(item.index, false);
}
}
throw new Exception("Branchable literal not found");
}
#endregion
}
}