-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unused functions and improve docs of remaining functions.
- Loading branch information
1 parent
2e87a79
commit db6ef46
Showing
1 changed file
with
59 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,151 +1,88 @@ | ||
import networkx as nx | ||
|
||
def row_string(fixed, size, row, col_sep='|', padding=0): | ||
""" | ||
Converts a dict of fixed cells to a string. | ||
fixed - a cell-symbol dictionary | ||
size - the dimension of the PLS | ||
row - the row to convert to a string | ||
col_sep - a string used as a separator between columns | ||
padding - number of spaces either side of a symbol | ||
def row(cell, size): | ||
""" | ||
s = col_sep | ||
for col in range(size): | ||
symbol = fixed.get(cell(row, col, size)) | ||
if symbol: | ||
s += ' '*padding + str(symbol) | ||
else: | ||
s += ' '*padding + '.' | ||
s += col_sep | ||
return s | ||
The row label of the row in a square of dimension 'size' | ||
containing the cell with label 'cell'. | ||
def dict_to_string(fixed, size, col_sep = '', row_sep = '', padding = 0, top = '', bottom = ''): | ||
:param cell: A cell index. | ||
:param size: The number of rows and columns. | ||
:return The row label of the row containing the given cell. | ||
""" | ||
Returns a puzzle string of dimension 'boxsize' from a dictionary of | ||
'fixed' cells. | ||
padding : number of spaces between adjacent symbols | ||
row_end : a string added to the last symbol of every row | ||
col_sep : a string added to the last symbol of every column | ||
""" | ||
rows = range(size) | ||
s = top | ||
for row in rows[:-1]: | ||
s += row_string(fixed, size, row, col_sep, padding) | ||
s += row_sep | ||
s += row_string(fixed, size, rows[size-1], col_sep, padding) | ||
s += ' '*padding | ||
s += bottom | ||
return s | ||
|
||
def dict_to_string_simple(fixed, size): | ||
return dict_to_string(fixed, size, col_sep = '|', row_sep = '\n', padding = 0, top = '', bottom = '') | ||
|
||
def cell(row, column, size): | ||
"""The label of the cell in position (row, col).""" | ||
return (row * size) + column + 1 | ||
|
||
def row(cell, size): | ||
"""The row label of the row in a square of dimension 'size' | ||
containing the cell with label 'cell'.""" | ||
return int((cell - 1)/size) | ||
|
||
def col(cell, size): | ||
"""The column label of the column in a square of dimension 'size' | ||
containing the cell with label 'cell'.""" | ||
""" | ||
The column label of the column in a square of dimension 'size' | ||
containing the cell with label 'cell'. | ||
:param cell: A cell index. | ||
:param size: The number of rows and columns. | ||
:return The column label of the row containing the given cell. | ||
""" | ||
return (cell - 1) % size | ||
|
||
def row_r(cell, size): | ||
"""A range of all labels of cells in the same row as 'cell'.""" | ||
""" | ||
A range of all labels of cells in the same row as 'cell'. | ||
:param cell: A cell index. | ||
:param size: The number of rows and columns. | ||
:return A range of labels in the same row as cell. | ||
""" | ||
return range(row(cell, size)*size + 1, (row(cell, size) + 1)*size + 1) | ||
|
||
def col_r(cell, size): | ||
"""A range of all labels of cells in the same column as 'cell'.""" | ||
return range(col(cell, size) + 1, size**2 + 1, size) | ||
|
||
def vertex(cell, size): | ||
"""The row and column labels of 'cell', as a pair.""" | ||
return (row(cell, size), col(cell, size)) | ||
""" | ||
A range of all labels of cells in the same column as 'cell'. | ||
def rect_r(tl, br, size): | ||
"""A rectangular range which extends from tl in the top-left to br in the | ||
bottom right.""" | ||
(i,j) = vertex(tl, size) | ||
(k,l) = vertex(br, size) | ||
cells = [(x, y) for x in range(i, k + 1) for y in range(j, l + 1)] | ||
return map(lambda x: cell(x[0], x[1], size), cells) | ||
:param cell: A cell index. | ||
:param size: The number of rows and columns. | ||
:return A range of labels in the same column as cell. | ||
""" | ||
return range(col(cell, size) + 1, size**2 + 1, size) | ||
|
||
def list_assignment(partial_latin_square, size): | ||
"""The (canonical) list assignment for a partial latin square. The list of | ||
def list_assignment(P, size): | ||
""" | ||
List assignment for a partial latin square. The list of | ||
a filled cell is the list containing just the element in that cell. The | ||
list of an empty cell contains only those symbols not already used in the | ||
same row and column as that cell.""" | ||
P = partial_latin_square | ||
same row and column as that cell. | ||
:param P: A partial latin square fixed cell dictionary. | ||
:param size: The number of rows and colums of the completed latin square. | ||
:return A dictionary representing the list assignment corresponding to P. | ||
""" | ||
L = {} | ||
# initialise lists | ||
for i in range(1, size**2 + 1): | ||
if i in P.keys(): | ||
L[row(i,size),col(i,size)] = [P[i]] | ||
else: | ||
L[row(i,size),col(i,size)] = list(range(1, size + 1)) | ||
if i in P.keys(): | ||
L[row(i,size),col(i,size)] = [P[i]] | ||
else: | ||
L[row(i,size),col(i,size)] = list(range(1, size + 1)) | ||
# update lists (remove used symbols from lists of same row/col) | ||
for i in range(1, size**2 + 1): | ||
if i in P.keys(): | ||
# then remove P[i] from any list of a cell not in P from the same row/col | ||
for j in list(row_r(i, size)) + list(col_r(i, size)): | ||
if j not in P.keys(): | ||
if P[i] in L[row(j, size), col(j, size)]: | ||
L[row(j, size), col(j, size)].remove(P[i]) | ||
if i in P.keys(): | ||
# then remove P[i] from any list of a cell not in P from the same row/col | ||
for j in list(row_r(i, size)) + list(col_r(i, size)): | ||
if j not in P.keys(): | ||
if P[i] in L[row(j, size), col(j, size)]: | ||
L[row(j, size), col(j, size)].remove(P[i]) | ||
return L | ||
|
||
def orthogonal_array(L, size): | ||
return [(i,j,L[i,j]) for i,j in range(size)] | ||
|
||
def sim_2_csm(P, size): | ||
"""Fixed cells from a list colouring. | ||
Convert a symbols-to-(row,col) pairs dictionary to a cell-label-to-symbol | ||
dictionary. | ||
def pls_list_colouring_problem(fixed, size): | ||
""" | ||
L = {} | ||
for i in P: | ||
for j in P[i]: | ||
row = j[0] | ||
column = j[1] | ||
L[cell(row, column, size)] = int(i) | ||
return L | ||
|
||
def com_2_csm(P, size): | ||
"""Fixed cells from a list of row dictionaries. | ||
Return a complete digraph (including self-loops on every node) with a list-assignment | ||
to edges such that the list on edge (i, j) is the set of all symbols not used in row i | ||
or column j in the partial latin square represented by the input dictionary. | ||
Convert a list of column-labels-to-symbols dictionaries to a | ||
cell-label-to-symbol dictionary. | ||
:param fixed: A dictionary of filled cells of a partial latin square. | ||
:param size: Number of rows and columns in the completed latin square. | ||
:return A complete digraph with edge list-assignment representing a partial latin square completion problem. | ||
""" | ||
L = {} | ||
for i in range(len(P)): | ||
for j in P[i]: | ||
if P[i][j] != '.': | ||
row = i | ||
col = j | ||
L[cell(row, col, size)] = int(P[i][j]) | ||
return L | ||
|
||
|
||
def pls_list_colouring_problem(fixed, size): | ||
""" | ||
Return a complete digraph (including self-loops on every node) with a list-assignment | ||
to edges such that the list on edge (i, j) is the set of all symbols not used in row i | ||
or column j in the partial latin square represented by the input dictionary. | ||
:param fixed: A dictionary of filled cells of a partial latin square. | ||
:param size: Number of rows and columns in the completed latin square. | ||
:return A complete digraph with edge list-assignment representing a partial latin square completion problem. | ||
""" | ||
G = nx.complete_graph(size, create_using = nx.DiGraph) | ||
for node in G.nodes(): | ||
G.add_edge(node, node) | ||
nx.set_edge_attributes(G, list_assignment(fixed, size), "permissible") | ||
return(G) | ||
G = nx.complete_graph(size, create_using = nx.DiGraph) | ||
for node in G.nodes(): | ||
G.add_edge(node, node) | ||
nx.set_edge_attributes(G, list_assignment(fixed, size), "permissible") | ||
return G | ||
|