Finished Hillclimber algorithm

master
Ruben Seitz 2025-10-05 14:40:38 +02:00
parent c96abad68f
commit b68e2fcfc3
2 changed files with 26 additions and 18 deletions

View File

@ -6,6 +6,7 @@ erfüllen.
""" """
import numpy as np import numpy as np
import random
board = np.array([ board = np.array([
[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9],
@ -20,15 +21,19 @@ board = np.array([
]) ])
board_size = len(board) # Board is always quadratic board_size = len(board) # Board is always quadratic
last_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)]) # -9
while -np.sum([len(set(board[:, i])) != 9 for i in range(9)]): while -np.sum([len(set(board[:, i])) != 9 for i in range(9)]):
for row in range(board_size): for row in range(board_size):
for col in range(board_size): for col in range(board_size):
# Create array of column values excluding current row # swap col in row with random other col
column_without_current = np.concatenate([board[:row, col], board[row + 1:, col]]) rand_col = random.randrange(board_size)
if board[row, col] in column_without_current: board[row, col], board[row, rand_col] = board[row, rand_col], board[row, col]
board[row, col], board[row, (col + 1) % board_size] = board[row, (col + 1) % board_size], board[row, col] current_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)])
break if current_fitness >= last_fitness:
# print(-np.sum([len(set(board[:, i])) != 9 for i in range(9)])) # debugging last_fitness = current_fitness
else:
board[row, rand_col], board[row, col], = board[row, col], board[row, rand_col] # swap back
# print(last_fitness) # debugging
print(board) print(board)

View File

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