-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.py
431 lines (342 loc) · 18.4 KB
/
algorithms.py
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from cnfparser import Parser
from helper import evaluate_clause, get_current_assignment, clause_to_dict, prop_occurencetype_in_clause, ID_GEN
import os
import time
import decisionheuristics as dh
#from implicationgraph import Implicationgraph
def enumeration_algorithm(clauses, proposition_list, default_value):
# collection of indices of unassigned propositions
#unassigned_propositions = [i for i in range(len(proposition_list))]
unassigned_propositions = [prop for prop in proposition_list]
trailstack = []
#default_value = 0
while True:
if unassigned_propositions:
# get last index on unassigned prop list
#ind = unassigned_propositions.pop()
proposition = unassigned_propositions.pop()
#proposition_list[ind].assign(default_value, False)
proposition.assign(default_value, False)
# trailstack.append(proposition_list[ind])
trailstack.append(proposition)
else:
satisfied = True
for clause in clauses:
if evaluate_clause(clause) == False:
satisfied = False
break
if satisfied == True:
return get_current_assignment(proposition_list)
while True:
if trailstack:
last_assigned_proposition = trailstack.pop()
if last_assigned_proposition.is_flippable() == True:
last_assigned_proposition.flip()
last_assigned_proposition.set_flippable(False)
trailstack.append(last_assigned_proposition)
break
else:
last_assigned_proposition.unassign()
unassigned_propositions.append(last_assigned_proposition)
else:
return False
class WATCHLIST:
def __init__(self, clauses, idgen, watch_heuristic = None):
# create a list of unique id's for each clause
self.idgen = idgen
self.watchlist = {}
self.heuristic = watch_heuristic
if self.heuristic is None:
# no elaborate heuristic: take first two propositions
print('Using standard watchlistheuristic')
self.heuristic = self.default_heuristic
for cl in clauses:
# generate and set new id for clause
clauseid = str(next(self.idgen.uniqueID()))
cl.set_id(clauseid)
# set watches for clause
if clauseid in self.watchlist:
self.watchlist[clauseid].append(self.heuristic(cl))
else:
self.watchlist[clauseid] = self.heuristic(cl)
# connect clauses current watches with our watchlist:
cl.current_watches = self.watchlist[clauseid]
def default_heuristic(self,clause):
reslist = []
for prop in clause.propositions:
# greedily add unassigned literals
if prop.assigned == False:
reslist.append(prop)
if len(reslist)==2:
break
return reslist
# in case we create a new clause, append it to our watchlist
def append_clause(self, clause):
if clause.id is not None:
if clause.id in self.watchlist:
if self.watchlist[clause.id] != []:
print(f'clause already has a nonempty watchlist')
else:
self.watchlist[clause.id].append(self.heuristic(clause))
else:
clauseid = str(next(self.idgen.uniqueID()))
cl.set_id(clauseid)
self.watchlist[clause.id] = self.heuristic(clause)
else:
# generate and set new id for clause
clauseid = str(next(self.idgen.uniqueID()))
cl.set_id(clauseid)
# set watches for clause
self.watchlist[clauseid] = self.heuristic(clause)
# davis-putnam-logemann-loveland algorithms
class DPLL:
def __init__(self):
pass
class Backtracking:
def __init__(self, default_value, clauses, propositions, idgen, wl_algorithm=None, decision_algorithm = None):
self.default_value = default_value
self.trailstack = []
self.clauses = clauses
self.proposition_list = propositions
self.idgen = idgen
self.unitclauses = []
self.last_set_literal = None
if decision_algorithm is not None:
print('using special algorithm')
self.decision_algorithm = dh.dynamic_largest_individual_sum
else:
self.decision_algorithm = self.decide
self.watchlist = WATCHLIST(clauses, idgen, wl_algorithm)
self.use_watchlist = False
#print('Complete watchlist:')
#print(self.watchlist.watchlist)
def dpll_algorithm(self, use_watchlist = False):
if use_watchlist == True:
self.use_watchlist = True
self.trailstack = []
if not self.BCP(self.clauses, self.proposition_list):
return False
print('finished initial BCP here')
while True:
if not self.decision_algorithm(self.clauses, self.proposition_list):
return get_current_assignment(self.proposition_list)
while not self.BCP(self.clauses, self.proposition_list):
#print('BCP returned FALSE')
if not self.backtrack(self.clauses, self.proposition_list):
return False
def BCP(self, clauses, proposition_list):
# update all states, check immediately for the state. If it is unsatisfied, return False immediately
print('================================================================================= New BCP call')
# this is ugly... will be called for every BCP call, but this if is only for the first call.. fix it at some point
if self.last_set_literal is None:
aggregated_literals = []
else:
aggregated_literals = [self.last_set_literal]
#print(aggregated_literals)
print(f'Propagating Constraints based on following assigned or implicated literals: {aggregated_literals}')
while len(aggregated_literals)>0:
#print('')
#print(f'ACCUMULATION: {int(n.identifier)+1 for n in aggregated_literals}')
#print('')
cur_literal = aggregated_literals.pop()
# get set of clauses that might change their status
#print(cur_literal)
clause_subset = cur_literal.contained_in_clauses
#print(clause_subset)
#print(f'Following classes contain the literal: {clause_subset}')
for clause in clause_subset:
if self.use_watchlist == True:
clause.update_state(self.watchlist)
else:
clause.update_state()
print(f'CLAUSE {int(clause.id)+1} HAS ___{clause.state}___')
# push proposition from a unit clause on trail
if clause.state == Parser.CLAUSESTATE.UNIT:
self.trailstack.append(clause.missing_proposition)
clause.missing_proposition.set_decided(True)
clause.missing_proposition.assign(clause.implied_unitvalue)
if clause.missing_proposition not in aggregated_literals:
aggregated_literals.append(clause.missing_proposition)
print(f'Implication for Literal {int(clause.missing_proposition.identifier)+1} by Clause {int(clause.id)+1}. Implied value: {clause.implied_unitvalue}')
else:
print('implication already found')
elif clause.state == Parser.CLAUSESTATE.UNSATISFIED:
print(f'Contradiction for clause: {clause.id}')
#print(f'Current watches: {clause.current_watches}')
#print(f'assigned watches: {clause.assigned_watches}')
return False
elif clause.state == Parser.CLAUSESTATE.SATISFIED:
print(f'Ignore clause. Reason: SATISFIED')
# only returns True, if there are no unsatisfied clauses
print(f'No new implications, and no unsatisfied clauses for BCP.')
# time.sleep(10)
#print(self.watchlist)
return True
def decide(self,clauses, proposition_list):
for prop in proposition_list:
if prop.assigned == False and prop.decided == False:
prop.assign(self.default_value)
prop.set_decided(False)
self.trailstack.append(prop)
self.last_set_literal = prop
print(f'Decision: {int(prop.identifier)+1} with value {self.default_value}')
return True
# no decision was possible. At this point we update clause states once more. to get an up to date representation of our solution or the unsatisfied state
for cl in clauses:
if self.use_watchlist == True:
cl.update_state(self.watchlist)
else:
cl.update_state()
return False
def backtrack(self, clauses, proposition_list):
while True:
if not self.trailstack:
return False
prop_from_unit = self.trailstack.pop()
if not prop_from_unit.decided:
self.trailstack.append(prop_from_unit)
prop_from_unit.flip()
prop_from_unit.set_decided(True)
self.last_set_literal = prop_from_unit
print(f'Backtracked to {int(prop_from_unit.identifier)+1}. Decision changed to {prop_from_unit.value} ')
return True
else:
print(f'////// UNASSIGN: {int(prop_from_unit.identifier)+1}')
prop_from_unit.unassign()
class Implicationgraph:
def __init__(self, default_value):
self.default_value = default_value
self.decisionlevel = 0
self.nodes_on_level = {'0': []}
self.vertices = []
# holds tuples with format: starting_node, target_node, clause_label
self.edges = []
self.last_assigned_prop = None
self.conflicting_clauses = []
self.asserting_clauses = []
self.last_decision = None
def cdcl_algorithm(self, clauses, proposition_list):
if not self.BCP(clauses, proposition_list):
return False
while True:
if not self.decide(clauses, proposition_list):
return get_current_assignment(proposition_list)
while not self.BCP(clauses, proposition_list):
if not self.resolve_conflict(clauses, proposition_list):
return False
def resolve_conflict(self, clauses, proposition_list):
if self.decisionlevel == 0:
return False
self.update_asserting_clauses()
# we want to reverse our last decision, because that decision created implications leading to current conflict
for node in self.nodes_on_level[str(self.decisionlevel)]:
#print(node)
# also reset antecedent, since we solve exactly that conflict
node.unassign(True)
self.vertices.remove(node)
for e in self.edges:
if e[1] in self.nodes_on_level[str(self.decisionlevel)]:
self.edges.remove(e)
self.nodes_on_level[str(self.decisionlevel)] = []
self.decisionlevel -= 1
# now create new clauses from asserting clauses and add those to our clause pool
return True
def update_asserting_clauses(self):
# with current implementation, only one conflicting clause should exist at every point in time
for cl in self.conflicting_clauses:
nodes_in_clause = 0
# all nodes on current level
for n in self.nodes_on_level[str(self.decisionlevel)]:
if n in cl.neg_propositions:
nodes_in_clause += 1
# todo: create new clauses and add them to our pool
# new_clause =
if nodes_in_clause ==1:
self.asserting_clauses.append(cl)
continue
else:
for n in self.nodes_on_level[str(self.decisionlevel)]:
if n in cl.pos_propositions:
nodes_in_clause +=1
if nodes_in_clause > 0:
self.asserting_clauses.append(cl)
def label_node(self, node, value):
if node not in self.vertices:
self.vertices.append(node)
node.set_label((value, self.decisionlevel))
# this function is very expensive. kind of O(n^2), we safe a bit, because we ignore mirrored connections.
# trying to minimize the damage, by keeping track of new nodes and new unit clauses
def update_edges(self, set_of_new_assignments, set_of_new_unit_clauses):
for ind, new_n1 in enumerate(set_of_new_assignments):
for new_n2 in set_of_new_assignments[ind:]:
if new_n1 != new_n2:
for antecedent_n2 in new_n2.antecedent:
if new_n1.value == 1:
if new_n1 in antecedent_n2.neg_propositions:
self.edges.append((new_n1, new_n2, antecedent_n2))
elif new_n1.value == 0:
if new_n1 in antecedent_n2.pos_propositions:
self.edges.append((new_n1, new_n2, antecedent_n2))
# at this point, the new props are interconnected. Only thing to do is, find connections from old nodes,
# and append the new nodes to the old_nodes_list
for old_n1 in self.vertices:
for new_n2 in set_of_new_assignments:
#should never be the case, but check anyways..
if old_n1 != new_n2:
for antecedent_n2 in new_n2.antecedent:
if old_n1.value == 1:
if old_n1 in antecedent_n2.neg_propositions:
self.edges.append((old_n1, new_n2, antecedent_n2))
elif old_n1.value == 0:
if old_n1 in antecedent_n2.pos_propositions:
self.edges.append((old_n1, new_n2, antecedent_n2))
for new in set_of_new_assignments:
self.vertices.append(new)
def decide(self, clauses, proposition_list):
for prop in proposition_list:
if prop.assigned == False and prop.decided == False:
prop.assign(self.default_value)
prop.set_decided(False)
self.last_assigned_prop = prop
# first increase decisionlevel. Like this the current decided value is always on a new level.
self.decisionlevel += 1
# create new index, and create list with prop in it.pass
# in the BCP function, we will append to this list
self.nodes_on_level[str(self.decisionlevel)] = [prop]
self.vertices.append(prop)
prop.set_label((self.default_value, self.decisionlevel))
self.last_decision = prop
return True
for cl in clauses:
cl.update_state()
return False
def BCP(self, clauses, proposition_list):
# update all states, check immediately for the state. If it is unsatisfied, return False immediately
while True:
found_something = False
set_of_new_assignments = []
set_of_new_unit_clauses = []
for clause in clauses:
clause.update_state()
# push proposition from a unit clause on trail
if clause.state == Parser.CLAUSESTATE.UNIT:
#update implicationgraph
found_something = True
clause.missing_proposition.set_decided(True)
clause.missing_proposition.assign(clause.implied_unitvalue)
# instantly add the correct label for node
self.label_node(clause.missing_proposition, clause.implied_unitvalue )
self.nodes_on_level[str(self.decisionlevel)].append(clause.missing_proposition)
# do not add to assigned vertices list yet, we will do this in the update edge function
# self.vertices.append(clause.missing_proposition)
# add to the set of assignments for one iteration over the clauses
set_of_new_assignments.append(clause.missing_proposition)
set_of_new_unit_clauses.append(clause)
elif clause.state == Parser.CLAUSESTATE.UNSATISFIED:
self.conflicting_clauses.append(clause)
return False
self.update_edges(set_of_new_assignments, set_of_new_unit_clauses)
if found_something == False:
break
# only returns True, if there are no unsatisfied clauses
return True