Created
November 4, 2018 04:16
-
-
Save gyu-don/835755f2d2ed272651752afbe6f8fc77 to your computer and use it in GitHub Desktop.
wildqatで4x4数独
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'1?????2??3?????4'" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"problem_str = '''\n", | |
"1? ??\n", | |
"?? 2?\n", | |
"\n", | |
"?3 ??\n", | |
"?? ?4'''\n", | |
"\n", | |
"# Indices of array are:\n", | |
"# 0 1 | 2 3\n", | |
"# 4 5 | 6 7\n", | |
"# ------+-------\n", | |
"# 8 9 | 10 11\n", | |
"# 12 13 | 14 15\n", | |
"\n", | |
"# Remove whitespace and newline\n", | |
"import re\n", | |
"problem_str = re.sub(r'[^0-9?]', '', problem_str)\n", | |
"\n", | |
"problem_str" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"n = 4**3\n", | |
"grouping = [\n", | |
" # rows\n", | |
" [ 0, 1, 2, 3],\n", | |
" [ 4, 5, 6, 7],\n", | |
" [ 8, 9, 10, 11],\n", | |
" [12, 13, 14, 15],\n", | |
" # columns\n", | |
" [ 0, 4, 8, 12],\n", | |
" [ 1, 5, 9, 13],\n", | |
" [ 2, 6, 10, 14],\n", | |
" [ 3, 7, 11, 15],\n", | |
" # blocks\n", | |
" [ 0, 1, 4, 5],\n", | |
" [ 2, 3, 6, 7],\n", | |
" [ 8, 9, 12, 13],\n", | |
" [10, 11, 14, 15],\n", | |
"]\n", | |
"\n", | |
"# Bits arrangement:\n", | |
"# i = 0..15: i-th value is 1\n", | |
"# i = 16..31: (i-16)-th value is 2\n", | |
"# i = 32..47: (i-32)-th value is 3\n", | |
"# i = 48..63: (i-48)-th value is 4\n", | |
"\n", | |
"qubo = np.eye(n) * -1\n", | |
"\n", | |
"for g in grouping:\n", | |
" for i in range(4):\n", | |
" for j in range(3):\n", | |
" for k in range(j+1, 4):\n", | |
" qubo[i*16 + g[j], i*16 + g[k]] += 2\n", | |
"\n", | |
"for i in range(16):\n", | |
" for j in range(3):\n", | |
" for k in range(j+1, k):\n", | |
" qubo[i+j*16, i+k*16] += 2.5\n", | |
"\n", | |
"for i, v in enumerate(problem_str):\n", | |
" if v == '?':\n", | |
" continue\n", | |
" v = int(v) - 1\n", | |
" qubo[v*16 + i, v*16 + i] -= 20\n", | |
" for j in range(4):\n", | |
" qubo[j*16 + i, j*16 + i] += 4\n", | |
" for g in grouping:\n", | |
" if i not in g:\n", | |
" continue\n", | |
" for j in g:\n", | |
" qubo[v*16 + j, v*16 + j] += 3 + v*0.3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import wildqat as wq\n", | |
"a = wq.opt()\n", | |
"a.qubo = qubo.tolist()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"result = a.sa()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"12 43\n", | |
"34 21\n", | |
"\n", | |
"43 12\n", | |
"21 34\n" | |
] | |
} | |
], | |
"source": [ | |
"board = ['?'] * 16\n", | |
"for i,b in enumerate(result):\n", | |
" if b == 1:\n", | |
" if board[i%16] != '?':\n", | |
" print('!', end='')\n", | |
" board[i%16] = (i//16) + 1\n", | |
"\n", | |
"print('''{}{} {}{}\n", | |
"{}{} {}{}\n", | |
"\n", | |
"{}{} {}{}\n", | |
"{}{} {}{}'''.format(*board))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment