I am very new to z3py.
I am trying to code the following 2 expressions in z3py
and
More information on the problem can be found here
I did search stackoverflow a lot and came across a similar question
But unfortunately I could not get a satisfactory enough answer.
I tried coding the first one in SMT, in following way:
#InputGroup, BlockGroup, OutputGroup contain some integer values to represent blocks
InputGroup = [0,1,2]
BlockGroup = [2,3,4,5,6]
OutputGroup = [7,8,9]
Groups = [InputGroup, BlockGroup, OutputGroup]
NumberOfTasks = len(InputGroup)+ len(BlockGroup)+ len(OutputGroup)
M = Function('M', Intsort(), Intsort())
Task = Function('Task', Intsort(), Intsort(), Intsort())
summation1 = Int('summation1')
# each group from the Groups is represented by its index number
for r, g in enumerate(Groups):
for m in range(0, NumberOfTasks):
if(m in g):
s.add(summation1 == summation1+ M(Task(r,m)))
and the second expression in SMT the following way:
NumberOfInputs = len(InputGroup)
NumberOfBlocks = len(BlockGroup)
NumberOfOutputs = len(OutputGroup)
Node = Function('Node', Intsort(), Intsort(), Intsort())
f = Function('f', Intsort(), Intsort(), Intsort())
for r, g in enumerate(Groups):
if(r != Groups.index(InputGroup) and r != Groups.index(OutputGroup)):
for i in range(0,(NumberOfInputs+NumberOfBlocks+NumberOfOutputs)):
summation2 = Int('summation2')
for m in range(0, (NumberOfTasks)):
if(m in g and i in g):
s.add(summation2 == summation2+ f(Node(r,i), Task(r,m)))
s.add(summation2 == 1)
Although I get satisfactory result from the above equations, the model I get from it is kind of questionable.
I just want to know if I am representing this correctly or not.
I think you want to change:
# each group from the Groups is represented by its index number
for r, g in enumerate(Groups):
for m in range(0, NumberOfTasks):
if(m in g):
s.add(summation1 == summation1+ M(Task(r,m)))
to something like this:
# each group from the Groups is represented by its index number
sumTerms = [M(Task(r,m))
for r, g in enumerate(Groups):
for m in range(0, NumberOfTasks):
if(m in g)]
s.add(summation1 == Sum(sumTerms))
and similarly for the second example.
Related
I want to solve a CSP with the help of Lookahead method in z3 Solver. The issue I'm facing is not being convinced that it's invoked by setting an according parameter like s.set("sat.lookahead_simplify", True) or s.set("sat.lookahead.delta_fraction", 0.7).
Below is the code for solving my combinatorial problem. I ren a similar solver on a complex instance A and the runtime without these settings on Lookahead was 3674 seconds versus 3670 secunds with these settings. So, seems like they have no effect.
Does anyone know how to correctly invoke Lookahead here?
import numpy as np
import timeit
from z3 import *
def as_long(self):
if z3_debug():
_z3_assert(self.is_int(), "Integer value expected")
return int(self.as_string())
def model_int(mo, R, C, m, n):
R_int = [mo[R[i]].as_long() for i in range(m)]
C_int = [mo[C[j]].as_long() for j in range(n)]
return [R_int, C_int] # integer model values
def CSP(A, D, bit_length=8):
A_shape = A.shape
m = A_shape[0]
n = A_shape[1]
#initialize solver
s = Solver()
s.set("lookahead_simplify", True)
s.set("sat.lookahead.delta_fraction", 0.7)
#initialize column and row variables c_j and r_i as bit vectors
C = [BitVec(f"c_{j + 1}", bit_length) for j in range(n)]
R = [BitVec(f"r_{i + 1}", bit_length) for i in range(m)]
#initialize search space restriction constraints for r_i's as lists
Constr_D = [Or([r == d for d in D]) for r in R]
#initialize permit-constraints as lists
Constr_permit = [R[i] & C[j] == C[j] for i in range(m) for j in range(n) if A[i,j]]
Constr_npermit = [R[i] & C[j] != C[j] for i in range(m) for j in range(n) if not A[i,j]]
#add constraints
s.add(Constr_D + Constr_permit + Constr_npermit)
#search solution
check = s.check()
if check == sat:
return model_int(s.model(), R, C, m, n)
return check
# model instance ........
D = [7,11,19,35,67,13,21,37,69,25,41,73,49,81,82,84,88] #domain
m = 15 #number of rows
n = 9 #number of columns
p=.11
A = np.random.choice(a=[1,0],size=(m,n),p=[p,1-p])
print(A)
# executing solver on model instance ........
starttime = timeit.default_timer()
print(CSP(A,D))
print("The time difference is :", timeit.default_timer() - starttime)
I have a problem where I want to limit the range of a real variable between the maximum and minimum value of another set of real variables.
s = Solver()
y = Real('y')
Z = RealVector('z', 10)
s.add(And(y >= min(Z), y <= max(Z)))
Is there a way to do this in z3py?
You can use Axel's solution; though that one requires you to create an extra variable and also asserts more constraints than needed. Moreover, it doesn't let you use min and max as simple functions. It might be easier to just program this in a functional way, like this:
# Return minimum of a vector; error if empty
def min(vs):
m = vs[0]
for v in vs[1:]:
m = If(v < m, v, m)
return m
# Return maximum of a vector; error if empty
def max(vs):
m = vs[0]
for v in vs[1:]:
m = If(v > m, v, m)
return m
Another difference is that in the functional style we throw an error if the vector is empty. In the other style, the result will essentially be unconstrained. (i.e., min/max can take any value.) You should consider which semantics is right for your application, in case the vector you're passing might be empty. (At the least, you should change it so it prints out a nicer error message. Currently it'll throw an IndexError: list index out of range error if given an empty vector.)
Now you can say:
s = Solver()
y = Real('y')
Z = RealVector('z', 10)
s.add(And(y >= min(Z), y <= max(Z)))
print (s.check())
print (s.model())
This prints:
sat
[z__7 = -1,
z__0 = -7/2,
z__4 = -5/2,
z__5 = -2,
z__3 = -9/2,
z__2 = -4,
z__8 = -1/2,
y = 0,
z__9 = 0,
z__6 = -3/2,
z__1 = -3]
You could benefit from Hakan Kjellerstrand's collection of useful z3py definitions:
from z3 import *
# Functions written by Hakan Kjellerstrand
# http://hakank.org/z3/
# The following can be used by importing http://www.hakank.org/z3/z3_utils_hakank.py
# v is the maximum value of x
def maximum(sol, v, x):
sol.add(Or([v == x[i] for i in range(len(x))])) # v is an element in x)
for i in range(len(x)):
sol.add(v >= x[i]) # and it's the greatest
# v is the minimum value of x
def minimum(sol, v, x):
sol.add(Or([v == x[i] for i in range(len(x))])) # v is an element in x)
for i in range(len(x)):
sol.add(v <= x[i]) # and it's the smallest
s = Solver()
y = Real('y')
zMin = Real('zMin')
zMax = Real('zMax')
Z = RealVector('z', 10)
maximum(s, zMin, Z)
minimum(s, zMax, Z)
s.add(And(y >= zMin, y <= zMax))
print(s.check())
print(s.model())
I have a z3 Array:
x = Array('x', IntSort(), IntSort())
A fixed number n:
n = 10
And a filtering condition based on simple arithmetic:
lambda i: Or(i == 0, i > 2)
What I want is to know the total number of elements from index 0 to index n which satisfy this condition (something that, if this were a normal python list, would look like len(filter(lambda i: i == 0 or i > 2, x)).
I can't figure out how to do this in z3. I tried
y == Int('y')
solver.add(y == sum([1 if Or(x[i] == 0, x[i] > 2) else 0 for i in range(0,n)]))
but get the following error:
z3.z3types.Z3Exception: Symbolic expressions cannot be cast to concrete Boolean values.
Is there a way to proceed? Many thanks!
It'd be best to post the exact code you tried. For one thing, the if-then-else should be coded using the If construct and the sum is best expressed using Sum. The following works fine, for instance:
from z3 import *
x = Array ('x', IntSort(), IntSort())
solver = Solver()
n = 10
y = Int('y')
solver.add(y == Sum([If(Or(x[i] == 0, x[i] > 2), 1, 0) for i in range(0,n)]))
print solver.check()
print solver.model()
But this'll be satisfiable for all values of y since there is no constraint on the contents of the array x.
I want to find the weakest precondition given an action and post condition using z3py.
Given the action N = N + 1 and the post condition N == 5 the weakest precondition would be N == 4.
Using the Tactic solve-eqs this approach works for some post conditions but not others.
When using the post condition N < 5 I get [[Not(4 <= N)]].
But when using N == 5 I get [[]], when I would like N == 4.
N2 = Int('N2') # N after the action
N = Int('N') # N before the action
weakestPreconditionGoal = Goal()
# 'N2 == n + 1' action
# 'N2 == 5' post condition.
weakestPreconditionGoal.add(N2 == N + 1, N2 == 5)
t = Tactic('solve-eqs')
wp = t(weakestPreconditionGoal)
print(wp)
Is this the best approach find the weakest precondition?
I have tried a several methods but am new to Z3 and can not figure out what approach to take or how to implement it.
Yes, solve-eqs can be used to eliminate equalities. The problem is that we have no control over which equalities will be eliminated. Another option is to use qe (quantifier elimination). The example is also available here.
N2 = Int('N2') # N after the action
N = Int('N') # N before the action
weakestPreconditionGoal = Goal()
# 'N2 == n + 1' action
# 'N2 == 5' post condition.
weakestPreconditionGoal.add(Exists([N2], And(N2 == N + 1, N2 == 5)))
t = Tactic('qe')
wp = t(weakestPreconditionGoal)
print(wp)
Another option is to use solve-eqs, but "guard" equations that we don't want to eliminate.
We can guard the equations by using an auxiliary predicate guard. Here is an example (also available online here). Of course, we will have to perform a second pass to eliminate guard from the result.
N2 = Int('N2') # N after the action
N = Int('N') # N before the action
guard = Function('guard', BoolSort(), BoolSort())
weakestPreconditionGoal = Goal()
# 'N2 == n + 1' action
# 'N2 == 5' post condition.
weakestPreconditionGoal.add(N2 == N + 1, guard(N2 == 5))
t = Tactic('solve-eqs')
wp = t(weakestPreconditionGoal)
print(wp)
I am trying to understand how the bound variables are indexed in z3.
Here in a snippet in z3py and the corresponding output. ( http://rise4fun.com/Z3Py/plVw1 )
x, y = Ints('x y')
f1 = ForAll(x, And(x == 0, Exists(y, x == y)))
f2 = ForAll(x, Exists(y, And(x == 0, x == y)))
print f1.body()
print f2.body()
Output:
ν0 = 0 ∧ (∃y : ν1 = y)
y : ν1 = 0 ∧ ν1 = y
In f1, why is the same bound variable x has different index.(0 and 1). If I modify the f1 and bring out the Exists, then x has the same index(0).
Reason I want to understand the indexing mechanism:
I have a FOL formula represented in a DSL in scala that I want to send to z3. Now ScalaZ3 has a mkBound api for creating bound variables that takes index and sort as arguments. I am not sure what value should I pass to the index argument. So, I would like to know the following:
If I have two formulas phi1 and phi2 with maximum bound variable indexes n1 and n2, what would be the index of x in ForAll(x, And(phi1, phi2))
Also, is there a way to show all the variables in an indexed form? f1.body() just shows me x in indexed form and not y. (I think the reason is that y is still bound in f1.body())
Z3 encodes bound variables using de Bruijn indices.
The following wikipedia article describes de Bruijn indices in detail:
http://en.wikipedia.org/wiki/De_Bruijn_index
Remark: in the article above the indices start at 1, in Z3, they start at 0.
Regarding your second question, you can change the Z3 pretty printer.
The Z3 distribution contains the source code of the Python API. The pretty printer is implemented in the file python\z3printer.py.
You just need to replace the method:
def pp_var(self, a, d, xs):
idx = z3.get_var_index(a)
sz = len(xs)
if idx >= sz:
return seq1('Var', (to_format(idx),))
else:
return to_format(xs[sz - idx - 1])
with
def pp_var(self, a, d, xs):
idx = z3.get_var_index(a)
return seq1('Var', (to_format(idx),))
If you want to redefine the HTML pretty printer, you should also replace.
def pp_var(self, a, d, xs):
idx = z3.get_var_index(a)
sz = len(xs)
if idx >= sz:
# 957 is the greek letter nu
return to_format('ν<sub>%s</sub>' % idx, 1)
else:
return to_format(xs[sz - idx - 1])
with
def pp_var(self, a, d, xs):
idx = z3.get_var_index(a)
return to_format('ν<sub>%s</sub>' % idx, 1)