Z3 C-API for solver timeout

2019-07-18 22:47发布

问题:

I am using Z3 4.1 C-API on linux. I want to specify a timeout for a solver.

I am using following commands, however I get a segmentation fault in the command Z3_solver_set_params().

Z3_context ctx = mk_context();     
    Z3_solver s = Z3_mk_solver(ctx);     

    Z3_params params = Z3_mk_params(ctx);     
    Z3_symbol r = Z3_mk_string_symbol(ctx, ":timeout");     

    Z3_params_set_uint(ctx, params, r, static_cast<unsigned>(10));     
    Z3_solver_set_params(ctx, s, params);     

It seems that I am not using APIs correctly.
I couldn't find any example for C-APIs to set a solver timeout in test_capi.c file containing examples. Can anyone help?

回答1:

You need to increment reference counts on the solver and parameters before doing anything else. Here is a snippet that will go through.

Z3_config cfg = Z3_mk_config();
Z3_context ctx = Z3_mk_context(cfg);     
Z3_solver s = Z3_mk_solver(ctx);   
Z3_solver_inc_ref(ctx, s); 
{

Z3_params params = Z3_mk_params(ctx);  
Z3_params_inc_ref(ctx, params);
{
Z3_symbol r = Z3_mk_string_symbol(ctx, ":timeout");    


Z3_params_set_uint(ctx, params, r, 10);
Z3_solver_set_params(ctx, s, params);  
Z3_params_dec_ref(ctx, params);
}
}
Z3_solver_dec_ref(ctx, s);
Z3_del_config(cfg);
Z3_del_context(ctx);


标签: z3