/* Work shift scheduling problem in Picat. From SAS/OR 9.22 User's Guide: Constraint Programming """ This example illustrates the use of the GCC constraint in finding a feasible solution to a work-shift scheduling problem and then using the element constraint to incorporate cost information in order to find a minimum cost schedule. Six workers (Alan, Bob, John, Mike, Scott, and Ted) are to be assigned to three working shifts. The first shift needs at least one and at most four people; the second shift needs at least two and at most three people; and the third shift needs exactly two people. Alan does not work on the first shift; Bob works only on the third shift. The others can work any shift. The objective is to find a feasible assignment for this problem. You can model the minimum and maximum shift requirements with a GCC constraint and formulate the problem as a standard CSP. The variables W1–W6 identify the shift to be assigned to each of the six workers: Alan, Bob, John, Mike, Scott, and Ted. """ There are 34 solutions. Here is one of them: x = [2,3,1,1,2,3] 1 = [John,Mike] 2 = [Alan,Scott] 3 = [Bob,Ted] This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> S = 3, % 3 working shifts P = 6, % 6 workers Lower = [1,2,2], Upper = [4,3,2], Workers = ["Alan", "Bob", "John", "Mike", "Scott", "Ted"], % Six workers (Alan, Bob, John, Mike, Scott and Ted) X = new_list(P), X :: 1..S, % global_cardinality_low_up(x, [i | i in 1..s], lower, upper) global_cardinality_low_up(X,1..S,Lower,Upper), X[1] #!= 1, % Alan doesn't work on the first shift. X[2] #= 3, % Bob works only on the third shift. solve(X), println(x=X), foreach(I in 1..S) println(I=[Workers[W] : W in 1..P, X[W] == I]) end, nl, fail, nl. go => true. % % global_cardinality_low_up(V,C,Low,Up) % % ensure that the occurrences of C[I] in the list V % are between Low[I] and Up[I] % global_cardinality_low_up(V,C,Low,Up) => T = new_list(C.len), foreach(I in 1..C.len) T[I] :: Low[I]..Up[I] end, Gcc = $[C[I]-T[I] : I in 1..C.len], global_cardinality(V,Gcc).