Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Example of MAXSMT? #4

Open
varunpatro opened this issue Mar 13, 2018 · 3 comments
Open

Example of MAXSMT? #4

varunpatro opened this issue Mar 13, 2018 · 3 comments

Comments

@varunpatro
Copy link

Hello!

I saw here that Z3 supports having hard and soft constraints using assert-soft. However, I'm wondering if you know how I can use the python API to do that?

Cheers,
Varun

@0vercl0k
Copy link
Owner

0vercl0k commented Mar 17, 2018 via email

@varunpatro
Copy link
Author

Thanks for the work on this repo, by the way :-) it definitely made it easier to get started. Z3 has been for the most part, quite useful to me so far.

I've finally figured out how to use the hard/soft constraints. I'll try to write them up proper and send a PR when I get some time. Meanwhile, I just wanted to share a simple example, in case it helps other people out there:

from z3 import *
x, y = Ints('x y')
z = x + y

opt = Optimize()

# hard constraints
# e.g. `add(<constraint>)`
opt.add(z == 0)
if opt.check() == sat:
    print opt.model() # should print [y = 0, x = 0]
else:
    print 'unsat'

# soft constraints 
# e.g. `add_soft(<constraint>, <constraint_weight>)`
opt.add_soft(x > 5, 10)
opt.add_soft(y > 5, 15)

if opt.check() == sat:
    print opt.model() # should print [y = 6, x = -6] because y > 5 has a larger weight than x > 5
else:
    print 'unsat'

@0vercl0k
Copy link
Owner

0vercl0k commented Mar 20, 2018 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants