Skip to content

Instantly share code, notes, and snippets.

@PeterSels
Last active April 22, 2020 17:00
Show Gist options
  • Save PeterSels/965c5ce567daf870d4817e2d8afc1e5b to your computer and use it in GitHub Desktop.
Save PeterSels/965c5ce567daf870d4817e2d8afc1e5b to your computer and use it in GitHub Desktop.
ChessCompressConcurrently.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# ChessCompressConcurrently, the Kijk 1985 chess challenge revisited and made concurrent, for current times' sake",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# set this notebook to use a large part of the browser window width\nfrom IPython.core.display import HTML, display\ndisplay(HTML(\"<style>.container { width:80% !important; }</style>\"))",
"execution_count": 2,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": "<style>.container { width:80% !important; }</style>"
},
"metadata": {}
}
]
},
{
"metadata": {
"heading_collapsed": true
},
"cell_type": "markdown",
"source": "# Leveraging our Previous Notebook\n\n## Introduction\n\n### Previous Chess Compress Challenge from 1985\n\nThis notebook expands on the ChessCompress.ipynb one. In that notebook we solved the problem of trying to put as many chess pieces on a board without one threatening another.\nMore precisely, we want to maximise the score of the solution of a problem with the mentioned constraints, but where the score is calculated as follows.\nEach queen and rook get 1/8 points. Each bishop gets 1/14 points. Each king gets 1/16 points and a knight gets 1.32 points. Paws are not allowed in the game.\n\n### New Multi-Floor Chess Compress (Concurrent) Challenge from 2020\n\nWe present a new take on this problem here, where only the non-threatening constraints are to be respected between pieces of the same family,\nbut pieces of a different family are supposed to not threaten each other, say because they would 'live' on different floors.\n\nCode was developed in that other notebook ChessCompress.ipynb, but is repeated here so that it is self-contained and we can independently execute it. \nSo if you want to have more written text to understand the need or rationale for this code refer to this ChessCompress.ipynb notebook.\n\n### Code developed in the ChessCompress.ipynb Notebook"
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "%%sh\npip3 install python-chess",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "Requirement already satisfied: python-chess in /usr/local/lib/python3.7/site-packages (0.30.1)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "import chess\nboard = chess.Board()\nboard",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white pawn\" id=\"white-pawn\"><path d=\"M22 9c-2.21 0-4 1.79-4 4 0 .89.29 1.71.78 2.38-1.95 1.12-3.28 3.21-3.28 5.62 0 2.03.94 3.84 2.41 5.03-3 1.06-7.41 5.55-7.41 13.47h23c0-7.92-4.41-12.41-7.41-13.47 1.47-1.19 2.41-3 2.41-5.03 0-2.41-1.33-4.5-3.28-5.62.49-.67.78-1.49.78-2.38 0-2.21-1.79-4-4-4z\" fill=\"#fff\" stroke=\"#000\" stroke-linecap=\"round\" stroke-width=\"1.5\" /></g><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g><g class=\"black pawn\" id=\"black-pawn\"><path d=\"M22 9c-2.21 0-4 1.79-4 4 0 .89.29 1.71.78 2.38-1.95 1.12-3.28 3.21-3.28 5.62 0 2.03.94 3.84 2.41 5.03-3 1.06-7.41 5.55-7.41 13.47h23c0-7.92-4.41-12.41-7.41-13.47 1.47-1.19 2.41-3 2.41-5.03 0-2.41-1.33-4.5-3.28-5.62.49-.67.78-1.49.78-2.38 0-2.21-1.79-4-4-4z\" stroke=\"#000\" stroke-linecap=\"round\" stroke-width=\"1.5\" /></g><g class=\"black knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"black-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#ececec; stroke:#ececec;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#ececec; stroke:#ececec;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /><path d=\"M 24.55,10.4 L 24.1,11.85 L 24.6,12 C 27.75,13 30.25,14.49 32.5,18.75 C 34.75,23.01 35.75,29.06 35.25,39 L 35.2,39.5 L 37.45,39.5 L 37.5,39 C 38,28.94 36.62,22.15 34.25,17.66 C 31.88,13.17 28.46,11.02 25.06,10.5 L 24.55,10.4 z \" style=\"fill:#ececec; stroke:none;\" /></g><g class=\"black bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"black-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zm6-4c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" fill=\"#000\" stroke-linecap=\"butt\" /><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke=\"#fff\" stroke-linejoin=\"miter\" /></g><g class=\"black rook\" fill=\"#000\" fill-rule=\"evenodd\" id=\"black-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12.5 32l1.5-2.5h17l1.5 2.5h-20zM12 36v-4h21v4H12z\" stroke-linecap=\"butt\" /><path d=\"M14 29.5v-13h17v13H14z\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M14 16.5L11 14h23l-3 2.5H14zM11 14V9h4v2h5V9h5v2h5V9h4v5H11z\" stroke-linecap=\"butt\" /><path d=\"M12 35.5h21M13 31.5h19M14 29.5h17M14 16.5h17M11 14h23\" fill=\"none\" stroke=\"#fff\" stroke-linejoin=\"miter\" stroke-width=\"1\" /></g><g class=\"black queen\" fill=\"#000\" fill-rule=\"evenodd\" id=\"black-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#000\" stroke=\"none\"><circle cx=\"6\" cy=\"12\" r=\"2.75\" /><circle cx=\"14\" cy=\"9\" r=\"2.75\" /><circle cx=\"22.5\" cy=\"8\" r=\"2.75\" /><circle cx=\"31\" cy=\"9\" r=\"2.75\" /><circle cx=\"39\" cy=\"12\" r=\"2.75\" /></g><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2.5-12.5L31 25l-.3-14.1-5.2 13.6-3-14.5-3 14.5-5.2-13.6L14 25 6.5 13.5 9 26zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11 38.5a35 35 1 0 0 23 0\" fill=\"none\" stroke-linecap=\"butt\" /><path d=\"M11 29a35 35 1 0 1 23 0M12.5 31.5h20M11.5 34.5a35 35 1 0 0 22 0M10.5 37.5a35 35 1 0 0 24 0\" fill=\"none\" stroke=\"#fff\" /></g><g class=\"black king\" fill=\"none\" fill-rule=\"evenodd\" id=\"black-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#000\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#000\" /><path d=\"M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M32 29.5s8.5-4 6.03-9.65C34.15 14 25 18 22.5 24.5l.01 2.1-.01-2.1C20 18 9.906 14 6.997 19.85c-2.497 5.65 4.853 9 4.853 9M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" stroke=\"#fff\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-queen\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#black-rook\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#black-knight\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#black-bishop\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#black-queen\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#black-king\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#black-bishop\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#black-knight\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#black-rook\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "def write_board_to_svg(board, filename):\n import chess.svg\n boardsvg = chess.svg.board(board)\n f = open(filename, \"w\")\n f.write(boardsvg)\n f.close()\nwrite_board_to_svg(board, 'OpeningChessBoard.svg')",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "board = chess.Board(\"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR\")\nwrite_board_to_svg(board, 'FirstMoveBoard.svg')\nboard",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "Board('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white pawn\" id=\"white-pawn\"><path d=\"M22 9c-2.21 0-4 1.79-4 4 0 .89.29 1.71.78 2.38-1.95 1.12-3.28 3.21-3.28 5.62 0 2.03.94 3.84 2.41 5.03-3 1.06-7.41 5.55-7.41 13.47h23c0-7.92-4.41-12.41-7.41-13.47 1.47-1.19 2.41-3 2.41-5.03 0-2.41-1.33-4.5-3.28-5.62.49-.67.78-1.49.78-2.38 0-2.21-1.79-4-4-4z\" fill=\"#fff\" stroke=\"#000\" stroke-linecap=\"round\" stroke-width=\"1.5\" /></g><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g><g class=\"black pawn\" id=\"black-pawn\"><path d=\"M22 9c-2.21 0-4 1.79-4 4 0 .89.29 1.71.78 2.38-1.95 1.12-3.28 3.21-3.28 5.62 0 2.03.94 3.84 2.41 5.03-3 1.06-7.41 5.55-7.41 13.47h23c0-7.92-4.41-12.41-7.41-13.47 1.47-1.19 2.41-3 2.41-5.03 0-2.41-1.33-4.5-3.28-5.62.49-.67.78-1.49.78-2.38 0-2.21-1.79-4-4-4z\" stroke=\"#000\" stroke-linecap=\"round\" stroke-width=\"1.5\" /></g><g class=\"black knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"black-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#ececec; stroke:#ececec;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#ececec; stroke:#ececec;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /><path d=\"M 24.55,10.4 L 24.1,11.85 L 24.6,12 C 27.75,13 30.25,14.49 32.5,18.75 C 34.75,23.01 35.75,29.06 35.25,39 L 35.2,39.5 L 37.45,39.5 L 37.5,39 C 38,28.94 36.62,22.15 34.25,17.66 C 31.88,13.17 28.46,11.02 25.06,10.5 L 24.55,10.4 z \" style=\"fill:#ececec; stroke:none;\" /></g><g class=\"black bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"black-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zm6-4c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" fill=\"#000\" stroke-linecap=\"butt\" /><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke=\"#fff\" stroke-linejoin=\"miter\" /></g><g class=\"black rook\" fill=\"#000\" fill-rule=\"evenodd\" id=\"black-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12.5 32l1.5-2.5h17l1.5 2.5h-20zM12 36v-4h21v4H12z\" stroke-linecap=\"butt\" /><path d=\"M14 29.5v-13h17v13H14z\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M14 16.5L11 14h23l-3 2.5H14zM11 14V9h4v2h5V9h5v2h5V9h4v5H11z\" stroke-linecap=\"butt\" /><path d=\"M12 35.5h21M13 31.5h19M14 29.5h17M14 16.5h17M11 14h23\" fill=\"none\" stroke=\"#fff\" stroke-linejoin=\"miter\" stroke-width=\"1\" /></g><g class=\"black queen\" fill=\"#000\" fill-rule=\"evenodd\" id=\"black-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#000\" stroke=\"none\"><circle cx=\"6\" cy=\"12\" r=\"2.75\" /><circle cx=\"14\" cy=\"9\" r=\"2.75\" /><circle cx=\"22.5\" cy=\"8\" r=\"2.75\" /><circle cx=\"31\" cy=\"9\" r=\"2.75\" /><circle cx=\"39\" cy=\"12\" r=\"2.75\" /></g><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2.5-12.5L31 25l-.3-14.1-5.2 13.6-3-14.5-3 14.5-5.2-13.6L14 25 6.5 13.5 9 26zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11 38.5a35 35 1 0 0 23 0\" fill=\"none\" stroke-linecap=\"butt\" /><path d=\"M11 29a35 35 1 0 1 23 0M12.5 31.5h20M11.5 34.5a35 35 1 0 0 22 0M10.5 37.5a35 35 1 0 0 24 0\" fill=\"none\" stroke=\"#fff\" /></g><g class=\"black king\" fill=\"none\" fill-rule=\"evenodd\" id=\"black-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#000\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#000\" /><path d=\"M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M32 29.5s8.5-4 6.03-9.65C34.15 14 25 18 22.5 24.5l.01 2.1-.01-2.1C20 18 9.906 14 6.997 19.85c-2.497 5.65 4.853 9 4.853 9M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" stroke=\"#fff\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-queen\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><use transform=\"translate(200, 200)\" xlink:href=\"#white-pawn\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#black-pawn\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#black-rook\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#black-knight\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#black-bishop\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#black-queen\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#black-king\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#black-bishop\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#black-knight\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#black-rook\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "from gurobipy import *\nimport itertools",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "class ChessCompress():\n \n def __init__(self, dim, piece_to_weight_dict):\n self.dim = dim\n self.m = Model()\n self.board_vars = {} # dictionary from piece type letter \n # to (row, col) tuple to binary solver var. \n self.board_vals = {} # dictionary from piece type letter \n # to (row, col) tuple to binary result val.\n self.count = {} # dictionary from piece type letter \n # to number of piece fittable\n self.add_variables(piece_to_weight_dict)\n \n def derive_parameters(self, dim):\n n_rows = dim\n n_cols = dim\n rows = list(range(n_rows))\n cols = list(range(n_cols))\n return n_rows, n_cols, rows, cols\n\n # define the binary core variables\n def add_variables(self, piece_to_weight_dict):\n n_rows, n_cols, rows, cols = self.derive_parameters(self.dim)\n for piece_letter in piece_to_weight_dict:\n piece_weight = piece_to_weight_dict[piece_letter]\n pieces = {(r, c): -piece_weight \\\n for r, c in itertools.product(rows, cols)}\n self.board_vars[piece_letter] = \\\n self.m.addVars(pieces.keys(), obj=pieces, \n vtype=GRB.BINARY, name=piece_letter)\n \n def add_long_range_piece_constraints(\n self, from_piece_letter, to_piece_letter, \n piece_weight=1.0, user_cuts=True):\n \n verbose = 0\n \n from_piece_upper_letter = from_piece_letter.upper()\n to_piece_upper_letter = to_piece_letter.upper()\n\n n_rows, n_cols, rows, cols = self.derive_parameters(self.dim)\n\n # These constraints are sufficient to define \n # the allowed search space.\n vertical_horizontal_dr_list = [ -1, 0, +1, 0] \n vertical_horizontal_dc_list = [ 0, +1, 0, -1]\n\n diagonal_dr_list = [ -1, +1, +1, -1]\n diagonal_dc_list = [ +1, +1, -1, -1]\n\n dr_list = []\n dc_list = []\n if from_piece_letter in ['q', 'r']:\n dr_list.extend(vertical_horizontal_dr_list)\n dc_list.extend(vertical_horizontal_dc_list)\n if from_piece_letter in ['q', 'b']:\n dr_list.extend(diagonal_dr_list) \n dc_list.extend(diagonal_dc_list)\n if verbose > 0:\n print(dr_list)\n print(dc_list)\n \n for r in rows:\n for c in cols:\n # dr = delta row, cr is delta column\n for dr, dc in list(zip(dr_list, dc_list)):\n r_, c_ = r+dr, c+dc\n # while (d+dr, c+dc) square falls within board\n while (r_ <= self.dim-1 and r_ >= 0) \\\n and (c_ <= self.dim-1 and c_ >= 0): \n constr_name = \\\n 'noQBThreat_r{:d}_c{:d}_dr{:d}_dc{:d}'.\\\n format(r, c, dr, dc)\n self.m.addConstr(\\\n self.board_vars[from_piece_letter][r,c] +\\\n self.board_vars[to_piece_letter][r_,c_] <= 1, \n constr_name)\n r_, c_ = r_+dr, c_+dc\n \n # The next constraints can be optionally added.\n # They are 'user cuts' intended to speed up the solving process.\n #They do not restrict the search\n # space compared to the constraint already defined above.\n # The above constraint work on a pair of binary variables,\n # while the below 'user cut' ones work for all binary variables\n # in a full row, column or diagonal at once.\n if (from_piece_letter == to_piece_letter) and user_cuts:\n # row-wise constraints\n if from_piece_letter in ['q', 'r']:\n for r in rows:\n constr_name = \\\n 'atMostOne' + from_piece_upper_letter +\\\n 'InRow_r{:d}'.format(r)\n self.m.addConstr(\n quicksum(self.board_vars[from_piece_letter][r,c] \\\n for c in cols) <= 1, \n constr_name)\n\n # column-wise constraints\n for c in cols:\n constr_name = 'atMostOne' + \\\n from_piece_upper_letter + \\\n 'InCol_c{:d}'.format(c)\n self.m.addConstr(\n quicksum(self.board_vars[from_piece_letter][r,c] \\\n for r in rows) <= 1, \n constr_name)\n\n # all diagonal constraints:\n if from_piece_letter in ['q', 'b']:\n for dr, dc in [(-1, +1), (+1, +1)]:\n if (dr, dc) == (-1, +1):\n # go over diagonals that go RightUp\n # start (r,c) for these diagonals are \n # left border of board and \n start_r_list = rows[1:]\n start_c_list = [0] * (self.dim-1)\n # lower border of board\n start_r_list.extend([self.dim-1] * (self.dim-2))\n start_c_list.extend(cols[1:][:-1])\n constr_name = 'atMostOne' + \\\n from_piece_upper_letter + 'InRightUpDiag'\n else:\n # go over diagonals that go RightD(ow)n\n # start (r,c) for these diagonals are \n # top border of board and\n start_r_list = [0] * (self.dim-1)\n start_c_list = cols[:-1] # last col dropped \n # since would result in a 1 q var \n # constraint <=1 which is redundant.\n # left border of board\n start_r_list.extend(rows[1:][:-1])\n start_c_list.extend([0] * (self.dim-2))\n constr_name = 'atMostOne' + \\\n from_piece_upper_letter + 'InRightDnDiag'\n\n if verbose > 0:\n print(constr_name + ':')\n print(' for start_r range: [' + ','.\\\n join([str(i) for i in start_r_list]) + ']')\n print(' for start_c range: [' + ','.\\\n join([str(i) for i in start_c_list]) + ']')\n assert len(start_r_list) == len(start_c_list)\n for start_r, start_c in list(zip(start_r_list, \n start_c_list)): \n r, c = start_r, start_c\n expr = LinExpr(0)\n while (c >= 0 and c <= self.dim-1) and\\\n (r >= 0 and r <= self.dim-1):\n if verbose > 0:\n print(r,c)\n expr += self.board_vars[from_piece_letter][r,c]\n r += dr\n c += dc\n if verbose > 0:\n print('---')\n self.m.addConstr(expr <= 1, \n constr_name + \\\n '_rs{:d}cs{:d}'.format(r,c))\n\n def set_solver_output(self, bool):\n self.m.setParam('OutputFlag', bool)\n \n def set_time_limit(self, time_limit_in_seconds):\n self.m.setParam('TimeLimit', time_limit_in_seconds) \n \n def solve_model(self):\n # minimise the objective function the model \n # Since coefficients in the objective are negative this will try to \n # maximize the number of queens in the board.\n self.m.optimize()\n\n # retrieve solution\n for piece_letter in self.board_vars:\n self.board_vals[piece_letter] = \\\n self.m.getAttr('x', self.board_vars[piece_letter])\n # the board_vals represent the board solutions \n # without any solver specific encoding\n self.count[piece_letter] = 0\n for r,c in self.board_vals[piece_letter]:\n if self.board_vals[piece_letter][r,c]==1:\n self.count[piece_letter] += 1 \n \n n_different_piece_letters = len(self.board_vars.keys()) \n if n_different_piece_letters == 1:\n assert self.count[piece_letter] == \\\n -int(self.m.getObjective().getValue())\n \n def get_objective_value(self):\n return self.m.getObjective().getValue()\n\n def get_mip_gap(self):\n return self.m.getAttr('MIPGap')\n \n def get_letter(self, r, c):\n vals = self.board_vals\n found_letter = ' ' # means nothing found yet, so empty square\n for letter in vals:\n val = int(vals[letter][(r,c)])\n if val == 1:\n assert found_letter == ' ' # can only find one letter thanks to \n # add_at_most_a_piece_per_square_constraints being called before\n found_letter = letter\n # keep checking that no other letter is found as well\n return found_letter\n \n def encode_to_fen_board_string(self, capitals_for_white=False):\n vals = self.board_vals\n n_rows, n_cols, rows, cols = self.derive_parameters(self.dim)\n s = ''\n space = ' '\n for r in rows:\n for c in cols: # we assume these couples lexically sorted\n letter = self.get_letter(r,c)\n bw_letter = letter.upper() if capitals_for_white else letter\n if self.dim == 8: # then we keep to the standard fen format \n # since we can use chess.Board(on it) later\n if letter != ' ':\n s += bw_letter\n else:\n assert letter == ' '\n if (len(s) == 0):\n s = '1'\n elif s[-1] in ['1','2','3','4','5','6','7','8','9']:\n s = s[:-1] + str(int(s[-1])+1)\n else: # increment last digit of s\n s += '1'\n\n if c == self.dim-1:\n if r != self.dim-1:\n s += '/'\n else: # The additional 'space' here is \n # there to end up with square boards\n if letter != ' ':\n s += bw_letter + space\n else:\n assert letter == ' '\n s += '.' + space\n \n if c == self.dim-1:\n if r != self.dim-1:\n s += '\\n'\n\n return s",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "## Queens only Problem"
},
{
"metadata": {
"hidden": true,
"trusted": true
},
"cell_type": "code",
"source": "cc_queens = ChessCompress(8, {'q' : 1.0})\ncc_queens.add_long_range_piece_constraints('q', 'q')\ncc_queens.set_solver_output(True)\ncc_queens.solve_model()\nprint('I could maximally fit {:d} queens on the chess board.'.\\\n format(cc_queens.count['q']))\nn_queens = cc_queens.count['q']\n\nboard_string = \\\n cc_queens.encode_to_fen_board_string(capitals_for_white=True)\nprint(board_string)\nboard8Q = chess.Board(board_string)\nwrite_board_to_svg(board8Q, 'EightQueensBoard.svg')\nboard8Q",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "Using license file /Library/gurobi901/gurobi.lic\nParameter OutputFlag unchanged\n Value: 1 Min: 0 Max: 1 Default: 1\nGurobi Optimizer version 9.0.1 build v9.0.1rc0 (mac64)\nOptimize a model with 1498 rows, 64 columns and 3164 nonzeros\nModel fingerprint: 0x5e6db8b3\nVariable types: 0 continuous, 64 integer (64 binary)\nCoefficient statistics:\n Matrix range [1e+00, 1e+00]\n Objective range [1e+00, 1e+00]\n Bounds range [1e+00, 1e+00]\n RHS range [1e+00, 1e+00]\nFound heuristic solution: objective -5.0000000\nPresolve removed 1456 rows and 0 columns\nPresolve time: 0.00s\nPresolved: 42 rows, 64 columns, 270 nonzeros\nVariable types: 0 continuous, 64 integer (64 binary)\n\nRoot relaxation: objective -8.000000e+00, 40 iterations, 0.00 seconds\n\n Nodes | Current Node | Objective Bounds | Work\n Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n\nH 0 0 -8.0000000 -64.00000 700% - 0s\n 0 0 - 0 -8.00000 -8.00000 0.00% - 0s\n\nExplored 0 nodes (62 simplex iterations) in 0.02 seconds\nThread count was 16 (of 16 available processors)\n\nSolution count 2: -8 -5 \nNo other solutions better than -8\n\nOptimal solution found (tolerance 1.00e-04)\nBest objective -8.000000000000e+00, best bound -8.000000000000e+00, gap 0.0000%\nI could maximally fit 8 queens on the chess board.\n3Q4/5Q2/7Q/2Q5/Q7/6Q1/4Q3/1Q6\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "Board('3Q4/5Q2/7Q/2Q5/Q7/6Q1/4Q3/1Q6 w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-queen\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-queen\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><use transform=\"translate(290, 245)\" xlink:href=\"#white-queen\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-queen\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><use transform=\"translate(110, 155)\" xlink:href=\"#white-queen\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-queen\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-queen\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#white-queen\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "## Rooks only Problem"
},
{
"metadata": {
"hidden": true,
"trusted": true
},
"cell_type": "code",
"source": "cc_rooks = ChessCompress(8, {'r':1.0})\ncc_rooks.add_long_range_piece_constraints('r', 'r')\ncc_rooks.m.setParam('OutputFlag', False)\ncc_rooks.solve_model()\nprint('I could maximally fit {:d} rooks on the chess board.'.\\\n format(cc_rooks.count['r']))\nn_rooks = cc_rooks.count['r']\n\nboard_string = \\\n cc_rooks.encode_to_fen_board_string(capitals_for_white=True)\nprint(board_string)\nboard8R = chess.Board(board_string)\n\nwrite_board_to_svg(board8R, 'EightRooksBoard.svg')\nboard8R",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "I could maximally fit 8 rooks on the chess board.\nR7/1R6/2R5/3R4/4R3/5R2/6R1/7R\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "Board('R7/1R6/2R5/3R4/4R3/5R2/6R1/7R w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-rook\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-rook\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><use transform=\"translate(200, 200)\" xlink:href=\"#white-rook\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-rook\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><use transform=\"translate(110, 110)\" xlink:href=\"#white-rook\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#white-rook\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-rook\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "## Bishops only Problem"
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "cc_bishops = ChessCompress(8, {'b': 1.0})\ncc_bishops.add_long_range_piece_constraints('b','b')\ncc_bishops.m.setParam('OutputFlag', False)\ncc_bishops.solve_model()\nprint('I could maximally fit {:d} bishops on the chess board.'.\\\n format(cc_bishops.count['b']))\nn_bishops = cc_bishops.count['b']\n\nboard_string = \\\n cc_bishops.encode_to_fen_board_string(\\\n capitals_for_white=True)\nprint(board_string)\nboard16B = chess.Board(board_string)\n\nwrite_board_to_svg(board16B, 'SixteenBishopsBoard.svg')\nboard16B",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "I could maximally fit 14 bishops on the chess board.\nBB2B1B1/8/B6B/B7/7B/B6B/8/BB1B2B1\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "Board('BB2B1B1/8/B6B/B7/7B/B6B/8/BB1B2B1 w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><use transform=\"translate(20, 155)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "## Kings only Problem"
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "class ChessCompress(ChessCompress): # extend the class\n \n def add_kings_constraints(self, to_piece_letter, \n piece_weight=1.0):\n n_rows, n_cols, rows, cols = self.derive_parameters(self.dim)\n kings = {(r, c): -piece_weight for r, \n c in itertools.product(rows, cols)}\n\n n_rows, n_cols, rows, cols = \\\n self.derive_parameters(self.dim)\n kings = {(r, c): -piece_weight for r, \n c in itertools.product(rows, cols)}\n\n for r in rows:\n for c in cols:\n # dr = delta row, cr is delta column\n dr_list = [ -1, 0, +1, +1, +1, 0, -1, -1]\n dc_list = [ +1, +1, +1, 0, -1, -1, -1, 0]\n for dr, dc in list(zip(dr_list, dc_list)):\n if (r+dr <= self.dim-1 and r+dr >= 0) \\\n and (c+dc <= self.dim-1 and c+dc >= 0): \n # if (d+dr, c+dc) square falls within board\n constr_name = \\\n 'noKingsThreat_r{:d}_c{:d}_dr{:d}_dc{:d}'.\\\n format(r, c, dr, dc)\n self.m.addConstr(\n self.board_vars['k'][r,c] + \n self.board_vars[to_piece_letter][r+dr,c+dc] <= 1, \n constr_name)\n\ncc_kings = ChessCompress(8, {'k':1.0})\ncc_kings.add_kings_constraints('k')\ncc_kings.m.setParam('OutputFlag', False)\ncc_kings.solve_model()\nprint('I could maximally fit {:d} kings on the chess board.'.\n format(cc_kings.count['k']))\nboard_string = \\\n cc_kings.encode_to_fen_board_string(capitals_for_white=True)\nn_kings = cc_kings.count['k']\n\nprint(board_string)\nboard16K = chess.Board(board_string)\n\nwrite_board_to_svg(board16K, 'SixteenKingsBoard.svg')\nboard16K",
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": "I could maximally fit 16 kings on the chess board.\nK1K2K1K/8/K1K2K1K/8/8/K1K2K1K/8/K1K2K1K\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "Board('K1K2K1K/8/K1K2K1K/8/8/K1K2K1K/8/K1K2K1K w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><use transform=\"translate(110, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-king\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><use transform=\"translate(110, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><use transform=\"translate(245, 110)\" xlink:href=\"#white-king\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#white-king\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#white-king\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "## Knights only Problem"
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "class ChessCompress(ChessCompress): # extend the class\n \n def add_knights_constraints(self, to_piece_letter, piece_weight=1.0):\n verbose = 0\n\n n_rows, n_cols, rows, cols = self.derive_parameters(self.dim)\n kings = {(r, c): -piece_weight for r, c in itertools.product(rows, cols)}\n\n # define the basic Constraints\n\n # row-wise constraints\n for r in rows:\n for c in cols:\n # dr = delta row, cr is delta column\n dr_list = [-2, -1, +1, +2, +2, +1, -1, -2]\n dc_list = [+1, +2, +2, +1, -1, -2, -2, -1]\n for dr, dc in list(zip(dr_list, dc_list)):\n if (r+dr <= self.dim-1 and r+dr >= 0) \\\n and (c+dc <= self.dim-1 and c+dc >= 0): \n # if (d+dr, c+dc) square falls within board\n constr_name = \\\n 'noTwoNWithinSingleJump_r{:d}_c{:d}_dr{:d}_dc{:d}'.\\\n format(r, c, dr, dc)\n self.m.addConstr(\n self.board_vars['n'][r,c] + \n self.board_vars[to_piece_letter][r+dr,c+dc] <= 1, \n constr_name)\n\ncc_knights = ChessCompress(8, {'n':1.0})\ncc_knights.add_knights_constraints('n')\ncc_knights.m.setParam('OutputFlag', False)\ncc_knights.solve_model()\nprint('I could maximally fit {:d} knights on the chess board.'.\n format(cc_knights.count['n']))\nprint('The board setup scores {:f}.'.format(-cc_knights.get_objective_value()))\n\nboard_string = cc_knights.encode_to_fen_board_string(capitals_for_white=True)\nn_knights = cc_knights.count['n']\nprint(board_string)\nboard32N = chess.Board(board_string)\n\nwrite_board_to_svg(board32N, 'ThirtyTwoKnightsBoard.svg')\nboard32N",
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": "I could maximally fit 32 knights on the chess board.\nThe board setup scores 32.000000.\nN1N1N1N1/1N1N1N1N/N1N1N1N1/1N1N1N1N/N1N1N1N1/1N1N1N1N/N1N1N1N1/1N1N1N1N\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "Board('N1N1N1N1/1N1N1N1N/N1N1N1N1/1N1N1N1N/N1N1N1N1/1N1N1N1N/N1N1N1N1/1N1N1N1N w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><use transform=\"translate(200, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><use transform=\"translate(110, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "So 32 knights is the maximum fittable on a board, so they get the weight of 1/32 in the final problem. They are on (all) the squares of one color only, white here, but another solution would be for them on all the black squares. \n\nLet's summarize by writing a function that calculates all the weights for a given board dimension."
},
{
"metadata": {
"trusted": true,
"hidden": true
},
"cell_type": "code",
"source": "def calculate_piece_weights(dim, user_cuts=True, \n capitals_for_white=True, solver_output=False):\n weights = {}\n boards = {}\n \n cc = ChessCompress(dim, {'k':1.0})\n cc.add_kings_constraints('k')\n cc.set_solver_output(solver_output)\n cc.solve_model()\n \n weights['k'] = 1/(-cc.get_objective_value())\n s = cc.encode_to_fen_board_string(capitals_for_white)\n boards['k'] = chess.Board(s) if dim == 8 else HTML(s)\n for piece_letter in ['q', 'b', 'r']:\n cc = ChessCompress(dim, {piece_letter:1.0})\n cc.add_long_range_piece_constraints(piece_letter, piece_letter, \n {piece_letter:1.0}, user_cuts)\n cc.set_solver_output(solver_output)\n cc.solve_model()\n weights[piece_letter] = 1/(-cc.get_objective_value())\n s = cc.encode_to_fen_board_string(capitals_for_white)\n boards[piece_letter] = chess.Board(s) if dim == 8 else HTML(s)\n\n cc = ChessCompress(dim, {'n':1.0})\n cc.add_knights_constraints('n')\n cc.set_solver_output(solver_output)\n cc.solve_model()\n weights['n'] = 1/(-cc.get_objective_value())\n s = cc.encode_to_fen_board_string(capitals_for_white)\n boards['n'] = chess.Board(s) if dim == 8 else HTML(s)\n \n return weights, boards\n\nweights_dim8, boards = calculate_piece_weights(8, user_cuts=True, \n capitals_for_white=False)\nfor key, val in weights_dim8.items():\n print(key, int(1/val))\nprint(weights_dim8)",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "k 16\nq 8\nb 14\nr 8\nn 32\n{'k': 0.0625, 'q': 0.125, 'b': 0.07142857142857142, 'r': 0.125, 'n': 0.03125}\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# The 2020 Problem, a Manual Solving Attempt\n\nIf you think it's fun to first try and aim to come up with some as good as possible solutions (optimal or not) without algorithms, \nhere we go. Combining the horses only solution with the bishops only solution, we get this solution."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "fen = \"NBN1N1N1/1N1N1N1N/N1N1N1NB/1N1N1N1N/N1N1N1NB/1N1N1N1N/N1N1N1N1/BN1N1NBN\"\nboard = chess.Board(fen)\nwrite_board_to_svg(board, 'ChessCompressConcurrentPeter1Board.svg')\nboard",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "Board('NBN1N1N1/1N1N1N1N/N1N1N1NB/1N1N1N1N/N1N1N1NB/1N1N1N1N/N1N1N1N1/BN1N1NBN w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><use transform=\"translate(200, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><use transform=\"translate(110, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "It's a bit exhausting, error prone and boring for a human to calculate the score, but I imagine that for a computer, it must be fun. :)\nSo let's write a function to do that based on the fen string and the piece weights as the two function inputs."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def calculate_score(fen, weights_dim8):\n score = 0.0\n for c in fen:\n c = c.lower()\n if c in weights_dim8.keys():\n score += weights_dim8[c]\n \n return score\n\ncalculate_score(fen, weights_dim8)",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "1.357142857142857"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# The 2020 Problem, Solved to Optimality by Integer Programming\n\n## Teams, assume position, all Piece Types get Equal Score 1\n\nHmm, shall we try to piece things together? \nSurely we still have to add a constraint saying that any square can contain at most one chess piece.\n\n### Plain Vanilla Model, no user cuts"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "class ChessCompress(ChessCompress): # extend the class\n\n def add_at_most_a_piece_per_square_constraints(self):\n n_rows, n_cols, rows, cols = self.derive_parameters(self.dim)\n for r in rows:\n for c in cols:\n constr_name = \\\n 'atMostOnePiecePerSquare_r{:d}_c{:d}'.format(r, c)\n self.m.addConstr(\n quicksum(self.board_vars[letter][r,c] \n for letter in self.board_vars) <= 1, \n constr_name)",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def max_pieces_on_board_forbidding_threat_within_own_type_only(\n piece_weights, user_cuts=False, solver_output=False):\n \n cc_all = ChessCompress(8, piece_weights)\n for piece_letter in ['q', 'b', 'r']:\n cc_all.add_long_range_piece_constraints(\n piece_letter, piece_letter,\n piece_weights[piece_letter], user_cuts)\n cc_all.add_kings_constraints('k')\n cc_all.add_knights_constraints('n')\n cc_all.add_at_most_a_piece_per_square_constraints()\n cc_all.set_solver_output(solver_output)\n cc_all.solve_model()\n solver_time_in_seconds = cc_all.m.getAttr('Runtime')\n print('Needed {:f} seconds to find optimal solution.'.format(\n solver_time_in_seconds))\n print('I could maximally fit {:d} queens, {:d} kings, {:d} bishops,'.\n format(cc_all.count['q'], cc_all.count['k'], cc_all.count['b']))\n print(' {:d} knights and {:d} rooks together on the chess board.'.\n format(cc_all.count['n'], cc_all.count['r']))\n\n print('The board setup scores {:f}.'.format(-cc_all.get_objective_value()))\n\n board_string = cc_all.encode_to_fen_board_string(capitals_for_white=True)\n print(board_string)\n board = chess.Board(board_string)\n return board\n\n# For now we assume here that each piece gets a score of 1.\nmax_pieces_board = max_pieces_on_board_forbidding_threat_within_own_type_only(\n piece_weights={'q':1, 'k':1, 'b':1, 'r':1, 'n':1}, \n user_cuts=False)\n\n\nwrite_board_to_svg(max_pieces_board, 'MaxPiecesBoard.svg')\nmax_pieces_board",
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"text": "Needed 89.915489 seconds to find optimal solution.\nI could maximally fit 5 queens, 15 kings, 10 bishops,\n 26 knights and 7 rooks together on the chess board.\nThe board setup scores 63.000000.\nNKBKNKNR/RNBNQNBB/NQNKNKNK/KBRN1QBN/NBNRNKNK/KBKNRBQN/NRNQNBNK/KNKNKNRN\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "Board('NKBKNKNR/RNBNQNBB/NQNKNKNK/KBRN1QBN/NBNRNKNK/KBKNRBQN/NRNQNBNK/KNKNKNRN w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-rook\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-queen\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-king\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><use transform=\"translate(110, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><use transform=\"translate(200, 245)\" xlink:href=\"#white-rook\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><use transform=\"translate(290, 245)\" xlink:href=\"#white-queen\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><use transform=\"translate(65, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><use transform=\"translate(155, 200)\" xlink:href=\"#white-rook\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><use transform=\"translate(200, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><use transform=\"translate(245, 200)\" xlink:href=\"#white-king\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-king\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><use transform=\"translate(20, 155)\" xlink:href=\"#white-king\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><use transform=\"translate(110, 155)\" xlink:href=\"#white-rook\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-queen\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><use transform=\"translate(290, 155)\" xlink:href=\"#white-bishop\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><use transform=\"translate(65, 110)\" xlink:href=\"#white-queen\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><use transform=\"translate(110, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><use transform=\"translate(155, 110)\" xlink:href=\"#white-king\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><use transform=\"translate(245, 110)\" xlink:href=\"#white-king\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#white-rook\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#white-bishop\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#white-queen\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#white-bishop\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-bishop\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-king\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#white-king\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#white-king\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#white-rook\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now that's surprising: 63 pieces could be fit but nothing in the last square. Verified with our score calculating function, the score of this solution is indeed 63."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "equal_one_weights = {'k': 1.0, 'q': 1.0, 'b': 1.0, 'r': 1.0, 'n': 1.0}\ncalculate_score(\"NKBKNKNR/RNBNQNBB/NQNKNKNK/KBRN1QBN/NBNRNKNK/KBKNRBQN/NRNQNBNK/KNKNKNRN\", equal_one_weights)",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "63.0"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Improving Time to Optimal Solution by Adding User Cuts\n\nThe above problem takes 88 seconds to solve on my laptop. Maybe this time can be improved with adding the above mentioned user cuts. Let's try."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "max_pieces_user_cuts_board = max_pieces_on_board_forbidding_threat_within_own_type_only(\n piece_weights={'q':1, 'k':1, 'b':1, 'r':1, 'n':1}, \n user_cuts=True)\n\nwrite_board_to_svg(max_pieces_user_cuts_board, 'MaxPiecesUserCutsBoard.svg')\nmax_pieces_user_cuts_board",
"execution_count": 20,
"outputs": [
{
"output_type": "stream",
"text": "Needed 7.014822 seconds to find optimal solution.\nI could maximally fit 5 queens, 15 kings, 8 bishops,\n 28 knights and 7 rooks together on the chess board.\nThe board setup scores 63.000000.\nKNKNKNRN/NRNBNQNK/KNKNKNBQ/NQN1NRNK/KNKNRNQN/BBQRNKNK/KNRBBNBN/NNNKNKBR\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "Board('KNKNKNRN/NRNBNQNK/KNKNKNBQ/NQN1NRNK/KNKNRNQN/BBQRNKNK/KNRBBNBN/NNNKNKBR w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-knight\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-king\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-rook\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><use transform=\"translate(110, 245)\" xlink:href=\"#white-queen\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-rook\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><use transform=\"translate(200, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-king\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><use transform=\"translate(290, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-king\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><use transform=\"translate(65, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-king\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><use transform=\"translate(155, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><use transform=\"translate(200, 200)\" xlink:href=\"#white-rook\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><use transform=\"translate(245, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-queen\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><use transform=\"translate(20, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-queen\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><use transform=\"translate(110, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><use transform=\"translate(200, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-rook\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><use transform=\"translate(290, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-king\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><use transform=\"translate(65, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><use transform=\"translate(110, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><use transform=\"translate(155, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><use transform=\"translate(245, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-queen\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><use transform=\"translate(65, 65)\" xlink:href=\"#white-rook\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-queen\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-rook\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#white-knight\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Yes! We only need about 7 seconds when including the user cuts. So this shows how big an effect this can have. In fact user cuts in business questions can come from business logic. It is something the solver cannot automatically identify, but the human modeler can add by having some higher level insight.\n\nThe solution has of course the same 'quality', objective function value -63 as the previous one without user cuts, but it need not be the same, and indeed here it isn't. Such is the nature of user cuts.\nLet's double check our score just to be sure."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "calculate_score(\"KNKNKNRN/NRNBNQNK/KNKNKNBQ/NQN1NRNK/KNKNRNQN/BBQRNKNK/KNRBBNBN/NNNKNKBR\", equal_one_weights)",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "63.0"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Okay, that's correct, since the score should equal the number of pieces on the board here.\n\n## Adding the Correct Weights per Chess Piece\n\nFor now, we have weighted pieces with the same weight 1. Maybe if we weigh them differently, we get a different solution or even structure. Let's try."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Plain Vanilla Model, Without User Cuts\n\nLet's try again first without user cuts, to see how long it takes Gurobi."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "max_weighted_pieces_board = max_pieces_on_board_forbidding_threat_within_own_type_only(\n piece_weights=weights_dim8, \n user_cuts=False)\n\nwrite_board_to_svg(max_weighted_pieces_board, 'MaxWeightedPiecesBoard.svg')\nmax_weighted_pieces_board",
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"text": "Needed 52.329156 seconds to find optimal solution.\nI could maximally fit 8 queens, 16 kings, 13 bishops,\n 16 knights and 8 rooks together on the chess board.\nThe board setup scores 4.428571.\nKBBQKBRB/N1KRNQNK/KN1NRKBQ/BQNKNRNK/BKRN1NQB/QRNKNKNK/BNQNBNBR/RKBKQKBK\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "Board('KBBQKBRB/N1KRNQNK/KN1NRKBQ/BQNKNRNK/BKRN1NQB/QRNKNKNK/BNQNBNBR/RKBKQKBK w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-queen\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-queen\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-rook\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-queen\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-rook\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><use transform=\"translate(110, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-king\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><use transform=\"translate(200, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-king\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><use transform=\"translate(290, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><use transform=\"translate(65, 200)\" xlink:href=\"#white-king\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-rook\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><use transform=\"translate(155, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><use transform=\"translate(245, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-queen\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><use transform=\"translate(20, 155)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-queen\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><use transform=\"translate(110, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-king\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><use transform=\"translate(200, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-rook\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><use transform=\"translate(290, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-king\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-king\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><use transform=\"translate(65, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><use transform=\"translate(155, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-rook\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><use transform=\"translate(245, 110)\" xlink:href=\"#white-king\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-queen\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-rook\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-queen\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#white-queen\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-rook\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#white-bishop\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Ha, by introduction of these different weights we get that the optimal solution does not contain 63 pieces anymore but only 61. The solver cannot improve the solution by putting any piece on the leftover 3 squares. Double checking with weights all equal to 1 gives "
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "optimal_2020_fen = \"KBBQKBRB/N1KRNQNK/KN1NRKBQ/BQNKNRNK/BKRN1NQB/QRNKNKNK/BNQNBNBR/RKBKQKBK\"\ncalculate_score(optimal_2020_fen, equal_one_weights)",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "61.0"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "indeed.\n\n\nDouble checking the score, but now with the different weights, gives"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "calculate_score(optimal_2020_fen, weights_dim8)",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "4.428571428571429"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "indeed.\n\n### Improving Time to Optimal Solution by Adding User Cuts\n\nLet's try again with user cuts, to see if this speeds up Gurobi, also in this weighted case."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "max_weighted_pieces_User_cuts_board = max_pieces_on_board_forbidding_threat_within_own_type_only(\n piece_weights=weights_dim8, \n user_cuts=True)\n\nwrite_board_to_svg(max_weighted_pieces_User_cuts_board, 'MaxWeightedPiecesUserCutsBoard.svg')\nmax_weighted_pieces_User_cuts_board",
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"text": "Needed 16.597019 seconds to find optimal solution.\nI could maximally fit 8 queens, 16 kings, 13 bishops,\n 16 knights and 8 rooks together on the chess board.\nThe board setup scores 4.428571.\nKNKBBQBR/B1NQKRNK/BK1NRNQB/QRNKNKNK/KNRN1NBQ/BQKRNKNK/RNBNQNBB/BKQKBKRK\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "Board('KNKBBQBR/B1NQKRNK/BK1NRNQB/QRNKNKNK/KNRN1NBQ/BQKRNKNK/RNBNQNBB/BKQKBKRK w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-queen\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-rook\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-queen\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-queen\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><use transform=\"translate(110, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-rook\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><use transform=\"translate(200, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-king\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><use transform=\"translate(290, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-king\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><use transform=\"translate(65, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-rook\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><use transform=\"translate(155, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><use transform=\"translate(245, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-queen\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><use transform=\"translate(20, 155)\" xlink:href=\"#white-queen\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-rook\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><use transform=\"translate(110, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-king\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><use transform=\"translate(200, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-king\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><use transform=\"translate(290, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-king\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><use transform=\"translate(65, 110)\" xlink:href=\"#white-king\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><use transform=\"translate(155, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-rook\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><use transform=\"translate(245, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-queen\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-queen\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-rook\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#white-queen\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#white-rook\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "So this is again a solution with 'the same' 63 pieces but not the same positioning as found without user cuts."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "optimal_2020_fen = \"KNKBBQBR/B1NQKRNK/BK1NRNQB/QRNKNKNK/KNRN1NBQ/BQKRNKNK/RNBNQNBB/BKQKBKRK\"\ncalculate_score(optimal_2020_fen, equal_one_weights)",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "61.0"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "and with the correct different weights"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "calculate_score(optimal_2020_fen, weights_dim8)",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 27,
"data": {
"text/plain": "4.428571428571429"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "So this is an optimal solution to the 2020 ChessCompressConcurrent challenge.\nThe top score, given by this solution found by Gurobi is $4.428571$. \nGurobi says: \"No other solutions better than -4.428571\" and that means it has proven this solution is optimal. \nTo be totally exact about the final number, we can express it in rational number notation.\n\nThe $8$ queens give score $8/8=1$, 16 kings give $16/16=1$, 13 bishops give $13/14$, 16 knights give $16/32=1/2$ and 8 rooks give $8/8=1$\ntogether that makes 1 + 1 + (1-1/14) + 1/2 + 1 = 5.5 - 1/14 = 9/2 -1/14 = (9*7 - 1)/14 = 62/14.\n\nSo this takes about 19 seconds on my laptop with the same user cuts, compared to about 53 seconds without user cuts.\nA significant improvement again.\n\n### Analysing the Structure of the Optimal Solution\n\nThere are some remarkable properties to be found in the optimal solution.\n\nFirst, there are many piece types that score a full one (queens, kings and rooks). So these make up a combined score of 3.\nThen the combined score of the knights is 16/32, so we can only put half of the number of knight (32) compared to the case where no other pieces would be allowed.\nFor the bishops we just have 13 i.o. 14.\nSo are the sub-structures formed per type of piece quite similar to the structures of the optimal solution to the problems of the solution with one allowed piece type only?\nWe guess they must be. But a human cannot easily separate the combined solution into the per piece type sub-solutions.\nA computer can (when taught)."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def get_subsolution_fen_and_subscore(fen, weights_dim8, piece_type):\n terminated_fen = fen + '/'\n digits = [str(x) for x in list(range(0,10))] # from 0 to 9\n sub_fen = ''\n sub_score = 0.0\n accumulated_read_not_yet_written_pieces_in_current_row = 0\n\n assert piece_type in weights_dim8.keys() \n unit_piece_type_score = weights_dim8[piece_type]\n\n row_fen = ''\n for c in terminated_fen:\n c_lower = c.lower()\n if c_lower in digits:\n accumulated_read_not_yet_written_pieces_in_current_row += int(c_lower)\n elif c_lower!=piece_type and c_lower != '/':\n assert c_lower in ['q', 'k', 'b', 'n', 'r']\n assert c_lower != piece_type\n accumulated_read_not_yet_written_pieces_in_current_row += 1 \n elif c_lower==piece_type:\n if accumulated_read_not_yet_written_pieces_in_current_row >= 1:\n row_fen += str(accumulated_read_not_yet_written_pieces_in_current_row) \n accumulated_read_not_yet_written_pieces_in_current_row = 0 # reset\n row_fen += c # don't change color compared to fen\n sub_score += unit_piece_type_score\n elif c_lower == '/':\n if accumulated_read_not_yet_written_pieces_in_current_row >= 1:\n row_fen += str(accumulated_read_not_yet_written_pieces_in_current_row) \n accumulated_read_not_yet_written_pieces_in_current_row = 0 # reset\n row_fen += '/'\n sub_fen += row_fen\n row_fen = '' \n else:\n assert False\n \n return sub_fen[:-1], sub_score # [:-1] drops last added '/'\n\ndef show_filtered_out_score_and_board(fen, weights_dim8, piece_type):\n sub_fen, sub_score = get_subsolution_fen_and_subscore(fen, weights_dim8, piece_type)\n print('sub_fen: ' + sub_fen)\n print('sub_score: {:f}'.format(sub_score))\n return chess.Board(sub_fen)",
"execution_count": 28,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Calling this function for queens, we get"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "board = show_filtered_out_score_and_board(optimal_2020_fen, weights_dim8, 'q')\nwrite_board_to_svg(board, 'CCC_optimal_board_filtered_q.svg')\nboard",
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"text": "sub_fen: 5Q2/3Q4/6Q1/Q7/7Q/1Q6/4Q3/2Q5\nsub_score: 1.000000\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 29,
"data": {
"text/plain": "Board('5Q2/3Q4/6Q1/Q7/7Q/1Q6/4Q3/2Q5 w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white queen\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-queen\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z\" /><path d=\"M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14l2 12zM9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z\" stroke-linecap=\"butt\" /><path d=\"M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0\" fill=\"none\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><use transform=\"translate(110, 335)\" xlink:href=\"#white-queen\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><use transform=\"translate(200, 290)\" xlink:href=\"#white-queen\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><use transform=\"translate(65, 245)\" xlink:href=\"#white-queen\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><use transform=\"translate(335, 200)\" xlink:href=\"#white-queen\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><use transform=\"translate(20, 155)\" xlink:href=\"#white-queen\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><use transform=\"translate(290, 110)\" xlink:href=\"#white-queen\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><use transform=\"translate(155, 65)\" xlink:href=\"#white-queen\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><use transform=\"translate(245, 20)\" xlink:href=\"#white-queen\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Note that this is an optimal solution for the maximum (=eight) queens (and no other pieces) problem, and even one possessing some symmetry.\n\nFiltering out the the kings, we get"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "board = show_filtered_out_score_and_board(optimal_2020_fen, weights_dim8, 'k')\nwrite_board_to_svg(board, 'CCC_optimal_board_filtered_k.svg')\nboard",
"execution_count": 30,
"outputs": [
{
"output_type": "stream",
"text": "sub_fen: K1K5/4K2K/1K6/3K1K1K/K7/2K2K1K/8/1K1K1K1K\nsub_score: 1.000000\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 30,
"data": {
"text/plain": "Board('K1K5/4K2K/1K6/3K1K1K/K7/2K2K1K/8/1K1K1K1K w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white king\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-king\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M22.5 11.63V6M20 8h5\" stroke-linejoin=\"miter\" /><path d=\"M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5\" fill=\"#fff\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z\" fill=\"#fff\" /><path d=\"M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><use transform=\"translate(65, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><use transform=\"translate(155, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><use transform=\"translate(245, 335)\" xlink:href=\"#white-king\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><use transform=\"translate(335, 335)\" xlink:href=\"#white-king\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><use transform=\"translate(110, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><use transform=\"translate(245, 245)\" xlink:href=\"#white-king\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><use transform=\"translate(335, 245)\" xlink:href=\"#white-king\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><use transform=\"translate(20, 200)\" xlink:href=\"#white-king\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><use transform=\"translate(155, 155)\" xlink:href=\"#white-king\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><use transform=\"translate(245, 155)\" xlink:href=\"#white-king\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><use transform=\"translate(335, 155)\" xlink:href=\"#white-king\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><use transform=\"translate(65, 110)\" xlink:href=\"#white-king\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><use transform=\"translate(200, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><use transform=\"translate(335, 65)\" xlink:href=\"#white-king\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><use transform=\"translate(20, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><use transform=\"translate(110, 20)\" xlink:href=\"#white-king\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "This is an optimal kings only solution as well, but not the one Gurobi finds with 4 kings in 4 corners of the board. So it is adapted because of the presence of other pieces, but its score does not get lowered by it."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "For the rooks, we get"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "board = show_filtered_out_score_and_board(optimal_2020_fen, weights_dim8, 'r')\nwrite_board_to_svg(board, 'CCC_optimal_board_filtered_r.svg')\nboard",
"execution_count": 31,
"outputs": [
{
"output_type": "stream",
"text": "sub_fen: 7R/5R2/4R3/1R6/2R5/3R4/R7/6R1\nsub_score: 1.000000\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 31,
"data": {
"text/plain": "Board('7R/5R2/4R3/1R6/2R5/3R4/R7/6R1 w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white rook\" fill=\"#fff\" fill-rule=\"evenodd\" id=\"white-rook\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5\" stroke-linecap=\"butt\" /><path d=\"M34 14l-3 3H14l-3-3\" /><path d=\"M31 17v12.5H14V17\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" /><path d=\"M31 29.5l1.5 2.5h-20l1.5-2.5\" /><path d=\"M11 14h23\" fill=\"none\" stroke-linejoin=\"miter\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><use transform=\"translate(290, 335)\" xlink:href=\"#white-rook\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><use transform=\"translate(20, 290)\" xlink:href=\"#white-rook\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><use transform=\"translate(155, 245)\" xlink:href=\"#white-rook\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><use transform=\"translate(110, 200)\" xlink:href=\"#white-rook\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><use transform=\"translate(65, 155)\" xlink:href=\"#white-rook\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><use transform=\"translate(200, 110)\" xlink:href=\"#white-rook\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><use transform=\"translate(245, 65)\" xlink:href=\"#white-rook\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><use transform=\"translate(335, 20)\" xlink:href=\"#white-rook\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The diagonal is used for 6 out of 8 rooks, where it was used by the optimal Gurobi solution for all the rooks in the rook only problem. But this solution also scores 1."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "For the bishops, we get"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "board = show_filtered_out_score_and_board(optimal_2020_fen, weights_dim8, 'b')\nwrite_board_to_svg(board, 'CCC_optimal_board_filtered_b.svg')\nboard",
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"text": "sub_fen: 3BB1B1/B7/B6B/8/6B1/B7/2B3BB/B3B3\nsub_score: 0.928571\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 32,
"data": {
"text/plain": "Board('3BB1B1/B7/B6B/8/6B1/B7/2B3BB/B3B3 w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white bishop\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-bishop\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><g fill=\"#fff\" stroke-linecap=\"butt\"><path d=\"M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.94 3-2 3-2zM15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2zM25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z\" /></g><path d=\"M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5\" stroke-linejoin=\"miter\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><use transform=\"translate(20, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><use transform=\"translate(200, 335)\" xlink:href=\"#white-bishop\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><use transform=\"translate(110, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><use transform=\"translate(290, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><use transform=\"translate(335, 290)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><use transform=\"translate(20, 245)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><use transform=\"translate(290, 200)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><use transform=\"translate(20, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><use transform=\"translate(335, 110)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><use transform=\"translate(20, 65)\" xlink:href=\"#white-bishop\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><use transform=\"translate(155, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><use transform=\"translate(200, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><use transform=\"translate(290, 20)\" xlink:href=\"#white-bishop\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "So for the bishops we have one less piece then in an optimal solution to the bishops only problem.\n\nFor the kights"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "board = show_filtered_out_score_and_board(optimal_2020_fen, weights_dim8, 'n')\nwrite_board_to_svg(board, 'CCC_optimal_board_filtered_n.svg')\nboard",
"execution_count": 33,
"outputs": [
{
"output_type": "stream",
"text": "sub_fen: 1N6/2N3N1/3N1N2/2N1N1N1/1N1N1N2/4N1N1/1N1N1N2/8\nsub_score: 0.500000\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 33,
"data": {
"text/plain": "Board('1N6/2N3N1/3N1N2/2N1N1N1/1N1N1N2/4N1N1/1N1N1N2/8 w - - 0 1')",
"image/svg+xml": "<svg height=\"400\" version=\"1.1\" viewBox=\"0 0 400 400\" width=\"400\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><g class=\"white knight\" fill=\"none\" fill-rule=\"evenodd\" id=\"white-knight\" stroke=\"#000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10\" style=\"fill:#ffffff; stroke:#000000;\" /><path d=\"M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z\" style=\"fill:#000000; stroke:#000000;\" /><path d=\"M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z\" style=\"fill:#000000; stroke:#000000;\" transform=\"matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)\" /></g></defs><rect class=\"square dark a1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"335\" /><rect class=\"square light b1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"335\" /><rect class=\"square dark c1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"335\" /><rect class=\"square light d1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"335\" /><rect class=\"square dark e1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"335\" /><rect class=\"square light f1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"335\" /><rect class=\"square dark g1\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"335\" /><rect class=\"square light h1\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"335\" /><rect class=\"square light a2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"290\" /><rect class=\"square dark b2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"290\" /><use transform=\"translate(65, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light c2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"290\" /><rect class=\"square dark d2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"290\" /><use transform=\"translate(155, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light e2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"290\" /><rect class=\"square dark f2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"290\" /><use transform=\"translate(245, 290)\" xlink:href=\"#white-knight\" /><rect class=\"square light g2\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"290\" /><rect class=\"square dark h2\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"290\" /><rect class=\"square dark a3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"245\" /><rect class=\"square light b3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"245\" /><rect class=\"square dark c3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"245\" /><rect class=\"square light d3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"245\" /><rect class=\"square dark e3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"245\" /><use transform=\"translate(200, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light f3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"245\" /><rect class=\"square dark g3\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"245\" /><use transform=\"translate(290, 245)\" xlink:href=\"#white-knight\" /><rect class=\"square light h3\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"245\" /><rect class=\"square light a4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"200\" /><rect class=\"square dark b4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"200\" /><use transform=\"translate(65, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light c4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"200\" /><rect class=\"square dark d4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"200\" /><use transform=\"translate(155, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light e4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"200\" /><rect class=\"square dark f4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"200\" /><use transform=\"translate(245, 200)\" xlink:href=\"#white-knight\" /><rect class=\"square light g4\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"200\" /><rect class=\"square dark h4\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"200\" /><rect class=\"square dark a5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"155\" /><rect class=\"square light b5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"155\" /><rect class=\"square dark c5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"155\" /><use transform=\"translate(110, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light d5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"155\" /><rect class=\"square dark e5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"155\" /><use transform=\"translate(200, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light f5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"155\" /><rect class=\"square dark g5\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"155\" /><use transform=\"translate(290, 155)\" xlink:href=\"#white-knight\" /><rect class=\"square light h5\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"155\" /><rect class=\"square light a6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"110\" /><rect class=\"square dark b6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"110\" /><rect class=\"square light c6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"110\" /><rect class=\"square dark d6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"110\" /><use transform=\"translate(155, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light e6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"110\" /><rect class=\"square dark f6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"110\" /><use transform=\"translate(245, 110)\" xlink:href=\"#white-knight\" /><rect class=\"square light g6\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"110\" /><rect class=\"square dark h6\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"110\" /><rect class=\"square dark a7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"65\" /><rect class=\"square light b7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"65\" /><rect class=\"square dark c7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"65\" /><use transform=\"translate(110, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light d7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"65\" /><rect class=\"square dark e7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"65\" /><rect class=\"square light f7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"65\" /><rect class=\"square dark g7\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"65\" /><use transform=\"translate(290, 65)\" xlink:href=\"#white-knight\" /><rect class=\"square light h7\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"65\" /><rect class=\"square light a8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"20\" y=\"20\" /><rect class=\"square dark b8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"65\" y=\"20\" /><use transform=\"translate(65, 20)\" xlink:href=\"#white-knight\" /><rect class=\"square light c8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"110\" y=\"20\" /><rect class=\"square dark d8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"155\" y=\"20\" /><rect class=\"square light e8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"200\" y=\"20\" /><rect class=\"square dark f8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"245\" y=\"20\" /><rect class=\"square light g8\" fill=\"#ffce9e\" height=\"45\" stroke=\"none\" width=\"45\" x=\"290\" y=\"20\" /><rect class=\"square dark h8\" fill=\"#d18b47\" height=\"45\" stroke=\"none\" width=\"45\" x=\"335\" y=\"20\" /><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"10\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"42\" y=\"390\">a</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"10\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"87\" y=\"390\">b</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"10\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"132\" y=\"390\">c</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"10\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"177\" y=\"390\">d</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"10\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"222\" y=\"390\">e</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"10\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"267\" y=\"390\">f</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"10\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"312\" y=\"390\">g</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"10\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"357\" y=\"390\">h</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"357\">1</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"312\">2</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"267\">3</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"222\">4</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"177\">5</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"132\">6</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"87\">7</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"10\" y=\"42\">8</text><text alignment-baseline=\"middle\" font-size=\"14\" text-anchor=\"middle\" x=\"390\" y=\"42\">8</text></svg>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "So we have only half the number of knights compared to the optimal solution to the knight only problem. They are still all on one square color though,\nso that much is left of 'the pattern'."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "All in all, I think it's remarkable that the score of the combined problem is so high."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "$\\square$"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# The following code will convert all the svg files to png files, \n# for example to be used in a Medium article. :)\nimport os\nimport glob\nsvg_list = glob.glob('*.svg')\nprint(svg_list)\nfor svg in svg_list:\n png = svg.replace('.svg', '.png')\n cmd = 'rsvg-convert -h 320 {:s} > {:s}'.format(svg, png)\n print(cmd)\n os.system(cmd)",
"execution_count": 34,
"outputs": [
{
"output_type": "stream",
"text": "['ThirtyTwoKnightsBoard.svg', 'MaxPiecesBoard.svg', 'OpeningChessBoard.svg', 'FirstMoveBoard.svg', 'MaxPiecesUserCutsBoard.svg', 'SixteenKingsBoard.svg', 'CCC_optimal_board_filtered_b.svg', 'SixteenBishopsBoard.svg', 'CCC_optimal_board_filtered_r.svg', 'EightQueensBoard.svg', 'CCC_optimal_board_filtered_q.svg', 'CCC_optimal_board_filtered_k.svg', 'MaxWeightedPiecesBoard.svg', 'ChessCompressConcurrentPeter1Board.svg', 'CCC_optimal_board_filtered_n.svg', 'MaxWeightedPiecesUserCutsBoard.svg', 'EightRooksBoard.svg']\nrsvg-convert -h 320 ThirtyTwoKnightsBoard.svg > ThirtyTwoKnightsBoard.png\nrsvg-convert -h 320 MaxPiecesBoard.svg > MaxPiecesBoard.png\nrsvg-convert -h 320 OpeningChessBoard.svg > OpeningChessBoard.png\nrsvg-convert -h 320 FirstMoveBoard.svg > FirstMoveBoard.png\nrsvg-convert -h 320 MaxPiecesUserCutsBoard.svg > MaxPiecesUserCutsBoard.png\nrsvg-convert -h 320 SixteenKingsBoard.svg > SixteenKingsBoard.png\nrsvg-convert -h 320 CCC_optimal_board_filtered_b.svg > CCC_optimal_board_filtered_b.png\nrsvg-convert -h 320 SixteenBishopsBoard.svg > SixteenBishopsBoard.png\nrsvg-convert -h 320 CCC_optimal_board_filtered_r.svg > CCC_optimal_board_filtered_r.png\nrsvg-convert -h 320 EightQueensBoard.svg > EightQueensBoard.png\nrsvg-convert -h 320 CCC_optimal_board_filtered_q.svg > CCC_optimal_board_filtered_q.png\nrsvg-convert -h 320 CCC_optimal_board_filtered_k.svg > CCC_optimal_board_filtered_k.png\nrsvg-convert -h 320 MaxWeightedPiecesBoard.svg > MaxWeightedPiecesBoard.png\nrsvg-convert -h 320 ChessCompressConcurrentPeter1Board.svg > ChessCompressConcurrentPeter1Board.png\nrsvg-convert -h 320 CCC_optimal_board_filtered_n.svg > CCC_optimal_board_filtered_n.png\nrsvg-convert -h 320 MaxWeightedPiecesUserCutsBoard.svg > MaxWeightedPiecesUserCutsBoard.png\nrsvg-convert -h 320 EightRooksBoard.svg > EightRooksBoard.png\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Conclusions\n\n\n## Time to Optimal Solution\n\nThe right user cuts seem to speed up solving these problems to optimality. The extent to which can apparently depend on weights in the objective function.\nIndeed when weights of pieces were all 1, adding user cuts lowered solution time from 88 to 7 seconds, but when weights were 1/n with n the maximum possible pieces of its chess piece type possible to put on a chess board without threats, the \ntime to optimal solution by just adding user cuts went down from 55 to 19 seconds. Nevertheless, in both cases solver time went down, so it is always useful to consider user cuts.\n\n## Decoupling Solutions into Chess Piece Families\n\nBy splitting the solution into sub-solutions per each chess family, we see that \na lot of the structures of the optimal solutions per chess piece family \nare still present in the solution to the combined problem.\n\n# Possible Speed Improvements\n\nTo minimize total solver time, it may be useful to first solve the problem with weights set to 1 and add user cuts, \nwhich only took 7 seconds, and then insert that solution to the problem with the 1/n weights. \nGurobi and other solvers can be warm started with a valid solution. Doing so may reduced total solver time to solve the combined problems.\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# References\n\n[1] https://github.com/PeterSels/OnComputation/tree/master/ChessCompress\n\n[2] https://gist.github.com/PeterSels/923d2f39fe742fce3cc13065e97c9f57\n\n[3] https://www.gurobi.com/"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Peter Sels, April 21st 2020. \n\nCopyright © 2020 Logically Yours BV."
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/965c5ce567daf870d4817e2d8afc1e5b"
},
"gist": {
"id": "965c5ce567daf870d4817e2d8afc1e5b",
"data": {
"description": "ChessCompressConcurrently.ipynb",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.6",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"width": "513px",
"left": "10px",
"top": "150px"
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment