Solve your model

Let’s submit the model to Qaekwy

In the previous article, we have defined a generic Sudoku solver by defining the grid and the distinct constraints for blocks, rows, columns, and future initial constant values. Based on the previous definition of the model, you will learn how to instantiate our Sudoku problem instance with values, to submit it to the DirectEngine of Qaekwy, and to retrieve the solution.

Instantiate a grid

To instantiate a Sudoku grid, let’s take our SudokuGrid defined in the previous article and instantiate it with real values:

    # Create a Sudoku grid with initial values. The values
    # set to 0 represent the values to find. The other values
    # are the constants.
    my_grid = SudokuGrid(
        grid_value=[
            0, 7, 0,  0, 0, 0,  6, 9, 0,
            0, 0, 0,  6, 1, 0,  0, 0, 0,
            0, 9, 2,  0, 0, 0,  0, 5, 0,

            0, 0, 0,  0, 8, 1,  7, 0, 9,
            4, 0, 0,  0, 0, 3,  0, 0, 0,
            0, 0, 0,  0, 5, 6,  1, 0, 8,

            0, 5, 9,  0, 0, 0,  0, 1, 0,
            0, 0, 0,  5, 6, 0,  0, 0, 0,
            0, 2, 0,  0, 0, 0,  5, 7, 0
        ]
    )

Instantiate the problem

Now that your SudokuGrid is ready, let’s instantiate the SudokuProblem defined in the previous article with the following code:

    # Create a Sudoku problem instance using the grid
    sp = SudokuProblem(grid=my_grid)

And that’s it. The sp object represents the Sudoku problem instantiated with your Sudoku grid values. Your Qaekwy model representing a Sudoku problem is now completed. Now, let’s solve this grid using the Qaekwy engine.

Instantiate the Qaekwy Engine

The Engine is the Qaekwy instance (a Cloud instance in our case) that will receive your model to be solved, search for the solution(s) and return it to you. This Python library provides all the methods for submitting a model to be solved to an instance and retrieving the solution.

There are three types of Engine (Cloud, stand-alone and Cloud/Cluster type). For tiny models, including the model of this tutorial, the free-to-use Cloud instance is perfectly appropriate.

To communicate with the free-to-use Cloud instance, we need to instantiate the following DirectEngine object:

from qaekwy.engine import DirectEngine

qaekwy_engine = DirectEngine()

Solve the problem

Now that we have:

  • The problem instance
  • The engine instance

Let’s submit the problem instance to the engine, retrieve, and display the solution using the following code:

    # Request the Qaekwy engine to solve the Sudoku problem
    response = qaekwy_engine.model(model=sp)

    # Retrieve the list of solutions from the response
    list_of_solutions = response.get_solutions()

    # Print the solution obtained for the Sudoku puzzle
    for solution_item in list_of_solutions:
        for c in range(0, 81, 9):
            print(solution_item.grid[c:c+9])

The output is the solution to our Sudoku problem:

[1, 7, 8, 3, 2, 5, 6, 9, 4]
[5, 4, 3, 6, 1, 9, 8, 2, 7]
[6, 9, 2, 7, 4, 8, 3, 5, 1]
[2, 6, 5, 4, 8, 1, 7, 3, 9]
[4, 8, 1, 9, 7, 3, 2, 6, 5]
[9, 3, 7, 2, 5, 6, 1, 4, 8]
[7, 5, 9, 8, 3, 2, 4, 1, 6]
[3, 1, 4, 5, 6, 7, 9, 8, 2]
[8, 2, 6, 1, 9, 4, 5, 7, 3]

That’s it !

By using the Qaekwy Python Library for Sudoku problem definition, you can now apply the same model to solve diverse Sudoku grids, with the only variation being your dataset (in this case, the grid).

Indeed, the development of the Qaekwy Python library has been designed in order to allow the developer to benefit from Object-Oriented Programming applied to constraint programming design for solution research and optimization.