Z3使用目标和战术

时间:2012-09-17 16:46:04

标签: c z3

我正在使用Z3和C-API。我试图了解如何理解如何在C-API中使用策略和目标。我检查了Z3的例子,但找不到适合我的那个。

我的C-API伪代码如下 -

contextId = mk_context();
solverId = Z3_mk_solver(contextId);
Z3_solver_inc_ref(contextId, solverId);
... 
then some assertions like x > 0, y > 0 and so on..
...
// now comes the final goal
mygoal = Z3_mk_less_than(contextId, ...) //  x < 50
Z3_solver_assert (contextId, solverId, mygoal)
...
// finally   check
Z3_solver_check(contextId, solverId)
Z3_reset_memory();
Z3_del_context(contextId);
Z3_solver_dec_ref(contextId, solverId);

现在我想对mygoal应用一些策略。 但是,我无法了解C-API究竟应该遵循的方式。 我检查了文档,但找不到这样做的例子。

我尝试使用Z3_goal_assert (); API。但它并没有以某种方式起作用。 有人可以用C-API给我一个简单的例子吗?

更新:我尝试了以下C代码,但它不起作用。 在函数调用Z3_tactic_apply(),求解器抛出这样的错误 -

pure virtual method called
terminate called without an active exception

一段代码:

goalId = Z3_mk_goal();      
Z3_goal_inc_ref(context, goalId);

assertionVector = Z3_solver_get_assertions (context, solver);
int vectorSize = Z3_ast_vector_size(assertionVector);

for(int i=0;i<vectorSize;i++)       
    Z3_goal_assert(context, goalId, Z3_ast_vector_get(context, assertionVector, i));

Z3_goal_assert(context, goalId, Z3_mk_eq(context, totalProcDecl, Z3_mk_int(context, numProcessors, int_sort)));
Z3_goal_assert(context, goalId, Z3_mk_eq(contextlatencyDecl, Z3_mk_int(context, latencyConstraint, int_sort)));

// This is what I am trying to apply
// (check-sat-using (then (! simplify :arith-lhs true) solve-eqs lia2pb pb2bv bit-blast sat))

tactic0 = Z3_mk_tactic (context, "simplify");
Z3_tactic_inc_ref (context,tactic0);

tactic1 = Z3_mk_tactic (context, "solve-eqs");
Z3_tactic_inc_ref (context, tactic1);

tactic2 = Z3_mk_tactic (context, "lia2pb");
Z3_tactic_inc_ref (context, tactic2);

tactic3 = Z3_mk_tactic (context, "pb2bv");
Z3_tactic_inc_ref (context, tactic3);

tactic4 = Z3_mk_tactic (context, "bit-blast");
Z3_tactic_inc_ref (context, tactic4);

tactic5 = Z3_mk_tactic (context, "sat");
Z3_tactic_inc_ref (context, tactic5);

temp = Z3_tactic_and_then (context, tactic0, tactic1);
temp = Z3_tactic_and_then (context, temp, tactic2);
temp = Z3_tactic_and_then (context, temp, tactic3);
temp = Z3_tactic_and_then (context, temp, tactic4);
temp = Z3_tactic_and_then (context, temp, tactic5);

result = Z3_tactic_apply (context, temp, goalId);
printf("Result : %s\n", Z3_apply_result_to_string (context, result));

// Finished Solving.
Z3_goal_dec_ref (context, goalId);
Z3_tactic_dec_ref (context, tactic0);
Z3_tactic_dec_ref (context, tactic1);
Z3_tactic_dec_ref (context, tactic2);
Z3_tactic_dec_ref (context, tactic3);
Z3_tactic_dec_ref (context, tactic4);
Z3_tactic_dec_ref (context, tactic5);

我还尝试了另外一个选项来添加参数以简化策略。

tactic0_without_param = Z3_mk_tactic (context, "simplify");
Z3_tactic_inc_ref (context,tactic0_without_param);

paramsId = Z3_mk_params(context);
Z3_params_inc_ref(context, paramsId);
Z3_params_set_bool (context, p, paramsId, Z3_mk_string_symbol(context, ":arith-lhs"), true);
tactic0 = Z3_tactic_using_params (context, tactic0, paramsId);

但又一次没有用。

感谢。

1 个答案:

答案 0 :(得分:3)

Z3 4.x附带了C ++头文件,使Z3 C API更易于使用(文件include/z3++.h)。 它还有一个基于C ++的示例(文件examples/c++/example.cpp)。该文件包含许多使用战术对象的示例。

话虽如此,战术应该适用于目标。为方便起见,我们还提供了一个API,可以根据策略Z3_mk_solver_from_tactic创建解算器。此API返回的求解器对象将尝试使用给定的策略解决可满足性查询。