% Adrian Pop 2003-12-05

:- use_module(library(clpfd)).
:- use_module(library(lists), [append/3]).

schedule(Ss, End) :-
     Ss = [A_F, B_F, C_F, D_F, A_G, B_G, C_G, D_G, A_E, B_E, C_E, D_E, A_S, B_S, C_S, D_S],
     A_F+60 #=< A_G, A_G+30 #=< A_E, A_E+2  #=< A_S, 
     B_G+75 #=< B_E, B_E+3  #=< B_F, B_F+25 #=< B_S, 
     C_E+5  #=< C_G, C_G+15 #=< C_F, C_F+10 #=< C_S, 
     D_S+90 #=< D_F, D_F+1  #=< D_G, D_G+1  #=< D_E, 
     Ds = [60, 25, 10,  1, 30, 75, 15,  1, 2,  3,  5,  1,  5, 10, 30, 90], 
     domain(Ss, 0, 200),
     domain([A_F], 0, 200),
     domain([B_G], 15, 200),
     domain([C_E], 15, 200),
     domain([D_S], 60, 200),     
     domain([End], 60, 200),
     FT = [A_F, B_F, C_F, D_F], FT_Ds = [60, 25, 10, 1], 
     GU = [A_G, B_G, C_G, D_G], GU_Ds = [30, 75, 15, 1],      
     EX = [A_E, B_E, C_E, D_E], EX_Ds = [2, 3, 5, 1],
     SU = [A_S, B_S, C_S, D_S], SU_Ds = [5, 10, 30, 90],
     Po_A = [A_F, A_G, A_E, A_S], Po_A_Ds = [60,  30,   2,   5],  
     Po_B = [B_G, B_E, B_F, B_S], Po_B_Ds = [75,   3,  25,  10],
     Po_C = [C_E, C_G, C_F, C_S], Po_C_Ds = [ 5,  15,  10,  30],  
     Po_D = [D_S, D_F, D_G, D_E], Po_D_Ds = [90,   1,   1,   1], 
     Rs_4 = [1, 1, 1, 1],
     after(Ss, Ds, End),
     cumulative(FT, FT_Ds, Rs_4, 1),
     cumulative(GU, GU_Ds, Rs_4, 1), 
     cumulative(EX, EX_Ds, Rs_4, 1), 
     cumulative(SU, SU_Ds, Rs_4, 1),
     cumulative(Po_A, Po_A_Ds, Rs_4, 1),
     cumulative(Po_B, Po_B_Ds, Rs_4, 1),
     cumulative(Po_C, Po_C_Ds, Rs_4, 1),
     cumulative(Po_D, Po_D_Ds, Rs_4, 1),
     append(Ss, [End], Vars),
     labeling([ff, minimize(End)],Vars),
     write('A_F:'), write(A_F), write(' A_G:'), write(A_G), write(' A_E:'), write(A_E), write(' A_S:'), write(A_S), nl, 
     write('B_G:'), write(B_G), write(' B_E:'), write(B_E), write(' B_F:'), write(B_F), write(' B_S:'), write(B_S), nl, 
     write('C_E:'), write(C_E), write(' C_G:'), write(C_G), write(' C_F:'), write(C_F), write(' C_S:'), write(C_S), nl, 
     write('D_S:'), write(D_S), write(' D_F:'), write(D_F), write(' D_G:'), write(D_G), write(' D_E:'), write(D_E), nl, 
     write('A_F:'), write(A_F), write(' A_G:'), write(A_G), write(' A_E:'), write(A_E), write(' A_S:'), write(A_S), nl, 
     write('B_F:'), write(B_F), write(' B_G:'), write(B_G), write(' B_E:'), write(B_E), write(' B_S:'), write(B_S), nl, 
     write('C_F:'), write(C_F), write(' C_G:'), write(C_G), write(' C_F:'), write(C_E), write(' C_S:'), write(C_S), nl, 
     write('D_F:'), write(D_F), write(' D_G:'), write(D_G), write(' D_E:'), write(D_E), write(' D_S:'), write(D_S), nl. 

after([], [], _).
after([S|Sz], [D|Dz], E) :- E #>= S+D, after(Sz, Dz, E).
