简体   繁体   English

最大连续数值(Minizinc)

[英]Max number of consecutive values (Minizinc)

I'm trying to model the next constraint in Minizinc: 我正在尝试在Minizinc中建模下一个约束:

Suppose S is an array of decision variables of size n. 假设S是大小为n的决策变量数组。 I want my decision variables to take a value between 1-k, but there is a maximum 'Cons_Max' on the number of consecutive values used. 我希望我的决策变量取一个介于1-k之间的值,但是在使用的连续值数上有一个最大的'Cons_Max'。

For example, suppose Cons_Max = 2, n = 8 and k = 15, then the sequence [1,2,4,5,7,8,10,11] is a valid sequence , while eg [1,2,3,5,6,8,9,11] is not a valid sequence because the max number of consecutive values is equal to 3 here (1,2,3). 例如,假设Cons_Max = 2,n = 8,k = 15,则序列[1,2,4,5,7,8,10,11]是有效序列,而例如[1,2,3, 5,6,8,9,11]不是有效的序列,因为连续值的最大数量在此处等于3(1,2,3)。 Important to mention is that sequence [1,3,5,7,9,10,12,14] is also valid, because the values don't need to be consecutive but the max number of consectuive values is fixed to 'Cons_Max'. 值得一提的是,序列[1,3,5,7,9,10,12,14]也有效,因为这些值不必是连续的,但是最大连续值的数量固定为'Cons_Max' 。

Any recommendations on how to model this in Minizinc? 关于如何在Minizinc中进行建模的任何建议?

Suppose you use array x to represent your decision variable. 假设您使用数组x表示决策变量。

array[1..n] of var 1..k: x;

then you can model the constraint like this. 那么您可以像这样对约束建模。

constraint not exists (i in 1..n-1)(
                       forall(j in i+1..min(n, i+Cons_Max))
                             (x[j]=x[i]+1)
                      );

Here's a model with a approach that seems to work. 这是一种方法可行的模型。 I also added the two constraints all_different and increasing since they are probably assumed in the problem. 我还添加了两个约束all_different和递增,因为它们可能是问题中假定的。

include "globals.mzn";
int: n = 8;
int: k = 15;
int: Cons_Max = 2;
% decision variables
array[1..n] of var 1..k: x;

constraint 
   forall(i in 1..n-Cons_Max) (
      x[i+Cons_Max]-x[i] > Cons_Max
   )
;

constraint 
  increasing(x) /\
  all_different(x)
;

%% test cases
% constraint 
%    % x = [1,2,4,5,7,8,10,11] % valid solution
%    % x = [1,3,5,7,9,10,12,14] % valid valid solution

%    % x = [1,2,3,5,6,8,9,11] % -> not valid solution (-> UNSAT)
%  ;


solve satisfy;
output ["x: \(x)\n" ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM