-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver(q1).py
153 lines (130 loc) · 3.73 KB
/
solver(q1).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
import numpy as np
from pysat.solvers import Solver
from pysat.card import *
import csv
import pandas as pd
s = Solver(name='mc')
k = input("Enter k value: ")
k = int(k)
#Opening input file
with open('input_q1.csv', 'r') as f:
file = csv.reader(f)
my_list = list(file)
for i in range(0,2*k*k):
for j in range(0,k*k):
my_list[i][j]=int(my_list[i][j])
#Sudoku 1:
#Cell: Each number at most once
for i in range(0,k*k ):
for j in range(0,k*k):
s.add_atmost(lits =range(1 + j*k*k + i*k**4, k*k + 1 + j*k*k + i*k**4), k = 1)
#Row: Each number at most once
for i in range(0,k*k):
for p in range(1,k*k + 1):
s.add_atmost( np.arange(p + i*k**4, p + k**4 + i*k**4, k*k, dtype = int).tolist(), k = 1)
#Col: Each number at most once
for j in range(0,k*k):
for p in range(1,k*k + 1):
s.add_atmost( np.arange(p + j*k*k, p + j*k*k + k**6, k**4, dtype = int).tolist(), k = 1)
#Box: Each number at least (and thus exactly) once
top_left = []
for i in np.arange(0,k*k,k,dtype = int).tolist():
for j in np.arange(0,k*k,k,dtype = int).tolist():
top_left.append(1 + j*k*k + i*k**4)
for t in top_left:
for p in range(0, k*k):
x = []
for i in range(0,k):
for j in range (0, k):
x.append(t + p + j*k*k + i*k**4)
s.add_clause(x)
#Sudoku 2:
#Cell: Each number at most once
for i in range(0,k*k ):
for j in range(0,k*k):
s.add_atmost(lits =range(1 + j*k*k + i*k**4 + k**6, k*k + 1 + j*k*k + i*k**4+ k**6), k = 1)
#Row: Each number at most once
for i in range(0,k*k):
for p in range(1,k*k + 1):
s.add_atmost( np.arange(p + i*k**4+ k**6, p + k**4 + i*k**4+ k**6, k*k, dtype = int).tolist(), k = 1)
#Col: Each number at most once
for j in range(0,k*k):
for p in range(1,k*k + 1):
s.add_atmost( np.arange(p + j*k*k+ k**6, p + j*k*k + k**6+ k**6, k**4, dtype = int).tolist(), k = 1)
#Box: Each number at least (and thus exactly) once
top_left = []
for i in np.arange(0,k*k,k,dtype = int).tolist():
for j in np.arange(0,k*k,k,dtype = int).tolist():
top_left.append(1 + j*k*k + i*k**4 + k**6)
for t in top_left:
for p in range(0, k*k):
x = []
for i in range(0,k):
for j in range (0, k):
x.append(t + p + j*k*k + i*k**4)
s.add_clause(x)
#Adding Sudoku Pair Condition
for i in range(1,k*k + 1):
for j in range(1,k*k + 1):
for p in range(1,k*k + 1):
s.add_atmost(lits = [(p + (j - 1)*k*k + (i - 1)*k**4),(p + (j - 1)*k*k + (i - 1)*k**4 + k**6)], k = 1)
#Assuming inputs
inp = []
#Sudoku 1 inputs:
for i in range(0,k*k):
for j in range(0,k*k):
if (my_list[i][j] != 0):
inp.append(my_list[i][j] + j*k*k + i*k**4)
#Sudoku 2 inputs:
for i in range(k*k,2*k*k):
for j in range(0,k*k):
if (my_list[i][j] != 0):
inp.append(my_list[i][j] + j*k*k + (i - k*k)*k**4 + k**6)
s.solve(inp)
x = s.get_model()
#Converting model to 2D list
if (x is not None):
arr = []
for i in range(0,k*k):
row = []
for j in range(0,k*k):
for p in range(0,k*k):
if ( x[p + j*k*k + i*k**4] >0 ):
row.append(p + 1)
arr.append(row)
for i in range(0,k*k):
row = []
for j in range(0,k*k):
for p in range(0,k*k):
if ( x[p + j*k*k + i*k**4+ k**6] >0 ):
row.append(p + 1)
arr.append(row)
print("Sudoku 1: ")
for i in range(0,k*k):
for j in range(0,k*k):
if (arr[i][j] < 10):
print('',arr[i][j],end = ' ')
else:
print(arr[i][j],end = ' ')
if ((j+1)%k == 0):
print(" ", end= '')
print('')
if ((i+1)%k == 0):
print("")
print("\n\n\nSudoku 2: ")
for i in range(k*k,2*k*k):
for j in range(0,k*k):
if (arr[i][j] < 10):
print('',arr[i][j],end = ' ')
else:
print(arr[i][j],end = ' ')
if ((j+1)%k == 0):
print(" ", end= '')
print('')
if ((i+1)%k == 0):
print('')
#Sending solution to .csv file
df=pd.DataFrame(arr)
df.to_csv('output_q1.csv', index=False)
else:
print("None")