{"id":92,"date":"2019-03-12T18:55:53","date_gmt":"2019-03-12T18:55:53","guid":{"rendered":"http:\/\/who.ioanpopovici.ro\/?p=92"},"modified":"2019-03-13T08:48:18","modified_gmt":"2019-03-13T08:48:18","slug":"probabilistic-sudoku-using-infer-net-1","status":"publish","type":"post","link":"http:\/\/who.ioanpopovici.ro\/index.php\/2019\/03\/12\/probabilistic-sudoku-using-infer-net-1\/","title":{"rendered":"Probabilistic sudoku using Infer.NET #1"},"content":{"rendered":"\n<p><a href=\"http:\/\/mbmlbook.com\">Model-based machine learning <\/a>and the<a href=\"https:\/\/dotnet.github.io\/infer\/\"> probabilistic programming paradigm <\/a>is coming to the .NET world. If, by any chance, you are unfamiliar with these topics, please feel free to check the links provided above \ud83d\ude42<\/p>\n\n\n\n<p>Given this happy situation, I figured that it would be nice to build a probabilistic sudoku solver \/ generator using the model-based machine learning principles and Infer.NET.<\/p>\n\n\n\n<p>I&#8217;ve done it! but trust me, it was a trip worth sharing.  I have decided to share this journey with everybody in a series of articles \/ talks \/ gatherings.<\/p>\n\n\n\n<p>First, does it make sense to have a probabilistic approach for the sudoku puzzle ? Well, yes it does. It is an <a href=\"https:\/\/en.wikipedia.org\/wiki\/NP-completeness\">NP-complete<\/a>  &#8211;<a href=\"http:\/\/www-imai.is.s.u-tokyo.ac.jp\/~yato\/data2\/SIGAL87-2.pdf\"> proven back in 2003<\/a> -problem, so it makes sense to optimize the solution as much as possible.<\/p>\n\n\n\n<p>Second, building the probabilistic model for the generic sudoku puzzle turned out to be a significant challenge. This small article is dedicated to just a fraction of the &#8220;probabilistic model for the  sudoku puzzke&#8221; problem, namely the reason for choosing a Probabilistical Graphical Model &#8211; a probabilistical model based on graphs. I hope I will be able to write an entire series of articles that will take you through the entire journey I took. Doing this in just one article is impossible, and, let me tell you, extenuating to read and understand. So let&#8217;s begin.<\/p>\n\n\n\n<p>Imagine a simple 4&#215;4 sudoku puzzle<br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"94\" data-permalink=\"http:\/\/who.ioanpopovici.ro\/index.php\/2019\/03\/12\/probabilistic-sudoku-using-infer-net-1\/image-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-1.png?fit=359%2C328\" data-orig-size=\"359,328\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-1.png?fit=300%2C274\" data-large-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-1.png?fit=359%2C328\" src=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-1.png?resize=190%2C174\" alt=\"\" class=\"wp-image-94\" width=\"190\" height=\"174\" srcset=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-1.png?w=359 359w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-1.png?resize=300%2C274 300w\" sizes=\"auto, (max-width: 190px) 85vw, 190px\" \/><figcaption>Fig 1. 4&#215;4 sudoku puzzle<\/figcaption><\/figure><\/div>\n\n\n\n<p>Trying to drill down the components of a sudoku puzzle, we can come up with the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The Solution elements. The elements of the solution (numbers inside). Let&#8217;s call such an element Sn,  In a row-scan order, for the sample above: (1,2,4,3,4,3,1,2,2,1,3,4,3,4,2,1)<\/li><li>The constraints. You know the rules of sudoku, right? Then, constraints are the groups in which solution elements live. For the 4&#215;4 sudoku puzzle, there are 12 of them. Please see the Fig 2. bellow.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1111\" height=\"526\" data-attachment-id=\"95\" data-permalink=\"http:\/\/who.ioanpopovici.ro\/index.php\/2019\/03\/12\/probabilistic-sudoku-using-infer-net-1\/image-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?fit=1111%2C526\" data-orig-size=\"1111,526\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?fit=300%2C142\" data-large-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?fit=840%2C398\" src=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?fit=840%2C398\" alt=\"\" class=\"wp-image-95\" srcset=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?w=1111 1111w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?resize=300%2C142 300w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?resize=768%2C364 768w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?resize=1024%2C485 1024w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption>Fig 2. Constraints for the sudoku puzzle<\/figcaption><\/figure>\n\n\n\n<p>The first constraint (C1) is the first row. You get the idea. (C9) is the first subgrid constraint.<\/p>\n\n\n\n<p>Now the relationship between the constraint and solution nodes (Cm and Sn) can be a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bipartite_graph\">bipartite graph<\/a> as follows<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1377\" height=\"744\" data-attachment-id=\"96\" data-permalink=\"http:\/\/who.ioanpopovici.ro\/index.php\/2019\/03\/12\/probabilistic-sudoku-using-infer-net-1\/image-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?fit=1377%2C744\" data-orig-size=\"1377,744\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?fit=300%2C162\" data-large-file=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?fit=840%2C454\" src=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?fit=840%2C454\" alt=\"\" class=\"wp-image-96\" srcset=\"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?w=1377 1377w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?resize=300%2C162 300w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?resize=768%2C415 768w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?resize=1024%2C553 1024w, https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-3.png?resize=1200%2C648 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption>Fig 3. The graphical model<\/figcaption><\/figure>\n\n\n\n<p>Again, you get the idea. The information held by the bipartite graph is: what constraint applies to what set of values.<\/p>\n\n\n\n<p>That&#8217;s enough for now. Trust me! In the next article I will try to show you how probabilities can fit into this model. <\/p>\n\n\n\n<p>If, by any chance you want to skip-forward to the Infer.NET solution, make sure you read and understand the <a href=\"https:\/\/pdfs.semanticscholar.org\/b45f\/423aac8b204a5f3cb6117da2ac57ebff5606.pdf\">following paper<\/a>&nbsp;that I used to create the probabilistic model for the sudoku puzzle, and snip out some pictures for this article. <\/p>\n\n\n\n<p>Until the next article in the series, remember: the world is beautiful! Probably.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Model-based machine learning and the probabilistic programming paradigm is coming to the .NET world. If, by any chance, you are unfamiliar with these topics, please feel free to check the links provided above \ud83d\ude42 Given this happy situation, I figured that it would be nice to build a probabilistic sudoku solver \/ generator using the &hellip; <a href=\"http:\/\/who.ioanpopovici.ro\/index.php\/2019\/03\/12\/probabilistic-sudoku-using-infer-net-1\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Probabilistic sudoku using Infer.NET #1&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":95,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[26,27],"tags":[33,31,28,29,30],"class_list":["post-92","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","category-probabilistic-programming","tag-net","tag-infer-net","tag-machine-learning","tag-probabilistic-programming","tag-sudoku"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/who.ioanpopovici.ro\/wp-content\/uploads\/2019\/03\/image-2.png?fit=1111%2C526","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paDECb-1u","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":5,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":105,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions\/105"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/media\/95"}],"wp:attachment":[{"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/who.ioanpopovici.ro\/index.php\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}