-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimised.py
More file actions
40 lines (38 loc) · 1.58 KB
/
optimised.py
File metadata and controls
40 lines (38 loc) · 1.58 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
#
# This file is just a draft of the optimised encoding.
#
#function which, given an encoding and a sudoku, outputs a trimmed down list of clauses.
#The 'reduction operators' from the paper are basically contained within the if clauses in the function body.
def optimised_encoding(encoding, sample_sudoku):
assigned = [encode_into_number(x, sample_sudoku) for x in assigned_variables(sample_sudoku)]
false = [encode_into_number(x, sample_sudoku) for x in create_falsehoods(sample_sudoku)]
new_encoding = encoding.copy()
for clause in new_encoding.copy():
for literal in clause.copy():
if literal in assigned:
new_encoding.remove(clause)
break
if -literal in false:
new_encoding.remove(clause)
break
if literal in false:
clause.remove(literal)
if -literal in assigned:
clause.remove(literal)
for literal in assigned:
new_encoding.append([literal])
for literal in false:
new_encoding.append([-literal])
return new_encoding
new_encoding = optimised_encoding(encoding, s_test)
print("Number of clauses in old encoding:", len(encoding))
print("Number of clauses in new encoding:", len(new_encoding))
def variable_counter(encoding):
counter = set()
for clause in encoding:
for literal in clause:
counter.add(literal**2)
return len(counter)
#
print("Number of variables in old encoding:", variable_counter(encoding))
print("Number of variables in new encoding:", variable_counter(new_encoding))