Minizinc的if-else语句与多个表达式

huangapple go评论79阅读模式
英文:

Minizinc if-else statement with multiple expressions

问题

在MiniZinc中,if-else语句的语法如下:

if boolexp then exp1 else exp2 endif

但是我需要使用多个表达式。例如:

var int: a;
var int: b;
if 5==abs(5) then a = 5; b > 2 else a = 0 endif

在这种情况下,我想将a设置为5,并在条件为真时对b施加大于2的限制。不幸的是,该语法只允许我一次执行一个操作。

英文:

The syntaxis of if-else statement in minizinc is

if 〈boolexp〉 then 〈exp1〉 else 〈exp2〉 endif

But i need to use more than just one exp

for example

var int: a;
var int: b;
if 5==abs(5) then a = 5, b > 2 else a = 0 endif

In this case, I want to set a to 5 and impose a restriction on b to be greater than 2 when the condition is true. Unfortunately, the syntax only allows me to perform one operation at a time.

答案1

得分: 2

MiniZinc是一种基于逻辑的语言。尽管容易陷入“操作”的思维方式,但在MiniZinc中,最好考虑你建立的关系。

这就是说,在这种情况下,你可以使用“逻辑与”操作符\来结合这两个布尔表达式,以强制执行这两个关系。

对于你的具体示例,你可以这样写:

var int: a;
var int: b;
constraint if 5==abs(5) then a = 5 /\ b > 2 else a = 0 endif
英文:

MiniZinc is a language based on logic. Although it is easy to fall into a mindset of “operations”, in MiniZinc it is better to think about relationships that you put in place.

This is all to say that in this case you can combine the two Boolean expressions using the “logical and” operation, /\, to enforce both relationships.

For your specific example, you can write:

var int: a;
var int: b;
constraint if 5==abs(5) then a = 5 /\ b > 2 else a = 0 endif

huangapple
  • 本文由 发表于 2023年6月12日 06:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定