route(adailya,bayan,['45 Rd.', 'North', '4 Ring Rd.', 'East', '22 Rd.', 'South East', '5 Ring Rd.', 'East']).
route(yermook,mishref,['23 Rd', 'South', '40 Rd', 'South', '24 Rd', 'East', '25 Rd', 'North East']).
route(shamiya,jabriya,['2 Ring Rd', 'East', '22 Rd', 'South East', '4 Ring Rd', 'East']).
route(dasman,surra,['2 Ring Rd', 'South West', '45 Rd', 'South', '4 Ring Rd', 'East']).
route(mAlabdullah,kaifan,['3 Ring Rd', 'East', '45 Rd', 'South', '4 Ring Rd', 'East', '22 Rd', 'Sout East', '40 Rd', 'South']).
printList([]).
printList([H|T]):-write(H),nl,printList(T).
revList(L1,L2):-rev(L1,[],L2),printList(L2).
rev([],L,L).
rev([A|L],L1,L2):-rev(L,[A|L1],L2).
driving(X,Y):- route(X,Y,[Lfirst|List]),printList([Lfirst|List]),!.
driving(X,Y):- reverse([City1,City2],[X,Y]),route(City1,City2,L1),revList(L1,L),!.
Ok, how to compared two strings to check if H variable equal to North or not and how to change the value of H variable for example from North to South ? Best Regards, FlaShow [Edited by - SB4 on May 5, 2008 10:19:15 AM]
[AI using Prolog] Driving Directions program
hello, i want to writing a database on how to drive from City to City2. Let's say the i want to go from City1 (adailya) to City2 (bayan) first take 45Rd (south)=>4Rd(east)=>22Rd(southEast)=>5Rt(east) for example: ?- driving(adailya,bayan). Take 45Rd. south Take 4Rd. east 22Rd. southEast 5Rt. east I wrote this code :
It's been a while, since I've done Prolog, but I think you can use = for equality. Or you can just declare
eq(X,X).
and use that. Though you rarely need to test for equality like this.
You can't change values of variables in Prolog. If you'd say what you're trying to do, I could probably be of more help.
eq(X,X).
and use that. Though you rarely need to test for equality like this.
You can't change values of variables in Prolog. If you'd say what you're trying to do, I could probably be of more help.
thanks SnotBob for reply
I'm trying to reverse the direction
for example this code:
if i want to go from adailya to bayan i will type :
driving(adailya,bayan).
to show the routes how to go from adailya to bayan.
but when i want to go from bayan to adailya i must reverse the list and the direction if it North must change to South or West to East ...etc
I'm trying to reverse the direction
for example this code:
route(adailya,bayan,['45 Rd.', 'North', '4 Ring Rd.', 'East', '22 Rd.', 'South East', '5 Ring Rd.', 'East']). printList([]).printList([H|T]):-write(H),nl,printList(T).revList(L1,L2):-rev(L1,[],L2),printList(L2).rev([],L,L).rev([A|L],L1,L2):-rev(L,[A|L1],L2).driving(X,Y):- route(X,Y,[Lfirst|List]),printList([Lfirst|List]),!.driving(X,Y):- reverse([City1,City2],[X,Y]),route(City1,City2,L1),revList(L1,L),!.
if i want to go from adailya to bayan i will type :
driving(adailya,bayan).
to show the routes how to go from adailya to bayan.
but when i want to go from bayan to adailya i must reverse the list and the direction if it North must change to South or West to East ...etc
Quote:
Original post by SB4
but when i want to go from bayan to adailya i must reverse the list and the direction if it North must change to South or West to East ...etc
Ok, now I get it. It's working otherwise ok, but it's giving the opposite directions for the reverse routes.
Well, first there's one other issue. Your program will actually print stuff like this:
45Rd.
South
4Rd.
East
If you reverse that list, you get
East
4Rd.
South
45Rd.
which isn't right, even if you swap East with West and South with North.
To fix this construct your routes like this:
route(adailya,bayan,[['45 Rd.', 'North'], ['4 Ring Rd.', 'East']).
E.g. pair your roads with the directions. Modify your print routines accordingly.
Next, the reversal of directions:
reverse('North','South').# Add other directions.reverse_dir([X,Y],[X,Z]) :- reverse(Y,Z).# A helper routine for mapping a list to another# something like this may be a part of your Prolog systemmap(_,[],[]).map(P,H1|T1,H2|T2) :- P(H1,H2),map(P,T1,T2).reverse_all_dirs(L1,L2) :- map(reverse_dir,L1,L2).
:)
I didn't understand the last part
this is the code :
can you modify it, please ? and tell me What actually happens ?
I appreciate your help
I didn't understand the last part
'reverse_dir([X,Y],[X,Z]) :- reverse(Y,Z).# A helper routine for mapping a list to another# something like this may be a part of your Prolog systemmap(_,[],[]).map(P,H1|T1,H2|T2) :- P(H1,H2),map(P,T1,T2).reverse_all_dirs(L1,L2) :- map(reverse_dir,L1,L2).'
this is the code :
route(adailya,bayan,[['45 Rd.', 'North'], ['4 Ring Rd.', 'East'], ['22 Rd.', 'South East'], ['5 Ring Rd.', 'East']]). route(yermook,mishref,[['23 Rd', 'South'], ['40 Rd', 'South'], ['24 Rd', 'East'], ['25 Rd', 'North East']]). route(shamiya,jabriya,[['2 Ring Rd', 'East'], ['22 Rd', 'South East'], ['4 Ring Rd', 'East']]).route(dasman,surra,[['2 Ring Rd', 'South West'], ['45 Rd', 'South'], ['4 Ring Rd', 'East']]). route(mAlabdullah,kaifan,[['3 Ring Rd', 'East'], ['45 Rd', 'South'], ['4 Ring Rd', 'East'], ['22 Rd', 'Sout East'], ['40 Rd', 'South']]). reverse('North','South').reverse('South','North').reverse('East','West').reverse('West','East').reverse('North East','South West').reverse('South West','North East').reverse('North West','South East').reverse('South East','North West').prinSubtList([]).prinSubtList([Subfirst|SubList]):-write(Subfirst),write(' '),prinSubtList(SubList).printList([]).printList([H|T]):-write('Take '),prinSubtList(H),nl,printList(T).revList(L1,L2):-rev(L1,[],L2),printList(L2).rev([],L,L).rev([A|L],L1,L2):-rev(L,[A|L1],L2).driving(X,Y):- route(X,Y,[Lfirst|List]),printList([Lfirst|List]),!.driving(X,Y):- reverse([City1,City2],[X,Y]),route(City1,City2,L1),revList(L1,L),!.
can you modify it, please ? and tell me What actually happens ?
I appreciate your help
Don't you ultimately want to have a system that finds the shortest path between two points? What I see here is a system where the program matches user input with pre-determined routes, and prints it. It seems to me you should be working on a design where you have the route defined between each pair of points, and have the program determine the shortest route. Right?
Prolog is a really fun language to program in.
Prolog is a really fun language to program in.
route(adailya,bayan,[['45 Rd.', 'North'], ['4 Ring Rd.', 'East'], ['22 Rd.', 'South East'], ['5 Ring Rd.', 'East']]). route(yermook,mishref,[['23 Rd', 'South'], ['40 Rd', 'South'], ['24 Rd', 'East'], ['25 Rd', 'North East']]). route(shamiya,jabriya,[['2 Ring Rd', 'East'], ['22 Rd', 'South East'], ['4 Ring Rd', 'East']]).route(dasman,surra,[['2 Ring Rd', 'South West'], ['45 Rd', 'South'], ['4 Ring Rd', 'East']]). route(mAlabdullah,kaifan,[['3 Ring Rd', 'East'], ['45 Rd', 'South'], ['4 Ring Rd', 'East'], ['22 Rd', 'Sout East'], ['40 Rd', 'South']]). reverse('North','South').reverse('South','North').reverse('East','West').reverse('West','East').reverse('North East','South West').reverse('South West','North East').reverse('North West','South East').reverse('South East','North West').# I simplified your print routine here to show, how you can do that:printList([]).printList([[X,Y]|T]):- write('Take '),write(X), write(' '),write(Y),nl,printList(T).revList(L1,L2):-rev(L1,[],L2),printList(L2).rev([],L,L).rev([A|L],L1,L2):-rev(L,[A|L1],L2).reverse_dir([X,Y],[X,Z]) :- reverse(Y,Z).map(_,[],[]).map(P,H1|T1,H2|T2) :- P(H1,H2),map(P,T1,T2).reverse_all_dirs(L1,L2) :- map(reverse_dir,L1,L2). # I made a small change here too, check the original: # Removed the cuts too.driving(X,Y):- route(X,Y,Z),printList(Z).driving(X,Y):- route(Y,Z,L1), reverse_all_dirs(L1,L2), revList(L2,L).
This should work, although I didn't try it out.
How it works is that map(P,L1,L2) is used to apply the predicate P to each member of L1 and L2. So if P and L1 are bound, what it does is act as a function that takes members of L1, applies P to produce L2. The predicate used for P is reverse_dir, which takes the road-direction pairs and switches the direction.
Also, you shouldn't use cuts unnecessarily as they can dramatically change the semantics of your program. In your case you only have one route per city pair, so there's no chance that driving(cityA,cityB) would produce a second answer. However, the cuts would have prevented from querying driving(cityA,X) to get all routes leaving from cityA.
[Edited by - SnotBob on May 6, 2008 2:06:46 AM]
Quote:
Original post by ID Merlin
Don't you ultimately want to have a system that finds the shortest path between two points? What I see here is a system where the program matches user input with pre-determined routes, and prints it. It seems to me you should be working on a design where you have the route defined between each pair of points, and have the program determine the shortest route. Right?
Prolog is a really fun language to program in.
the problem is i don't know all the routes from city1 to city2 and all i know about prolog is how to write rules, goals and list but i know it's a good challenge to write such a program.
like u said it's a really fun language :)
thank u very much SnotBob :)
there is a problem with the code when u type driving(bayan,adailya). the result will be fail. hmmmmmmmmm i will know the answer sooner or later
I was thinking about a solution before I sleep :) and take the idea from SnotBob when he said:
the reversal of directions:
reverse('North','South').
# Add other directions.
This is the code:
route(adailya,bayan,[['45 Rd.', 'North'], ['4 Ring Rd.', 'East'], ['Mgreb.', 'South East'], ['5 Ring Rd.', 'East']]). route(yermook,mishref,[['Reyad Rd', 'South'], ['Faisle Rd', 'South'], ['alWaleed Rd', 'East'], ['Khalid Rd', 'North East']]). route(shamiya,jabriya,[['2 Ring Rd', 'East'], ['Mgreb Rd', 'South East'], ['4 Ring Rd', 'East']]).route(dasman,surra,[['2 Ring Rd', 'South West'], ['45 Rd', 'South'], ['4 Ring Rd', 'East']]). route(mAlabdullah,kaifan,[['3 Ring Rd', 'East'], ['45 Rd', 'South'], ['4 Ring Rd', 'East'], ['Mgreb Rd', 'Sout East'], ['40 Rd', 'South']]). revDirection('North','South').revDirection('South','North').revDirection('East','West').revDirection('West','East').revDirection('North East','South West').revDirection('South West','North East').revDirection('North West','South East').revDirection('South East','North West').changeDirection(X):-revDirection(X,Y),write(Y).changeDirection(X):-write(X).printRevList([]).printRevList([[X,Y]|T]):-write('Take '),write(X),changeDirection(Y),nl,printRevList(T).printList([]).printList([[X,Y]|T]):-write('Take '),write(X),write(' '),write(Y),nl,printList(T).revList(L1,L2):-rev(L1,[],L2),printRevList(L2).rev([],L,L).rev([A|L],L1,L2):-rev(L,[A|L1],L2).driving(X,Y):- route(X,Y,[Lfirst|List]),printList([Lfirst|List]).driving(X,Y):- reverse([City1,City2],[X,Y]),route(City1,City2,L1),revList(L1,L).
Best Regards,
SB4
If that's your code, driving(bayan,adailya) fails because reverse([City1,City2],[X,Y]) doesn't match anything. I guess you forgot to remove it.
Quote:
Original post by SnotBob
If that's your code, driving(bayan,adailya) fails because reverse([City1,City2],[X,Y]) doesn't match anything. I guess you forgot to remove it.
I'm sorry but the fail in your code
route(adailya,bayan,[['45 Rd.', 'North'], ['4 Ring Rd.', 'East'], ['22 Rd.', 'South East'], ['5 Ring Rd.', 'East']]). route(yermook,mishref,[['23 Rd', 'South'], ['40 Rd', 'South'], ['24 Rd', 'East'], ['25 Rd', 'North East']]). route(shamiya,jabriya,[['2 Ring Rd', 'East'], ['22 Rd', 'South East'], ['4 Ring Rd', 'East']]).route(dasman,surra,[['2 Ring Rd', 'South West'], ['45 Rd', 'South'], ['4 Ring Rd', 'East']]). route(mAlabdullah,kaifan,[['3 Ring Rd', 'East'], ['45 Rd', 'South'], ['4 Ring Rd', 'East'], ['22 Rd', 'Sout East'], ['40 Rd', 'South']]). reverse('North','South').reverse('South','North').reverse('East','West').reverse('West','East').reverse('North East','South West').reverse('South West','North East').reverse('North West','South East').reverse('South East','North West').# I simplified your print routine here to show, how you can do that:printList([]).printList([[X,Y]|T]):- write('Take '),write(X), write(' '),write(Y),nl,printList(T).revList(L1,L2):-rev(L1,[],L2),printList(L2).rev([],L,L).rev([A|L],L1,L2):-rev(L,[A|L1],L2).reverse_dir([X,Y],[X,Z]) :- reverse(Y,Z).map(_,[],[]).map(P,H1|T1,H2|T2) :- P(H1,H2),map(P,T1,T2).reverse_all_dirs(L1,L2) :- map(reverse_dir,L1,L2). # I made a small change here too, check the original: # Removed the cuts too.driving(X,Y):- route(X,Y,Z),printList(Z).driving(X,Y):- route(Y,Z,L1), reverse_all_dirs(L1,L2), revList(L2,L).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement