include "globals.mzn";
include "chuffed.mzn";
% constant definitions
    set of int: T; % tasks
	set of int:P; % precedences between tasks
	set of int:M; % machines
	set of int:S; % stages
	set of int:tuples; % transport time table size
	int:lb; % lower bound on cost
	int:ub; % upper bound on domain
	int:m; % number of machines over all stages

% constant arrays
	array[T] of int:duration;
	array[T] of int:stage;
	array[P,1..2] of int:prec; % precedence pairs
	array[T] of set of int:machines; % machine domains
	array[tuples,1..3] of int:transportTime; % table of transport times between machines
	array[S] of int:capacity;

% data file
	include "cp2021/instance20_1.dzn";

% variables
	array[T] of var 0..ub:start;
	array[T] of var M:machine;
	array[T] of var 0..ub: travel; % transport from machine of task before
	var lb..ub:cmax;
	
% constraints
	constraint forall (i in T)
               (cmax >= start[i]+duration[i]);
	constraint forall (p in P)
               (start[prec[p,2]] >= start[prec[p,1]]+
                                          duration[prec[p,1]]+
                                          travel[prec[p,2]]);
	constraint forall(i in T) (machine[i] in machines[i]);
	constraint forall(s in S) 
	    (diffn([start[i]|i in T where stage[i]=s],
	                [machine[i]|i in T  where stage[i] = s],
	                [duration[i]|i in T where stage[i] = s],
	                [1|i in T where stage[i] = s]));
	constraint forall (p in P)
               (table([machine[prec[p,1]],machine[prec[p,2]],travel[prec[p,2]]],transportTime));

% solve
%	solve satisfy;
%solve minimize cmax;

solve ::priority_search(start,
                [int_search([start[i],machine[i]],input_order,indomain_min)| i in  T],
				smallest,complete) minimize cmax;
    
	