Wannabe Hill-Climber implemented
commit
c96abad68f
|
|
@ -0,0 +1,34 @@
|
||||||
|
"""
|
||||||
|
Implementiere einen Hill-Climbing-Algorithmus, der die Zahlen in
|
||||||
|
einem SUDOKU-Feld durch vertauschen innerhalb einer Zeile so
|
||||||
|
umsortiert, dass sie die SUDOKU-Bedingung auch für die Spalten
|
||||||
|
erfüllen.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
board = np.array([
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
])
|
||||||
|
|
||||||
|
board_size = len(board) # Board is always quadratic
|
||||||
|
|
||||||
|
while -np.sum([len(set(board[:, i])) != 9 for i in range(9)]):
|
||||||
|
for row in range(board_size):
|
||||||
|
for col in range(board_size):
|
||||||
|
# Create array of column values excluding current row
|
||||||
|
column_without_current = np.concatenate([board[:row, col], board[row + 1:, col]])
|
||||||
|
if board[row, col] in column_without_current:
|
||||||
|
board[row, col], board[row, (col + 1) % board_size] = board[row, (col + 1) % board_size], board[row, col]
|
||||||
|
break
|
||||||
|
# print(-np.sum([len(set(board[:, i])) != 9 for i in range(9)])) # debugging
|
||||||
|
|
||||||
|
print(board)
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
"""
|
||||||
|
Erweitere das SUDOKU aus Aufgabe 1 so, dass auch die 9
|
||||||
|
üblichen 3x3 Quadrate alle Zahlen von 1-9 enthalten. Außerdem
|
||||||
|
soll der Hill-Climber in einen Simulated-Annealing-Algorithmus
|
||||||
|
abgewandelt werden. Wichtig ist, dass dabei die Wahrscheinlichkeit
|
||||||
|
berechnet und ausgegeben wird, falls ein Schritt zu einer kleineren
|
||||||
|
Fitness auftreten würde.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
board = np.array([
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
])
|
||||||
|
|
||||||
|
board_size = len(board) # Board is always quadratic
|
||||||
|
|
||||||
|
while -np.sum([len(set(board[:, i])) != 9 for i in range(9)]):
|
||||||
|
for row in range(board_size):
|
||||||
|
for col in range(board_size):
|
||||||
|
# Create array of column values excluding current row
|
||||||
|
column_without_current = np.concatenate([board[:row, col], board[row + 1:, col]])
|
||||||
|
if board[row, col] in column_without_current:
|
||||||
|
board[row, col], board[row, (col + 1) % board_size] = board[row, (col + 1) % board_size], board[row, col]
|
||||||
|
break
|
||||||
|
# print(-np.sum([len(set(board[:, i])) != 9 for i in range(9)])) # debugging
|
||||||
|
|
||||||
|
print(board)
|
||||||
Loading…
Reference in New Issue