use black on examples/python
This commit is contained in:
126
examples/python/hidato_sat.py
Normal file → Executable file
126
examples/python/hidato_sat.py
Normal file → Executable file
@@ -11,6 +11,7 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Solves the Hidato problem with the CP-SAT solver."""
|
||||
|
||||
from absl import app
|
||||
@@ -21,25 +22,29 @@ from ortools.sat.python import cp_model
|
||||
def build_pairs(rows, cols):
|
||||
"""Build closeness pairs for consecutive numbers.
|
||||
|
||||
Build set of allowed pairs such that two consecutive numbers touch
|
||||
each other in the grid.
|
||||
Build set of allowed pairs such that two consecutive numbers touch
|
||||
each other in the grid.
|
||||
|
||||
Returns:
|
||||
A list of pairs for allowed consecutive position of numbers.
|
||||
Returns:
|
||||
A list of pairs for allowed consecutive position of numbers.
|
||||
|
||||
Args:
|
||||
rows: the number of rows in the grid
|
||||
cols: the number of columns in the grid
|
||||
"""
|
||||
Args:
|
||||
rows: the number of rows in the grid
|
||||
cols: the number of columns in the grid
|
||||
"""
|
||||
result = []
|
||||
for x in range(rows):
|
||||
for y in range(cols):
|
||||
for dx in (-1, 0, 1):
|
||||
for dy in (-1, 0, 1):
|
||||
if (x + dx >= 0 and x + dx < rows and y + dy >= 0 and
|
||||
y + dy < cols and (dx != 0 or dy != 0)):
|
||||
result.append(
|
||||
(x * cols + y, (x + dx) * cols + (y + dy)))
|
||||
if (
|
||||
x + dx >= 0
|
||||
and x + dx < rows
|
||||
and y + dy >= 0
|
||||
and y + dy < cols
|
||||
and (dx != 0 or dy != 0)
|
||||
):
|
||||
result.append((x * cols + y, (x + dx) * cols + (y + dy)))
|
||||
return result
|
||||
|
||||
|
||||
@@ -54,7 +59,7 @@ def print_solution(positions, rows, cols):
|
||||
position = positions[k]
|
||||
board[position // cols][position % cols] = k + 1
|
||||
# Print the board.
|
||||
print('Solution')
|
||||
print("Solution")
|
||||
print_matrix(board)
|
||||
|
||||
|
||||
@@ -63,12 +68,12 @@ def print_matrix(game):
|
||||
rows = len(game)
|
||||
cols = len(game[0])
|
||||
for i in range(rows):
|
||||
line = ''
|
||||
line = ""
|
||||
for j in range(cols):
|
||||
if game[i][j] == 0:
|
||||
line += ' .'
|
||||
line += " ."
|
||||
else:
|
||||
line += '% 3s' % game[i][j]
|
||||
line += "% 3s" % game[i][j]
|
||||
print(line)
|
||||
|
||||
|
||||
@@ -84,34 +89,60 @@ def build_puzzle(problem):
|
||||
puzzle = [[6, 0, 9], [0, 2, 8], [1, 0, 0]]
|
||||
|
||||
elif problem == 2:
|
||||
puzzle = [[0, 44, 41, 0, 0, 0, 0], [0, 43, 0, 28, 29, 0, 0],
|
||||
[0, 1, 0, 0, 0, 33, 0], [0, 2, 25, 4, 34, 0, 36],
|
||||
[49, 16, 0, 23, 0, 0, 0], [0, 19, 0, 0, 12, 7, 0],
|
||||
[0, 0, 0, 14, 0, 0, 0]]
|
||||
puzzle = [
|
||||
[0, 44, 41, 0, 0, 0, 0],
|
||||
[0, 43, 0, 28, 29, 0, 0],
|
||||
[0, 1, 0, 0, 0, 33, 0],
|
||||
[0, 2, 25, 4, 34, 0, 36],
|
||||
[49, 16, 0, 23, 0, 0, 0],
|
||||
[0, 19, 0, 0, 12, 7, 0],
|
||||
[0, 0, 0, 14, 0, 0, 0],
|
||||
]
|
||||
|
||||
elif problem == 3:
|
||||
# Problems from the book:
|
||||
# Gyora Bededek: "Hidato: 2000 Pure Logic Puzzles"
|
||||
# Problem 1 (Practice)
|
||||
puzzle = [[0, 0, 20, 0, 0], [0, 0, 0, 16, 18], [22, 0, 15, 0, 0],
|
||||
[23, 0, 1, 14, 11], [0, 25, 0, 0, 12]]
|
||||
puzzle = [
|
||||
[0, 0, 20, 0, 0],
|
||||
[0, 0, 0, 16, 18],
|
||||
[22, 0, 15, 0, 0],
|
||||
[23, 0, 1, 14, 11],
|
||||
[0, 25, 0, 0, 12],
|
||||
]
|
||||
|
||||
elif problem == 4:
|
||||
# problem 2 (Practice)
|
||||
puzzle = [[0, 0, 0, 0, 14], [0, 18, 12, 0, 0], [0, 0, 17, 4, 5],
|
||||
[0, 0, 7, 0, 0], [9, 8, 25, 1, 0]]
|
||||
puzzle = [
|
||||
[0, 0, 0, 0, 14],
|
||||
[0, 18, 12, 0, 0],
|
||||
[0, 0, 17, 4, 5],
|
||||
[0, 0, 7, 0, 0],
|
||||
[9, 8, 25, 1, 0],
|
||||
]
|
||||
|
||||
elif problem == 5:
|
||||
# problem 3 (Beginner)
|
||||
puzzle = [[0, 26, 0, 0, 0, 18], [0, 0, 27, 0, 0, 19],
|
||||
[31, 23, 0, 0, 14, 0], [0, 33, 8, 0, 15, 1],
|
||||
[0, 0, 0, 5, 0, 0], [35, 36, 0, 10, 0, 0]]
|
||||
puzzle = [
|
||||
[0, 26, 0, 0, 0, 18],
|
||||
[0, 0, 27, 0, 0, 19],
|
||||
[31, 23, 0, 0, 14, 0],
|
||||
[0, 33, 8, 0, 15, 1],
|
||||
[0, 0, 0, 5, 0, 0],
|
||||
[35, 36, 0, 10, 0, 0],
|
||||
]
|
||||
elif problem == 6:
|
||||
# Problem 15 (Intermediate)
|
||||
puzzle = [[64, 0, 0, 0, 0, 0, 0, 0], [1, 63, 0, 59, 15, 57, 53, 0],
|
||||
[0, 4, 0, 14, 0, 0, 0, 0], [3, 0, 11, 0, 20, 19, 0, 50],
|
||||
[0, 0, 0, 0, 22, 0, 48, 40], [9, 0, 0, 32, 23, 0, 0, 41],
|
||||
[27, 0, 0, 0, 36, 0, 46, 0], [28, 30, 0, 35, 0, 0, 0, 0]]
|
||||
puzzle = [
|
||||
[64, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 63, 0, 59, 15, 57, 53, 0],
|
||||
[0, 4, 0, 14, 0, 0, 0, 0],
|
||||
[3, 0, 11, 0, 20, 19, 0, 50],
|
||||
[0, 0, 0, 0, 22, 0, 48, 40],
|
||||
[9, 0, 0, 32, 23, 0, 0, 41],
|
||||
[27, 0, 0, 0, 36, 0, 46, 0],
|
||||
[28, 30, 0, 35, 0, 0, 0, 0],
|
||||
]
|
||||
return puzzle
|
||||
|
||||
|
||||
@@ -123,18 +154,16 @@ def solve_hidato(puzzle, index):
|
||||
r = len(puzzle)
|
||||
c = len(puzzle[0])
|
||||
if not visualization.RunFromIPython():
|
||||
print('')
|
||||
print('----- Solving problem %i -----' % index)
|
||||
print('')
|
||||
print(('Initial game (%i x %i)' % (r, c)))
|
||||
print("")
|
||||
print("----- Solving problem %i -----" % index)
|
||||
print("")
|
||||
print(("Initial game (%i x %i)" % (r, c)))
|
||||
print_matrix(puzzle)
|
||||
|
||||
#
|
||||
# declare variables
|
||||
#
|
||||
positions = [
|
||||
model.NewIntVar(0, r * c - 1, 'p[%i]' % i) for i in range(r * c)
|
||||
]
|
||||
positions = [model.NewIntVar(0, r * c - 1, "p[%i]" % i) for i in range(r * c)]
|
||||
|
||||
#
|
||||
# constraints
|
||||
@@ -153,8 +182,7 @@ def solve_hidato(puzzle, index):
|
||||
# We use an allowed assignment constraint to model it.
|
||||
close_tuples = build_pairs(r, c)
|
||||
for k in range(0, r * c - 1):
|
||||
model.AddAllowedAssignments([positions[k], positions[k + 1]],
|
||||
close_tuples)
|
||||
model.AddAllowedAssignments([positions[k], positions[k + 1]], close_tuples)
|
||||
|
||||
#
|
||||
# solution and search
|
||||
@@ -170,12 +198,10 @@ def solve_hidato(puzzle, index):
|
||||
val = solver.Value(var)
|
||||
x = val % c
|
||||
y = val // c
|
||||
color = 'white' if puzzle[y][x] == 0 else 'lightgreen'
|
||||
output.AddRectangle(x, r - y - 1, 1, 1, color, 'black',
|
||||
str(i + 1))
|
||||
color = "white" if puzzle[y][x] == 0 else "lightgreen"
|
||||
output.AddRectangle(x, r - y - 1, 1, 1, color, "black", str(i + 1))
|
||||
|
||||
output.AddTitle('Puzzle %i solved in %f s' %
|
||||
(index, solver.WallTime()))
|
||||
output.AddTitle("Puzzle %i solved in %f s" % (index, solver.WallTime()))
|
||||
output.Display()
|
||||
else:
|
||||
print_solution(
|
||||
@@ -183,10 +209,10 @@ def solve_hidato(puzzle, index):
|
||||
r,
|
||||
c,
|
||||
)
|
||||
print('Statistics')
|
||||
print(' - conflicts : %i' % solver.NumConflicts())
|
||||
print(' - branches : %i' % solver.NumBranches())
|
||||
print(' - wall time : %f s' % solver.WallTime())
|
||||
print("Statistics")
|
||||
print(" - conflicts : %i" % solver.NumConflicts())
|
||||
print(" - branches : %i" % solver.NumBranches())
|
||||
print(" - wall time : %f s" % solver.WallTime())
|
||||
|
||||
|
||||
def main(_):
|
||||
@@ -194,5 +220,5 @@ def main(_):
|
||||
solve_hidato(build_puzzle(pb), pb)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
app.run(main)
|
||||
|
||||
Reference in New Issue
Block a user