From f8b889c213d5a0a38010354344da24a00ac66861 Mon Sep 17 00:00:00 2001 From: Ruben-FreddyLoafers Date: Wed, 8 Oct 2025 10:15:36 +0200 Subject: [PATCH] Adjusted assignment 1 and 2 --- 01_sudoku_hillclimber.py | 21 ++++++------- 02_sudoku_sa.py | 64 ++++++++++++++++++++-------------------- 03_euler_gen_alg.py | 2 ++ 3 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 03_euler_gen_alg.py diff --git a/01_sudoku_hillclimber.py b/01_sudoku_hillclimber.py index 8125a08..468fd36 100644 --- a/01_sudoku_hillclimber.py +++ b/01_sudoku_hillclimber.py @@ -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) diff --git a/02_sudoku_sa.py b/02_sudoku_sa.py index d2a8983..720cbb3 100644 --- a/02_sudoku_sa.py +++ b/02_sudoku_sa.py @@ -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 diff --git a/03_euler_gen_alg.py b/03_euler_gen_alg.py new file mode 100644 index 0000000..3ca1f42 --- /dev/null +++ b/03_euler_gen_alg.py @@ -0,0 +1,2 @@ +import numpy as np +import matplotlib.pyplot as plt \ No newline at end of file