finished simulated annealing

master
Ruben Seitz 2025-10-05 15:59:52 +02:00
parent b68e2fcfc3
commit b478119660
2 changed files with 33 additions and 7 deletions

View File

@ -34,6 +34,6 @@ while -np.sum([len(set(board[:, i])) != 9 for i in range(9)]):
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(last_fitness) # debugging
print(board)

View File

@ -7,6 +7,7 @@ erfüllen.
import numpy as np
import random
import math
board = np.array([
[1, 2, 3, 4, 5, 6, 7, 8, 9],
@ -21,19 +22,44 @@ board = np.array([
])
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)]):
def calculate_fitness(board):
# Previous fitness
column_violations = np.sum([len(set(board[:, i])) != 9 for i in range(9)])
# plus checking 3x3 sub-grids
grid_violations = 0
for block_row in range(0, 9, 3):
for block_col in range(0, 9, 3):
# Extract the 3x3 block
block = board[block_row:block_row + 3, block_col:block_col + 3].flatten()
if len(set(block)) != 9:
grid_violations += 1
return -(column_violations + grid_violations) # Negative because we want to maximize
last_fitness = calculate_fitness(board)
T = 10
while calculate_fitness(board) < 0: # Continue until no violations
for row in range(board_size):
for col in range(board_size):
# swap col in row with random other col
rand_col = random.randrange(board_size)
board[row, col], board[row, rand_col] = board[row, rand_col], board[row, col]
current_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)])
current_fitness = calculate_fitness(board)
if current_fitness >= last_fitness:
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
p = math.e ** (-(last_fitness - current_fitness) / T) # adjusted formula
# print(p) # debugging
if p > random.random(): # if probability occurs
last_fitness = current_fitness
else:
board[row, rand_col], board[row, col] = board[row, col], board[row, rand_col] # swap back
T = max(T - 0.1, 0.1) # Decrease T more slowly and don't let it reach 0
print(last_fitness) # debugging
print(board)