Solve your model

Model Instantiation and Resolution

Following the definition of the generic Sudoku model, this document outlines the procedure for its application to a concrete puzzle instance. The process involves instantiating the model with specific data, submitting it to the Qaekwy DirectEngine for resolution, and retrieving the computed solution.

Grid Instantiation

The first step is to instantiate the SudokuGrid class with a specific puzzle. The initial state of the grid is provided as a list of 81 integers, where a value of zero indicates an unknown that the solver must determine. All non-zero values are treated as fixed constants.

    # Instantiate the SudokuGrid with a specific puzzle configuration.
    # Values of 0 represent variables to be solved.
    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
        ]
    )

Problem Instantiation

With the grid data prepared, an instance of the SudokuProblem is created. This object binds the generic model constraints defined previously to the specific my_grid data instance, resulting in a fully specified and solvable problem.

    # Create an instance of the Sudoku problem using the specified grid.
    sp = SudokuProblem(grid=my_grid)

Engine Instantiation

The Qaekwy solver is accessed via a DirectEngine object. It provides a direct, synchronous interface to the underlying solver.

from qaekwy.engine import DirectEngine

# Instantiate the solver engine.
qaekwy_engine = DirectEngine()

Solving and Solution Retrieval

The instantiated problem (sp) is submitted to the engine using the model() method. The engine performs the search for a valid assignment of variables that satisfies all constraints. The response from the engine contains the solution(s).

The solution is then retrieved and can be displayed or otherwise processed. The following code demonstrates iterating through the solution set and printing the completed grid.

    # Submit the problem model to the Qaekwy engine for resolution.
    response = qaekwy_engine.model(model=sp)

    # Extract the list of solutions from the engine's response.
    list_of_solutions = response.get_solutions()

    # Iterate through and display the solution(s).
    for solution_item in list_of_solutions:
        for c in range(0, 81, 9):
            print(solution_item.grid[c:c+9])

The resulting output is the solved Sudoku grid:

[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]

Conclusion

The Qaekwy library facilitates an object-oriented approach to constraint programming. By decoupling the abstract model (SudokuProblem) from the concrete data (SudokuGrid), it is possible to solve any valid Sudoku puzzle by simply providing a new grid instance. This methodology promotes code reuse and a clear separation of concerns between problem modeling and problem-solving.