Hello everybody, i am a student in computing, and i have been given a project where i need to create a coach program, i have done everything, my main concern is " how to implant the route between 2 cities" by including the via city....
please help me resolve that problem...find attached my program below...
Thank you in advance.
% coaches.pl
%
% Idrissa Cisse & Darren Campbell
%
% Prolog Assignment
%
% the file 'useful.pl' should be copied from the KBS&AI website into the
% same folder as 'coaches.pl'
:- reconsult('useful.pl'). % loading useful.pl
:- nl,write(' Loading coaches.pl ').
% The map of england - road(Start,Dest,Mileage).
:- dynamic(road/3).
road(glasgow, edinburgh, 46).
road(glasgow, carlisle, 98).
road(edinburgh, newcastle, 107).
road(carlisle, newcastle, 57).
road(carlisle, manchester, 119).
road(newcastle, leeds, 93).
road(leeds, sheffield, 35).
road(sheffield, manchester, 37).
road(manchester, liverpool, 35).
road(manchester, stoke, 36).
road(sheffield, nottingham, 44).
road(stoke, nottingham, 51).
road(nottingham, norwich, 120).
road(norwich, cambridge, 64).
road(cambridge, northampton, 54).
road(northampton, birmingham, 55).
road(birmingham, stoke, 46).
road(birmingham, nottingham, 54).
road(nottingham, northampton, 66).
road(birmingham, oxford, 68).
road(oxford, london, 57).
road(northampton, london, 68).
road(cambridge, london, 60).
road(oxford, bristol, 74).
road(bristol, cardiff, 47).
road(london, dover, 80).
road(dover, brighton, 78).
road(london, brighton, 60).
road(bristol, exeter, 84).
road(london, exeter, 200).
road(exeter, plymouth, 45).
% a small sample database of coaches (coach/10)
:- dynamic(coach/10).
% coach(ID, Make, Reg, TotalMiles, MilesSinceService, Capacity, HireStatus, ClientId, ReservedStatus, ReservedName)
coach(1, daf, abc123, 7243, 1436, 50, available, nil, nil, nil).
coach(2, volvo, abc234, 5162, 5162, 40, available, nil, nil, nil).
coach(3, iveco, abc345, 496, 0, 24, on_hire, 1, nil, nil).
coach(4, optare, abc456, 0, 0, 18, available, nil, nil, nil).
coach(5, volvo, wew334,7020, 4600, 32, on_hire, 2, nil, nil).
coach(6, peugeot, def246, 375, 54, 16, on_hire,3, nil, nil).
coach(7, bmw, qwe844, 876, 97, 86, available, nil, nil, nil).
coach(8, renault, lkj987, 9875, 4675,43, on_hire, 4, nil, nil).
coach(9, renault, uyg654, 653, 76, 12, available, nil, nil, nil).
coach(10, chrysler, dsf245, 5434, 758, 13, on_hire, 5, nil, nil).
% The client database (client/3).
:- dynamic(client/3).
% client(ClientID, FirstName, LastName).
client(1, idrissa, cisse).
client(2, paul, harper).
client(3, ben, cisse).
client(4, betty, diaz).
client(5, nina, gomis).
client(6, michael, jackson).
client(7, barack, obama).
client(8, hilary, clinton).
client(9, david, beckham).
client(10, didier, drogba).
% start - top level recursive loop predicate
start:-
menu(Choice),
Choice \= 0,
process(Choice),
pause,
start.
start :- nl,write(' End of Coach hire program'),nl.
menu(Choice):-
nl,nl,
write('0 Exit program'),nl,
write('1 Add a new coach to the fleet'),nl,
write('2 Book a coach out on hire'),nl,
write('3 Book a coach back in'),nl,
write('4 Update the service record'),nl,
write('5 Reserve a coach'),nl,
write('6 Display all the coaches available for hire'),nl,
write('7 Display all the coaches in the Fleet'),nl,
write('8 Show capacity of coach'),nl,
write('9 Add new clients'),nl,
write('10 Find route'),nl,
nl,nl,
write('Enter choice then <return>'),
readatom(Choice),
nl.
% process/1 - takes menu option choice as argument and calls
% appropriate sub-goals
process(1) :-
nl,
write(' Adding a coach to the fleet'),nl,
write('-------------'),nl,
get_coach(Ident, Make, RegNo),
assert(coach(Ident, Make, RegNo, 0, 0, _, available, nil, nil, nil)), %new coach added
write(' Coach no : '), write(Ident),
write(' Make : '), write(Make),
write(' Registration no : '), write(RegNo),nl,
write(' - has now been added to the fleet '),nl,nl.
process(1) :-
nl,
write(' This identity number has already been used '),nl.
process(2) :-
nl,
write(' Booking a coach out on hire'),nl,
write('-------------'),nl,
get_coach_id(Ident),
get_client_id(ClientId),
hire_coach(Ident, ClientId).
process(3) :-
write(' Booking a coach back in'),nl,
write('-------------'),nl,
get_coach_id(Ident),
return_coach(Ident).
process(4) :-
write(' Update the service record'),nl,
write(' --------------------'), nl,
get_coach_id(Ident),
service_coach(Ident).
process(5) :-
write(' Reserve a coach'),nl,
write(' --------------------'), nl,
get_coach_id(Ident),
get_client_id(ClientId),
reserve_coach(Ident, ClientId). % needs to be completed
% NOTE : Implementing the 'reserve' option will require a modification to the coach
% structure to include an argument for the client name.
process(6) :-
write(' Coaches available for hire'),nl,
write(' --------------------'), nl,
write(' Number Registration no. Make Status'),
nl,nl,
coach(Ident, Make, RegNo,_,_,_, available,_,_,_),
tab(5),write(Ident),
tab(15),write(RegNo),
tab(15),write(Make),
tab(10),write(available),nl,
fail.
process(6).
process(7) :-
write(' Coaches in the Fleet'),nl,
write('-------------'),nl,nl,
write(' Number Registration no. Make Capacity Status'),
nl,nl,
coach(Ident, Make, RegNo, Miles, NextService, Capacity, Status,_,_,_),
tab(5),write(Ident),
tab(15),write(RegNo),
tab(15),write(Make),
tab(15),write(Capacity),
tab(15),write(Status),nl,
fail. % causes backtracking to force REDO of 'coach'
process(7). % finally succeeds when there are no more solutions to 'coach' in previous clause
process(8) :-
write(' Show capacity of coach'),nl,
write(' --------------------'), nl,
get_coach_id(Ident),
show_coach_capacity(Ident, Capacity),nl.
coach(Ident, Make, RegNo, Miles, NextService, Capacity, Status,ClientId).
process(9) :-
write(' Add new clients'),nl,
write(' -------------------- '),nl,
get_client_details(ClientId,FirstName, LastName),
add_client(ClientId, FirstName, LastName),nl.
process(10) :-
write(' Find route'),nl,
write(' --------------------'),nl,
get_route(From,To),
find_all_routes(From, To, _,Dist).
road(From,To,Dist).
process(_) :-
nl,write('Enter a valid menu option : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10:'), nl.
% check_id/1 - fails if argument is an existing coach identity number
check_id(Ident) :-
coach(Ident,_,_,_,_,_,_), !, fail.
check_id(_).
% get_coach/3 allows the user to enter details of a new coach. A check is
% made to ensure that an existing ID number is not used again
get_coach(Ident, Make, RegNo):-
write('Enter the identity number : '),
readatom(Ident), nl,
check_id(Ident),
write('Enter the make of the coach : '),
readatom(Make), nl,
write('Enter the registration number : '),
readatom(RegNo), nl.
% get_coach_id/1 and get_miles/1 - two more input predicates using readatom/1
get_coach_id(Ident):-
write('Enter the identity number : '),
readatom(Ident), nl.
get_miles(Miles):-
write('Enter the miles travelled on this booking : '),
readatom(Miles), nl.
% get_client_details/3 .
get_client_details(ClientId,FirstName,LastName):-
write('Enter the client number '),
readatom(ClientId), nl,
write('Enter the client First Name '),
readatom(FirstName),nl,
write('Enter the client Last Name '),
readatom(LastName),nl.
% get_client_id/1
get_client_id(ClientId):-
write('Enter the client number '),
readatom(ClientId), nl.
% get_name/1
get_name(ClientId):-
client(ClientId, FirstName, LastNime),
write(FirstName), write(' '), write(LastName),nl.
% hire_coach/1 - will update the database if a coach is hired or give an
% appropriate message if the coach is unavailable
hire_coach(Ident,ClientId) :- % coach available for hire
retract(coach(Ident, Make, RegNo, Miles, LastService, Capacity, available,nil,nil,nil)),
assert(coach(Ident, Make, RegNo, Miles, LastService, Capacity, on_hire,ClientId,nil,nil)),
write(' Coach no : '), write(Ident),
write(' Make : '), write(Make),
write(' Registration no : '), write(RegNo),nl,
write('- is now out on hire. '),nl,nl.
hire_coach(Ident,ClientId) :- % coach already on hire
coach(Ident, _,_,_,_,_,on_hire,_,_,_),
write(' Coach no : '),write(Ident),
write(' is already on hire. '),nl,nl.
hire_coach(Ident,ClientId) :- % coach already on hire
coach(Ident, _,_,_,_,_,_,_,reserved,ReservedName),
write(' Coach no : '),write(Ident),
write(' is currently reserved. '),nl,nl.
hire_coach(Ident,ClientId) :- % coach not in fleet
check_id(Ident),
write(' Coach no : '),write(Ident),
write(' is not in the fleet '),nl,nl.
% return_coach/1 - updates the database when a coach is returned or gives an
% appropriate message
% IMPORTANT NOTE : This predicate will need to be modified to include the
% service_check/3 predicate (see below) which has not yet been implemented
return_coach(Ident) :-
retract(coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity, on_hire,ClientId,ReservedStatus,ReservedName)),
get_miles(Miles),
NewTotalMiles is Miles + TotalMiles,
NewMilesSinceService is Miles + MilesSinceService,
write(' Coach no : '),write(Ident),
service_check(NewMilesSinceService,Status),
assert(coach(Ident, Make, Reg, NewTotalMiles, NewMilesSinceService, Capacity, available,nil,ReservedStatus,ReservedName)),
write(' Coach no: '), write(Ident),
write(' is back in the garage now'),nl,nl.
return_coach(Ident) :- % coach not on hire
coach(Ident, _,_,_,_,available,nil,_,_,_),
write(' Coach no : '),write(Ident),
write(' is not on hire. '),nl,nl.
return_coach(Ident) :- % coach not in fleet
check_id(Ident),
write(' Coach no : '),write(Ident),
write(' is not in the fleet '),nl,nl.
%reserve_coach/1 - reserves a coach in advance
%even if the coach is already on hire.
reserve_coach(Ident, ClientId) :-
client(ClientId,FirstName, LastName),
retract(coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity, on_hire, Hired_Client_Id, nil, nil)),
assert(coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity, on_hire, Hired_Client_Id, reserved, FirstName)),
write(' Coach no :'), write(Ident),
write(' is now reserved.'),nl,nl.
reserve_coach(Ident, ClientId) :- % Coach already reseved.
coach(Ident, _, _, _, _, _, on_hire, _, reserved, _),
write(' Sorry but Coach no :'), write(Ident),
write(' is already reserved.'),nl,nl.
reserve_coach(Ident, ClientId) :- % Coach available for hire only.
coach(Ident, _, _, _, _, _, available, _, _, _),
write(' Coach no :'), write(Ident),
write(' is not currently on hire, you cannot reserve it but you can hire it instead'),nl,nl.
%show_coach_capacity/2 - shows the capacity of a coach.
show_coach_capacity(Ident, Capacity) :-
coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity,_),
write(' Coach no :'), write(Ident),
write(' has a capacity of '), write(Capacity),
write(' passengers.'),nl,nl.
% service_check/2 - calculates whether the next service is due
% (i.e. 5000 miles after the previous service).
% If the mileage since the last service is over 5000 the status must be changed to 'service'
% service_check(LastService, Status) (to be implemented)
service_check(LastService, Status):-
coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity,_,_,_,_),
LastService >= 5000,
write(' is now due for a service'),nl,nl.
service_check(LastService, Available):-
coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity,_,_,_,_),
LastService < 5000,
write(' is not yet due for service'),nl,nl.
% service_coach/1 - updates the status of a coach when it has been serviced or
% or gives an appropriate message - e.g. 'service not needed', 'coach not in fleet' etc.
% service_coach(Ident) (to be implemented)
service_coach(Ident):-
retract(coach(Ident, Make, Reg, TotalMiles, MilesSinceService, Capacity, Service, _,_,_)),
assert(coach(Ident, Make, Reg, TotalMiles, 0, Capacity, available,_,_,_)),
write(' Coach no : '), write(Ident),
write(' has been serviced and is now available'),nl,nl.
service_coach(Ident):-
coach(Ident, Make, RegNo, Miles, LastService, Capacity, _,_,_,_),
write(' Coach no : '), write(Ident),
write(' does not need a service'),nl,nl.
service_coach(Ident):-
check_id(Ident),
write(' Coach no : '),write(Ident),
write(' is not in the fleet '),nl,nl.
% Adding a new client/3 .
add_client(ClientId, FirstName, LastName):-
client(ClientId,FirstName, LastName),
nl,write('Sorry, this person does already exist as a client.'),nl,nl.
add_client(ClientId, FirstName, LastName):-
assert(client(ClientId, FirstName, LastName)),
write(FirstName ),write(' '), write(LastName),
write(' has been added to the client list.'),nl.
% Going from a city to another one .
route(From,To,Dist):-
road(From,To,Dist).
route(From,To,Dist):-
road(To,From,Dist).
/**okroad(From, To,Dist) :-
droad(From, To),
not(member(To,Dist)).
**/
% travel/4 recursively seeks the destination
% Argument 3 of travel/4 is an accumulator building up the path
% travelled so far.
% Argument 4 is instantiated as the final route travelled :
travel(Dest,Dest, _ ,[Dest],Dist). % stopping clause
travel(From,To,Travelled,[From|Path],Dist) :- % recursive clause
okroad(From, Inter, Travelled, NextDist),
travel(Inter,To,[Inter|Travelled],Path,NewDist),
NewDist is Dist + NextDist.
% route/3 calls travel/4 with only input and output arguments -
route(From,To,Route,Dist) :-
travel(From,To,[From],Route,Dist).
% To find the best route we need to look at all possible routes and choose the
% shortest one :
% find_all_routes/3 - repeatedly calls route/3 to get all the routes
% between two stations and puts them into a list (Argument 3).
find_all_routes(From, To, _,Dist) :-
route(From, To, Route,Dist),
assert(r(Route)),
fail.
find_all_routes(_, _, Routes) :-
collect_routes(Routes),!.
% get_route/3 allows the user to enter details of a new route.
get_route(From, To):-
write('Enter the departure town '),
readatom(From), nl,
write('Enter the destination town '),
readatom(To), nl.
:- write(' - File loaded OK'),nl,nl.
:- write('to run the coaches program type ''start'' at the ?- prompt'),nl,nl.
:- write('| ?- start. '),nl,nl.