Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraints #4

Merged
merged 5 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions board.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from main import Graph, Variable

# Code to load the box constratins from constraints.txt
with open('constraints.txt','r') as file: lines=file.readlines()
lines = [line.replace('\n','').replace(' ','') for line in lines]

# 0's are blank cells (no-assignments)
board = [
[0,0,3, 0,2,0, 6,0,0],
Expand All @@ -23,7 +27,7 @@ def get_adjacent(board,i,j): # Return a list of adjacent indexs as tuples, use i
if j!=0: adjacent.append(f'board[{i}][{j-1}]') # Left (if not left most col)
return adjacent

def alldiff(board,i,j): # Given the puzzle and a cell's index, this function returns the all diff as a binary constraint relative to that cell
def get_alldiff_constraints(board,i,j): # Given the puzzle and a cell's index, this function returns the all diff as a binary constraint relative to that cell
given = (i,j)
constraints = []
# Get all the other indexs in the same row and column as a tuple
Expand All @@ -34,8 +38,15 @@ def alldiff(board,i,j): # Given the puzzle and a cell's index, this function ret
if (x,j)!=given: constraints.append(f'board[{i}][{j}]!=board[{x}][{j}]')
return constraints

def get_box_constraints(i,j):
return [line for line in lines if f'board[{i}][{j}]' in line]

def get_constraints(board,i,j):
constraints = alldiff(board,i,j)
constraints = []
all_diff_constraints = get_alldiff_constraints(board,i,j)
constraints.append(all_diff_constraints)
box_constraints = get_box_constraints(i,j)
constraints.append(box_constraints)
return constraints

def sudokuGraphify(board:list)->Graph:
Expand All @@ -45,16 +56,21 @@ def sudokuGraphify(board:list)->Graph:
for i in range(0,len(board),1):
for j in range(0,len(board),1):
constraints = get_constraints(board,i,j) # generate all the constraints for the current (from) node (i,j)

# If the node has a value, add it to the constraints as board[i][j]==value
if board[i][j]!=0: constraints.append(f'board[{i}][{j}]=={board[i][j]}')

adjacent = get_adjacent(board,i,j)
for cell in adjacent:
from_node = Variable(f'board[{i}][{j}]',board[i][j],sudokuDomain)
from_node = Variable(f'board[{i}][{j}]',board[i][j],sudokuDomain if board[i][j]==0 else [board[i][j]]) # Last field to make sure that the domain for assigned variables is limited to the value assigned to it alone
to_node = Variable(cell,eval(cell),sudokuDomain)
graph.add_edge(from_node,to_node,constraints)

return graph


# Contains the correct amount of nodes
# Contains the correct amount of direct edges
# Contains the current alldiff binarized constraints
# Mising the box constraints
# Constains box constraints
graph: Graph = sudokuGraphify(board)
30 changes: 0 additions & 30 deletions boxConstraints.txt

This file was deleted.

36 changes: 36 additions & 0 deletions constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
board = [
[0,0,3, 0,2,0, 6,0,0],
[9,0,0, 3,0,5, 0,0,1],
[0,0,1, 8,0,6, 4,0,0],

[0,0,8, 1,0,2, 9,0,0],
[7,0,0, 0,0,0, 0,0,8],
[0,0,6, 7,0,8, 2,0,0],

[0,0,2, 6,0,9, 5,0,0],
[8,0,0, 2,0,3, 0,0,9],
[0,0,5, 0,1,0, 3,0,0],
]
def constraints(x, y, w, z):
f = open("constraints2.txt", "a")

for i in range(x, y):
for j in range(w, z):
for k in range(x,y):
for l in range(w, z):
if (k,l) != (i, j):
temp = f'board[{i}][{j}]!=board[{k}][{l}]'
f.write(f'{temp}\n')
print("board[",i,"]","[",j,"]!=","board[",k,"]","[",l,"]" ) # have to fix spacing here
f.close()
constraints(0,3, 0,3)
constraints(0,3, 3,6)
constraints(0,3, 6,9)

constraints(3,6, 0,3)
constraints(3,6, 3,6)
constraints(3,6, 6,9)

constraints(6,9, 0,3)
constraints(6,9, 3,6)
constraints(6,9, 6,9)
Loading