Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me program in prolog!

Status
Not open for further replies.

giuprolog7

Programmer
Sep 4, 2012
3
IT
I am not an expert in logic programming and Prolog and I'm struggling with this project to make the university "taking pleasure in a domain, write two programs that make, one depth-first search and another in breadth" ... I have chosen as domain a journey through the various Italian cities, place the following code:
Code:
[dazed]
arc_weighed(roma,firenze,3).
arc_weighed(firenze,genova,3).
arc_weighed(genova,torino,2).
arc_weighed(torino,milano,2).
arc_weighed(torino,aosta,1).
arc_weighed(firenze,bologna,2).
arc_weighed(bologna,venezia,2).
arc_weighed(venezia,trento,2).
arc_weighed(venezia,trieste,2).
arc_weighed(napoli,potenza,2).
arc_weighed(palermo,cagliari,9).
arc_weighed(potenza,bari,2).
arc_weighed(potenza,catanzaro,4).
arc_weighed(catanzaro,palermo,5).
arc_weighed(roma,laquila,1).
arc_weighed(laquila,campobasso,3).
arc_weighed(laquila,ancona,2).
arc_weighed(campobasso,napoli,2).

calculated_route:- write('Hi, welcome we give in to our travel agency virtuale.Le propose, starting from Rome an interesting tourist route.'), write('Enter capital of arrival:'),nl, read(X), search_loc('roma',X).

search_loc(X,X):-  write('Already here, good sightseeing!'),nl.

search_loc(X,Y):-search(X,Y,L,[X],Costo), reverse_append(L,_),write('. To take this route:'), write(Costo), write(' hours.') .

search(X,X,L,L,0).
search(X,Y,L,P,O):- arc_weighed(X,W,C),ricerca(W,Y,L,[W|P],K),weighted_path(C,K,O).

reverse_append([],[]).
reverse_append([A|C],Q):-reverse_append(C,K),
append(K,[A],Q),write(A), write(' ').

weighted_path(A,C,Costo):- Costo is C+A.

not(X):- X,!,fail.
not(_).
 :uhh:

with this code I do I make a journey .. how can I make the two different research?
Can you help me in writing algorithms? thank you! :)
 
You should seach the route without writing anything.
You should write the towns after the search is finished.
For DFS you can start with that :
Code:
calculated_route:-
	write('Hi, welcome we give in to our travel agency virtuale.Le propose, starting from Rome an interesting tourist route.'),
	write('Enter capital of arrival:'),nl,
	read(X),
	search_loc(X, [[roma], 0], P),
	maplist(writeln,P).

search_loc(X,[[X | T], Cost], [Path, Cost]) :-
	reverse([X | T], Path).

search_loc(X,[[Y | T], C1] , FP):-
	arc_weighed(Y ,Z,Costo),
	C2 is C1 + Costo,
	search_loc(X,[[Z, Y | T], C2],FP).
You get :
Code:
 ?- calculated_route.
Hi, welcome we give in to our travel agency virtuale.Le propose, starting from Rome an interesting tourist route.Enter capital of arrival:
|: napoli.
[roma,laquila,campobasso,napoli]
6
true
Be aware that the code IS NOT correct, you must improve it !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top