Unprintable Solver.model() - z3

The following program generates a Z3 model that cannot be printed (that is, print solver.model() throws an exception), using the latest version of Z3 from the master git branch (commit 89c1785b):
x = Int('x')
a = Array('a', IntSort(), BoolSort())
b = Array('b', IntSort(), BoolSort())
c = Array('c', BoolSort(), BoolSort())
e = ForAll(x, Or(Not(a[x]), c[b[x]]))
print e
solver = Solver()
solver.add(e)
c = solver.check()
print c
if c == sat:
print solver.model()
produces:
ForAll(x, Or(Not(a[x]), c[b[x]]))
sat
Traceback (most recent call last):
File "z3bug.py", line 16, in <module>
print solver.model()
File "src/api/python/z3.py", line 5177, in __repr__
File "src/api/python/z3printer.py", line 939, in obj_to_string
File "src/api/python/z3printer.py", line 841, in __call__
File "src/api/python/z3printer.py", line 831, in main
File "src/api/python/z3printer.py", line 760, in pp_model
File "src/api/python/z3printer.py", line 794, in pp_func_interp
File "src/api/python/z3.py", line 5088, in else_value
File "src/api/python/z3.py", line 818, in _to_expr_ref
File "src/api/python/z3core.py", line 2307, in Z3_get_ast_kind
z3types.Z3Exception: 'invalid argument'
I can also reproduce the same behavior in the online z3py interface, at http://rise4fun.com/Z3Py/lfQG. Slightly more debugging suggests that the model's assignment for c is a z3.FuncInterp that throws an 'invalid argument' exception when you call else_value() on it.
Is this a bug in Z3, or are my expectations not quite right? My expectation was that it should always be possible to get the else_value() of a FuncInterp, since otherwise it's not a complete function, but perhaps this is not always correct?

This is a bug in the Z3 Python printer. I fixed the bug, and the fix is already available at codeplex.
http://z3.codeplex.com/SourceControl/changeset/f8014f54c18a
To get the fix (now), we have to retrieve the "work-in-progress" (unstable) branch. The fix will be available in the master branch in the next official release. To retrieve the unstable branch, we should use:
git clone https://git01.codeplex.com/z3 -b unstable
Another option is to use print solver.model().sexpr(). It will use Z3 internal printer instead of the Python based one.
Regarding else_value(), its value may not be specified by Z3. The meaning it: it is a "don't care". That is, any interpretation can be used to satisfy the formula.
I also fixed the Z3 Python API to return None when the else_value is not specified.

Related

In Sympy be, why does get_field fail for the finite field GF(9) (defined as a finite extension of GF(3))?

I'm trying to use the groebner function in Sympy to solve a system of multivariate polynomials in GF(9) = GF(3)[g]/(g**2 + 1). When I do this I get the error
File
"/Applications/Spyder.app/Contents/Resources/lib/python3.9/sympy/polys/groebnertools.py",
line 37, in groebner
orig, ring = ring, ring.clone(domain=domain.get_field())
File
"/Applications/Spyder.app/Contents/Resources/lib/python3.9/sympy/polys/domains/domain.py",
line 846, in get_field
raise DomainError('there is no field associated with %s' % self)
DomainError: there is no field associated with GF(3)[g]/(g**2 + 1)
I reduced the code to the minimum that gives this get_field() failure. I also looked at is_field ... which was True!?
I know that GF and similar mechanisms fail for non-prime finite fields, but I thought that all would be well, if I defined my own GF(9) using a finite extension.
Am I missing something, or does get_field() not realize that gf9 is a field?
My minimal code
from sympy import Symbol, Poly
from sympy.polys.agca.extensions import FiniteExtension
g = Symbol('g')
gf9 = FiniteExtension(Poly(g**2 + 1, g, modulus=3)) # correct GF(9)
print('gf9 = ', gf9, 'type =', type(gf9), 'is_field?', gf9.is_Field)
print()
print('field associated with', gf9, '=', gf9.get_field())
Response
gf9 = GF(3)[g]/(g**2 + 1) is_field? True
Traceback (most recent call last):
File
"/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder_kernels/py3compat.py",
line 356, in compat_exec
exec(code, globals, locals)
File "/Users/rick/.spyder-py3/temp.py", line 17, in
print('field associated with', gf9, '=', gf9.get_field())
File
"/Applications/Spyder.app/Contents/Resources/lib/python3.9/sympy/polys/domains/domain.py",
line 846, in get_field
raise DomainError('there is no field associated with %s' % self)
DomainError: there is no field associated with GF(3)[g]/(g**2 + 1)
Any help appreciated :)
Addendum: I just noticed that I never asked my real question: How can I use Sympy's groebner function with GF(3)[g]/(g**2 + 1)?

Why does the result of my sage command becomes question mark?

I'm trying to use sagetex package, but then I found this kind of problem when running the code
the result
Here is the code I tried to run:
\documentclass{article}
\usepackage{sagetex}
\usepackage{graphicx}
\usepackage{fancyvrb}
\begin{document}
Using Sage\TeX, one can use Sage to compute things and put them into
your \LaTeX{} document. For example, there are
$\sage{number_of_partitions(1269)}$ integer partitions of $1269$.
You don't need to compute the number yourself, or even cut and paste
it from somewhere.
Here's some Sage code:
\begin{sageblock}
f(x) = exp(x) * sin(2*x)
\end{sageblock}
The second derivative of $f$ is
\[
\frac{\mathrm{d}^{2}}{\mathrm{d}x^{2}} \sage{f(x)} =
\sage{diff(f, x, 2)(x)}.
\]
Here's a plot of $f$ from $-1$ to $1$:
\sageplot{plot(f, -1, 1)}
\sageplot[scale=.5]{plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1))}
we know that 2010 factors to $\sage{factor(2010)}$
\begin{sagesilent}
m=identity_matrix(QQ,3)
m[0]=m[0]+m[1]
m[1]=m[1]-m[2]
m[2]=m[2]-2*m[1]
m[1]=m[1]+3*m[0]
m[0]=2*m[0]
\end{sagesilent}
Compute the rref of $\sage{m}$
\begin{sageblock}
g(x)=taylor(tan(x),x,0,10)
\end{sageblock}
$$\tan(x)=\sage{g(x)}$$
\end{document}
When I try to compile this, I get:
**** Error in Sage code on line 23 of file.tex! Traceback follows.
Traceback (most recent call last):
File "file.sagetex.sage.py", line 39, in <module>
_st_.plot(_sage_const_1 , format='notprovided', _p_=plot3d(sin(pi*(x**_sage_const_2 +y**_sage_const_2 ))/_sage_const_2 ,(x,-_sage_const_1 ,_sage_const_1 ),(y,-_sage_const_1 ,_sage_const_1 )))
NameError: name 'y' is not defined
Once a single error occurs, the rest of the Sage output may be lost, leading to all of the question marks. The problem is this line:
\sageplot[scale=.5]{plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1))}
and in particular, you have not defined y. (In SageMath, x is automatically defined to be a variable, but not y.) If you add this before the plot, it should work:
\begin{sagesilent}
var('y')
\end{sagesilent}

Save and reload z3py solver constraints

Can I save the constraints I created for a z3 solver and later reload them to continue looking for more solutions?
I have learned there is the SMT-LIB2 format for such things and that z3 and z3py have an API for saving and loading in that format. Unfortunately I cannot make it work.
Here's my example program which pointlessly saves and reloads:
import z3
filename = 'z3test.smt'
# Make a solver with some arbitrary useless constraint
solver = z3.Solver()
solver.add(True)
# Save to file
smt2 = solver.sexpr()
with open(filename, mode='w', encoding='ascii') as f: # overwrite
f.write(smt2)
f.close()
# Load from file
solver.reset()
solver.from_file(filename)
It fails with:
Exception has occurred: ctypes.ArgumentError
argument 3: <class 'TypeError'>: wrong type
File "C:\Users\Marian Aldenhövel\Desktop\FridgeIQ\z3\z3-4.8.4.d6df51951f4c-x64-win\bin\python\z3\z3core.py", line 3449, in Z3_solver_from_file
_elems.f(a0, a1, _to_ascii(a2))
File "C:\Users\Marian Aldenhövel\Desktop\FridgeIQ\z3\z3-4.8.4.d6df51951f4c-x64-win\bin\python\z3\z3.py", line 6670, in from_file
_handle_parse_error(e, self.ctx)
File "C:\Users\Marian Aldenhövel\Desktop\FridgeIQ\src\z3test.py", line 17, in <module>
solver.from_file(filename)
Is this a problem with my understanding or my code? Can it be done like this? Are sexpr() and from_file() the right pair of API calls?
I am using z3 and z3py 4.8.4 from https://github.com/z3prover/z3/releases on Windows 10 64bit.
More detail if required:
I am playing with z3 in Python to find solutions for a large disection-puzzle.
To find all solutions I am calling solver.check(). When it returns a sat verdict I interpret the model as image of my puzzle solution. I then add a blocking clause ruling out that specific solution and call solver.check() again.
This works fine and I am delighted.
The runtime to find all solutions will be on the order of many days or until I get bored. I am concerned that my machine will not be running continuously for that long. It may crash, run out of power, or be rebooted for other reasons.
I can easily recreate the initial constraints which is the whole point of the program. But the blocking clauses are a runtime product and a function of how far we have gotten.
I thought I could save the state of the solver and if at runtime I find such a file restart by loading that with the blocking clauses intact and go on finding more solutions instead of having to start over.
Thank you for taking your time reading and thinking.
Marian
With z3 4.4.1 and z3 4.8.5, I would dump (and reload) the constraints in smt2 format as follows:
import z3
filename = "z3test.smt2"
x1 = z3.Real("x1")
x2 = z3.Real("x2")
solver = z3.Solver()
solver.add(x1 != x2)
#
# STORE
#
with open(filename, mode='w') as f:
f.write(solver.to_smt2())
#
# RELOAD
#
solver.reset()
constraints = z3.parse_smt2_file(filename, sorts={}, decls={})
solver.add(constraints)
print(solver)
output:
~$ python t.py
[And(x1 != x2, True)]
file z3test.smt2:
(set-info :status unknown)
(declare-fun x2 () Real)
(declare-fun x1 () Real)
(assert
(and (distinct x1 x2) true))
(check-sat)
I have no idea whether the API changed in the version you are using. Feedback is welcome.

How to use marginal, probability method in pycrfsuite.Tagger()

Documentation is not helpful to me at all.
First, I tried using set() ,but I don't understand what it means by
set an instance for future calls
I could successfully feed my data using my dataset's structure described below.
So, I am not sure why I need to use set for that as it mentioned.
Here is my feature sequence of type scipy.sparse after I called nonzero() method.
[['66=1', '240=1', '286=1', '347=10', '348=1'],...]
where ... imply, same structure as previous elements
Second problem I encountered is Tagger.probability() and Tagger.marginal().
For Tagger.probability, I used the same input as Tagget.tag(), and I get this follwoing error.
and if my input is just a list instead of list of list. I get the following error.
Traceback (most recent call last):
File "cliner", line 60, in <module>
main()
File "cliner", line 49, in main
train.main()
File "C:\Users\Anak\PycharmProjects\CliNER\code\train.py", line 157, in main
train(training_list, args.model, args.format, args.use_lstm, logfile=args.log, val=val_list, test=test_list)
File "C:\Users\Anak\PycharmProjects\CliNER\code\train.py", line 189, in train
model.train(train_docs, val=val_docs, test=test_docs)
File "C:\Users\Anak\PycharmProjects\CliNER\code\model.py", line 200, in train
test_sents=test_sents, test_labels=test_labels)
File "C:\Users\Anak\PycharmProjects\CliNER\code\model.py", line 231, in train_fit
dev_split=dev_split )
File "C:\Users\Anak\PycharmProjects\CliNER\code\model.py", line 653, in generic_train
test_X=test_X, test_Y=test_Y)
File "C:\Users\Anak\PycharmProjects\CliNER\code\machine_learning\crf.py", line 220, in train
train_pred = predict(model, X) # ANAK
File "C:\Users\Anak\PycharmProjects\CliNER\code\machine_learning\crf.py", line 291, in predict
print(tagger.probability(xseq[0]))
File "pycrfsuite/_pycrfsuite.pyx", line 650, in pycrfsuite._pycrfsuite.Tagger.probability
ValueError: The numbers of items and labels differ: |x| = 12, |y| = 73
For Tagger.marginal(), I can only produce error similar to first error shown of Tagger.probabilit().
Any clue on how to use these 3 methods?? Please give me shorts example of use cases of these 3 methods.
I feel like there must be some example of these 3 methods, but I couldn't find one. Am I looking at the right place. This is the website I am reading documentation from
Additional info: I am using CliNER. in case any of you are familiar with it.
https://python-crfsuite.readthedocs.io/en/latest/pycrfsuite.html
I know this questions is over a year old, but I just had to figure out the same thing as well -- I am also leveraging some of the CliNER framework. For the CliNER specific solution, I forked the repo and rewrote the predict method in the ./code/machine_learning/crf.py file
To obtain the marginal probability, you need to add the following line to the for loop that iterates over the pycrf_instances after yseq is created (see line 196 here)
y_probs = [tagger.marginal(y, ii) for ii, y in enumerate(yseq)]
And then you can return that list of marginal probabilities from the predict method -- you will in turn be required to rewrite additional functions in the to accommodate this change.

timeout for z3 solver in python

I have problems setting a timeout for my solver:
s = Solver()
encoding = parse_smt2_file("ex.smt2")
s.add(encoding)
s.set("timeout", 600)
solution = s.check()
but I get the following error
Traceback (most recent call last):
File "/Users/X/Documents/encode.py", line 145, in parse_polyedra("file")
File "/Users/X/Documents/encode.py", line 108, in parse_polyedra s.set("timeout",1) File "/Users/X/z3/build/z3.py", line 5765, in set Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
File "/Users/X/z3/build/z3core.py", line 3969, in Z3_solver_set_params raise Z3Exception(lib().Z3_get_error_msg_ex(a0, err))
Z3Exception: unknown parameter 'timeout'
A list of legal parameters appears, but timeout is not part of it. I took a look to this post, but the problem is not the same. As far as I understand, the parameter should be accepted, it is just that in the stable version the timeout may never happens, but there should not be problems to compile.
Anyway I installed the unstable branch and the problem persist.
I was able to use the timeout option (and it actually timeouts, and returns the best known solution in the case of optimization) with the following versions:
Python 2.7.9 (default, Mar 1 2015, 12:57:24)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import z3
>>> s = z3.Solver()
>>> s.set("timeout", 600)
>>> z3.get_version_string()
'4.4.2'

Resources