Z3 JAVA-API for solver timeout - timeout

How to set solver's timeout for Z3 JAVA API?
Back to this question again:
Here is my code:
Context ctx = getZ3Context();
solver = ctx.MkSolver();
Params p = ctx.MkParams();
p.Add("timeout", 1);
solver.setParameters(p);
Not work, the solver just running the query forever. Any idea about this?

I haven't used the Java API, but from Looking at the official Java example and at this snippet, I'd assume that something along the following lines should work:
Solver s = ctx.MkSolver();
Params p = ctx.MkParams();
p.Add("timeout", valueInMilliseconds); /* "SOFT_TIMEOUT" or ":timeout"? */
s.setParameters(p);

Okay, finally found a solution myself:
Context ctx = getZ3Context();
solver = ctx.MkSolver();
Params p = ctx.MkParams();
/* Also tried
* p.Add("timeout", 1),
* p.Add(":timeout", 1),
* neither worked.
*/
p.Add("soft_timeout", 1);
solver.setParameters(p);

Related

Get unsat core with z3py for non-linear constraints

I have a set of simple but non linear constraints like:
v0= Real('v0')
v1= Real('v1')
v2= Real('v2')
v3= Real('v3')
v4= Real('v4')
v5= Real('v5')
v6= Real('v6')
v7= Real('v7')
v8= Real('v8')
v9= Real('v9')
v10= Real('v10')
v11= Real('v11')
v12= Real('v12')
v13= Real('v13')
v14= Real('v14')
v15= Real('v15')
v16= Real('v16')
v17= Real('v17')
v18= Real('v18')
v19= Real('v19')
s = Solver()
s.set(unsat_core=True)
s.add(v0>=0, v0<=1)
s.add(v1>=0, v1<=1)
s.add(v2>=0, v2<=1)
s.add(v3>=0, v3<=1)
s.add(v4>=0, v4<=1)
s.add(v5>=0, v5<=1)
s.add(v6>=0, v6<=1)
s.add(v7>=0, v7<=1)
s.add(v8>=0, v8<=1)
s.add(v9>=0, v9<=1)
s.add(v10>=0, v10<=1)
s.add(v11>=0, v11<=1)
s.add(v12>=0, v12<=1)
s.add(v13>=0, v13<=1)
s.add(v14>=0, v14<=1)
s.add(v15>=0, v15<=1)
s.add(v16>=0, v16<=1)
s.add(v17>=0, v17<=1)
s.add(v18>=0, v18<=1)
s.add(v19>=0, v19<=1)
s.add(v0==v1*v4)
s.add(v4==(v5+v6+v19)-(v5*v6+v5*v19+v6*v19)+(v5*v6*v19))
s.add(v6==v7*v11)
s.add(v11==(v12+v15+v18)-(v12*v15+v12*v18+v15*v18)+(v12*v15*v18))
s.add(v15==(v16+v17)-(v16*v17))
s.add(v12==v13*v14)
s.add(v7==(v8+v9+v10)-(v8*v9+v8*v10+v9*v10)+(v8*v9*v10))
s.add(v1==(v2+v3)-(v2*v3))
(this is basically probability computation)
I want to add a set of contraints like
v0_pred = Bool('v0_pred')
s.add(Implies(v0_pred, v0==0.0046))
v19_pred = Bool('v19_pred')
s.add(Implies(v19_pred, v19==0.015))
v16_pred = Bool('v16_pred')
s.add(Implies(v16_pred, v16==0.0094))
v12_pred = Bool('v12_pred')
s.add(Implies(v12_pred, v12==0.0172))
v5_pred = Bool('v5_pred')
s.add(Implies(v5_pred, v5==0.0038))
result = s.check(v0_pred,v19_pred,v16_pred,v12_pred,v5_pred)
and to track these 5 constraints into the unsat core. But then, more often than not, the solver reports that it cannot find a solution (unknown). If I do just s.add(v0==0.0046, v19==0.015, v16==0.0094, v12==0.0172, v5==0.0038), it works excellently and quickly finds a solution (sat). But using just s.add() I cannot get the unsat core for unsat cases. What am I doing wrong?
UPD:
Example of the "unknown" problem with Solver.assert_and_track():
from z3 import *
def is_number(s):
try:
float(s.numerator_as_long())
return True
except AttributeError:
return False
v0= Real('v0')
v1= Real('v1')
v2= Real('v2')
v3= Real('v3')
v4= Real('v4')
v5= Real('v5')
v6= Real('v6')
v7= Real('v7')
v8= Real('v8')
v9= Real('v9')
v10= Real('v10')
v11= Real('v11')
v12= Real('v12')
v13= Real('v13')
v14= Real('v14')
v15= Real('v15')
v16= Real('v16')
v17= Real('v17')
v18= Real('v18')
v19= Real('v19')
s = Solver()
s.set(unsat_core=True)
s.add(v0>=0, v0<=1)
s.add(v1>=0, v1<=1)
s.add(v2>=0, v2<=1)
s.add(v3>=0, v3<=1)
s.add(v4>=0, v4<=1)
s.add(v5>=0, v5<=1)
s.add(v6>=0, v6<=1)
s.add(v7>=0, v7<=1)
s.add(v8>=0, v8<=1)
s.add(v9>=0, v9<=1)
s.add(v10>=0, v10<=1)
s.add(v11>=0, v11<=1)
s.add(v12>=0, v12<=1)
s.add(v13>=0, v13<=1)
s.add(v14>=0, v14<=1)
s.add(v15>=0, v15<=1)
s.add(v16>=0, v16<=1)
s.add(v17>=0, v17<=1)
s.add(v18>=0, v18<=1)
s.add(v19>=0, v19<=1)
s.add(v0==v1*v4)
s.add(v4==(v5+v6+v19)-(v5*v6+v5*v19+v6*v19)+(v5*v6*v19))
s.add(v6==v7*v11)
s.add(v11==(v12+v15+v18)-(v12*v15+v12*v18+v15*v18)+(v12*v15*v18))
s.add(v15==(v16+v17)-(v16*v17))
s.add(v12==v13*v14)
s.add(v7==(v8+v9+v10)-(v8*v9+v8*v10+v9*v10)+(v8*v9*v10))
s.add(v1==(v2+v3)-(v2*v3))
v0_pred = Bool('v0_pred')
s.assert_and_track(v0==0.0046, v0_pred)
v19_pred = Bool('v19_pred')
s.assert_and_track(v19==0.015, v19_pred)
v16_pred = Bool('v16_pred')
s.assert_and_track(v16==0.0094, v16_pred)
v12_pred = Bool('v12_pred')
s.assert_and_track(v12==0.0172, v12_pred)
v5_pred = Bool('v5_pred')
s.assert_and_track(v5==0.0038, v5_pred)
result = s.check()
print result
if result == z3.sat:
m = s.model()
for d in m.decls():
if (is_number(m[d])):
print "%s = %s" % (d.name(), float(m[d].numerator_as_long())/float(m[d].denominator_as_long()))
else:
print "%s = %s" % (d.name(), m[d])
elif result == z3.unsat:
print s.unsat_core()
It returns me unknown, while it is sat, and is solved if using Solver.add() instead.
You should use assert_and_track to track them into unsat cores. See this: https://z3prover.github.io/api/html/classz3py_1_1_solver.html#ad1255f8f9ba8926bb04e1e2ab38c8c15
Handling non-linearity
Since your problem includes non-linear constraints, z3 will possibly have hard time handling those with default settings. Heuristic selections depend on a lot of factors and picking the right tactics is more of an art. For the problem you posted, the following works:
t = z3.Then('simplify', 'qfnra-nlsat')
s = t.solver()
result = s.check()
With other problems, your mileage might vary. But I suspect the above tactic should work for you so long as the structure of your problem stays the same.

Solving simulataneous equation using maxima?

Hi I want to solve three equation, please find the details in picture. I want to find the value of alpha, h and Q in terms of alpha and h how do i do this using Maxima?
[B1=\frac{{{h}^{2}}}{\mathit{Qs}}]
[B2={{h}^{2}}+\alpha+1]
[B3=\frac{1}{\mathit{Qs}}]
[B0={{h}^{2}}]
[C2=\frac{{{h}^{4}}}{{{\mathit{Qs}}^{2}}}-2{{h}^{2}}\,\left( {{h}^{2}}+\alpha+1\right) =0]
[C4={{\left( {{h}^{2}}+\alpha+1\right) }^{2}}-\frac{2{{h}^{2}}}{{{\mathit{Qs}}^{2}}}-2{{h}^{2}}=0]
[C6=\frac{1}{{{\mathit{Qs}}^{2}}}-2\left( {{h}^{2}}+\alpha+1\right) =0]
algsys([C2,C4,C6],[h,alpha,Qs]);?????
I see that by just adding QS to the list of variables to solve for, I get some solutions. I didn't check them. Some of them may be redundant, I didn't check that either.
(%i20) algsys ([C2, C4, C6], [h, alpha, QS]);
(%o20) [[h = -1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = sqrt(2-sqrt(2))/2],
[h = 1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = sqrt(2-sqrt(2))/2],
[h = -1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = -sqrt(2-sqrt(2))/2],
[h = 1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = -sqrt(2-sqrt(2))/2],
[h = -1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = sqrt(sqrt(2)+2)/2],
[h = 1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = sqrt(sqrt(2)+2)/2],
[h = -1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = -sqrt(sqrt(2)+2)/2],
[h = 1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = -sqrt(sqrt(2)+2)/2]]
Try replacing h^2 with a single variable, say h2 (and replace h^4 with h2^2).
Try simplifying the equations with expand and/or ratsimp.
In solve(eqns, vars), the variables vars are the ones you are trying to solve for. So I think you want alpha, h, Q (well, actually alpha, h2, Q if you follow my previous advice).
Finally, PLEASE paste your input and output into the question instead of linking to an image, which makes it much more difficult for someone else to try working with your equations.

Error adding containts to solver in z3

assign wfwe = wb_acc & (adr_i == 2'b10) & ack_o & we_i;
For the above assign statement which is in verilog, i getting error while implememting it in z3
My code:
BitVecExpr[] wfwe = new BitVecExpr[1];
BitVecExpr[] wb_acc = new BitVecExpr[1];
BitVecExpr[] adr_i = new BitVecExpr[1];
BitVecExpr[] ack_o = new BitVecExpr[1];
BitVecExpr[] we_i = new BitVecExpr[1];
wfwe[0] = ctx.mkBVConst("wfwe",1);
wb_acc[0] = ctx.mkBVConst("wb_acc",1);
adr_i[0] = ctx.mkBVConst("adr_i",2);
ack_o[0] = ctx.mkBVConst("ack_o",1);
we_i[0] = ctx.mkBVConst("we_i",1);
Solver s = ctx.mkSolver();
s.add(ctx.mkBVAND(wb_acc[0],ctx.mkEq(adr_i[0],ctx.mkNumeral("2",2)),ack_o[0],we_i[0]));
I am getting error in above add statement:
error: method mkBVAND in class Context cannot be applied to given types;
required: BitVecExpr,BitVecExpr
found: BitVecExpr,BoolExpr
Which is true. Can anyone suggest me workaround. Am i implementing it incorrectly please let me know.
This error is reported because the second argument of mkBVAND is a Boolean expression (ctx.mkEq ...). Note that Booleans and BitVectors of size 1 are not the same thing, and they will not be converted automatically. The easiest way to convert between them is an if-then-else the selects the right values.
These are the problems with this example:
1) ctx.mkNumeral("2",2) is incorrect. I guess the intention was to create a bv-numeral of 2 bits with value 2; the easiest way to achieve that is ctx.mkBV(2, 2)
2) The 2nd argument of mkBVAND needs to be converted from Bool to BitVector, e.g., like so:
BoolExpr c = ctx.mkEq(adr_i[0], ctx.mkBV(2, 2));
BitVecExpr e = (BitVecExpr) ctx.mkITE(c, ctx.mkBV(1, 1), ctx.mkBV(0, 1));
e being the result.
3) ctx.mkBVAND takes exactly 2 arguments, no more and no less. Thus, the BVAND expression needs to be rewritten, e.g., like so:
ctx.mkBVAND(ctx.mkBVAND(wb_acc[0], e), ctx.mkBVAND(ack_o[0], we_i[0])))
4) The result needs to be converted to a Boolean expression again, e.g.
ctx.mkEq(q, ctx.mkBV(1, 1))
where q is the result of the BVAND.

Z3 C-API for solver timeout

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?
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);

An error appears when running exist quantifier and fixedpoint Z3 in C#

I tried to use ctx.mkExist in the fixedpoint, howwever, it occurs error said "contains recursive predicate", I don't know why? and How to use ctx.MkExists in fixedpoint?For example:
exist (lamda real) that lamb>=0 AND inv(c,i) AND phi(c+lamb,i) => phi(c,i)
using (Context ctx = new Context())
{
var s = ctx.MkFixedpoint();
IntSort B = ctx.IntSort;
BoolSort T = ctx.BoolSort;
RealSort R = ctx.RealSort;
FuncDecl phi = ctx.MkFuncDecl("phi", new Sort[] { R,B }, T);
s.RegisterRelation(phi);
FuncDecl Inv = ctx.MkFuncDecl("inv", new Sort[] { R, B }, T);
s.RegisterRelation(Inv);
RealExpr c= (RealExpr)ctx.MkBound(0, R);
IntExpr i = (IntExpr) ctx.MkBound(1, B);
Expr[] InvArg=new Expr[2];
InvArg[0] = ctx.MkConst("inv0" , Inv.Domain[0]);
InvArg[1] = ctx.MkConst("inv1", Inv.Domain[1]);
Expr invExpr = ctx.MkImplies(ctx.MkOr(
ctx.MkAnd(ctx.MkEq(InvArg[1], ctx.MkInt(0)), ctx.MkGe((RealExpr)InvArg[0], ctx.MkReal(0))),
ctx.MkAnd(ctx.MkEq(InvArg[1], ctx.MkInt(1)), ctx.MkGe((RealExpr)InvArg[0], ctx.MkReal(2)))
),
(BoolExpr)Inv[InvArg]);
Quantifier invQ = ctx.MkForall(InvArg, invExpr, 1);
s.AddRule(invQ);
RealExpr[] lamb = new RealExpr[1];
lamb[0] = ctx.MkRealConst("lamb");
Expr existExpr = ctx.MkAnd(
(BoolExpr)Inv[c,i],
(BoolExpr)phi[ctx.MkAdd(c,lamb[0]),i],
ctx.MkGe(lamb[0], ctx.MkReal(0)));
BoolExpr t= ctx.MkExists(lamb, existExpr, 1);
s.AddRule(ctx.MkImplies(t,(BoolExpr)phi[c,i]));
}
sometimes, there is an error said "AccessViolationException was unhandlered,Attempted to read or write protected memory. This is often an indication that other memory is corrupt." when running to ctx.MkExists()
The fixedpoint solver only supports universal quantifiers at the top-level.
You should rewrite the rule as follows:
s.AddRule(ctx.MkForall(lamb,
ctx.MkImplies((BoolExpr)existExpr,(BoolExpr)phi[c,i])));
Z3 should ideally not result in any access violation. This is typically indicating a bug.
I would really appreciate repros for such bugs when/if you encounter them.

Resources