A Constraint Solver created by Microsoft.
Installation
pip install z3 z3-solver
Solver Process
- Declare variables
- Create solver object
- Add constraints to solver
- Solve the solver
Boilerplate
from z3 import *
# Create a solver instance
solver = Solver()
# Define the variables
x = Int('x')
y = Int('y')
# Add constraints
solver.add(x + y == 10)
solver.add(x > 0)
solver.add(y > 0)
# Check if the constraints are satisfiable
if solver.check() == sat:
# Get the model (solution)
model = solver.model()
print(f'Solution: x = {model[x]}, y = {model[y]}')
else:
print('No solution found')