Solved

# Prolog - Searching within a list of lists - how to do it

Posted on 2004-11-13
Medium Priority
546 Views
Hello,

I'm working on a program that lacks this one thing to be complete. :(
It's about a simple robot that cleans a room; the room is represented through a 10x10 map divided into squares, each square having it's own unique pair of x,y coordinates. I'm representing the coordinates via a list that contains two elements: [Xcoord,Ycoord], and a path through the room is represented via a sequence of squares in a list: [[0,0],[0,1][1,1]], for example. The robot doesn't move diagonally.
Now I need to find out how to get to a certain square being on another square; for example, I'm on [5,4] and I need to get to [0,0] - I need a predicate that returns me a list with a possible path through these squares, and I just can't do it :((( The closest I got didn't even work correctly. :(((( I know I could just subtract/sum the coordinates of the original position to get the result I want, but that's not what I need :(

Can someone please post some ideas, links and/or code examples on this?

0
Question by:bass20

LVL 4

Expert Comment

ID: 12576723
Might be that you find the answer that here:
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/phi3DGeom.html#fivereg

I've had use of this site before when it came to mathematical problems, maybe it will help you as well?
Good luck,
/B
0

LVL 1

Author Comment

ID: 12577968
No Prolog there...
0

LVL 3

Assisted Solution

gerodim earned 315 total points
ID: 12678184
lets say that your command would work like this
?-pathfind([0,1],[4,3])

the best way to get the correct path would be to move horizontaly at first till you reach the spot and then vertically towards it (or vice versa if you like)...
so you could create a recursive command that gives u all the horizontal coordinates and then the vertical (you may have to rearrange your coordinates list to
more lists). I'll post the code a.s.a.p.
0

LVL 2

Assisted Solution

funkyfinger earned 60 total points
ID: 12678821
What you want is a recursive algorithm,
This is what it would do:

mark current location as "been here"
if can go north go north if at destination return true
if can go south go south if at destination return true
if can go east go east if at destination return true
if can go west go west if at destination return true
if no move available return false
unmark location
repeat

if you want I'll find the algorithm, I have it somewhere

Because you want a list of every posible solution you will need to find the number of possible solutions and call this recursive function that many times.

However you will get list of moves that are unnecessary such as
east, north, east, south
and
east, east
and
east, south, east, north

(all bring robot to same location)
Is this what you want?

0

LVL 3

Accepted Solution

gerodim earned 315 total points
ID: 12680134
take a look at this one: it's a bit different than your type of map&pathfinding but it'll give you some ideas...
http://ai-depot.com/Tutorial/PathFinding.html

you can also see if u can grasp anything out of this sourcecode i found in a book....
:- [pmeters].
graph :-
start(X),
finish(Y),
path([X],Y,Path).

path([H|T], Y, [H|T]) :-
H = Y.

path([H|T], Y, Path) :-
\+ member(Next,[H|T]),
path([Next,H|T],Y, Path).

If you combine this algorithm with the horizontal/vertical movement you can get it to work pretty easily...

You may also want to check this link:
http://www.cs.mu.oz.au/303/project/
Or this one:
http://www.tjhsst.edu/~rlatimer/prolog/programs.html

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
As computers become more powerful, jobs will be replaced with artificial intelligence, leaving no place in the future for humans in the workforce. In the oncoming technological revolution, the staggering change to human mind labor will prove the ski…
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.
###### Suggested Courses
Course of the Month15 days, 12 hours left to enroll