Solving Sudoku with AI | Learning Intelligence 1

Daniel Bourke starts his Udacity AI Nanodegree by building a Sudoku solver. The hardest part is implementing the "naked twins" constraint-propagation technique, which takes hours of debugging. He submits the project, gets feedback from Udacity, and learns Python list comprehensions and how to explain algorithmic logic clearly.
Chapters
Hello y'all, what's going on? We're back with another series. I'm gonna— I don't know what I'm gonna call this one yet. Maybe learning intelligence or building intelligence. I think learning intelligence is a good one. Why is that? Well, because I'm starting my artificial intelligence nano degree on Udacity. Oh, and by the way, if you've seen my other videos, yes, I did shave my head. Yes, it is incredibly short and my friend cut my hair. Long story short. I felt like cutting all my hair off. Not this short, but it'll grow back, right? So, I took a few days off because it was my birthday last Friday. I'm 24 years old. Woo! And now I'm back into studying. So, I finished the Deep Learning Nanodegree. You can see that series there in the 100 Days of Code series. But I'm going to start this as a new one. I'll do— I won't do too much of an intro on this video because I'll do another intro, like an earlier on video introducing my goals and whatnot. In a future video. So it'll be like Star Wars. This— think of this as like Episode 1, and Episode 0 is going to come sometime in the future. But what have I been learning? So we started off, we're diving straight into it. This is my first project I'm up to already, and we are a whole, I don't know, 3 hours into this course. So it's due this Friday, I believe. Yeah, today is Tuesday, the 5th of September, and it is due on the 8th, I have to build an artificial intelligence to solve any Sudoku puzzle. I can barely solve Sudokus myself. Check this out. If we go here, this one here is like the example. Took me 49 minutes and 40 seconds. What is that? So by the end of this week, I will have built an AI that can solve Sudoku. And I will show it all to you. And as for this series, I want to bring you along with my journey on sort of learning artificial intelligence and building things with it. You can follow along on my Trello board. I'll link that in the description as well. And also link an article by Peter Norvig, who's like one of the fathers of artificial intelligence, researcher at Google's Deep Brain or some sort of Google's lab. DeepMind, maybe that is. But yeah, and he wrote an article on how to solve every Sudoku puzzle using Python, which I think is really cool. I'm terrible at Sudoku, so I'm just gonna build some AI to solve it for me. We're making progress on the Sudoku project. So check this out. It's a bit different this time. It's not in a Jupyter Notebook, my beloved Jupyter Notebook for the projects. It's all in an IDE, which I still have to get used to. So the project is to solve, write an algorithm to solve any Sudoku. Implementing the naked twins technique. So for example, if two numbers have the same, same two-digit possibility that they could be in Sudoku, they're considered naked twins. I won't go too much into depth on that, I'll just let you Google it. I think there's a Sudoku Dragon is the website to check it out, as well as the ability to solve a diagonal Sudoku. So not only with the columns and the rows and the little boxes, but diagonally as well. So these digits here along the diagonals, they have to be 1 to 9 and contain the digits 1 to 9. But I've hit a bit of a roadblock. I was kind of doubting myself because I couldn't solve this one part of the challenge, particularly the naked twins part of it. I'm trying to still— I've never done a Sudoku until yesterday. Well, fully, not that I can remember. I was kind of like reconsidering my enrollment in AI nano degree considering this is the first project and apparently it's one of the easier ones. But then I remembered, no, I can work through this. This is the point of it, right? If it was easy, everyone would be doing it. So I've left myself, I'm going to finish off for the day, take a break, let it sink into my subconscious. I've let myself a little checklist here of what I can do next step. So 4 little steps: implement Peter Norvig code in Jupyter Notebook, because that's where I'm most familiar for further understanding, read through Medium article, Go through Sudoku class before submission. Insert comments from class code into my submission code. So just a bit of a checklist to work on tomorrow. I think that's, that's a, that's a thing to remember is when you have a big project and you sort of look at the big picture like I was before and sort of having doubts about yourself, if you break it down into small little steps and achieve each one of those steps, suddenly something that was once big is now just a bunch of small little things you You can tick those off relatively easy. So I'm gonna go work out. Check out my YouTube other videos for more workouts. I think I'm gonna, I want to post more workout videos actually and more cooking videos. But this will be the final clip for today and we'll check back in tomorrow, see if I've made progress. I wanna submit the project by the end of tomorrow. Let's do it. Look at that, the night shift mode is engaged. You know it means I've been coding for a while. Get Flux by the way, really helps. And I've got night shift on as well. So like I don't know which one overrides which. But what I've been up to today, going through this Medium article, which is actually really good. I think I mentioned it in the previous clip, Sudoku Solver by Peter Norvig, and it virtually breaks down all the steps that Peter Norvig took to create his solving every Sudoku puzzle with AI. So what I've been doing is reimplementing the article in Python notebook or Jupyter Notebook. Sorry, I was about to say IPython. Jupyter Notebook. Because that's where I'm most familiar. I'm not entirely familiar with IDEs and running code through, through the terminal just yet. So this is my Jupyter Notebook check-ins over here. So I've been trying to replicate it in a familiar territory before trying to put it back into the IDE before submission. I'm not going to be submitting it today, which is— I don't know, I've sort of hit a roadblock. I've been coding for about how long? Let's check it out. Been working for 3 solid hours on this problem. And that's concentrated hours, by the way. So it's like, this is all I'm focused on. And right now I sort of hit a time where I'm like, okay, I need to take some time away, go for a run or something, clear my head so I can come back to it either later tonight, probably not later tonight because I don't know, as I have dinner, my brain power will go down. Tomorrow morning, get back into it. I'm gonna submit it tomorrow. Be a little bit past the due date, but that's the beauty of online courses is that you can, as long as you submit them all before the sort of the cutoff date, which is 4 weeks after the final assignment, you can still pass it. So I'll get this done tomorrow and I'll check in with you once it's done. But I've also— it works at the moment, but I'm trying to implement the Naked Twins function, which is the naked twins is a strategy to solve Sudoku. But it does, it is able to solve apparently the hardest Sudoku ever. So, is it making a liar of me now? Oh no. Oh, there we go, sorry. Hardest ever Sudoku. I had a link to it before. But essentially a Finnish mathematician, I'll put it in the description by the way, a Finnish mathematician thought of the hardest Sudoku he could possibly make and it's apparently only solvable by the brightest minds. Or the algorithm that I created with the help of Udacity and Peter Norvig and the Medium article, of course. But I'm gonna go for a run, clear my head. I livestreamed a workout video today, actually, so check that out. That'll be somewhere on my channel. But I'll see you tomorrow. Woo-hoo! Finally got the project submitted. It is almost 6 PM. I was going for a lot longer than what I thought I would. But look at this cool little message. You submit things in the terminal now for Udacity projects. Well, at least for this one so far. Results, everything looks good. And then you get a zip file that'll go into here, into this folder, and then I can upload it there. But finally, after I was working literally all day, probably 4 or 5 hours on the Naked Twins function, which is here, I can't really explain it to you at the moment because I'm a bit, a bit scatterbrained, but essentially Naked Twins goes through all the Sudoku and finds all the boxes with 2-digit possibilities, and then finds all the boxes of those 2-digit possibilities that have the same digits, and then eliminates those digits from the other values in its peer group. And now that was a lot to sort of take in, and especially if you're not familiar with Sudoku, and I'm nowhere near familiar with Sudoku either, but don't worry. I think I may do a Jupyter Notebook up for this with it more in depth and how I actually went over this and potentially a blog post to go with it. So if I do, you'll be the first to know that. But Project 1 for the Udacity Artificial Intelligence Nanodegree is submitted. Woo! That was a lot of effort actually. And that was apparently Project 1 is one of the most easiest ones in the whole thing. So I'm in for a good treat. I learned a lot about Python and list comprehension in this one. That's what I think projects, working on projects is really good for. Is you learn a lot more than what you would sort of, I don't know, not implementing it or just going over different pieces of theory. So if I've got to work on something in the future, it's my Python, just Python in general. I think that's a big skill to have. Like if you're good at just programming in general, that is a really good skill to have. I've also got to work on sort of my ability to communicate what the program's actually doing. So I think because I'm still such a novice at Python, I struggle to sort of understand what's going on line by line with the code. But I find if I break it down myself and sort of comment on each line, what, what I think is doing, I get a better understanding. And also printing, printing what each line is doing, what each output, what each variable looks like, that's really helpful for me. But it's almost 6 PM. I haven't eaten all day. I'm going to get some food. And I'll check back in with you guys. I think I'll end this video once I get my once I've passed this project, 'cause that'll be, I don't know, that'll be a good way to end the video, get a project done. So hopefully the next clip I'm getting feedback from my reviewer to say that I've passed the project. Fingers crossed. Okay, so the first time I submitted this Udacity project file, I forgot to fill in these questions here in the README file. So let's see. How did we use constraint propagation to solve the naked twins problem? Firstly, find all the squares with only 2 possible solutions. Secondly, of these squares, find those with matching values. These are considered the naked twins. This constrains the possible options heavily. Once you've got the naked twins, find all the common pairs of these squares. With the common pairs known, you can safely remove the possibility of the digits within the naked twins as being options for the pairs of the naked twins. Now that is a mouthful, but if you understand the naked twins philosophy— and I know I've talked— it's a strategy, sorry— talked about that a lot in this video, so if you're unfamiliar with Sudoku, I apologize. But Sudoku is actually pretty fun, playing it on your own. Like, I mean, I know this AI can solve it in a few microseconds, but playing it on your own, it's a good fun game in my opinion. So I resubmitted my project. I won't read this one out. That'll be on my GitHub later, so you can read that. Resubmitted it. So let's see. Your project has been reviewed. By the way, Udacity support team, uh, probably the fastest reviewers. I know I've said this in the past, fastest reviewers I've ever experienced. So I submitted this and sort of went to have a break and then 2 hours later it was done, it was reviewed. But I'm not checking it till this morning 'cause I wanted to film it as I checked it. And apologize for no shirt, might be a bit sweaty. I just went for a run. It's Sunday morning where I live. I like to wake up and, I don't know, do some sort of physical activity on Sunday mornings. But woohoo! You're watching this as I'm watching this. It meets specifications. So all done. I put in the notes there how I was excited and submitting it too quickly without answering the questions. I completely understand the feeling of being excited about you getting your code to work. Well done. Hope you have many more of it. Oh, that's nice. I really like Udacity reviewers. So documentation very neatly formatted. Well done. README, good answer, good answer. Excellent. And then, oh, I haven't seen this before. Oh, let's see. Code review comments. Awesome, good answer. And what do we have here? Suggestion: it's not needed for this project, but if you ever use this code to impress your friends and solve all the Sudoku puzzles, you should remember to only include diagonal new units when you need a diagonal puzzle. Ah, that makes sense. "Most Sudoku puzzles, including the ones shown up in the newspapers, don't have diagonal constraints." Ah, I see. And what did I get here? I didn't get anything there. Okay, I realized if I tap the autofocus on the iPhone, it puts everything out of blur. So I just got to leave it on autofocus. So don't tap if you're recording video and you want to, I don't know, make it keep it autofocusing. But essentially, Udacity reviewers are amazing. The speed is incredibly quick. What are my plans for the next week or the rest of today? Today I'm going to do some writing, also drive some Uber later so I can get some cash. And then next— oh, I'll plan my next week as well. And then next week I'm going to be working on week 2 of the AI Nanodegree classes as well as some other things. Um, I think I'm going to publish some sort of blog post as well on how I'm learning artificial intelligence, but I'll make sure you, you get everything. So thank you so much for watching. We'll see in the next video. Peace!
More from Daniel

Small Language Models (SLMs) Are the Future: Fine-Tuning AI That Runs on Your iPhone

Introducing Sunny - A MedGemma-Powered Skin Health Tracking App (Kaggle Competition Entry)

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 4 - Comparing the results

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 3 - Running LLMs on the RTX 4090

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 2 - Formalizing the tests

Benchmarking: NVIDIA DGX Spark & NVIDIA RTX 4090 | Part 1 - Creating a series of tests

Local Multimodal RAG on the NVIDIA DGX Spark | Part 4 - Publishing our demo to Hugging Face

Local Multimodal RAG on the NVIDIA DGX Spark | Part 3 - Putting it all together


