%
% Use: from within airport_pickup.enc.loop
%



%
% NOTE: I *should* allow refueling at destination at the end of the path.
%       This will prevent the vehicle from taking a diversion to get
%	with almost full tank to destination, when it could just go
%	to the destination and fill up there.
%
%
%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PATH FROM START TO DESTINATION THAT ENDS WITH THE MOST GAS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% reachable_from_start(X,G)
% X is reachable from the start (starting with gas level gas(.))
% finishing with gas level G
%

%
% INPUT: (out-dated!)
%
%   driveway(X,Y,C)
%   location(X)
%   gasstation(L)
%   start(L)
%   destination(D)
%   gas(G)		- gas level at start
%   tank(T)		- size of the vehicle's tank
%
%   destination1(D)
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT
%-----------

vehicle_at(V,L) :-
	vehicle(V),
	init_at(V,L).

passenger_at(P,L) :-
	passenger(P),
	init_at(P,L).

vehicle_gas(V,G) :-
	init_gas(V,G).

tank(V,M) :-
	vehicle(V,M).


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


connected(X,Y,C) :-
	driveway(X,Y,C).
connected(Y,X,C) :-
	driveway(X,Y,C).


vehicle(V) :-
	vehicle(V,M).

destination(P,A2) :-
	passenger(P),
	airport(A1),
	airport(A2),
	A1 != A2,
	init_at(P,A1).


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

trans_connected(X,Y) :-
	connected(X,Y,C).
trans_connected(X,Y) :-
	connected(X,Z,C),
	trans_connected(Z,Y).

good(V,P) :-
	passenger(P),
	vehicle(V,M),
	init_at(P,SP),
	init_at(V,SV),
	destination(P,A),
	trans_connected(SP,SV),
	trans_connected(SV,A).


1{ assigned(P,V) : selected(P) : vehicle(V,M) : good(V,P)  }1.

:- assigned(P,V), destination(P,D), not passenger_reachable_from_start.
:- assigned(P,V), destination(P,D), not destination_reachable_from_passenger.

passenger_reachable_from_start :-
	destination1(D),
	reachable_from_start(D,G).

destination_reachable_from_passenger :-
	destination(D),
	reachable_from_d1(D,G).


start(S) :-
	assigned(P,V),
	vehicle_at(V,S).

gas(G) :-
	assigned(P,V),
	vehicle_gas(V,G).

tank(T) :-
	assigned(P,V),
	tank(V,T).

destination1(D) :-
	assigned(P,V),
	passenger_at(P,D).

destination(D) :-
	assigned(P,V),
	destination(P,D).


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

reachable_from_start(S,G) :-
	start(S),
	gas(G).

% refuel before starting
reachable_from_start(S,T) :-
	start(S),
	gasstation(S),
	tank(T).

reachable_from_start(Y,G-C) :-
	reachable_from_start(X,G),
%%%	destination1(D1), X != D1,			% no use in going farther
	connected(X,Y,C),
	G>=C.	% must have enough gas to get there

% refuel at X
reachable_from_start(X,T) :-
	reachable_from_start(X,G),
	gasstation(X),
	tank(T).


best_destination1_gas(BG) :-
	destination1(D),
	BG=#max[ reachable_from_start(D,G)=G ].

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

reachable_from_d1(D1,G) :-
	destination1(D1),
	best_destination1_gas(G).

% refuel before starting
reachable_from_d1(D1,T) :-
	destination1(D1),
	gasstation(D1),
	tank(T),
	best_destination1_gas(G).

reachable_from_d1(Y,G-C) :-
	reachable_from_d1(X,G),
%%%%	destination(D), X != D,			% no use in going farther
	connected(X,Y,C),
	G>=C.	% must have enough gas to get there

% refuel at X
reachable_from_d1(X,T) :-
	reachable_from_d1(X,G),
	gasstation(X),
	tank(T).


best_destination_gas(BG) :-
	destination(D),
	BG=#max[ reachable_from_d1(D,G)=G ].

#hide.
#show assigned/2.
