Prolog - testing values of a variable

510990f59852cf37a3c904d0d1901b56
0
TiagoISMT 101 May 31, 2008 at 12:59

Hello,

I’m having some trouble in my prolog homework for some class, and needed some assistance.
I’m kind of newbie in prolog but I’ve been trying to read some tutorials to learn the “mysteries” behind the recursive functions and the backtracking.
My project its kind of simple, i have to make an agent (a) move randomly through a room in 4 directions (up,down,left,right) and he has to find the goal (o), but there are some walls (x), so he can only move to the spots that are free (-) or to the goal.
To accomplish this I’m using 1 fact that’s a list of all the “world”, the list has 16 members and it’s printed has an array of 4x4.

To move the agent, I’m adding or subtracting to his index an amount of 1 (if its to the right or left), or a amount of 4 (if its to up or down).

The problem begins here, i need to make sure that the agent doesn’t move to some values (A1>0 and A1<17, since the values A1=2,A1=10,A1=11 are walls he can’t move there too), for that i’m trying to build up a recursive function that tests the values before the agent move (function test), if the eventual move of the agent its not valid the function gets a new value to the Random move (moves up,down,left,right) and re-tests it until its a valid value. I’ve first built up the function in a .pl document, then inserted it in the project, but there is some problem in the recursive testing.

lanca(A,Z,Nestado):-
R is random(4),
teste(A,R),
move(A,Z,Nestado,R).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

teste(A,R):-
R=0,
A1 is A-4,
valores(A1,R).

teste(A,R):-
R=1,
A1 is A-4,
valores(A1,R).

teste(A,R):-
R=2,
A1 is A-4,
valores(A1,R).

teste(A,R):-
R=3,
A1 is A-4,
valores(A1,R).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

valores(A1,R):-
A1<1,!,
R is random(4),
teste(A1,R).

valores(A1,R):-
A1>16,!,
R is random(4),
teste(A1,R).

valores(A1,R):-
A1=2,!,
R is random(4),
teste(A1,R).

valores(A1,R):-
A1=10,!,
R is random(4),
teste(A1,R).

valores(A1,R):-
A1=11,!,
R is random(4),
teste(A1,R).

Thanks for the help,

Tiago

3 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 May 31, 2008 at 17:50

Sorry, this is not a forum for homework questions. If you are having trouble you should talk to your professor, TA, or classmates.

510990f59852cf37a3c904d0d1901b56
0
TiagoISMT 101 May 31, 2008 at 18:09

My professor wants us to use this kind of forum, so there is no problem. My classmates have already gave up on this project!

4413189eb92ecb0c72aa193596bda383
0
Trap_D 101 Jun 02, 2008 at 17:17

Hello

Your code can’t work :

lanca(A,Z,Nestado):-
  R is random(4),
  teste(A,R),
  ...

I Think you start with lanca(A,Z,Nestado).
You first unify R with a random value.
This means that R will keep this value all along the programm with nop possibility of backtrack.

Next, you call teste(A,R).

teste(A,R):-
  R=0,
  A1 is A-4,
  valores(A1,R).

If we suppose that R is 0, you compute and unify A1 with A-4 thne you call valores(A1, 0).

valores(A1,R):-
  A1<1,!,
  R is random(4),
  teste(A1,R).

R is 0 and after you unify R with random(4). It succeed one time on four, and fail three times.
You must change the design of your programm.