Optimize context, allowing you to find solutions that maximize or minimize objectives while satisfying constraints. This is particularly useful for MaxSMT problems, resource allocation, scheduling, and multi-objective optimization.
Overview
The Z3 optimization API extends standard satisfiability checking with the ability to:- Minimize or maximize arithmetic expressions
- Handle soft constraints with weights (MaxSMT)
- Solve multi-objective optimization problems
- Extract optimal models with bounds on objectives
Basic Usage
Creating an Optimize Context
Minimization Example
Soft Constraints and MaxSMT
Soft constraints can be violated, but each violation incurs a penalty. The optimizer tries to minimize the total penalty.Adding Soft Constraints
Weighted Constraint Groups
You can group soft constraints using symbols to organize related constraints:Multi-Objective Optimization
Z3 supports multiple objectives with lexicographic ordering (objectives are prioritized in order):Advanced Features
Push/Pop with Optimization
Like solvers, optimization contexts support backtracking:Setting Initial Values
Provide hints to guide the search (available in Z3 4.13.1+):Retrieving Bounds
For optimization objectives, you can retrieve lower and upper bounds:Practical Example: Resource Allocation
C API Reference
The optimization API is available in C throughz3_optimization.h:
Key Functions
Configuration Parameters
Common parameters for optimization:See Also
- API Reference - Complete Optimize API documentation
- Solver - Standard constraint solving
- Tactics - Simplification and preprocessing strategies
