Optimize solver goes beyond finding any solution - it finds optimal solutions by minimizing or maximizing objective functions while satisfying constraints.
Optimize vs Solver
The standardSolver finds any satisfying assignment:
Optimize solver finds the best solution according to objectives:
Basic Optimization
1
Import and create optimizer
Optimize() creates an optimizer that supports objective functions.2
Create variables and add constraints
3
Add objective function
minimize() or maximize() to specify what to optimize. You can even have multiple objectives.4
Solve and extract result
Complete Example: Linear Programming
Multi-Objective Optimization
You can optimize multiple objectives with priorities:Objectives are handled in order. Later objectives are optimized subject to earlier ones being optimal.
MaxSMT: Soft Constraints
MaxSMT finds solutions that satisfy all hard constraints while maximizing satisfied soft constraints.1
Add hard constraints
Hard constraints must be satisfied:
2
Add soft constraints
Soft constraints are preferences, not requirements:Higher weights mean stronger preferences. Z3 maximizes the total weight of satisfied soft constraints.
3
Solve
Complete MaxSMT Example
Real-World Example: Resource Allocation
Allocate limited resources across projects to maximize total value:Real Numbers vs Integers
Optimization works with both integer and real variables:Checking Objectives
You can query the objective value:Common Patterns
Minimize cost
Minimize cost
Maximize profit
Maximize profit
Lexicographic optimization
Lexicographic optimization
Soft constraints with priorities
Soft constraints with priorities
Performance Considerations
Next Steps
Basic Solving
Review fundamental constraint solving
Program Verification
Use Z3 to verify program correctness
