您将如何处理以下约束优化问题:
- 我有一组整数变量
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.
发布评论
评论(2)
您可以将每个变量编码为一对二进制变量:
现在可以部分解决不等式的约束:
条件约束
转换为
上述编码,可以直接写入连词正常形式(。例如 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:
The inequality constraints can now be partly resolved:
Conditional constraint
translates to
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 value3
, 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.
实际上,对于此特定问题,有一种相当简单有效的算法。
当下限变为
&gt; = 2
时,维持和传播间隔并开始传播条件约束就足够了。最后,如果间隔完全
[3,4]
,最佳解决方案是选择4
。更准确地说:
l [i]:= 1
,u [i]:= 4
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]
somax(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]
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 ])
sou [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
sox [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)
so3&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]
and3&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 select4
.More precisely:
l[i]:=1
,u[i]:=4
C<=x[i]
":l[i]:=max(l[i],C)
x[i]<=C
":u[i]:=min(u[i],C)
x[i]<=x[j]
":l[j]:=max(l[j],l[i])
andu[i]:=min(u[i],u[j])
2<=x[i] ==> 3<=x[j]
: if2<=l[i]
, thenl[j]:=max(l[j], 3)
u[i]<l[i]
, there is no solutionx[i]=l[i]
ifu[i]<=3
x[i]=u[i]
otherwiseThis is correct and optimal because:
l[i]<=x[i]<=u[i]
, so ifu[i]<l[i]
, there is no solutionx[i]=l[i]
is clearly a solution (but NOTx[i]=u[i]
because it can be thatu[i]>=2
butu[j]
is not>=3
)x[i]
from3
to4
when possible is still a solution because this change doesn't activate any new conditional constraints3
(l[i]=u[i]=3
), so we have found a solution with the minimal number of3
In more details, here is a full proof:
x[i]
is such thatl[i]<=x[i]<=u[i]
and let's prove that this invariant is preserved by application of any propagation rule:x[i]<=x[j]
":x[i]<=x[j]<=u[j]
and sox[i]
is both<=u[i]
and<=u[j]
and hence<=min(u[i],u[j])
. Similarly,l[i]<=x[i]<=x[j]
somax(l[i],l[j])<=x[j]
x[i]<=C
" and "C<=x[i]
" are similar2<=x[i] ==> 3<=x[j]
": eitherl[i]<2
and the propagation rule doesn't apply or2<=l[i]
and then2<=l[i]<=x[i]
implying3<=x[j]
. So3<=x[j]
andl[j]<=x[j]
hencemax(3,l[j])<=x[j]
i
is such thatu[i]<l[i]
, then there is no solutionx[i]
is a solution where:x[i]=l[i]
ifu[i]<=3
andx[i]=u[i]
otherwise:x[i]
is eitherl[i]
oru[i]
, sol[i]<=x[i]<=u[i]
C<=x[i]
", at fixpoint, we havel[i]=max(l[i],C)
, i.e.,C<=l[i]<=x[i]
and the constraint is satisfiedx[i]<=C
", at fixpoint, we similarly haveu[i]<=C
andx[i]<=u[i]<=C
and the constraint is satisfiedx[i]<=x[j]
", at fixpoint, we have:u[i] = min(u[i],u[j])
sou[i]<=u[j]
andl[j] = max(l[j],l[i])
, sol[i]<=l[j]
. Then:u[j]<=3
thenu[i]<=u[j]<=3
sox[i]=l[i]<=l[j]=x[j]
x[j]=u[j]
andx[i]<=u[i]<=u[j]=x[j]
2<=x[i] ==> 3<=x[j]
": assume2<=x[i]
:u[i]<=3
, then either:l[i]<=2
and the fixpoint meansl[j]:=max(l[j], 3)
so3<=l[j]<=x[j]
l[i]=3
and3=l[i]<=l[j]<=x[j]
u[i]>3
, then3<u[i]<=u[j]
and3<u[i]<=u[j]=x[j]
l[i]=u[i]=3
, any solution must havex[i]=3
x[i] != 3
: ifu[i]<=3
, then eitheru[i]=3
andx[i]=l[i]<3
orx[i]<=u[i]<3
; and ifu[i]>3
thenx[i]=u[i]!=3