How to make path restrictions using Prolog CLP FD?

482 views Asked by At

I'm trying to use restrictions programming through Prolog CLP FD to solve a puzzle that was proposed. This puzzle consists in the next simple rules:

Yin Yang puzzle description

Now, in my code, I already cover the restrictions for the 2x2 grid AND that one piece MUST be connected to AT LEAST one of the same color.

The problem is that I cannot find a way to build the restriction that says that one piece MUST have a PATH (be connected) to all the other pieces of the same color, without passing through pieces of the opposite color, so I'm getting this kind of outputs:

0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 0

0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 1

where the 1s are not all connected to each other.

How can I write this kind of graph restrictions in CLP FD?

EDIT: I am using SICStus Prolog.

2

There are 2 answers

7
mat On

To reword your situation so that we can more clearly think about it:

  1. you can already generate answers
  2. but the answers are currently too general.

To make your program more specific, you must find a condition that is currently violated in one of the answers you generate, but which must hold in every solution, and then express this condition with constraints.

For example, consider again you case:

0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 1

Which condition is violated here? Obviously, the 1 pieces are not along a path. But describing a full path with CLP(FD) is extremely tedious, and since this is apparently taken from an exam or homework question, the idea suggests itself that there is a simple local criterion to express the desired condition.

By "local", I mean that you only have to take into account a few neighbours instead of the whole board.

So, consider again the 1 pieces. Obviously, every 1 piece has a neighbour that is also 1 in this answer. What else? Does every 1 piece have 2 neighbours? No currently, not. Should every 1 piece have 2 neighbours that are also 1? If not, how many exceptions are admissible?

If you think along these conditions, you will definitely obtain a nice solution.

One hint: Sometimes reified constraints are useful in such tasks. This means that you can for example say: B #<==> (X #= Y), and let B denote whether X #= Y holds. Note that you may not even need this in this case.

0
Taku Koyahata On

did either of you solve this problem at last?

I'm very interested in this problem and want to see the code,if exists.

I think circuit/1 cannot help and want to see the usage for solving this.