英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论