Skip to main content

Overview

The Optimization API provides functions for solving optimization problems - finding models that maximize or minimize objective functions while satisfying constraints.

Types

Z3_optimize

Context for solving optimization queries. Supports hard constraints, soft constraints, and multiple objectives.

Optimizer Creation

Z3_mk_optimize

Create an optimization context.
Z3_optimize
New optimizer instance
Example:

Z3_optimize_inc_ref

Increment the reference counter of the optimizer.

Z3_optimize_dec_ref

Decrement the reference counter of the optimizer.

Hard Constraints

Z3_optimize_assert

Assert a hard constraint (must be satisfied). Description: Hard constraints must be satisfied by any solution. If hard constraints are unsatisfiable, the optimizer will return UNSAT. Example:

Z3_optimize_assert_and_track

Assert a hard constraint with a tracking literal.

Soft Constraints

Z3_optimize_assert_soft

Assert a soft constraint with weight (for MaxSMT).
unsigned
Index of the soft constraint
Description: Soft constraints are preferably satisfied but may be violated. The optimizer minimizes the sum of weights of violated soft constraints. Example:

Objectives

Z3_optimize_maximize

Add a maximization objective.
unsigned
Objective handle
Description: Adds an objective to maximize the value of the expression. Example:

Z3_optimize_minimize

Add a minimization objective.
unsigned
Objective handle
Description: Adds an objective to minimize the value of the expression. Example:

Checking and Results

Z3_optimize_check

Solve the optimization problem.
Z3_lbool
Z3_L_TRUE if satisfiable, Z3_L_FALSE if unsatisfiable, Z3_L_UNDEF if unknown
Example:

Z3_optimize_get_model

Retrieve the model from the last check.
Z3_model
Model (must increment reference)

Z3_optimize_get_upper

Retrieve the upper bound for an objective.
Z3_ast
Upper bound expression

Z3_optimize_get_lower

Retrieve the lower bound for an objective.
Z3_ast
Lower bound expression

Backtracking

Z3_optimize_push

Create a backtracking point.

Z3_optimize_pop

Backtrack one level. Example:

Information Retrieval

Z3_optimize_get_reason_unknown

Return a string describing why the last check returned unknown.
Z3_string
Reason string

Z3_optimize_to_string

Convert optimizer state to a string.
Z3_string
String representation

Z3_optimize_get_assertions

Return the set of asserted formulas.
Z3_ast_vector
Vector of assertions

Z3_optimize_get_objectives

Return the objectives.
Z3_ast_vector
Vector of objective expressions

Z3_optimize_get_unsat_core

Retrieve the unsat core.
Z3_ast_vector
Unsat core (subset of assertions)

Complete Example