如何将 Minizinc 中的变量限制为集合的一部分

How to constraint a variable in Minizinc to be part of a set

我正在寻找一个约束条件,它会强制变量数组从特定集合或数组中获取值

我的常量是: 已知的数组(或集合数组)数组(使用 python 脚本计算)

我的决策变量是 3 个数组

我有一个限制: 决策变量应采用数组常量数组的数组之一

%constants 
array [int] of set of int: possible_set;
possible_set= [{1,2,3},
              {4,5,6},
              {3,5,7},
              {7,8,9}];

%decision variables
array[1..3] of var 1..9: loc1;

constraint loc1 subset possible_set;   

 %The expected result is the loc1 will be one of the sets {1,2,3},
 %                 {4,5,6},
 %                 {3,5,7},
 %                {7,8,9}

但是,我收到一个错误:

MiniZinc: type error: type error in operator application for 'subset'. No matching operator found with left-hand side type array[int] of var int' and right-hand side typearray[int] of set of int` Process finished with non-zero exit code 1

你报错的原因是possible_set不是一个集合,而是一个集合数组。

我认为你最好重新制定一下模型。这是一个似乎可以满足您要求的变体。我的更改是标记为 hakank.

的行
%constants 
array [int] of set of int: possible_set;
possible_set= [{1,2,3},
               {4,5,6},
               {3,5,7},
               {7,8,9}];

%decision variables
%% array[1..3] of var 1..9: loc1; % ORIGINAL
var set of 1..9: loc1_set; % hakank
var 1..4: loc1; % index of the set that's selected.

% constraint loc1 subset possible_set; %% ORIGINAL 
constraint loc1_set = possible_set[loc1]; % hakank: set loc1_set to the loc1'th set

solve satisfy;

有四种解法(符合预期):

loc1_set = 1..3;
loc1 = 1;
----------
loc1_set = 4..6;
loc1 = 2;
----------
loc1_set = {3,5,7};
loc1 = 3;
----------
loc1_set = 7..9;
loc1 = 4;