forked from 0vercl0k/stuffz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqueens_z3.py
137 lines (112 loc) · 4.2 KB
/
nqueens_z3.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# nqueens.py - Solve the nqueens problem thanks to recursivity & z3 (constraint programming)
# Copyright (C) 2012 Axel "0vercl0k" Souchet - http://www.twitter.com/0vercl0k
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from time import time
from pprint import pprint
from z3 import *
import sys
def good_move(i, j, solutions):
"""Is it an allowed move ?"""
for x, y in solutions:
# a queen can't be on the same column / line / diag
if x == i or y == j or abs(x - i) == abs(y - j):
return False
return True
def recurse_nqueens(n, ni, solution_list):
"""A queen can't be placed on the same diag/col/lin of an other"""
i, j = 0, 0
while i < n:
while good_move(i, j, solution_list) == False and j < n:
j += 1
if j != n:
if ni + 1 == n:
return [(i, j)]
r = recurse_nqueens(n, ni + 1, solution_list + [(i, j)])
if r != None:
return r + [(i, j)]
else:
j = 0
i += 1
return None
def nqueens(n):
"""Solves the problem of the nqueens thanks to recursivity/backtracking
and returns the coordinates of the queens"""
return recurse_nqueens(n, 0, [])
def abs_z3(a):
"""Get the absolute value of a Z3 variable"""
return If(a >= 0, a, -a)
def nqueens_constraint_programming(n):
"""Solves the problem of the nqueens thanks to constraint programming/z3"""
columns = [Int('c%d' % i) for i in range(n)]
lines = [Int('l%d' % i) for i in range(n)]
s = Solver()
for i in range(n):
s.add(columns[i] >= 0,columns[i] < n, lines[i] >= 0, lines[i] < n)
for i in range(n - 1):
for j in range(i + 1, n):
s.add(columns[i] != columns[j])
s.add(lines[i] != lines[j])
s.add(abs_z3(columns[i] - columns[j]) != abs_z3(lines[i] - lines[j]))
if s.check() == unsat:
raise Exception('Unsat bitch')
m = s.model()
return [(m[x].as_long(), m[y].as_long()) for x, y in zip(columns, lines)]
def nqueens_constraint_programming_opti(n):
"""Solves the problem of the nqueens thanks to constraint programming/z3 & a little trick"""
columns = [Int('c%d' % i) for i in range(n)]
# optimization trick: we set each column at a specific value, 0..N, it avoids a lot of useless constraint
# thx fireboot!
lines = range(n)
s = Solver()
for i in range(n):
# each queen must be in the chessboard's limits
s.add(columns[i] >= 0, columns[i] < n)
for i in range(n - 1):
for j in range(i + 1, n):
s.add(columns[i] != columns[j])
s.add(lines[i] != lines[j])
s.add(abs_z3(columns[i] - columns[j]) != abs(lines[i] - lines[j]))
if s.check() == unsat:
raise Exception('Unsat bitch')
m = s.model()
return [(m[x].as_long(), y) for x, y in zip(columns, lines)]
def display_solutions(s):
chessboard = [[0] * len(s) for i in range(len(s))]
for x,y in s:
chessboard[x][y] = 1
pprint(chessboard)
def main(argc, argv):
if argc != 2:
print 'Usage: nqueens <n>'
return 0
implementations = [
nqueens_constraint_programming_opti,
nqueens_constraint_programming,
nqueens
]
for implementation in implementations:
n = int(argv[1], 10)
t1 = time()
q = implementation(n)
t2 = time()
display_solutions(q)
print 'With %s: %fs, %r' % (implementation.__name__, t2 - t1, q)
return 1
if __name__ == '__main__':
sys.exit(main(len(sys.argv), sys.argv))