我正在尝试使用 minizinc 进行我的第一个约束编程。我正在尝试创建一个包含n
插槽和n
人员的时间表,每个插槽分配一个不同的人。我正在使用一个array of var int
来模拟日程安排,alldifferent()
用来确保每个插槽中都有不同的人。一个单独array
的 size n
,names
包含它们的名称,如下所示:
% Pseudo enum
set of int: NameIndex = 1..2;
int: Forename = 1;
int: Surname = 2;
int: n = 4; % Number of slots and people
set of int: slots = 1..n;
array[slots, NameIndex] of string: names = [| "John", "Doe"
| "Ann", "Jones"
| "Fred", "Doe"
| "Barry", "Doe" |];
% The schedule
array[slots] of var slots: schedule;
% Every person is scheduled:
include "alldifferent.mzn";
constraint alldifferent(schedule);
% How to constrain by a value from names, say alphabetic order by forename.
% Want to compare each value in schedule to the next one.
%constraint forall (i in 1..n-1) (names[schedule[i],Forename] < names[schedule[i+1],Forename]);
solve satisfy;
output [show(i) ++ ": " ++ show(names[schedule[i], Forename]) ++ " " ++ show(names[schedule[i], Surname]) ++ "\n"
| i in slots]
% should be:
% | i in schedule]
如何通过名称中的值限制 schdule?在我上面的(损坏的)示例中,当forall
取消注释约束时,我得到(使用 Minizinc IDE):
in call 'forall'
in array comprehension expression
with i = 1
in binary '<' operator expression
in array access
cannot find matching declaration
我遵循错误,直到不理解找不到哪个声明。show()
当我从 schdule 值索引到数组时,名称中的输出块s 值非常高兴。
我错过了什么?有没有更好的方法来模拟名称?我希望将名称扩展到人的其他“属性”并创建额外的约束。我确信模型和我的 forall 约束都非常幼稚!