MiniZinc:语法错误:语法错误,意外的 if,期望文件结尾
我不知道为什么我的代码在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题是
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 - M1d
,没有给出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
ifM1 > 0
,因为v
永远不会取该值,这是正确的,因为如果M1
M1
0
模型将访问数组的边界之外(如果模型能够编译的话),因为cards
的所有索引都是正数[1..n, 1 ..2]
。如果所有这些都适用,那么当v=1
位于cards[v- 时,
,也许您的模型对您使用 constraint forall(v in 2..M1)(cards[v,2] > cards[v-1,2]); 仍然有意义(v in 1..M1)
也会使另一个数组访问越界1,2]最后,你有
cards
位于all different
约束中,但卡片未声明为变量,如果卡片确实是决策变量,请使用array [1..n, 1.. 2] of var 1..n: cards;
我测试了模型的修改版本并得到了可行的解决方案。
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 wholeif
in parenthesis, like thisconstraint 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 variablesM1, m1, M2
have no bounds given by their definitions and their constraints don't restrict the domains, soM2 > m1 > M1
andM2 - M1 < d
, gives no upper bound forM2
or lower forM1
, and makes Minizinc try all the values in the domain ofint
for one of the variables and test the other with values within a gap ofd
(check variable-bounds), try bounding the variables from their definition, something likevar 1..x
, wherex
might be the maximum value any of those variables could take in a solution that still makes sense, from theforall
definitions it seems like you consider thatm1+1 <= n
, so maybe usen
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 eliminateif v!=0
ifM1 > 0
, becausev
will never take that value, and rightly so, because ifM1 < 0
the model will access out of the bounds of the array (if the model compiles at all), because all the indices ofcards
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 whenv=1
atcards[v-1,2]
, perhaps your model still makes sense to you usingconstraint forall(v in 2..M1)(cards[v,2] > cards[v-1,2]);
Finally, you have
cards
in analldifferent
constraint, but cards is not declared as a variable, if cards really is a decision variable usearray [1..n, 1..2] of var 1..n: cards;
I tested this modified version of you model and got a feasible solution.