2

I'm a beginner with GProlog and I have a school project about square packing problem (packing sub-squares in a main square).

bool_to_int(B, I) :-
B -> I = 1; I = 0.

sum_size([], [], _, 0).
sum_size([X|Xs], [T|Ts], V, S) :-
    sum_size(Xs, Ts, V, S2),
    bool_to_int(X #=< V #/\ V #< X + T, I),
    S #= S2 + T * I.

Here is an independent part of my code, its purpose is to optimize the solving of square packing problem by checking if the sum of aligned squares equals a given constant (here named S). Xs are the X-axis positions of the sub-squares to pack into the main square, and Ts is their size.

Here is my problem with this code :

sum_size([0, 0, 1, 2, 2, 2], [2, 1, 1, 1, 1, 1], 0, 3).
-> true

In this example, the main square is 3x3. So, this is the expected answer to pack all of the sub-squares in the main one : [0, 0, 1, 2, 2, 2] are valid coordinates.

Now, I want GProlog to solve it for me by writing this :

length(L, 6), sum_size(L, [2, 1, 1, 1, 1, 1], 0, 3).
-> no

And I don't get here why the answer is "no" instead of giving me a possible value for L.

Mikkel
  • 7,693
  • 3
  • 17
  • 31

0 Answers0