{"id":289,"date":"2022-04-25T09:00:00","date_gmt":"2022-04-25T09:00:00","guid":{"rendered":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/?p=289"},"modified":"2024-03-10T16:44:17","modified_gmt":"2024-03-10T16:44:17","slug":"solving-sudoku-with-metaheuristics-gvns","status":"publish","type":"post","link":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/2022\/04\/25\/solving-sudoku-with-metaheuristics-gvns\/","title":{"rendered":"Solving Sudoku with Metaheuristics: GVNS"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 5<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<p>The last time I travelled, I saw a little old lady in the airport with her crossword puzzle book. My grandmother travels with her word search book. Me? In my old age, I will travel with a Sudoku book. Before I hit old age, I will take advantage of the opportunity to combine my studies with my favourite game. As it turns out, heuristic algorithms are pretty popular for solving, creating and rating Sudoku puzzles. In this post, we will look at how a metaheuristic, <strong>general variable neighbourhood search<\/strong>, has been used to solve <strong>Sudoku<\/strong> puzzle instances. <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>What is Sudoku?<\/li><li>What are metaheuristics?<\/li><li>General Variable Neighbourhood Search<\/li><li>Solving Sudoku<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is Sudoku?<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Sudoku is Japanese puzzle which consists of an <em>n<sup>2<\/sup> \u00d7 n<sup>2<\/sup><\/em> grid divided into <em>n<sup>2<\/sup><\/em> sub-grids each of size <em>n \u00d7 n<\/em>. The word Sudoku is a combination of two Japanese words, <em>Su <\/em>(Number) and <em>Duko<\/em> (Single) and loosely translates to \u201csolitary number\u201d. <em>n<\/em> is the order of the puzzle, with <em>n<\/em>=3 being the most popular. <\/p>\n\n\n\n<p>The <strong>objective of Sudoku<\/strong> is to fill each cell in a way that every row, column and sub-square contains each integer between 1 and  <em>n<sup>2<\/sup><\/em> inclusive<br>exactly once.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/sudoku-sample-board-1024x590.png\" alt=\"\" class=\"wp-image-292\" width=\"398\" height=\"229\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/sudoku-sample-board-1024x590.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/sudoku-sample-board-300x173.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/sudoku-sample-board-768x443.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/sudoku-sample-board.png 1126w\" sizes=\"auto, (max-width: 398px) 100vw, 398px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>Sudoku is an example of a combinatorial optimisation (CO) problem, which is a class of problems whose solution is in a finite or countably infinite set. Constructing or completing a Sudoku puzzle from a partially filled grid are both <em>NP<\/em>-complete problems. This means that there is no deterministic algorithm which can solve all possible Sudoku problem instances in polynomial time. The solution space for an empty 9 \u00d7 9 Sudoku grid contains approximately 6.7 \u00d7 10<sup>21<\/sup> possible combinations. However, the pre-filled cells serve as constraints and reduce the number of possible combinations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. What are metaheuristics?<\/h2>\n\n\n\n<p>When it comes to solving optimisation problems, there are 2 main types of approaches: exact methods and approximate methods. Exact methods are guaranteed to find an<br>optimal solution for every finite problem instance of an CO problem. <\/p>\n\n\n\n<p>Approximate methods such as heuristic algorithms can be used when there is no known exact method that can be used to solve the problem or when the known exact methods are too computationally expensive to be used practically. In the context of optimisation problems, a <strong>heuristic <\/strong>is a well-defined intelligent procedure &#8211; based on intuition, problem context and structure &#8211; designed to find an approximate solution to the problem. Unlike exact methods, the solutions found may not be optimal, but are some type of &#8220;acceptable&#8221;. The effectiveness of a heuristic depends on the quality of the approximations that it produces. <\/p>\n\n\n\n<p>The performance of heuristics can be improved using <strong>metaheuristics<\/strong>, which are high-level,<br>problem-independent strategies used to develop heuristic optimisation algorithms. They are designed to approximately solve a wide range of problems without needing to fundamentally change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. General Variable Neighbourhood Search<\/h2>\n\n\n\n<p><strong>Variable neighbourhood search (VNS) <\/strong>algorithms, which were originally proposed by <a rel=\"noreferrer noopener\" href=\"https:\/\/www.sciencedirect.com\/science\/article\/pii\/S0305054897000312\" data-type=\"URL\" data-id=\"https:\/\/www.sciencedirect.com\/science\/article\/pii\/S0305054897000312\" target=\"_blank\">Mladenovi\u0107 and Hansen (1997)<\/a>, are single-solution based metaheuristics. They successively explore a set of predefined neighbourhoods which are typically increasingly distant from the current candidate solution. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/vns-1024x551.png\" alt=\"\" class=\"wp-image-299\" width=\"535\" height=\"288\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/vns-1024x551.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/vns-300x161.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/vns-768x413.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/vns.png 1462w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><figcaption>Illustration of the main idea of a basic VNS algorithm<\/figcaption><\/figure><\/div>\n\n\n\n<p>VNS\u2019 main cycle is composed of three phases:<em> shaking, local search and move.<\/em><\/p>\n\n\n\n<p>In the shake phase, a random solution <em>s\u2032<\/em> is selected from the <em>k<\/em>th neighbourhood of the current solution <em>s<\/em>. This <em>s\u2032<\/em> is then used as the initial solution for the local search algorithm being used which produces a new candidate solution <em>s\u2032\u2032<\/em>. In the move phase, if <em>s\u2032\u2032<\/em> is better than the current solution <em>s<\/em>, then it replaces <em>s<\/em> and the cycle is restarted with this new solution; otherwise, the cycle is restarted with the same solution but a different neighbourhood.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/Screenshot-2022-05-05-211513.png\" alt=\"\" class=\"wp-image-300\" width=\"510\" height=\"317\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/Screenshot-2022-05-05-211513.png 868w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/Screenshot-2022-05-05-211513-300x187.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/Screenshot-2022-05-05-211513-768x479.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/Screenshot-2022-05-05-211513-436x272.png 436w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/figure><\/div>\n\n\n\n<p>Variable Neighbourhood Descent (VND) is a deterministic variant of the VNS algorithm.  The VNS main cycle uses a best improvement method, choosing <em>s\u2032\u2032<\/em> as the local optimum in neighbourhood <em>N<sub>k<\/sub><\/em>. <\/p>\n\n\n\n<p>The <strong>General Variable Neighbourhood Search (GVNS)<\/strong> uses the VND as the local search procedure (line 7 of the algorithm).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Solving Sudoku with GVNS<\/h2>\n\n\n\n<p>We will now look at the different elements from the algorithm above and how <a href=\"https:\/\/dl.acm.org\/doi\/abs\/10.1007\/s00500-018-3307-6\" data-type=\"URL\" data-id=\"https:\/\/dl.acm.org\/doi\/abs\/10.1007\/s00500-018-3307-6\" target=\"_blank\" rel=\"noreferrer noopener\">Sevkli and Hamza (2019)<\/a> used this metaheuristic to solve 9 \u00d7 9 Sudoku puzzles.<\/p>\n\n\n\n<p><strong>Solution representation:<\/strong> each sub-grid is numbered from 1 to 9, and each cell in a sub-grid is numbered from 1 to 9. So <em>x<sub>ij<\/sub><\/em> denotes the <em>j<\/em>th cell in sub-grid <em>i<\/em> (see grid above for example of labelled cell).<\/p>\n\n\n\n<p><strong>Solution Initialisation<\/strong>: To initialise the solution, for each cell, a random number is selected<br>from a list of numbers that include all the numbers that could be assigned to the cell without violating any of the constraints with respect to the fixed cells. This is done in such a way that the sub-grid rule is satisfied. To reduce the solution space, they fixed the<br>cells that only have one possible value and repeated that until there were no more such cells.<\/p>\n\n\n\n<p><strong>Cost function <em>f(s)<\/em>: <\/strong>evaluates the violation of the row and column constraints and counts how many values are repeated in each row and in each column (illustrated in figure below). The goal is to minimise the cost function. The optimal solution will have <em>f(s)=0<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/obj-function.png\" alt=\"\" class=\"wp-image-302\" width=\"384\" height=\"308\" \/><figcaption>A candidate solution of the Sudoku puzzle with its fitness value. Repeated digits highlighted in first row and column.<\/figcaption><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Neighbourhood structures<\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:40%\">\n<p>Only one neighbourhood structure, <strong>Invert<\/strong>, is defined for the shake phase. In this structure two cells in the sub-grid are selected and the order of the sub-sequence of cells between them is reversed.<\/p>\n\n\n\n<p>There are 3 neighbourhood structures defined which are used in the VND local search:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Insert <\/strong>&#8211; the value of a chosen cell in a sub-grid is inserted in front of another chosen cell.<\/li><li><strong>Swap <\/strong>&#8211; the values of 2 unfixed cells in the same sub-grid are exchanged.<\/li><li><strong>A Centered Point Oriented Exchange <\/strong>&#8211; a cell between the second and sixth cell in a sub-grid is selected as the center point to find exchange pairs. Values for pair of cells, each equidistant from the center, are swapped until at least one cell in the pair is fixed.<\/li><\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/invert-1024x261.png\" alt=\"\" class=\"wp-image-303\" width=\"379\" height=\"96\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/invert-1024x261.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/invert-300x76.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/invert-768x196.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/invert.png 1206w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/insert-1024x254.png\" alt=\"\" class=\"wp-image-304\" width=\"376\" height=\"93\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/insert-1024x254.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/insert-300x75.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/insert-768x191.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/insert.png 1525w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/swap-1024x252.png\" alt=\"\" class=\"wp-image-305\" width=\"375\" height=\"92\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/swap-1024x252.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/swap-300x74.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/swap-768x189.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/swap.png 1200w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/cpoe-1024x390.png\" alt=\"\" class=\"wp-image-306\" width=\"376\" height=\"142\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/cpoe-1024x390.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/cpoe-300x114.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/cpoe-768x293.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-content\/uploads\/sites\/38\/2022\/05\/cpoe.png 1228w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>Each of these structures apply to a single sub-grid, and in the local search, the neighbourhoods of each of the sub-grids are explored. Within the VND local search, a deep local search algorithm is used. This uses the best improvement strategy which exploits the whole current neighbourhood structure search area to find the best neighbourhood solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/dl.acm.org\/doi\/10.1145\/937503.937505\" data-type=\"URL\" data-id=\"https:\/\/dl.acm.org\/doi\/10.1145\/937503.937505\" target=\"_blank\">Review of Metaheuristics<\/a><\/li><li><a href=\"https:\/\/medium.com\/@davidcarmel\/solving-sudoku-by-heuristic-search-b0c2b2c5346e\" data-type=\"URL\" data-id=\"https:\/\/medium.com\/@davidcarmel\/solving-sudoku-by-heuristic-search-b0c2b2c5346e\" target=\"_blank\" rel=\"noreferrer noopener\">Another cool blog post about Sudoku<\/a> <\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 5<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>The last time I travelled, I saw a little old lady in the airport with her crossword puzzle book. My grandmother travels with her word search book. Me? In my old age, I will travel with a Sudoku book. Before I hit old age, I will take advantage of the opportunity to combine my studies [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":294,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[13],"tags":[20,16,19],"class_list":{"0":"post-289","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-a-few-of-my-favourite-things","8":"tag-metaheuristics","9":"tag-operations-research","10":"tag-optimisation","12":"post-with-thumbnail","13":"post-with-thumbnail-large"},"_links":{"self":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":10,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/posts\/289\/revisions\/324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/media\/294"}],"wp:attachment":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/danielle-notice\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}