[英]k consecutive integers constraint
How can I state the following constraint in Constraint Programming? 如何在约束编程中声明以下约束? (Preferably in Gurobi or Comet). (最好是在Gurobi或Comet中)。
S is an array of integers of size n. S是大小为n的整数数组。 The set of integers that I can use to fill the array are in the range 1-k. 我可以用来填充数组的整数集在1-k范围内。 There is a constraint ci for each of the integers that can be used. 每个可以使用的整数都有一个约束ci 。 ci denotes the minimum number of consecutive integers i . ci表示连续整数i的最小数量。
For example if c1 = 3, c2 = 2 then 1112211112111 is not a valid sequence since there must be two consecutive 2's, whereas 1112211122111 is a valid sequence. 例如,如果c1 = 3,c2 = 2,则1112211112111不是有效序列,因为必须有两个连续的2,而1112211122111是有效序列。
Perhaps using the regular constraint (automaton in Comet) would be the best approach. 也许使用常规约束(Comet中的自动机)将是最好的方法。
However, here is a straightforward solution in MiniZinc which use a lot of reifications. 但是,这是MiniZinc中的一个直接解决方案,它使用了大量的改进方法。 It should be possible to translate it to Comet at least (I don't think Gurobi support reifications). 至少应该有可能将其翻译成Comet(我不认为Gurobi支持修订)。
The decision variables (the sequences) are in the array "x". 决策变量(序列)在数组“ x”中。 It also use a helper array ("starts") which contains the start positions of each sequences; 它还使用一个辅助数组(“ starts”),其中包含每个序列的起始位置。 this makes it easier to reason about the sequences in "x". 这使得更容易推断“ x”中的序列。 The number of sequences is in "z" (eg for optimization problems). 序列数为“ z”(例如,针对优化问题)。
Depending on the size of x and other constraints, one can probably add more (redundant) constraints on how many sequences there can be etc. This is not done here, though. 根据x的大小和其他约束,可以在可能存在的序列数等上添加更多(冗余)约束。但是,这里不做此操作。
Here's the model: http://www.hakank.org/minizinc/k_consecutive_integers.mzn 这是模型: http : //www.hakank.org/minizinc/k_consecutive_integers.mzn
It's also shown below. 它也在下面显示。
int: n;
int: k;
% number of consecutive integers for each integer 1..k
array[1..k] of int: c;
% decision variables
array[1..n] of var 1..k: x;
% starts[i] = 1 -> x[i] starts a new sequence
% starts[i] = 0 -> x[i] is in a sequence
array[1..n] of var 0..k: starts;
% sum of sequences
var 1..n: z = sum([bool2int(starts[i] > 0) | i in 1..n]);
solve :: int_search(x, first_fail, indomain_min, complete) satisfy;
constraint
forall(a in 1..n, b in 1..k) (
(starts[a] = b ) ->
(
forall(d in 0..c[b]-1) (x[a+d] = b )
/\
forall(d in 1..c[b]-1) (starts[a+d] = 0 )
/\
(if a > 1 then x[a-1] != b else true endif) % before
/\
(if a <= n-c[b] then x[a+c[b]] != b else true endif) % after
)
)
/\
% more on starts
starts[1] > 0 /\
forall(i in 2..n) (
starts[i] > 0 <-> ( x[i]!=x[i-1] )
)
/\
forall(i in 1..n) (
starts[i] > 0 -> x[i] = starts[i]
)
;
output [
"z : " ++ show(z) ++ "\n" ++
"starts: " ++ show(starts) ++ "\n" ++
"x : " ++ show(x) ++ "\n"
];
%
% data
%
%% From the question above:
%% It's a unique solution.
n = 13;
k = 2;
c = [3,2];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.