Skip to content

Commit

Permalink
bug detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Horadmard committed Jun 9, 2024
1 parent 80a37a2 commit 9e89964
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def cost_func(*, sudoku: list):
num = sudoku[row].count(item)
# print(item , '-', num)

if num:
if num > 1:
cost += num - 1


Expand All @@ -82,7 +82,7 @@ def cost_func(*, sudoku: list):

num = tra[row].count(item)

if num:
if num > 1:
cost += num - 1


Expand Down
24 changes: 24 additions & 0 deletions src/new_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


import random


def fill_missing_numbers(sudoku):
for block_row in range(0, 9, 3):
for block_col in range(0, 9, 3):
fill_block(sudoku, block_row, block_col)

def fill_block(sudoku, block_row, block_col):
empty_cells = []
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for r in range(block_row, block_row + 3):
for c in range(block_col, block_col + 3):
if sudoku[r][c] != 0:
numbers.remove(sudoku[r][c])
else:
empty_cells.append((r, c))

random.shuffle(numbers)
for (r, c), number in zip(empty_cells, numbers):
sudoku[r][c] = number
2 changes: 1 addition & 1 deletion src/simulated_annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def solve(*, sudoku, initial_temp=1000.0, cooling_rate=0.995, min_temp=0.1) -> l
if delta_energy < 0 or random.uniform(0, 1) < math.exp(-delta_energy / T):
cur_solution = new_solution
cur_energy = new_energy
# print(cur_energy)

T = T * cooling_rate


print(cur_energy)

Expand Down
31 changes: 31 additions & 0 deletions src/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from data import *
from initial import *
from new_func import *
from new_solutions import *
from simulated_annealing import *

Expand All @@ -27,3 +28,33 @@
# for row in original:
# print(row)

# matrix = [

# [1, 3, 2, 6, 9, 4, 5, 7, 8],
# [6, 5, 7, 8, 1, 3, 4, 9, 2],
# [9, 4, 8, 7, 2, 5, 1, 6, 3],
# [4, 2, 1, 9, 5, 8, 7, 3, 6],
# [7, 6, 9, 1, 3, 2, 8, 5, 4],
# [3, 8, 5, 4, 6, 7, 9, 2, 1],
# [8, 7, 4, 2, 9, 6, 3, 1, 5],
# [2, 9, 3, 5, 8, 1, 6, 4, 7],
# [5, 1, 6, 3, 7, 4, 2, 8, 9]


# ]

# print (cost_func(sudoku=matrix))

c = 0

orginal = importData()

mat = copy.deepcopy(orginal)
fill_missing_numbers(mat)

for row in mat:
print(row)

num = cost_func(sudoku=mat)


0 comments on commit 9e89964

Please sign in to comment.