Microsoft Solver Foundation SAT CNF

时间:2014-09-11 14:23:48

标签: c# solver cnf

我正在尝试使用Microsoft Solver Foundation SatSolver通过Visual Studio(C#或VB)解决简单的CNF问题。 任何人都可以发一个简单的例子来解释如何做到这一点吗?

这是一个简短的例子:

        ConstraintSystem s1 = ConstraintSystem.CreateSolver();

        CspTerm t1 = s1.CreateBoolean("v1");
        CspTerm t2 = s1.CreateBoolean("v2");
        CspTerm t3 = s1.CreateBoolean("v3");
        CspTerm t4 = s1.CreateBoolean("v4");

        CspTerm tOr12 = s1.Or(s1.Neg(t1), s1.Neg(t2));
        CspTerm tOr13 = s1.Or(s1.Neg(t1), s1.Neg(t3));
        CspTerm tOr14 = s1.Or(s1.Neg(t1), s1.Neg(t4));

        CspTerm tOr23 = s1.Or(s1.Neg(t2), s1.Neg(t3));
        CspTerm tOr24 = s1.Or(s1.Neg(t2), s1.Neg(t4));

        CspTerm tOr34 = s1.Or(s1.Neg(t3), s1.Neg(t4));

        CspTerm tOr = s1.Or(t1, t2, t3, t4);

        s1.AddConstraints(tOr12);
        s1.AddConstraints(tOr13);
        s1.AddConstraints(tOr14);
        s1.AddConstraints(tOr23);
        s1.AddConstraints(tOr24);
        s1.AddConstraints(tOr34);
        s1.AddConstraints(tOr);

        ConstraintSolverSolution solution1 = s1.Solve();
        Console.WriteLine(solution1[t1]);
        Console.WriteLine(solution1[t2]);
        Console.WriteLine(solution1[t3]);
        Console.WriteLine(solution1[t4]);

结果应该只有一个值为1的变量,其余的应该为0,但解决方案是1,1,1,0。

由于 盖

1 个答案:

答案 0 :(得分:0)

您应该使用s1.Neg(t1)代替{{1}}。