:- module(_,_).
:- use_package(clpfd).

% -------------------------------------------------------------------
% - A project management problem (3)

% The job dependencies and task lengths
% are now given by this graph, where we
% now have two independent tasks B and D
% whose lengths X and Y are not fixed:
% 
%        0*G
%        ^ ^
%       /  |
%      /   |
%    4*E  1*F
%     ^^   ^^
%     | \  | \   
%     |  \ |  \ 
%    X*B  2*C  Y*D
%     ^    ^    ^
%      \   |   /
%       \  |  /
%         0*A
%
% Still, the whole job should be finished in 10
% time units or less.
% 
% But we can finish any of B or D in 2 time units at best
% -> X #>=2, Y #>=2
% 
% Plus, some shared resource disallows finishing both 
% tasks in 2 time units: they will instead take 6 time units
% -> X #>=2, Y #>=2, X + Y #= 6

pn3(A,B,C,D,E,F,G,X,Y) :- 
    domain([A,B,C,D,E,F,G,X,Y], 0, 10),
    A #>= 0, G #=< 10, 
    X #>= 2, Y #>= 2, X + Y #= 6, 
    B #>= A, C #>= A, D #>= A, 
    E #>= B + X, E #>= C + 2, 
    F #>= C + 2, F #>= D + Y, 
    G #>= E + 4, G #>= F + 1.

% Queries:
% ?- use_package(clpfd).
% ?- minimize(pn3(A,B,C,D,E,F,G,X,Y), G).
% -> We must devote more resources to task B
% -> All tasks but F and D are critical now
%
% We can use labeling for D and F: 
% ?- minimize(pn3(A,B,C,D,E,F,G,X,Y),G), labeling([],[D,F]).
%
% Also:
% ?- pn3(A,B,C,D,E,F,G,X,Y), minimize(labeling([],[A,B,C,D,E,F,G,X,Y]),G).
