有条件的约束解决

发布于 2025-02-10 06:50:34 字数 411 浏览 1 评论 0 原文

您将如何处理以下约束优化问题:

  • 我有一组整数变量 x [i] ,可以在 [1,4] 范围内仅获取4个
  • 值是表单的约束 c< = x [i] x [i]< = c x [i]< = x [ j]
  • 也有有条件约束,但仅具有“如果 2< = x [i] ,然后 3< = x [j]
  • 我想最大程度地减少具有值 3 编辑的变量数量

:因为我有大量(数千)变量,约束和性能很重要,所以我m寻找专用算法,不使用通用约束求解器。

How would you approach the following constraint optimization problem:

  • I have a set of integer variables x[i] that can takes only 4 values in the [1,4] range
  • There are constraints of the form C <= x[i], x[i] <= C, and x[i] <= x[j]
  • There are also conditional constraints, but exclusively of the form "if 2 <= x[i] then 3 <= x[j]"
  • I want to minimize the number of variables that have the value 3

Edit: because I have a large (thousands) number of variables and constraints and performance is critical, I’m looking for a dedicated algorithm, not using a general-purpose constraint solver.

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

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

发布评论

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

评论(2

萌无敌 2025-02-17 06:50:34

您可以将每个变量编码为一对二进制变量:

x[i] = 1 + 2*x2[i] + x1[i]

现在可以部分解决不等式的约束:

1 <= x[i]   can be ignored, as always true for any variable
2 <= x[i]   implies (x2[i] or x1[i])
3 <= x[i]   implies x2[i]
4 <= x[i]   implies (x2[i] and x1[i])

1 >= x[i]   implies (!x2[i] and !x1[i])
2 >= x[i]   implies !x2[i]
3 >= x[i]   implies (!x2[i] or !x1[i])
4 >= x[i]   can be ignored, as always true for any variable

x[i] <= x[j] implies (!x2[i] or x2[j]) and 
                     (!x1[i] or x2[j] o x1[j]) and
                     (!x2[i] or !x1[i] or x1[j])

条件约束

if 2 <= x[i] then 3 <= x[j]

转换为

x2[j] or !x1[i]

上述编码,可以直接写入连词正常形式(。例如 satinterface bc2cnf 有助于自动化此翻译。

为了最大程度地减少具有值 3 的变量数量,可以构建/建模与数字比较器的计数电路结合使用。

变量 x [i] 具有值 3 ,如果(x2 [i]和!x1 [i])是正确的。这些表达式可以是计数器的输入。然后可以将计数结果与一些值进行比较,该值将减少,直到找不到更多解决方案为止。

底线:
可以使用通用求解器来解决该问题https://github.com/arminbiere/cadical“ rel =“ nofollow noreferrer”> cadical , cryptominisat )或像 minizinc 。我不知道有一种专用算法,该算法的表现会优于通用求解器。

You could encode each variable as a pair of binary variables:

x[i] = 1 + 2*x2[i] + x1[i]

The inequality constraints can now be partly resolved:

1 <= x[i]   can be ignored, as always true for any variable
2 <= x[i]   implies (x2[i] or x1[i])
3 <= x[i]   implies x2[i]
4 <= x[i]   implies (x2[i] and x1[i])

1 >= x[i]   implies (!x2[i] and !x1[i])
2 >= x[i]   implies !x2[i]
3 >= x[i]   implies (!x2[i] or !x1[i])
4 >= x[i]   can be ignored, as always true for any variable

x[i] <= x[j] implies (!x2[i] or x2[j]) and 
                     (!x1[i] or x2[j] o x1[j]) and
                     (!x2[i] or !x1[i] or x1[j])

Conditional constraint

if 2 <= x[i] then 3 <= x[j]

translates to

x2[j] or !x1[i]

The encoding shown above can be directly written as Conjunctive Normal Form (CNF) suitable for a SAT solver. Tools like SATInterface or bc2cnf help to automate this translation.

To minimize the number of variables which have value 3, a counting circuit combined with a digital comparator could be constructed/modelled.

Variable x[i] has value 3, if (x2[i] and !x1[i]) is true. These expressions could be inputs of a counter. The counting result could then be compared to some value which is decreased until no more solutions can be found.

Bottom line:
The problem can be solved with a general purpose solver like a SAT solver (CaDiCal, Z3, Cryptominisat) or a constraint solver like Minizinc. I am not aware of a dedicated algorithm which would outperform the general purpose solvers.

温柔戏命师 2025-02-17 06:50:34

实际上,对于此特定问题,有一种相当简单有效的算法。
当下限变为&gt; = 2 时,维持和传播间隔并开始传播条件约束就足够了。
最后,如果间隔完全 [3,4] ,最佳解决方案是选择 4

更准确地说:

  • 初始化 l [i]:= 1 u [i]:= 4
  • 传播约束,直到fixpoint如下:
    • 约束“ c&lt; = x [i] “: l [i]:= max(l [i],c)
    • 约束“ x [i]&lt; = c “: u [i]:= min(u [i],c)
    • 约束“ x [i]&lt; = x [j] “: l [j]:= max(l [j],l [i])) u [i]:= min(u [i],u [j])
    • 约束 2&lt; = x [i] ==&gt; 3&lt; = x [j] :如果 2&lt; = l [i] ,然后 l [j]:= max(l [j],3)
  • 如果 u [i]&lt; l [i] ,则没有解决方案
  • ,否则请选择:
    • x [i] = l [i] 如果 u [i]&lt; = 3
    • x [i] = u [i] 否则

否则是正确且最佳的,因为:

  • 任何解决方案都以至于 l [i]&lt; = x [i]&lt; = u [i] ,因此,如果 u [i]&lt; l [ i] ,没有解决方案
  • ,否则 x [i] = l [i] 显然是一种解决方案(但不是 x [i] = u [i] 因为可以是 u [i]&gt; = 2 ,但 u [j] 不是&gt; = 3
  • ) 到 4 在可能的情况下仍然是解决方案
  • x [i] 3 被迫为 3 的变量( l [i] = u [i] = 3 ),因此我们找到了一个解决方案,该解决方案的最小数量 3

在更多详细信息中,这是一个完整的证明:

  • 假设解决方案 x [i] 使得 l [i]&lt; = x [i]&lt; = u [i] ,让我们证明通过应用任何传播规则来保留此不变性:
    • 约束“ x [i]&lt; = x [j] “: x [i]&lt; = x [j]&lt; = u [j] 因此 x [i] 既是&lt; = u [i] and &lt; = u [j] ,因此&lt ; = min(u [i],u [j])。同样, l [i]&lt; = x [i]&lt; = x [j] so max(l [i],l [j])&lt; = x [j]
    • 约束“ x [i]&lt; = c “和” c&lt; = x [i] “相似
    • 对于约束“ 2&lt; = x [i] ==&gt; 3&lt; = x [j] “: l [i]&lt; 2 和传播规则不应用或 2&lt; = l [i] ,然后 2&lt; = l [i]&lt; = x [i] 暗示 3&lt ; = x [j] 3&lt; = x [j] l [j]&lt; = x [j] 因此 max(3,l [j])&lt; = = x [j]
  • 结果, 当达到fixpoint并且无法应用不再应用规则时,如果任何 i 是如此,那么 u [i]&lt; l [i ] ,然后没有解决方案
  • ,否则,让我们证明此 x [i] 是一个解决方案,其中: x [i] = l [i] 如果 u [i]&lt; = 3 x [i] = u [i] 否则:否则:
    • 请注意, X [i] l [i] u [i] ,所以 l [i] &lt; = x [i]&lt; = u [i]
    • 对于所有约束,“ c&lt; = x [i] ”,在fixpoint,我们有 l [i] = max(l [i],c),即, c&lt; = l [i]&lt; = x [i] ,并且满足约束
    • 对于所有约束,“ x [i]&lt; = c ”,在FIXPOINT上,我们同样具有 u [i]&lt; = C and code> x [i]&lt; = u [i]&lt; = c ,并且满足约束
    • 对于所有“ x [i]&lt; = x [j] ',在fixpoint上,我们有: u [i] = min(u [i],u [j ]) so u [i]&lt; = u [j] l [j] = max(l [j],l [i]) ,因此 l [i]&lt; = l [j] 。然后:
      • 如果 u [j]&lt; = 3 然后 u [i]&lt; = u [j]&lt; = 3 so x [i] = l [i]&lt; = l [j] = x [j]
      • 否则, x [j] = u [j] x [i]&lt; = u [i]&lt; = u [j] = x [j]


    • 对于所有“ 2&lt; = x [i] ==&gt; 3&lt; = x [j] “:假设 2&lt; = x [i]
      • 如果 u [i]&lt; = 3 ,则是:
        • l [i]&lt; = 2 ,fixpoint表示 l [j]:= max(l [j],3) so 3&lt; = l [j]&lt; = x [j]
        • l [i] = 3 3 = l [i]&lt; = l [j]&lt; = x [j]
      • 如果 u [i]&gt; 3 ,则 3&lt; u [i]&lt; = u [j] and 3&lt; u [i] &lt; = u [j] = x [j]


  • 最后该解决方案是最佳的,因为:
    • 如果 l [i] = u [i] = 3 ,任何解决方案都必须具有 x [i] = 3
    • 否则, x [i]!= 3 :如果 u [i]&lt; = 3 ,那么 u [i] = 3 x [i] = l [i]&lt; 3 x [i]&lt; = u [i]&lt; 3 ;如果 u [i]&gt; 3 然后 x [i] = u [i]!= 3


Actually, there is a fairly simple and efficient algorithm for this particular problem.
It is enough to maintain and propagate intervals and start propagating the conditional constraints when the lower bounds become >= 2.
At the end, if the interval is exactly [3,4], the optimal solution is to select 4.

More precisely:

  • initialize l[i]:=1, u[i]:=4
  • propagate constraints until fixpoint as follows:
    • Constraint "C<=x[i]": l[i]:=max(l[i],C)
    • Constraint "x[i]<=C": u[i]:=min(u[i],C)
    • Constraint "x[i]<=x[j]": l[j]:=max(l[j],l[i]) and u[i]:=min(u[i],u[j])
    • Constraint 2<=x[i] ==> 3<=x[j]: if 2<=l[i], then l[j]:=max(l[j], 3)
  • If u[i]<l[i], there is no solution
  • Otherwise, select:
    • x[i]=l[i] if u[i]<=3
    • x[i]=u[i] otherwise

This is correct and optimal because:

  • any solution is such that l[i]<=x[i]<=u[i], so if u[i]<l[i], there is no solution
  • otherwise, x[i]=l[i] is clearly a solution (but NOT x[i]=u[i] because it can be that u[i]>=2 but u[j] is not >=3)
  • bumping all x[i] from 3 to 4 when possible is still a solution because this change doesn't activate any new conditional constraints
  • what remains are the variables that are forced to be 3 (l[i]=u[i]=3), so we have found a solution with the minimal number of 3

In more details, here is a full proof:

  • assume that a solution x[i] is such that l[i]<=x[i]<=u[i] and let's prove that this invariant is preserved by application of any propagation rule:
    • Constraint "x[i]<=x[j]": x[i]<=x[j]<=u[j] and so x[i] is both <=u[i] and <=u[j] and hence <=min(u[i],u[j]). Similarly, l[i]<=x[i]<=x[j] so max(l[i],l[j])<=x[j]
    • The constraints "x[i]<=C" and "C<=x[i]" are similar
    • For the constraint "2<=x[i] ==> 3<=x[j]": either l[i]<2 and the propagation rule doesn't apply or 2<=l[i] and then 2<=l[i]<=x[i] implying 3<=x[j]. So 3<=x[j] and l[j]<=x[j] hence max(3,l[j])<=x[j]
  • as a result, when the fixpoint is reached and no rule can be applied anymore, if any i is such that u[i]<l[i], then there is no solution
  • otherwise, let's prove that this x[i] is a solution where: x[i]=l[i] if u[i]<=3 and x[i]=u[i] otherwise:
    • Note that x[i] is either l[i] or u[i], so l[i]<=x[i]<=u[i]
    • For all constraints "C<=x[i]", at fixpoint, we have l[i]=max(l[i],C), i.e., C<=l[i]<=x[i] and the constraint is satisfied
    • For all constraints "x[i]<=C", at fixpoint, we similarly have u[i]<=C and x[i]<=u[i]<=C and the constraint is satisfied
    • For all "x[i]<=x[j]", at fixpoint, we have: u[i] = min(u[i],u[j]) so u[i]<=u[j] and l[j] = max(l[j],l[i]), so l[i]<=l[j]. Then:
      • If u[j]<=3 then u[i]<=u[j]<=3 so x[i]=l[i]<=l[j]=x[j]
      • Otherwise, x[j]=u[j] and x[i]<=u[i]<=u[j]=x[j]
    • For all "2<=x[i] ==> 3<=x[j]": assume 2<=x[i]:
      • If u[i]<=3, then either:
        • l[i]<=2 and the fixpoint means l[j]:=max(l[j], 3) so 3<=l[j]<=x[j]
        • or l[i]=3 and 3=l[i]<=l[j]<=x[j]
      • If u[i]>3, then 3<u[i]<=u[j] and 3<u[i]<=u[j]=x[j]
  • Finally the solution is optimal because:
    • if l[i]=u[i]=3, any solution must have x[i]=3
    • otherwise, x[i] != 3: if u[i]<=3, then either u[i]=3 and x[i]=l[i]<3 or x[i]<=u[i]<3; and if u[i]>3 then x[i]=u[i]!=3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文