MiniZinc MILP 约束,用于限制数组中最多 3 个连续的特定项

发布于 2025-01-12 10:18:00 字数 239 浏览 3 评论 0原文

我有下一个 MiniZinc 代码

array [0..5] of var 0..1: a;
constraint sum(a) = 3;
output [show(a)]

我需要添加两个约束以确保最多有 3 个连续的 1。我认为添加 constraint sum(a) = 3; 有帮助,但是如何添加约束来确保我有 3 个连续的 1,例如 111000 或 011100?

I have the next MiniZinc code

array [0..5] of var 0..1: a;
constraint sum(a) = 3;
output [show(a)]

And I need to add two constraints to ensure that at most I have 3 contiguous 1's. I thought that adding constraint sum(a) = 3; helps but how can I add a constraint to make sure I have 3-contiguous 1s for example 111000 or 011100?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我鬧 2025-01-19 10:18:00

另一种选择是限制第一个和最后一个 1 之间的距离:

int: n = 5;
set of int: N = 1..n;
array [N] of var 0..1: a;

var N: first = n + 1 - max(i in N)(i * a[n - i + 1]);
var N: last = max(i in N)(i * a[i]);
constraint sum(a) == 3;
constraint (last - first + 1) == 3;
output [show(a)]

Another alternative is to constrain the distance between first and last 1:

int: n = 5;
set of int: N = 1..n;
array [N] of var 0..1: a;

var N: first = n + 1 - max(i in N)(i * a[n - i + 1]);
var N: last = max(i in N)(i * a[i]);
constraint sum(a) == 3;
constraint (last - first + 1) == 3;
output [show(a)]
度的依靠╰つ 2025-01-19 10:18:00

这是一种将 4 个连续元素的每个块的总和限制为 3 的方法:

array [0..5] of var 0..1: a;

int: MAX_CONTIGUOUS_ONES = 3;
constraint forall(start in 0..5-MAX_CONTIGUOUS_ONES)
  (sum([a[i] | i in start..start+MAX_CONTIGUOUS_ONES]) <= MAX_CONTIGUOUS_ONES);

output [show(a)]

Here is one way by constraining the sum of each block of 4 consecutive elements to 3:

array [0..5] of var 0..1: a;

int: MAX_CONTIGUOUS_ONES = 3;
constraint forall(start in 0..5-MAX_CONTIGUOUS_ONES)
  (sum([a[i] | i in start..start+MAX_CONTIGUOUS_ONES]) <= MAX_CONTIGUOUS_ONES);

output [show(a)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文