Minizinc: How to apply this constraint for scheduling model? -
i working on scheduling model , i'd figure out how use constraint: using minizinc 2.0.2 version & minizincide-0.9.7-linux , g12-mip & gecode solvers.
array[1..2,1..48] of var 0..1: table; array[1..48] of int:demand; array[1..2] of int: cost; constraint forall(j in 1..48)(sum(i in 1..2)(table[i,j]) >= demand[j] ); solve minimize sum(i in 1..2)(sum(j in 1..48)(table[i,j])*cost[i]); output [show(table)];
sample data.dzn file:
demand=[0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; cost=[3,5];
output table array using g12-mip solver gives result:
[0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
this model 2 points , 48 hours(i.e 2 days).the constraint wanted add each employee have shift each day without break if alloted any. in desired output :
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
approach tried:
var 1..5: k; array[1..2,1..2] of var 1..48: key2; array[1..2,1..2] of var 1..48: key1; % aim store first , last index table[i,j]=1 each point (i) each day (k) constraint forall(i in 1..2,j in 1..48 k= ceil(j/24))(if table[i,j]==1 key2[i,k]=j else true endif) /\ forall(i in 1..2,j in 48..1 k= ceil(j/24))(if table[i,j]==1 key1[i,k]=j else true endif); % make table[i,j]=1 between first index , last index table[i,j]=1 constraint forall(i in 1..2,k in 1..2)(forall(t in key1[i,k]..key2[i,k])(table[i,t]=1));
when ran through linux terminal using command: mzn-g12mip test.mzn data.dzn gave same previous result. when ran through minizincide-0.9.7-linux gave error :
minizinc: type error: type error in operator application `..'. no matching operator found left-hand side type `var int' , right-hand side type `var int'
is there problem code or there other way satisfy constraint?
two notes:
i got same result you: running via command line works. using minizincide give same error, after resetting mzn_stdlib_dir in terminal run minizincide.sh works. have mzn_stdlib_dir set? try reset command.
export mzn_stdlib_dir=
regarding shift constraint, can use global constraint "regular". see minizinc tutorial example. specific constraint want here "contiguity" constraint requires 1s collected. it's not built-in in minizinc, can see model example: http://hakank.org/minizinc/contiguity_regular.mzn
here's version of model, including definition of contiguity.
include "globals.mzn"; array[1..2,1..48] of var 0..1: table; array[1..48] of int:demand; array[1..2] of int: cost; var int: z = sum(i in 1..2)(sum(j in 1..48)(table[i,j])*cost[i]); constraint forall(j in 1..48)(sum(i in 1..2)(table[i,j]) >= demand[j] ); constraint forall(i in 1..2) ( contiguity([table[i,j] | j in 1..48]) ) ; predicate contiguity(array[int] of var int: a) = let { int: n_states = 3, int: input_max = 2, int: initial_state = 1, set of int: accepting_states = {1,2,3}, % transition matrix array[1..n_states, 1..input_max] of int: transition_fn = array2d(1..n_states, 1..input_max, [ % note:all states accepting states 1,2, % state 1 (start): input 0 -> state 1, input 1 -> state 2 i.e. 0* 3,2, % state 2: 1* 3,0, % state 3: 0* ]), int: len = length(a), % note: regular cannot handle 0 values, must > 0 array[1..len] of var 1..2: reg_input } in forall(i in 1..len) ( reg_input[i] = a[i] + 1 ) /\ regular(reg_input, n_states, input_max, transition_fn, initial_state, accepting_states) ; solve minimize z; output [ "table: ", show(table), "\n", "z: ", show(z), "\n", ] ++ ["\ntable:"] ++ [ if j = 1 "\n" else " " endif ++ show(table[i,j]) | in 1..2, j in 1..48 ] ; demand=[0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; cost=[3,5];
the optimal solution using model not same solution since tasks without demand costs if person 1 must take "empty" tasks between mandatory tasks. if have other constraint don't allow separation of tasks, have add in model.
table: 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Comments
Post a Comment