Posted in

solving sudoku with hill climbing

### Article: Solving Sudoku with Hill Climbing Algorithm

The Sudoku puzzle is a popular logic-based combinatorial number-placement puzzle. It consists of a 9×9 grid divided into nine 3×3 subgrids called “regions”. The objective is to fill the grid so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called “boxes”, “blocks”, or “regions”) contain all of the digits from 1 to 9. The technique of hill climbing can be effectively applied to solve Sudoku puzzles.

#### Hill Climbing Algorithm Overview

Hill climbing is a heuristic search algorithm that combines features of both deterministic and stochastic algorithms to find a near-optimal solution. In the context of Sudoku solving, hill climbing is used to incrementally build a solution until it reaches a local maximum, which is typically the solution to the Sudoku puzzle.

#### Key Steps in Hill Climbing for Sudoku

1. **Initial State Setup**: Start with an incomplete Sudoku grid.
2. **Selection**: Choose an empty cell in the grid.
3. **Modification**: For the selected cell, try different numbers from 1 to 9 until a valid number is found.
4. **Evaluation**: After modifying the cell, evaluate the fitness of the new state using a heuristic function.
5. **Iteration**: Repeat the process of selection, modification, and evaluation until a solution is found or no improvement can be made.

#### Heuristic Function

The heuristic function plays a crucial role in hill climbing. In Sudoku, the function can be based on the number of conflicts in the grid. A conflict occurs when a cell contains a duplicate number in its row, column, or region. The fewer the conflicts, the higher the fitness score.

#### Challenges and Improvements

– **Local Optima**: Hill climbing might get stuck in a local optimum, failing to find the global optimal solution.
– **Backtracking**: To overcome this, a backtracking approach can be integrated with hill climbing to explore other possibilities when a local optimum is reached.

#### Example Implementation

Here’s a simple implementation of the hill climbing algorithm for solving Sudoku:

“`python
def solve_sudoku(board):
empty_cell = find_empty_cell(board)
if not empty_cell:
return True # Puzzle solved
row, col = empty_cell

for num in range(1, 10):
if is_valid(board, num, (row, col)):
board[row][col] = num
if solve_sudoku(board):
return True
board[row][col] = 0 # Reset the cell value

return False

def find_empty_cell(board):
for i in range(9):
for j in range(9):
if board[i][j] == 0:
return (i, j)
return None

def is_valid(board, num, pos):
for i in range(9):
if board[pos[0]][i] == num and pos[1] != i:
return False
if board[i][pos[1]] == num and pos[0] != i:
return False
if board[3 * (pos[0] // 3) + i // 3][3 * (pos[1] // 3) + i % 3] == num and (pos[0] // 3) != i // 3 or (pos[1] // 3) != i % 3:
return False
return True
“`

### FAQ

**Q: What is the hill climbing algorithm?**
A: The hill climbing algorithm is a heuristic search algorithm used to find an optimal or near-optimal solution by incrementally building a solution until it reaches a local maximum.

**Q: How does hill climbing work for Sudoku?**
A: Hill climbing works for Sudoku by incrementally filling in numbers in empty cells while checking for conflicts in rows, columns, and regions. The process continues until a solution is found or no improvement can be made.

**Q: What is a heuristic function in hill climbing?**
A: A heuristic function in hill climbing is used to evaluate the fitness of a solution. In Sudoku, the function can be based on the number of conflicts in the grid.

**Q: Can hill climbing get stuck in local optima?**
A: Yes, hill climbing can get stuck in local optima. To overcome this, backtracking can be integrated with hill climbing to explore other possibilities.

**Q: How can backtracking be integrated with hill climbing?**
A: Backtracking can be integrated with hill climbing by exploring alternative solutions when a local optimum is reached. This involves resetting the current cell and trying a different number, then repeating the process until a valid solution is found.