%
% Use: php ~/SOLVERS/LOOP/loop.php airport_pickup.enc.loop ../../train_instances/2-airport_pickup-500-0.asp
%



%
% 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:
%
%   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)
%


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

in_reach_check :-
	state(initial).

selected(reach_test(P)) :-
	in_reach_check,
	passenger(P).

next_state(first_assignment) :-
	in_reach_check.

oracle(reach_test(P),asp(clasp)) :-
	selected(reach_test(P)).
%file(reach_test(P),"reach-test.lp") :-
file(reach_test(P),CWD,"ezcspEnc3") :-
	selected(reach_test(P)),
	cwd(CWD).
param(reach_test(P),selected(P)) :-
	selected(reach_test(P)).
param(reach_test(P),driveway(X,Y,C)) :-
	selected(reach_test(P)),
	driveway(X,Y,C).
param(reach_test(P),location(L)) :-
	selected(reach_test(P)),
	location(L).
param(reach_test(P),airport(L)) :-
	selected(reach_test(P)),
	airport(L).
param(reach_test(P),vehicle(V,M)) :-
	selected(reach_test(P)),
	vehicle(V,M).
param(reach_test(P),init_gas(V,G)) :-
	selected(reach_test(P)),
	init_gas(V,G).
param(reach_test(P),passenger(P)) :-
	selected(reach_test(P)).
param(reach_test(P),gasstation(G)) :-
	selected(reach_test(P)),
	gasstation(G).
param(reach_test(P),init_at(O,L)) :-
	selected(reach_test(P)),
	init_at(O,L).


:- result(reach_test(P),no_models).


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

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

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

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

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

curr_step(0) :-
	state(first_assignment).
curr_step(N) :-
	state(curr_step(N)).


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


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


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

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


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

at_destination(P) :-
	not in_reach_check,
	passenger(P),
	destination(P,A),
	passenger_at(P,A).

-at_destination(P) :-
	not in_reach_check,
	passenger(P),
	not at_destination(P).

-all_at_destination :-
	not in_reach_check,
	passenger(P),
	-at_destination(P).

all_at_destination :-
	not in_reach_check,
	not -all_at_destination.

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

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

already_at_passenger(V,P) :-
	-at_destination(P),
	passenger_at(P,L),
	vehicle_at(V,L).

some_already_at_passenger :-
	already_at_passenger(V,P).

1{ use_at_passenger, -use_at_passenger }1 :- -all_at_destination.
:- use_at_passenger, not some_already_at_passenger.
#minimize[ use_at_passenger=1, -use_at_passenger=2 ].

%
% Performance improvement: loop.php will only
% backtrack over these literals.
%
backtrackable(assigned(P,V)) :-
	passenger(P),
	vehicle(V).

1{ assigned(P,V) : passenger(P) : not at_destination(P) : vehicle(V,M) : good(V,P) : already_at_passenger(V,P) }1 :-
	-all_at_destination,
	use_at_passenger.

1{ assigned(P,V) : passenger(P) : not at_destination(P) : vehicle(V,M) : good(V,P)  }1 :-
	-all_at_destination,
	-use_at_passenger.

:- 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 ].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OUTPUT
%-----------

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

next_state(curr_step(N+1)) :-
	-all_at_destination,
	curr_step(N).

next_state(assigned(P,V,N)) :-
	assigned(P,V),
	curr_step(N).
next_state(assigned(P,V,N)) :-
	state(assigned(P,V,N)).

next_state(start(S,N)) :-
	start(S),
	curr_step(N).
next_state(start(S,N)) :-
	state(start(S,N)).

next_state(destination(D,N)) :-
	destination(D),
	curr_step(N).
next_state(destination(D,N)) :-
	state(destination(D,N)).

next_state(destination1(D,N)) :-
	destination1(D),
	curr_step(N).
next_state(destination1(D,N)) :-
	state(destination1(D,N)).

next_state(gas(G,N)) :-
	gas(G),
	curr_step(N).
next_state(gas(G,N)) :-
	state(gas(G,N)).

next_state(tank(T,N)) :-
	tank(T),
	curr_step(N).
next_state(tank(T,N)) :-
	state(tank(T,N)).

next_state(best_destination_gas(BG,N)) :-
	best_destination_gas(BG),
	curr_step(N).
next_state(best_destination_gas(BG,N)) :-
	state(best_destination_gas(BG,N)).
next_state(best_destination1_gas(BG,N)) :-
	best_destination1_gas(BG),
	curr_step(N).
next_state(best_destination1_gas(BG,N)) :-
	state(best_destination1_gas(BG,N)).

next_state(vehicle_at(V,D)) :-
	assigned(P,V),
	destination(P,D).
next_state(vehicle_at(V,L)) :-
	not assigned(V),
	vehicle_at(V,L).

next_state(passenger_at(P,D)) :-
	assigned(P,V),
	destination(P,D).
next_state(passenger_at(P,L)) :-
	not assigned(P),
	passenger_at(P,L).

next_state(vehicle_gas(V,G)) :-
	assigned(P,V),
	best_destination_gas(G).
next_state(vehicle_gas(V,G)) :-
	not assigned(V),
	vehicle_gas(V,G).

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

next_state(drive(V,D,S)) :-
	executed(plan(I,Ph)),
	state(assigned(P,V,I)),
	result(plan(I,Ph),do(X,G,drive(D),S)).
next_state(refuel(V,S)) :-
	executed(plan(I,Ph)),
	state(assigned(P,V,I)),
	result(plan(I,Ph),do(X,G,refuel,S)).

next_state(pick(V,P,K)) :-
	must_pick_passenger,
	pick_step(K),
	selected(plan(I,Ph)),
	state(assigned(P,V,I)).

next_state(drop(V,P,K)) :-
	must_drop_passenger,
	drop_step(K),
	executed(plan(I,Ph)),
	state(assigned(P,V,I)).

next_state(drive(V,D,S)) :-
	state(drive(V,D,S)).
next_state(refuel(V,S)) :-
	state(refuel(V,S)).
next_state(pick(V,P,S)) :-
	state(pick(V,P,S)).
next_state(drop(V,P,S)) :-
	state(drop(V,P,S)).

assignment_completed :-
	all_at_destination.

-first_planning_step :-
	assignment_completed,
	executed(plan(I,P)).

first_planning_step :-
	assignment_completed,
	not -first_planning_step.


assignment_exists(I) :-
	assignment_completed,
	state(assigned(P,V,I)).

planning_completed :-
	executed(plan(I,d)),
	not assignment_exists(I+1).
planning_completed :-
	state(final).
next_state(final) :-
	planning_completed.

single_path_type(a,I) :-
	assignment_completed,
	assignment_exists(I),
	state(assigned(P,V,I)),
	state(start(S,I)),
	state(gas(G,I)),
	state(destination1(S,I)),
	state(best_destination1_gas(G,I)).

single_path_type(b,I) :-
	assignment_completed,
	assignment_exists(I),
	state(assigned(P,V,I)),
	state(destination1(D,I)),
	state(best_destination1_gas(G,I)),
	state(destination(D,I)),
	state(best_destination_gas(G,I)).

single_path_required(I) :-
	single_path_type(a,I).
single_path_required(I) :-
	single_path_type(b,I).

selected(plan(I+1,d)) :-
	executed(plan(I,d)),
	assignment_exists(I+1),
	single_path_required(I+1).
	
selected(plan(I+1,p)) :-
	executed(plan(I,d)),
	assignment_exists(I+1),
	not single_path_required(I+1).

selected(plan(I,d)) :-
	executed(plan(I,p)).

terminal :- state(final).

terminal :-
	assignment_completed,
	not assignment_exists(0).

selected(plan(0,p)) :-
	assignment_completed,
	first_planning_step,
	not single_path_required(0),
	not planning_completed,
	not terminal.

selected(plan(0,d)) :-
	assignment_completed,
	first_planning_step,
	single_path_required(0),
	not planning_completed,
	not terminal.

must_pick_passenger :-
	selected(plan(I,d)).

drop_step(K) :-
	must_drop_passenger,
	step_offset_from_planner(K).

must_drop_passenger :-
	executed(plan(I,d)).

pick_step(K) :-
	step_offset_from_planner(K),
	must_pick_passenger,
	not must_drop_passenger.
pick_step(K+1) :-
	step_offset_from_planner(K),
	must_drop_passenger,
	must_pick_passenger.


step_offset_from_planner(0) :-
	assignment_completed,
	first_planning_step,
	selected(plan(0,P)).
step_offset_from_planner(K+1) :-
	executed(plan(I,P)),
	result(plan(I,P),last_step(K)).

step_offset(K+2) :-
	step_offset_from_planner(K),
	must_drop_passenger,
	must_pick_passenger.
step_offset(K+1) :-
	step_offset_from_planner(K),
	must_drop_passenger,
	not must_pick_passenger.
step_offset(K+1) :-
	step_offset_from_planner(K),
	must_pick_passenger,
	not must_drop_passenger.
step_offset(K) :-
	step_offset_from_planner(K),
	not must_drop_passenger,
	not must_pick_passenger.

oracle(plan(I,P),asp(iclingo)) :-
	selected(plan(I,P)).
%file(plan(I,P),"single-planner.lp") :-
file(plan(I,P),CWD,"ezcspEnc2") :-
	selected(plan(I,P)),
	cwd(CWD).

param(plan(I,P),step_offset(K)) :-
	selected(plan(I,P)),
	step_offset(K).
param(plan(I,P),driveway(X,Y,C)) :-
	selected(plan(I,P)),
	driveway(X,Y,C).
param(plan(I,P),location(L)) :-
	selected(plan(I,P)),
	location(L).
param(plan(I,P),gasstation(G)) :-
	selected(plan(I,P)),
	gasstation(G).

param(plan(I,P),tank(T)) :-
	selected(plan(I,P)),
	state(tank(T,I)).

param(plan(I,p),start(S)) :-
	selected(plan(I,p)),
	state(start(S,I)).
param(plan(I,p),gas(G)) :-
	selected(plan(I,p)),
	state(gas(G,I)).

param(plan(I,d),start(S)) :-
	selected(plan(I,d)),
	state(start(S,I)),
	single_path_required.
param(plan(I,d),gas(G)) :-
	selected(plan(I,d)),
	state(gas(G,I)),
	single_path_required.

param(plan(I,d),start(S)) :-
	selected(plan(I,d)),
	state(destination1(S,I)),
	not single_path_required.
param(plan(I,d),gas(G)) :-
	selected(plan(I,d)),
	state(best_destination1_gas(G,I)),
	not single_path_required.

param(plan(I,p),destination(D)) :-
	selected(plan(I,p)),
	state(destination1(D,I)).
param(plan(I,p),best_destination_gas(BG)) :-
	selected(plan(I,p)),
	state(best_destination1_gas(BG,I)).

param(plan(I,d),destination(D)) :-
	selected(plan(I,d)),
	state(destination(D,I)).
param(plan(I,d),best_destination_gas(BG)) :-
	selected(plan(I,d)),
	state(best_destination_gas(BG,I)).
