Constraint Logic Programming in Prolog: Hanjie Puzzle Solver

Topics: The Grid, Constraint logic programming, Constraint programming Pages: 25 (9928 words) Published: October 30, 2014
Constraint Logic Programming in Prolog:
Hanjie Puzzle Solver
Lu´ıs Cleto and Jo˜ao Marinheiro
FEUP-PLOG, Turma 3MIEIC05, Group 23
{ei11077,ei11129}@fe.up.pt
http://www.fe.up.pt

Abstract. The purpose of this project was to use constraint logic programming in Prolog to implement a solver for the 2D puzzle, Hanjie. For this purpose we used the clp(FD) library provided by SICStus Prolog 4.2.3, specifically the sum/3 and automaton/3 combinatorial constraints. The program we developed is able to solve puzzles with dimensions up to 88x88, with only one possible solution, in less than one second. When there are multiple solutions, the execution time for the obtaining the first solution varies with the number of possible solutions.

These results show that the execution time of the program is primarily affected by the amount of possible results. While larger grid dimensions do increase the execution time, the increase is linear if the number of possible solutions is maintained. On the other hand, increasing the number of possible solutions will lead to an exponential growth in execution time.

1

Introduction

The goal of this project is to use constraint logic programming in Prolog to develop a logic program capable of solving a decision problem in the form of the 2D puzzle, Hanjie. This puzzle consists of a rectangular grid with ’clues’ on top of every column and to the left of every row that indicate the number and length of gray blocks in that column/row. To achieve this goal, first had to be chosen the data structures that would be used to represent the problem. It was decided to use lists of lists, containing the clues for each column/row in every sublist, and a similar structure for the puzzle grid where every sublist is a row of the grid. Every square of the grid can be either a 0 or a 1 where a 0 represents a blank square and 1 represents a gray square. Our implementation also allows the lists of clues to be partially or completely uninstantiated, providing possible values for the uninstantiated elements. Additionally, it allows the puzzle to be written to a file instead of to the terminal as some solutions may be too large to be displayed correctly on the terminal.

The most complex aspect of creating constraints for this puzzle is creating a set of rules that will ensure that every gray block occurs in sequence on the proper row, with the proper dimensions, without bordering any other blocks in that row. To solve this particular problem of sequences, it was opted to use automaton/3

2

Hanjie Puzzle Solver

constraints to establish the main rules of the puzzle and ensure the integrity of the gray blocks in every row and column.
In addition, a subgoal of the project was to allow for random generation of valid hanjie puzzles, which was achieved with an algorithm similar to how a human being would create the puzzle. The grid is created first by randomly filling it with painted squares and the clues for each row/column are obtained afterwards, this way the puzzle is guaranteed to have at least one possible solution. The remainder of this article will contain several sections dedicated to describing the problem, the data files containing examples of the problem to solve, the decision variables used, the constraints implemented, the adopted search strategy, the solution presentation, the results obtained and the conclusion we achieved. Additionally it will contain in the annex the source code developed for the project.

2

Problem Description

Hanjie is a two dimensional puzzle that starts with a grid of blank squares with clues on top of every column and to the left of every row. Every list of clues contains numbers that indicate the length of every painted block in that row. The length of the list will be the number of blocks that row must contain and every block must be separated from the others by at least one blank square. Additionally, the order of the clues is the order of the blocks in that...

References: 1. Russel, Stuart; Norvig, Peter: Artificial Intelligence: A Modern Approach. Pearson,
New Jersey (2009)
2. Sterling, Lehon; Shapiro, Ehud: The Art of Prolog: Advanced Programming Techniques. The MIT Press, Massachusetts (1994)
3
5. Ullman, Jeffrey; Motwani, Rajeev; Hopcroft, John: Introduction to Automata Theory, Languages, and Computation. Addison-Wesley (2001)
6
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Prolog Essay
  • Fundamentals of Programming with Algorithms and Logic Essay
  • Intro to programming logic Essay
  • Solver: Constraint Satisfaction and Dialogue Box Essay
  • Programming Logic Essay
  • Essay about Programming and Logic
  • Puzzles Essay
  • Introductions to Prolog Essay

Become a StudyMode Member

Sign Up - It's Free