Prolog restrictions

I need to solve the following problem in Prolog. Alex, Fred and Jane are 3 children who made a seesaw from a 10 meter long board. They mark 11 seats along the board, each at a distance of one meter. The number of seats is -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, where 0 is the center position. Alex, Fred and Jane weigh 4, 3 and 2 units of weight. They are interested to know how they can make the swing balance when all three are sitting somewhere along the board, and each child should sit in a different position.

One place to sit is when Alex, Fred and Jane are in positions -4, 2 and 5, because (-4 * 4) + (2 * 3) + (5 * 2) = -16 + 6 + 10 = 0 Another seat for Alex, Fred and Jane is -4, 4 and 2, and another is -3, 2 and 3.

I tried to solve this problem, but we get the error: ERROR: Error like: integer' expected, found [_ G11889, _G11892, _G11895]'

Can someone help explain where I did wrong / how to do it?

Thank you very much in advance

 :-use_module(library(clpfd)). find(Seats):- Seats=[Alex, Fred, Jane], Seats in -5..5, all_different(Seats), (Alex*4+Fred*3+Jane*2)#=0, % I am not sure about this line labeling([],Seats). 
+6
source share
1 answer

I would use symmetry breaking to reduce the number of solutions. Simple symmetry is that (A, F, J) is a solution, as well as (-A, -F, -J). Thus, we could limit J #> = 0 and keep in mind if J # \ = 0, that is, an inverted solution.

So, first we start by importing the CLP library (FD):

 Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.16) Copyright (c) 1990-2014 University of Amsterdam, VU Amsterdam ?- use_module(library(clpfd)). 

And then we formulate our request, instead of (in) / 2 we should use (ins) / 2, as larsman already noted. Using (in) / 2 reasons is also a messsage error. Brackets do not need a linear shape. So we get:

 ?- Seats=[Alex, Fred, Jane], Seats ins -5..5, Jane #> 0, all_different(Seats), Alex*4+Fred*3+Jane*2 #= 0, label(Seats), write(Seats), nl, fail; true. [-4,2,5] [-4,4,2] [-3,2,3] [-2,0,4] [-2,2,1] [-1,-2,5] [-1,0,2] [0,-2,3] [1,-4,4] true. 

You get unique solutions for different weights, and if you need no one to sit in the middle of a swing. Weights 15, 10 and 6 do this work. Here's an example run, notice the modified (ins) / 2 statement:

 ?- Seats=[Alex, Fred, Jane], Seats ins -5.. -1\/1..5, Jane #> 0, all_different(Seats), Alex*15+Fred*10+Jane*6 #= 0, label(Seats), write(Seats), nl, fail; true. [-4,3,5] true. 

Bye

+3
source

Source: https://habr.com/ru/post/969955/


All Articles