2.4. 选项类型¶

2.4.1. 声明和使用选项类型¶

var opt <类型> : <变量名>:


Listing 2.4.1 使用选项类型的灵活车间作业调度模型 (flexible-js.mzn).
int: horizon;                  % 时间范围
set of int: Time = 0..horizon;
enum Machine;

array[Task,Machine] of int: d; % 每个机器上的持续时间
int: maxd = max([ d[t,m] | t in Task, m in Machine ]);
int: mind = min([ d[t,m] | t in Task, m in Machine ]);

array[Task] of var Time: S;             % 起始时间
array[Task] of var mind..maxd: D;       % 持续时间
array[Task,Machine] of var opt Time: O; % 可选择的任务起始

[O[t,m]|m in Machine],[d[t,m]|m in Machine]));
constraint forall(m in Machine)

solve minimize max(t in Task)(S[t] + D[t]);


2.4.2. 隐藏选项类型¶

var set of 1..n: x;
constraint sum(i in x)(i) <= limit;


var set of 1..n: x;
constraint sum(i in 1..n)(if i in x then i else <> endif) <= limit;


array[1..n] of var int: x;
constraint forall(i in 1..n where x[i] >= 0)(x[i] <= limit);


array[1..n] of var int: x;
constraint forall(i in 1..n)(if x[i] >= 0 then x[i] <= limit else <> endif);


var set of 1..9: x;
constraint card(x) <= 4;
constraint length([ i | i in x]) > 5;
solve satisfy;


constraint length([ if i in x then i else <> endif | i in 1..9 ]) > 5;


var set of 1..n: x;
constraint sum(i in 1..n)(bool2int(i in x)*i) <= limit;


array[1..n] of var int: x;
constraint forall(i in 1..n)(x[i] >= 0 -> x[i] <= limit);