Weighted MaxSAT Backend
The ocean.maxsat module exposes the weighted MaxSAT formulation backed
by PySAT. The main public entry point is ocean.maxsat.Explainer; the
remaining classes document the underlying Boolean model, variable bundles, and
manager helpers used to build that formulation. For random forests, the public
explainer also supports hard_voting=True to encode majority voting at the
tree level instead of comparing averaged class proportions.
Weighted MaxSAT backend for optimal counterfactual search.
- class BaseModel[source]
Bases:
ABC,WCNFBase weighted CNF model used by the MaxSAT backend.
- add_hard(lits, return_id=False)[source]
Add a hard clause (must be satisfied).
- Returns:
The clause ID if return_id is True, otherwise -1.
- property solver_epoch
- vpool
- class Explainer(ensemble, *, mapper, weights=None, hard_voting=False, epsilon=1, model_type=Type.MAXSAT)[source]
Bases:
Model,BaseExplainerMaxSAT-based explainer for tree ensemble classifiers.
Initialize an empty weighted MaxSAT model for a parsed ensemble.
- Parameters:
trees – Parsed trees whose leaf activations define the ensemble class scores.
mapper – Feature mapper aligning processed query coordinates with the Boolean feature encoding.
weights – Optional tree weights.
hard_voting – Whether to build the hard-voting MaxSAT encoding.
max_samples – Reserved parameter used by compatible higher-level explainers.
epsilon – Integer tie-breaking margin used in the score constraints.
model_type – Backend builder variant.
- Status = 'UNKNOWN'
- explain(x, *, y, norm, return_callback=False, verbose=False, max_time=60, num_workers=None, random_seed=42, clean_up=True)[source]
Solve one counterfactual query with the weighted MaxSAT backend.
- Parameters:
x – Query instance in the processed feature space.
y – Target class enforced by the counterfactual.
norm – Distance norm. The MaxSAT backend currently supports only
1.return_callback – Accepted for API compatibility but ignored by this backend.
verbose – Whether to enable RC2 logging.
max_time – Time limit in seconds.
num_workers – Optional thread count forwarded to the MaxSAT solver.
random_seed – Accepted for API compatibility but currently ignored.
clean_up – Whether to remove query-specific clauses after the solve.
- Returns:
The decoded counterfactual, or
Nonewhen no feasible counterfactual is found within the given limits.- Return type:
Explanation | None
- Raises:
RuntimeError – If the MaxSAT solver raises an error that is not UNSAT or timeout.
- get_anytime_solutions()[source]
Return the intermediate solution trace for the last MaxSAT solve.
- Returns:
The MaxSAT backend currently exposes only the final solution.
- Return type:
None
- get_distance()[source]
Return the post-processed distance of the last CF.
- Returns:
Post-processed \(L_p\) distance for the last successful solve.
- Return type:
float
- Raises:
RuntimeError – If no explanation has been computed yet.
- class Explanation(mapping=None, *, columns=None, validate=True)[source]
Bases:
Mapper[FeatureVar],BaseExplanationConcrete explanation container returned by the MaxSAT backend.
Initialize a mapper from feature metadata and transformed columns.
- Parameters:
mapping – Mapping from original feature names to metadata objects.
columns – Processed pandas index describing the transformed coordinates.
validate – Whether to verify that
mappingandcolumnsare consistent.
- property query
- property value
- property x
- class FeatureManager(mapper)[source]
Bases:
objectManage MaxSAT feature variables for the counterfactual point \(x\).
This manager owns the Boolean or interval-selector variables representing the processed coordinates of the counterfactual explanation. The query \(\hat{x}\) only appears later in the soft objective clauses.
Wrap parsed features in MaxSAT-specific backend variables.
- FEATURE_VAR_FMT = 'feature[{key}]'
- property explanation
- property mapper
- property n_columns
- property n_features
- class FeatureVar(feature, name)[source]
Bases:
Var,FeatureKeeperMaxSAT variable bundle associated with a single parsed feature.
- TH_VAR_NAME_FMT = '{name}[{idx}]'
- X_VAR_NAME_FMT = 'x[{name}]'
- property has_threshold_encoding
- property split_threshold_values
- class MaxSATSolver(solver_name='cadical195', TimeLimit=60, n_threads=1)[source]
Bases:
objectThin RC2 wrapper to keep a stable interface.
- property cost
- model(v)[source]
Return 1.0 if variable v is true in the model, 0.0 otherwise.
- Args:
v: Variable to check in the model.
- Returns:
1.0 if variable v is true in the model, 0.0 otherwise.
- Raises:
ValueError: If no model has been found solve() must be called first.
- property state_token
Opaque token identifying the currently cached RC2 instance.
- property state_version
Monotonic version incremented whenever RC2 is rebuilt.
- property synced_counts
Number of hard and soft clauses synchronized into RC2.
- class Model(trees, mapper, *, weights=None, hard_voting=False, max_samples=0, epsilon=1, model_type=Type.MAXSAT)[source]
Bases:
BaseModel,FeatureManager,GarbageManager,TreeManagerWeighted MaxSAT formulation for tree ensemble explanations.
The Boolean feature variables encode the counterfactual point
xand the Boolean tree variables encode the active leaf decisionsp_(t,l).Initialize an empty weighted MaxSAT model for a parsed ensemble.
- Parameters:
trees – Parsed trees whose leaf activations define the ensemble class scores.
mapper – Feature mapper aligning processed query coordinates with the Boolean feature encoding.
weights – Optional tree weights.
hard_voting – Whether to build the hard-voting MaxSAT encoding.
max_samples – Reserved parameter used by compatible higher-level explainers.
epsilon – Integer tie-breaking margin used in the score constraints.
model_type – Backend builder variant.
- DEFAULT_EPSILON = 1
- add_objective(x, *, norm=1)[source]
Encode the \(L_1\) distance \(d_1(x, \hat{x})\) as soft clauses.
- Parameters:
x – Query point \(\hat{x}\) in the processed feature space. The code parameter is named
x, but mathematically it represents the query.norm – Distance norm. The MaxSAT backend currently supports only \(L_1\).
- Raises:
ValueError – If
xdoes not have the expected size ornormis unsupported.
- build()[source]
Create the Boolean encoding of features, leaves, and path consistency.
After
build(), the model contains the Boolean variables encoding the counterfactual point \(x\), the active leaves \(p_{t,\ell}\), and the hard clauses linking both.
- class TreeManager(trees, *, weights=None)
Bases:
objectManage MaxSAT tree variables and the support encoding of \(f\).
Each
TreeVarencodes the active leaf decisions \(p_{t,\ell}\) of one tree. The stored function representation is a Boolean support view of \(f(x)\) used by the MaxSAT encoding.Initialize the tree manager and validate the estimator weights.
- TREE_VAR_FMT = 'tree[{t}]'
- build_trees(model)
Create the MaxSAT tree variables and cache \(f(x)\) support.
- property estimators
- property function
- property n_classes
- property n_estimators
- property n_trees
- property shape
- property trees
- weighted_function()
Return the Boolean support representation used for \(f(x)\).
Each entry
(op, c)stores the leaf literals whose active leaf supports class \(c\) for outputop.- Returns:
Boolean support literals grouped by output and class.
- Return type:
dict[tuple[NonNegativeInt, NonNegativeInt], list[int]]
- property weights