Skip to content

Commit

Permalink
Add constraints to slightly speed up Nurikabe example
Browse files Browse the repository at this point in the history
Associated with issue #10
  • Loading branch information
obijywk committed Jul 4, 2020
1 parent 34d5dc4 commit d6a8dce
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions examples/nurikabe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
(8, 8): 4,
}


def constrain_sea(sym, sg, rc):
"""Add constraints to the sea cells."""

Expand All @@ -42,7 +41,10 @@ def constrain_sea(sym, sg, rc):
p = Point(y, x)
sg.solver.add(Implies(
sg.cell_is(p, sym.B),
rc.region_id_grid[p] == sea_id
And(
rc.region_id_grid[p] == sea_id,
rc.region_size_grid[p] == HEIGHT * WIDTH - sum(GIVENS.values())
)
))
sg.solver.add(Implies(
sg.cell_is(p, sym.W),
Expand Down Expand Up @@ -110,7 +112,12 @@ def main():
sym = grilops.SymbolSet([("B", chr(0x2588)), ("W", " ")])
lattice = grilops.get_rectangle_lattice(HEIGHT, WIDTH)
sg = grilops.SymbolGrid(lattice, sym)
rc = grilops.regions.RegionConstrainer(lattice, solver=sg.solver)
rc = grilops.regions.RegionConstrainer(
lattice,
solver=sg.solver,
min_region_size=min(GIVENS.values()),
max_region_size=HEIGHT * WIDTH - sum(GIVENS.values())
)

constrain_sea(sym, sg, rc)
constrain_islands(sym, sg, rc)
Expand Down

0 comments on commit d6a8dce

Please sign in to comment.