MiniZinc:语法错误:语法错误,意外的 if,期望文件结尾

发布于 2025-01-16 03:06:38 字数 746 浏览 2 评论 0原文

我不知道为什么我的代码在 Minizinc 中不起作用...你能帮助我吗? 这是错误,但我认为还有另一个错误: MiniZinc:语法错误:语法错误,意外的 if,期望文件结尾

array [1..n, 1..2] of 1..n: cards;

var int: M1;
var int: M2;
var int: m1;

include "globals.mzn";


constraint(M1 < m1);

constraint(M2 > m1);

constraint(M2 - M1 > d); 

constraint alldifferent ( [cards[v,1] | v in 1..n]);

constraint forall(v in 1..M1) if v!=0 then (cards[v,2] > cards[v-1,2]) endif;
constraint forall(v in M1+1..m1)(cards[v,2] < cards[v-1,2]);
constraint forall(v in m1+1..M2)(cards[v,2] > cards[v-1,2]);
constraint forall(v in m1+1..n) if v!=n then (cards[v,2] < cards[v-1,2]) endif;

solve satisfy;

output
["Cards (" ++ show(cards[v1,v2]) ++ ")" ++ "/n" | v1 in 1..n, v2 in 1..2];

I don't know why my code doesn't work in Minizinc... Can you help me?
This is the error but i think there are another errors:
MiniZinc: syntax error: syntax error, unexpected if, expecting end of file

array [1..n, 1..2] of 1..n: cards;

var int: M1;
var int: M2;
var int: m1;

include "globals.mzn";


constraint(M1 < m1);

constraint(M2 > m1);

constraint(M2 - M1 > d); 

constraint alldifferent ( [cards[v,1] | v in 1..n]);

constraint forall(v in 1..M1) if v!=0 then (cards[v,2] > cards[v-1,2]) endif;
constraint forall(v in M1+1..m1)(cards[v,2] < cards[v-1,2]);
constraint forall(v in m1+1..M2)(cards[v,2] > cards[v-1,2]);
constraint forall(v in m1+1..n) if v!=n then (cards[v,2] < cards[v-1,2]) endif;

solve satisfy;

output
["Cards (" ++ show(cards[v1,v2]) ++ ")" ++ "/n" | v1 in 1..n, v2 in 1..2];

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

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

发布评论

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

评论(1

疑心病 2025-01-23 03:06:38

第一个问题是 forall 正在从 Minizinc 文档 "("")" "("")",将整个 if 括在括号中,就像这样 constraint forall(v in m1+1..n)(if v!=n then (cards[v,2] < cards[v-1,2]) endif);

修复后你会发现模型仍然失败, MiniZinc:评估错误:理解迭代无限集,因为变量 M1、m1、M2 没有其定义给出的界限,并且它们的约束不限制域,所以 M2 > m1> M1 和 M2 - M1 d,没有给出 M2 的上限或 M1 的下限,并让 Minizinc 尝试 int 域中的所有值对于其中一个变量,并使用 d 间隙内的值测试另一个变量(检查 变量边界),尝试根据变量的定义来限制变量,例如 var 1..x,其中 x 可能是任意最大值这些变量可以采用仍然有意义的解决方案,从 forall 定义来看,您似乎认为 m1+1 <= n,所以也许使用 >n 作为上限。

constraint forall(v in 1..M1) if v!=0 then (cards[v,2] > cards[v-1,2]) endif; 你可以消除 if v!=0 if M1 > 0,因为 v 永远不会取该值,这是正确的,因为如果 M1 M1 0 模型将访问数组的边界之外(如果模型能够编译的话),因为 cards 的所有索引都是正数 [1..n, 1 ..2]。如果所有这些都适用,那么当 v=1 位于 cards[v- 时,(v in 1..M1) 也会使另一个数组访问越界1,2],也许您的模型对您使用 constraint forall(v in 2..M1)(cards[v,2] > cards[v-1,2]); 仍然有意义

最后,你有cards 位于 all different 约束中,但卡片未声明为变量,如果卡片确实是决策变量,请使用array [1..n, 1.. 2] of var 1..n: cards;

我测试了模型的修改版本并得到了可行的解决方案。

int: n = 5;
int: d = 3;
array [1..n, 1..2] of var 1..n: cards;

var 1..n: M1;
var 1..n: M2;
var 1..n: m1;

include "globals.mzn";


constraint(M1 < m1);

constraint(M2 > m1);

constraint(M2 - M1 > d); 

constraint alldifferent ( [cards[v,1] | v in 1..n]);

constraint forall(v in 2..M1)(cards[v,2] > cards[v-1,2]);
constraint forall(v in M1+1..m1)(cards[v,2] < cards[v-1,2]);
constraint forall(v in m1+1..M2)(cards[v,2] > cards[v-1,2]);
constraint forall(v in m1+1..n)(if v!=n then (cards[v,2] < cards[v-1,2]) endif);

solve satisfy;

output
["Cards (" ++ show(cards[v1,v2]) ++ ")" ++ if v2 != 2 then ", " else "\n" endif | v1 in 1..n, v2 in 1..2];
Cards (5), Cards (4)
Cards (4), Cards (3)
Cards (3), Cards (2)
Cards (2), Cards (1)
Cards (1), Cards (2)

The first problem is that forall is looking for its expression clause, from the Minizinc documentation <ident-or-quoted-op> "(" <comp-tail> ")" "(" <expr> ")", wrap the whole if in parenthesis, like this constraint forall(v in m1+1..n)(if v!=n then (cards[v,2] < cards[v-1,2]) endif);

Upon fixing that you will find that the model still fails, MiniZinc: evaluation error: comprehension iterates over an infinite set, because the variables M1, m1, M2 have no bounds given by their definitions and their constraints don't restrict the domains, so M2 > m1 > M1 and M2 - M1 < d, gives no upper bound for M2 or lower for M1, and makes Minizinc try all the values in the domain of int for one of the variables and test the other with values within a gap of d (check variable-bounds), try bounding the variables from their definition, something like var 1..x, where x might be the maximum value any of those variables could take in a solution that still makes sense, from the forall definitions it seems like you consider that m1+1 <= n, so maybe use n as the upper bound.

From constraint forall(v in 1..M1) if v!=0 then (cards[v,2] > cards[v-1,2]) endif; you can eliminate if v!=0 if M1 > 0, because v will never take that value, and rightly so, because if M1 < 0 the model will access out of the bounds of the array (if the model compiles at all), because all the indices of cards are positive [1..n, 1..2]. And if all of that applies, also (v in 1..M1) will make another array access out of bounds when v=1 at cards[v-1,2], perhaps your model still makes sense to you using constraint forall(v in 2..M1)(cards[v,2] > cards[v-1,2]);

Finally, you have cards in an alldifferent constraint, but cards is not declared as a variable, if cards really is a decision variable use array [1..n, 1..2] of var 1..n: cards;

I tested this modified version of you model and got a feasible solution.

int: n = 5;
int: d = 3;
array [1..n, 1..2] of var 1..n: cards;

var 1..n: M1;
var 1..n: M2;
var 1..n: m1;

include "globals.mzn";


constraint(M1 < m1);

constraint(M2 > m1);

constraint(M2 - M1 > d); 

constraint alldifferent ( [cards[v,1] | v in 1..n]);

constraint forall(v in 2..M1)(cards[v,2] > cards[v-1,2]);
constraint forall(v in M1+1..m1)(cards[v,2] < cards[v-1,2]);
constraint forall(v in m1+1..M2)(cards[v,2] > cards[v-1,2]);
constraint forall(v in m1+1..n)(if v!=n then (cards[v,2] < cards[v-1,2]) endif);

solve satisfy;

output
["Cards (" ++ show(cards[v1,v2]) ++ ")" ++ if v2 != 2 then ", " else "\n" endif | v1 in 1..n, v2 in 1..2];
Cards (5), Cards (4)
Cards (4), Cards (3)
Cards (3), Cards (2)
Cards (2), Cards (1)
Cards (1), Cards (2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文