%
% Use: iclingo single-planner.lp test2.lp
%



%
% 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.
%
%
%



#base.
%
% INPUT:
%
%   driveway(X,Y,C)
%   location(X)
%   gasstation(G)
%   start(L)
%   destination(D)
%   gas(G)		- gas level at start
%   tank(T)		- size of the vehicle's tank
%
%   destination1(D)
%
%   best_destination1_gas(G).
%   best_destination_gas(G).
%
%   step_offset(N)      - offset for the numbering of do/4
%
%

#base.

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	
	
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FIND A SHORTEST PATH AMONG THE ABOVE %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


arc(S,SG,S,SG,0) :-	% DO I NEED THIS?????
	start(S),
	gas(SG).

at_dest(0) :-
	start(S),
	destination(S),
	gas(BG),
	best_destination_gas(BG).

arc(S,SG,X,SG-C,1) :-
	start(S),
	gas(SG),
	connected(S,X,C),
	SG>=C.

arc(S,SG,S,T,1) :-
	start(S),
	gas(SG),
	gasstation(SG),
	tank(T).


#cumulative s.
arc(X,G1,Y,G1-C,s+1) :-
	arc(Z,G0,X,G1,s),
	connected(X,Y,C),
	G1>=C.

arc(X,G1,X,T,s+1) :-
	arc(Z,G0,X,G1,s),
	gasstation(X),
	tank(T).

at_dest(s+1) :-
	arc(X,G,D,BG,s+1),
	destination(D),
	best_destination_gas(BG).

complete(s+1) :-
	at_dest(s+1).

#volatile s.
:- not complete(s+1).

last_step(s+1+K-1) :-
	step_offset(K).

% V3
do(X,G,drive(D),s+1+K-1) :-
	complete(s+1),
	destination(D),
	best_destination_gas(BG),
	X!=D,
	arc(X,G,D,BG,s+1),
	step_offset(K).

1{ do(X,G1,drive(Y),S+K-1) : arc(X,G1,Y,G2,S) : X!=Y,
   do(Y,G1,refuel,S+K-1) : arc(Y,G1,Y,G2,S) }1 :-
	S=1..s,
   	complete(s+1),
	step_offset(K),
	do(Y,G2,ACT,S+1+K-1).

% problem instance may follow
#base.

#hide.
#show do/4.
#show last_step/1.
