1

我正在尝试在 C# 实现中使用 Z3 sat 求解器。这段代码非常接近微软自己在“ http://z3.codeplex.com/SourceControl/latest#examples/dotnet/Program.cs ”中给出的示例。我的代码是:

using (Context ctx = new Context(new Dictionary<string, string>() { { "proof", "true" } }))
{
...
Expr x = ctx.MkConst("x", ctx.MkIntSort());
Expr y = ctx.MkConst("y", ctx.MkIntSort());
Expr zero = ctx.MkNumeral(0, ctx.MkIntSort());
Expr one = ctx.MkNumeral(1, ctx.MkIntSort());
Expr five = ctx.MkNumeral(5, ctx.MkIntSort());

Solver s = ctx.MkSolver();

s.Assert(ctx.MkGt((ArithExpr)x, (ArithExpr)zero)); // x > 0
s.Assert(ctx.MkLt((ArithExpr)y, (ArithExpr)five)); // y < 5
s.Assert(ctx.MkLt((ArithExpr)x, (ArithExpr)zero)); // x < 0
s.Assert(ctx.MkEq((ArithExpr)y, ctx.MkAdd((ArithExpr)x, (ArithExpr)one))); // y = x + 1

 Status result = s.Check();

 if (result == Status.UNSATISFIABLE)
 {
     Console.WriteLine("unsat");
     Console.WriteLine("proof: {0}", s.Proof);
     Console.WriteLine("core: ");
     foreach (Expr c in s.UnsatCore)
     {
         Console.WriteLine("{0}", c);
     }
 }

但是当我到达这个模型的“s.UnsatCore”时,它仍然是空的。

我也尝试过输入这样的断言:

BoolExpr constraint1 = ctx.MkBoolConst("Constraint1");
s.AssertAndTrack(ctx.MkGt((ArithExpr)x, (ArithExpr)zero), constraint1);
BoolExpr constraint2 = ctx.MkBoolConst("Constraint2");
s.AssertAndTrack(ctx.MkLt((ArithExpr)y, (ArithExpr)five), constraint2);
BoolExpr constraint3 = ctx.MkBoolConst("Constraint3");
s.AssertAndTrack(ctx.MkLt((ArithExpr)x, (ArithExpr)zero), constraint3);
BoolExpr constraint4 = ctx.MkBoolConst("Constraint4");
s.AssertAndTrack(ctx.MkEq((ArithExpr)y, ctx.MkAdd((ArithExpr)x, (ArithExpr)one)), constraint4);

我希望 UnsatCore 返回“constraint1,constint3”。谁能帮我解决我做错了什么?

4

1 回答 1

0

您在创建 ctx 时是否启用了证明生成(Solver.UnsatCore 的文档指出如果禁用,则结果为空)?您需要将 Dictionary ("proof -> "true") 传递给 Context 的构造函数来执行此操作。

于 2015-02-11T16:50:42.623 回答