Adjusted assignment 1 and 2

master
Ruben-FreddyLoafers 2025-10-08 10:15:36 +02:00
parent 27ca92b80b
commit f8b889c213
3 changed files with 45 additions and 42 deletions

View File

@ -25,16 +25,17 @@ last_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)]) # -9
print("Working...")
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):
# 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)])
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
# swap col in row with random other col
rand_row = random.randrange(board_size)
rand_col = random.randrange(board_size)
swap_col = (rand_col + random.randrange(1, board_size)) % board_size
board[rand_row, rand_col], board[rand_row, swap_col] = board[rand_row, swap_col], board[rand_row, rand_col]
current_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)])
if current_fitness >= last_fitness:
last_fitness = current_fitness
else:
board[rand_row, swap_col], board[rand_row, rand_col] = board[rand_row, rand_col], board[rand_row, swap_col]
print(last_fitness) # debugging
print(board)

View File

@ -9,20 +9,6 @@ import numpy as np
import random
import math
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
def calculate_fitness(board):
# Previous fitness
column_violations = np.sum([len(set(board[:, i])) != 9 for i in range(9)])
@ -38,28 +24,42 @@ def calculate_fitness(board):
return -(column_violations + grid_violations) # Negative because we want to maximize
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
last_fitness = calculate_fitness(board)
T = 10
print("Working...")
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 = calculate_fitness(board)
if current_fitness >= last_fitness:
last_fitness = current_fitness
else:
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
while calculate_fitness(board) < 0:
# swap col in row with random other col
rand_row = random.randrange(board_size)
rand_col = random.randrange(board_size)
swap_col = (rand_col + random.randrange(1, board_size)) % board_size
board[rand_row, rand_col], board[rand_row, swap_col] = board[rand_row, swap_col], board[rand_row, rand_col]
current_fitness = calculate_fitness(board)
if current_fitness >= last_fitness:
last_fitness = current_fitness
else:
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[rand_row, swap_col], board[rand_row, rand_col] = board[rand_row, rand_col], board[rand_row, swap_col]
T = max(T - 0.1, 0.1) # Decrease T more slowly and don't let it reach 0
print(last_fitness) # debugging

View File

@ -0,0 +1,2 @@
import numpy as np
import matplotlib.pyplot as plt