将布尔FlatZinc转换为CNF DIMACS

时间:2014-03-13 22:20:36

标签: constraint-programming satisfiability minizinc

要解决set of Boolean equations,我正在尝试使用以下输入Constraint-Programming Solver MiniZinc

%  Solve system of Brent's equations modulo 2

%  Matrix dimensions
int: aRows = 3;
int: aCols = 3;
int: bCols = 3;
int: noOfProducts = 23;

%  Dependent parameters
int: bRows = aCols;
int: cRows = aRows;
int: cCols = bCols;
set of int: products = 1..noOfProducts;

%  Corefficients are stored in arrays
array[1..aRows, 1..aCols, products] of var bool: A;
array[1..bRows, 1..bCols, products] of var bool: B;
array[1..cRows, 1..cCols, products] of var bool: C;

constraint
    forall(rowA in 1..aRows, colA in 1..aCols) (
        forall(rowB in 1..bRows, colB in 1..bCols) (
            forall (rowC in 1..cRows, colC in 1..cCols) (
                xorall (k in products) (
                    A[rowA, colA, k] /\ B[rowB, colB, k] /\ C[rowC, colC, k]
                ) == ((rowA == rowC) /\ (colB == colC) /\ (colA == rowB))
            )
        )
    );

solve satisfy;

%  Output solution as table of variable value assignments
output 
["\nSolution for <" ++ show(aRows) ++ ", " ++ show(aCols) ++ 
                 ", " ++ show(bCols) ++ "> " ++ show(noOfProducts) ++ " products:"] ++
["\nF" ++ show(100*rowA+10*colA+k) ++ " = " ++ 
show(bool2int(A[rowA, colA, k])) |
rowA in 1..aRows, colA in 1..aCols, k in products] ++

["\nG" ++ show(100*rowB+10*colB+k) ++ " = " ++ 
show(bool2int(B[rowB, colB, k])) |
rowB in 1..bRows, colB in 1..bCols, k in products] ++

["\nD" ++ show(100*rowC+10*colC+k) ++ " = " ++ 
show(bool2int(C[rowC, colC, k])) |
rowC in 1..cRows, colC in 1..cCols, k in products];

MiniZinc确实找到了小参数(rows=cols=2, products=7)的解决方案,但是稍微增加的参数并没有结束。 我想将生成的FlatZinc模型反馈到SAT solverCryptominisatLingelingClasp。我希望这些工具可能胜过现有的MiniZinc后端。

我的问题:
有没有可用于将纯布尔FlatZinc模型转换为CNF (DIMACS)的工具? 我可以做些什么来替换xorall()谓词,因为一些MiniZinc后端似乎不支持它?

1 个答案:

答案 0 :(得分:4)

我不知道有任何将FlatZinc文件转换为CNF(DIMACS)文件的工具。 (MiniZinc发行版有一个将flatzinc转换为XCSP格式的程序。也许有一个XCSP到CNF的工具?)

然而,有一些基于SAT /灵感的解算器可能更好,例如minicsp,fzn2smt。问题是它们 - 正如你所提到的 - 不支持相当新的xorall()函数。

此外,使用带标签的搜索可能是个好主意,例如这样的事情(请注意bool_search)

  solve :: bool_search(
       [A[i,j,k] | i in 1..aRows, j in 1..aCols, k in products],
       first_fail,
       indomain_min,
       complete)
     satisfy;

另外,我建议您测试转换为基于0..1的模型,这些解算器可以像其他模型一样进行测试。

这是我转换后的模型,我刚刚将var bool更改为var 0..1并用sum()和bool2int()替换了xorall()[我希望我转换正确。]更新:我已经改变了到Axel建议的版本。

 %  Solve system of Brent's equations modulo 2

 %  Matrix dimensions
 int: aRows = 3;
 int: aCols = 3;
 int: bCols = 3;
 int: noOfProducts = 23;

 %  Dependent parameters
 int: bRows = aCols;
 int: cRows = aRows;
 int: cCols = bCols;
 set of int: products = 1..noOfProducts;

 %  Corefficients are stored in arrays
 array[1..aRows, 1..aCols, products] of var 0..1: A; % hakank: change to 0..1
 array[1..bRows, 1..bCols, products] of var 0..1: B;
 array[1..cRows, 1..cCols, products] of var 0..1: C;

constraint
     forall(rowA in 1..aRows, colA in 1..aCols) (
         forall(rowB in 1..bRows, colB in 1..bCols) (
             forall (rowC in 1..cRows, colC in 1..cCols) (
                 % hakank: changed
                 sum (k in products) (
                     bool2int(A[rowA, colA, k]=1/\ B[rowB, colB, k]=1 /\ C[rowC, colC, k]=1)
                ) == 
                     %% bool2int(rowA == rowC)+ bool2int(colB == colC) + bool2int(colA == rowB)
                     bool2int((rowA == rowC)/\(colB == colC)/\(colA == rowB))
             )
         )
     );


     solve :: int_search(
         [A[i,j,k] | i in 1..aRows, j in 1..aCols, k in products] ++ 
         [B[i,j,k] | i in 1..aRows, j in 1..aCols, k in products] ++ 
         [C[i,j,k] | i in 1..aRows, j in 1..aCols, k in products] 
         ,
         first_fail,
         indomain_min,
         complete)
     satisfy;

    %  Output solution as table of variable value assignments
    output 
    ["\nSolution for <" ++ show(aRows) ++ ", " ++ show(aCols) ++ 
             ", " ++ show(bCols) ++ "> " ++ show(noOfProducts) ++ " products:"] ++
    ["\nF" ++ show(100*rowA+10*colA+k) ++ " = " ++ 
        show(A[rowA, colA, k]) |
        rowA in 1..aRows, colA in 1..aCols, k in products] ++

    ["\nG" ++ show(100*rowB+10*colB+k) ++ " = " ++ 
       show(B[rowB, colB, k]) |
       rowB in 1..bRows, colB in 1..bCols, k in products] ++

    ["\nD" ++ show(100*rowC+10*colC+k) ++ " = " ++ 
       show(C[rowC, colC, k]) |
       rowC in 1..cRows, colC in 1..cCols, k in products];

以下是模型:http://www.hakank.org/minizinc/akemper1_2.mzn

[更新:这些时间适用于较早的,错误的模型。]模型中的问题实例由minicsp在3s(包括展平),5s内的Opturion CPX求解器,fzn2smt解决(第一解决方案) 6S。并且可以使用标签等进一步调整模型。

提到的解决方案的链接:

另请参阅我的MiniZinc页面,查看更长的FlatZinc解算器列表:http://www.hakank.org/minizinc/