【发布时间】:2017-03-06 12:26:06
【问题描述】:
我正在尝试使用 minizinc 进行我的第一个约束编程。我正在尝试创建一个时间表,其中包含n 插槽和n 人员,并为每个插槽分配了不同的人员。我使用array of var int 来模拟日程安排,使用alldifferent() 来确保每个位置都有不同的人。
大小为n、names 的单独array 包含它们的名称,如下所示:
% 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
我一直跟踪错误,直到不明白找不到哪个声明。当我从 schdule 值索引到数组时,输出块 show()s 的值来自名称。
我错过了什么?有没有更好的方法来模拟名称?我希望将名称扩展到人的其他“属性”并创建额外的约束。我确信模型和我的 forall 约束都非常幼稚!
【问题讨论】:
标签: minizinc