0

我正在尝试使用 minizinc 进行我的第一个约束编程。我正在尝试创建一个包含n插槽和n人员的时间表,每个插槽分配一个不同的人。我正在使用一个array of var int来模拟日程安排,alldifferent()用来确保每个插槽中都有不同的人。一个单独array的 size nnames包含它们的名称,如下所示:

% 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 约束都非常幼稚!

4

1 回答 1

0

问题是 MiniZinc 对字符串的支持不多。并针对您的示例:不支持比较字符串(“<”)。

也就是说,有一些计划支持 var 字符串(即使用字符串作为决策变量),但我不知道它何时发布的确切状态。

这是一个非常简单的修复,但它需要一些预处理:

1)创建一个新数组,其中包括每个名称的(排序)索引:

  array[slots] of int: names_ix = [4, % John Doe
                                   1, % Ann Jones
                                   3, % Fred Doe
                                   2, % Barry Doe
                                   ];                  

2) 更改排序约束以使用这个新数组

constraint 
   forall (i in 1..n-1) (
      names_ix[schedule[i]] <= names_ix[schedule[i+1]]
   );

[有一个更复杂的变体,要求您将名称中的每个字符转换为整数(在“var int”的矩阵中),然后要求单词 - 作为整数的集合 - 是有序的。请注意,这往往很混乱,例如处理不同长度的字符串等]

于 2016-10-24T17:58:04.163 回答