-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKronecker.py
56 lines (44 loc) · 1.92 KB
/
Kronecker.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
from scipy import sparse
import numpy as np
from collections import defaultdict
from Factorizer import Factorizer
from Board import Board
from ConstraintPropagationSolver import ConstraintPropagationSolver
from LocalSearchSolver import LocalSearchSolver
from GlobalSearchSolver import GlobalSearchSolver
class Kronecker:
def __init__(self, n, method="CP", print_board=False):
self.n = n
self.print_board = print_board
if method == 'CP':
self.method="CP"
else:
self.method = "GS"
def solve(self):
factors = Factorizer(self.n).factorize()
intermediate, couples = [], {}
if self.method == 'CP':
for k, _ in factors.items():
couples[k] = ConstraintPropagationSolver(k).solve()
else:
for k, _ in factors.items():
couples[k] = GlobalSearchSolver(k).solve()
solved_clean = {k: v.get_as_matrix() for k, v in couples.items()}
for _, j in solved_clean.items(): j = sparse.csr_matrix(np.array(j))
for number, solution in solved_clean.items():
for i in range(factors[number]-1):
solution = sparse.csr_matrix(solution)
solution = sparse.kron(solved_clean[number], solution).toarray()
intermediate.append(solution)
for _ in range(len(intermediate)-1):
s0 = sparse.csr_matrix(np.array(intermediate[0]))
s1 = sparse.csr_matrix(np.array(intermediate[1]))
intermediate[0] = sparse.kron(s0, s1).toarray()
intermediate.remove(intermediate[1])
if self.print_board == False:
result = Board(self.n)
for i in range(len(intermediate[0])):
for j in range(len(intermediate[0])):
if (intermediate[0][j][i] == 1):
result.add_queen(j)
return result