Adjusted assignment 1 and 2
parent
27ca92b80b
commit
f8b889c213
|
|
@ -25,16 +25,17 @@ last_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)]) # -9
|
||||||
|
|
||||||
print("Working...")
|
print("Working...")
|
||||||
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):
|
# swap col in row with random other col
|
||||||
for col in range(board_size):
|
rand_row = random.randrange(board_size)
|
||||||
# swap col in row with random other col
|
rand_col = random.randrange(board_size)
|
||||||
rand_col = random.randrange(board_size)
|
swap_col = (rand_col + random.randrange(1, board_size)) % 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)])
|
board[rand_row, rand_col], board[rand_row, swap_col] = board[rand_row, swap_col], board[rand_row, rand_col]
|
||||||
if current_fitness >= last_fitness:
|
current_fitness = -np.sum([len(set(board[:, i])) != 9 for i in range(9)])
|
||||||
last_fitness = current_fitness
|
if current_fitness >= last_fitness:
|
||||||
else:
|
last_fitness = current_fitness
|
||||||
board[row, rand_col], board[row, col], = board[row, col], board[row, rand_col] # swap back
|
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(last_fitness) # debugging
|
||||||
|
|
||||||
print(board)
|
print(board)
|
||||||
|
|
|
||||||
|
|
@ -9,20 +9,6 @@ import numpy as np
|
||||||
import random
|
import random
|
||||||
import math
|
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):
|
def calculate_fitness(board):
|
||||||
# Previous fitness
|
# Previous fitness
|
||||||
column_violations = np.sum([len(set(board[:, i])) != 9 for i in range(9)])
|
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
|
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)
|
last_fitness = calculate_fitness(board)
|
||||||
T = 10
|
T = 10
|
||||||
|
|
||||||
print("Working...")
|
print("Working...")
|
||||||
while calculate_fitness(board) < 0: # Continue until no violations
|
while calculate_fitness(board) < 0:
|
||||||
for row in range(board_size):
|
# swap col in row with random other col
|
||||||
for col in range(board_size):
|
rand_row = random.randrange(board_size)
|
||||||
# swap col in row with random other col
|
rand_col = random.randrange(board_size)
|
||||||
rand_col = random.randrange(board_size)
|
swap_col = (rand_col + random.randrange(1, board_size)) % board_size
|
||||||
board[row, col], board[row, rand_col] = board[row, rand_col], board[row, col]
|
|
||||||
current_fitness = calculate_fitness(board)
|
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
|
if current_fitness >= last_fitness:
|
||||||
else:
|
last_fitness = current_fitness
|
||||||
p = math.e ** (-(last_fitness - current_fitness) / T) # adjusted formula
|
else:
|
||||||
# print(p) # debugging
|
p = math.e ** (-(last_fitness - current_fitness) / T) # adjusted formula
|
||||||
if p > random.random(): # if probability occurs
|
# print(p) # debugging
|
||||||
last_fitness = current_fitness
|
if p > random.random(): # if probability occurs
|
||||||
else:
|
last_fitness = current_fitness
|
||||||
board[row, rand_col], board[row, col] = board[row, col], board[row, rand_col] # swap back
|
else:
|
||||||
T = max(T - 0.1, 0.1) # Decrease T more slowly and don't let it reach 0
|
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
|
print(last_fitness) # debugging
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
Loading…
Reference in New Issue