修正されたクイーン問題のブール式
Aug 20 2020
ここから、Nクイーン問題のブール式を見ました。
私の修正したNクイーンのルールはもっと単純です:
ap * pチェス盤の場合、N個のクイーンを次のように配置したいと思います。
- クイーンは隣接して配置され、行が最初に埋められます。
- p * pチェス盤のサイズは、N個のクイーンを保持できるようになるまで調整されます
たとえば、N = 17とすると、5 * 5のチェス盤が必要になり、配置は次のようになります。
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_*_*_*
*_*_*_*_*
問題は、この問題のブール式を考え出そうとしていることです。
回答
1 IoannisFilippidis Sep 10 2020 at 01:02
この問題は、Pythonパッケージhumanizeとを使用して解決できますomega。
"""Solve variable size square fitting."""
import humanize
from omega.symbolic.fol import Context
def pick_chessboard(q):
ctx = Context()
# compute size of chessboard
#
# picking a domain for `p`
# requires partially solving the
# problem of computing `p`
ctx.declare(p=(0, q))
s = '''
(p * p >= {q}) # chessboard fits the queens, and
/\ ((p - 1) * (p - 1) < {q}) # is the smallest such board
'''.format(q=q)
u = ctx.add_expr(s)
d, = list(ctx.pick_iter(u)) # assert unique solution
p = d['p']
print('chessboard size: {p}'.format(p=p))
# compute number of full rows
ctx.declare(x=(0, p))
s = 'x = {q} / {p}'.format(q=q, p=p) # integer division
u = ctx.add_expr(s)
d, = list(ctx.pick_iter(u))
r = d['x']
print('{r} rows are full'.format(r=r))
# compute number of queens on the last row
s = 'x = {q} % {p}'.format(q=q, p=p) # modulo
u = ctx.add_expr(s)
d, = list(ctx.pick_iter(u))
n = d['x']
k = r + 1
kword = humanize.ordinal(k)
print('{n} queens on the {kword} row'.format(
n=n, kword=kword))
if __name__ == '__main__':
q = 10 # number of queens
pick_chessboard(q)
二分決定図で乗算(および整数除算とモジュロ)を表すと、次のように、変数の数が指数関数的に複雑になります。 https://doi.org/10.1109/12.73590