Prolog exercise that has to connect a conveyor belt to an oven in a factory is not working

101 Views Asked by At

I want to create a Prolog program that can connect conveyor belts to ovens and an output belt. The program has the following facts:

factory(12,10).
oven((2,5),(4,5)). 
oven((7,5),(7,5)).
blocked(0,9).
blocked(3,1).
blocked(5,1).
blocked(4,9).
blocked(5,9). 

Where factory(X,Y) gives you the width of the factory (X) and the height (Y), oven((X,Y),(OutputX,OutputY)) gives you the x-y coordinates of the bottom leftcorner of the oven (each oven is of size 3 by 3) and their output coordinates (OutputX, OutputY), blocked(X,Y) represents a square in the factory that cannot contain an oven or a belt.

Then, given the following code pos_empty(X,Y) that returns whether a position can have a belt or not, create a term serveOven(Oven, Length, InBelts, Belts) that constructs a belt line from each starting belt in the list InBelts to the output coordinates of the Oven and that line can have a max length of Length. The code to check if a position is empty:

pos_empty(X,Y) :-
    \+(blocked(X,Y)),
    \+(has_oven(X,Y)).

has_oven(X,Y) :-
    oven((X1,Y1), _),
    X2 is X1 + 3,
    Y2 is Y1 + 3,
    between(X1, X2, X),
    between(Y1, Y2, Y).

The code I got is the following:

serveOven( oven(_, (OutX, OutY)), Length, InBelts, Belts) :-
findall(Belt, (member(InBelt, InBelts), make_belt(InBelt, OutX, OutY, Length, Belt)), Belts).

make_belt((X,Y), OutX, OutY, Length, [(X,Y) | Tail]) :-
pos_empty(X,Y),
(X = OutX, Y = OutY;
(X < OutX, NewX is X + 1, make_belt((NewX, Y), OutX, OutY, Length, Tail));
(X > OutX, NewX is X - 1, make_belt((NewX, Y), OutX, OutY, Length, Tail));
(Y < OutY, NewY is Y + 1, make_belt((X, NewY), OutX, OutY, Length, Tail));
(Y > OutY, NewY is Y - 1, make_belt((X, NewY), OutX, OutY, Length, Tail))),
length(Tail, Len),
Len < Length.

But this returns nothing. Can anybody help me? Thanks!

So, as said in the details: The code I got is the following:

serveOven( oven(_, (OutX, OutY)), Length, InBelts, Belts) :-
findall(Belt, (member(InBelt, InBelts), make_belt(InBelt, OutX, OutY, Length, Belt)), Belts).

make_belt((X,Y), OutX, OutY, Length, [(X,Y) | Tail]) :-
pos_empty(X,Y),
(X = OutX, Y = OutY;
(X < OutX, NewX is X + 1, make_belt((NewX, Y), OutX, OutY, Length, Tail));
(X > OutX, NewX is X - 1, make_belt((NewX, Y), OutX, OutY, Length, Tail));
(Y < OutY, NewY is Y + 1, make_belt((X, NewY), OutX, OutY, Length, Tail));
(Y > OutY, NewY is Y - 1, make_belt((X, NewY), OutX, OutY, Length, Tail))),
length(Tail, Len),
Len < Length.

When running the query serveOven(oven((2,5),(4,5)), 20, [(11,2)], Belts). this resulted in an empty list of Belts. Which should not happen.

0

There are 0 best solutions below