HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Nonogram puzzle solution space

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
solutionnonogrampuzzlespace

Problem

I've written some code to brute-force enumerate the solution space of nonogram puzzles up to a certain size. It does a good job up to the first ~4 billion puzzles, but I run out of disk space to store any bigger collection of the solution space set.

Because of some incredibly interesting patterns within the encoded set of solvable instances, I'd appreciate my code being reviewed for correctness. This is an NP-complete problem, but shows self-similarity and self-affinity in the solution space with the current implementation. Please, help me shoot down this idea as quickly as possible -- I'd like to regain a productive life :)

Here's how the code is supposed to work. It outputs an XYZ point cloud of the nonogram solution space given a puzzle's maximum width, great for visualization.

The code generates all the possible permutations of an arbitrarily sized boolean image. Each permutation is a puzzle solution to at least one, if not many, boolean image input pairs. The input pairs are "projections" of the solution from each lattice direction, while having the same axis-constrained continuous runs of set bits. The only difference between solution and input is very subtle: the unset padding between contiguous runs is flexible along an axis.

Here's an example pairing of inputs and solution. Note that the pictured top-right solution may not be unique for the given input images, and the given input images don't necessarily construct only that solution. It's merely an example of the "nonogram property."

You'll notice in the code I have a peculiar encoding of the inputs and solutions as integers, essentially a traversal of the image's cells converted to a bitstring. This encoding is chosen as a visualization convenience, and I've attained similar patterns with different traversal orders. The main goal with the encoding is to reduce dimensionality for plotting with a one-to-one correspondence of images to integers, avoiding the problem of colliding identifiers.

I'm includi

Solution

-
There are no docstrings. What do these functions do? Lack of docstrings make the code hard to review, because we don't know what the functions are supposed to do.

-
The call indices(n) generates the Cartesian product of range(n) × range(n) in a particular order. A natural question is, does the order matter, or could we use itertools.product instead:

itertools.product(range(width), repeat=2)


The post explains why you've chosen the order. But how is someone reading the code supposed to know that? There needs to be a comment.

-
encode could be simplified to avoid the if:

sum(bit<<i for i, bit in enumerate(...))


-
This code:

for matrix in product((False, True), repeat=width**2):
    candidate = list(zip(*[iter(matrix)]*width))


is effectively the same as:

sol_matrices = (list(zip(*[iter(matrix)]*width)) for matrix
                in product((False, True), repeat=width**2))


and so would benefit from having its own function (which would have a name and docstring that would explain what it does).

-
The algorithm takes every nonogram puzzle, and then compares the run counts with the run counts for every nonogram puzzle of the same size. If width is \$ n \$, there are \$ 2^{n^2} \$ nonogram puzzles, and it takes \$ Ω(n^2) \$ to compute the run counts for a single puzzle, so the overall runtime is the ludicrous \$ Ω(n^24^{n^2}) \$.

This could be brought down to \$ Ω(n^22^{n^2}) \$ if you prepared a dictionary mapping run counts to sets of encoded nonogram solutions, and down to \$ Ω(n2^{n^2}) \$ if you prepared the list of all rows, prepared a dictionary mapping each row to its run counts, and then generated the puzzles and their run counts simultaneously using the Cartesian product of the rows.

Code Snippets

itertools.product(range(width), repeat=2)
sum(bit<<i for i, bit in enumerate(...))
for matrix in product((False, True), repeat=width**2):
    candidate = list(zip(*[iter(matrix)]*width))
sol_matrices = (list(zip(*[iter(matrix)]*width)) for matrix
                in product((False, True), repeat=width**2))

Context

StackExchange Code Review Q#43770, answer score: 2

Revisions (0)

No revisions yet.