Skip to content

Create an AI agent to solve Sodoku by implementing constraint propagation

Notifications You must be signed in to change notification settings

foxan/AIND-Sudoku

Repository files navigation

Artificial Intelligence Nanodegree

Introductory Project: Diagonal Sudoku Solver

Summary

This project is to create an AI agent to solve Sodoku. Multiple functions have been implemented to reduce the solution space. For example, constraint propagation is used to solved a particular case in Sudoku (naked twins problems) by eliminating values from common peers of the naked twins.


Question 1 (Naked Twins)

Q: How do we use constraint propagation to solve the naked twins problem?
A: To solve the naked twins problem, a function naked_twins is defined and used to eliminate values from common peers of the naked twins.

The function is then called in reduce_puzzle to reduce the number of possibilities of the sudoku. Since reduce_puzzle is called in search, the constraint will be propagated to the search tree.

def reduce_puzzle(values):
    solved_values = [box for box in values.keys() if len(values[box]) == 1]
    stalled = False
    while not stalled:
        solved_values_before = len([box for box in values.keys() if len(values[box]) == 1])
        values = eliminate(values)
        values = only_choice(values)
        values = naked_twins(values)
        solved_values_after = len([box for box in values.keys() if len(values[box]) == 1])
        stalled = solved_values_before == solved_values_after
        if len([box for box in values.keys() if len(values[box]) == 0]):
            return False
    return values

Question 2 (Diagonal Sudoku)

Q: How do we use constraint propagation to solve the diagonal sudoku problem?
A: To solve the diagonal sudoku problem, I have defined the two diagonal units and added them to the unit list.

diagonal_units = [[r + c for (r, c) in zip(rows, cols)], [r + c for (r, c) in zip(rows, cols[::-1])]]
unitlist = row_units + column_units + square_units + diagonal_units

Units, peers and other dependencies will then be updated and puzzle checking will be applied to those diagonal units.


Install

This project requires Python 3.

We recommend students install Anaconda, a pre-packaged Python distribution that contains all of the necessary libraries and software for this project. Please try using the environment we provided in the Anaconda lesson of the Nanodegree.

Optional: Pygame

Optionally, you can also install pygame if you want to see your visualization. If you've followed our instructions for setting up our conda environment, you should be all set.

If not, please see how to download pygame here.

Code

  • solution.py - You'll fill this in as part of your solution.
  • solution_test.py - Do not modify this. You can test your solution by running python solution_test.py.
  • PySudoku.py - Do not modify this. This is code for visualizing your solution.
  • visualize.py - Do not modify this. This is code for visualizing your solution.

Visualizing

To visualize your solution, please only assign values to the values_dict using the assign_value function provided in solution.py

Submission

Before submitting your solution to a reviewer, you are required to submit your project to Udacity's Project Assistant, which will provide some initial feedback.

The setup is simple. If you have not installed the client tool already, then you may do so with the command pip install udacity-pa.

To submit your code to the project assistant, run udacity submit from within the top-level directory of this project. You will be prompted for a username and password. If you login using google or facebook, visit this link for alternate login instructions.

This process will create a zipfile in your top-level directory named sudoku-.zip. This is the file that you should submit to the Udacity reviews system.

About

Create an AI agent to solve Sodoku by implementing constraint propagation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages