简体   繁体   English

获取 MiniZinc 中变量的附加约束数

[英]Get number of attached constraints on a variable in MiniZinc

I have two sets of variables in my Minizinc program.我的 Minizinc 程序中有两组变量。 Each variable from the first set necessarily has several constraints placed on it, but the variables in the second set are only implicitly constrained via their interactions with variables in the first set.第一组中的每个变量都必然有几个约束,但第二组中的变量仅通过它们与第一组中的变量的交互而受到隐式约束。 This means that each of the variables in the second set may have anywhere from 0 to ~8 constraints placed on it, depending on the values taken by the variables in the first set.这意味着第二组中的每个变量可能具有从 0 到 ~8 的任何约束,具体取决于第一组中的变量所取的值。

I see that there is a way to reference the number of constraints placed on a variable at search time via the dom_w_deg search annotation, but I was wondering if there was anyway to access this information at runtime?我看到有一种方法可以通过dom_w_deg搜索注释在搜索时引用放置在变量上的约束数量,但我想知道是否有任何方法可以在运行时访问这些信息? I want to do this because I would like to specify additional constraints related to the number of constraints already placed on the variables.我想这样做是因为我想指定与已经放置在变量上的约束数量相关的附加约束。

I realize this is a weird question, and I may be approaching this whole thing the wrong way, but I've been banging my head against this problem for a while now, so figured I'd ask.我意识到这是一个奇怪的问题,我可能以错误的方式处理整个事情,但我一直在努力解决这个问题一段时间,所以我想我会问。

As a general rule, I think that you are approaching your problem erroneously.作为一般规则,我认为您正在错误地处理您的问题。 There are several mis-conceptions in the approach that I can identify leading to this:我可以识别出导致这种情况的方法中有几个误解:

  • Different solver back-ends might do very different things with the model and how it is solved不同的求解器后端对 model 及其解决方式可能会做非常不同的事情
  • "A constraint" is not a meaningful concept for the solver. “约束”对于求解器来说不是一个有意义的概念。 A single constraint might be multiple propagators in the back-end solver, a single propagator, or even just part of a propagator covering several constraints (assuming that it is a propagator based back-end).单个约束可能是后端求解器中的多个传播器,单个传播器,甚至只是覆盖多个约束的传播器的一部分(假设它是基于传播器的后端)。
  • Constraint models have monotonic behavior, so you can not in a well-defined and meaningful way change the model based on the number of constraints connected to a variable.约束模型具有单调行为,因此您无法根据连接到变量的约束数量以明确且有意义的方式更改 model。
  • Given that a constraint maps to a single propagator, it may still have very different propagation strength, meaning that it might be done early or very late in the solving process.鉴于约束映射到单个传播器,它可能仍然具有非常不同的传播强度,这意味着它可能在求解过程的早期或非常晚期完成。

Without knowing what you are actually trying to achieve, as a general technique you might be interested in using reification, where the truth of a constraint is reflected onto a binary Boolean variable.在不知道您实际尝试实现什么的情况下,作为一种通用技术,您可能对使用具体化感兴趣,其中约束的真实性反映到二进制 Boolean 变量上。 In general, it is good practice to have as little reification as possible, since it does not propagate much, but sometimes it is needed.一般来说,最好的做法是尽可能少地进行具体化,因为它不会传播太多,但有时需要它。

As a very simple example of using reification, this is a (probably not very good) model that tries to maximize the number of constraints satisfied.作为使用具体化的一个非常简单的示例,这是一个(可能不是很好)model,它试图最大化满足的约束数量。

set of int: Domain = 1..10;
var Domain: x;
var Domain: y;
var Domain: z;
array[1..3] of var bool: holds;
constraint holds[1] <-> x < y;
constraint holds[2] <-> y < z;
constraint holds[3] <-> z < x;
var int: goal;
constraint goal = sum(holds);
solve maximize goal;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM