C++ Reference: class PresolveContext

This documentation is automatically generated.

Wrap the CpModelProto we are presolving with extra data structure like the in-memory domain of each variables and the constraint variable graph.
Method
AddImplication

Return type: void

Arguments: int a, int b

a => b.

AddImplyInDomain

Return type: void

Arguments: int b, int x, const Domain& domain

b => x in [lb, ub].

AffineRelationDebugString

Return type: std::string

Arguments: int ref

CanBeUsedAsLiteral

Return type: bool

Arguments: int ref

CanonicalizeDomainOfSizeTwo

Return type: void

Arguments: int var

If not already done, adds a Boolean to represent any integer variables that take only two values. Make sure all the relevant affine and encoding relations are updated. Note that this might create a new Boolean variable.

CanonicalizeObjective

Return type: ABSL_MUST_USE_RESULT bool

ClearPrecedenceCache

Return type: void

Clear the precedence cache.

ClearStats

Return type: void

Clears the "rules" statistics.

ConstraintToVars

Return type: const std::vector<int>&

Arguments: int c

Variable <-> constraint graph. The vector list is sorted and contains unique elements. Important: To properly handle the objective, var_to_constraints[objective] contains -1 so that if the objective appear in only one constraint, the constraint cannot be simplified.

ConstraintVariableGraphIsUpToDate

Return type: bool

At the beginning of the presolve, we delay the costly creation of this "graph" until we at least ran some basic presolve. This is because during a LNS neighbhorhood, many constraints will be reduced significantly by this "simple" presolve.

ConstraintVariableUsageIsConsistent

Return type: bool

Returns true if our current constraints <-> variables graph is ok. This is meant to be used in DEBUG mode only.

DomainContains

Return type: bool

Arguments: int ref, int64_t value

DomainIsEmpty

Return type: bool

Arguments: int ref

Helpers to query the current domain of a variable.

DomainOf

Return type: Domain

Arguments: int ref

DomainOfVarIsIncludedIn

Return type: bool

Arguments: int var, const Domain& domain

This function takes a positive variable reference.

ExploitFixedDomain

Return type: void

Arguments: int var

Regroups fixed variables with the same value. TODO(user): Also regroup cte and -cte?

GetAbsRelation

Return type: bool

Arguments: int target_ref, int* ref

GetAffineRelation

Return type: AffineRelation::Relation

Arguments: int ref

This makes sure that the affine relation only uses one of the representative from the var_equiv_relations.

GetLiteralRepresentative

Return type: int

Arguments: int ref

Returns the representative of a literal.

GetOrCreateConstantVar

Return type: int

Arguments: int64_t cst

GetOrCreateReifiedPrecedenceLiteral

Return type: int

Arguments: int time_i, int time_j, int active_i, int active_j

The following helper adds the following constraint: result <=> (time_i <= time_j && active_i is true && active_j is true) and returns the (cached) literal result. Note that this cache should just be used temporarily and then cleared with ClearPrecedenceCache() because there is no mechanism to update the cached literals when literal equivalence are detected.

GetOrCreateVarValueEncoding

Return type: int

Arguments: int ref, int64_t value

Gets the associated literal if it is already created. Otherwise create it, add the corresponding constraints and returns it. Important: This does not update the constraint<->variable graph, so ConstraintVariableGraphIsUpToDate() will be false until UpdateNewConstraintsVariableUsage() is called.

GetVariableRepresentative

Return type: int

Arguments: int ref

Returns another reference with exactly the same value.

HasVarValueEncoding

Return type: bool

Arguments: int ref, int64_t value, int* literal = nullptr

Returns true if a literal attached to ref == var exists. It assigns the corresponding to `literal` if non null.

InitializeNewDomains

Return type: void

Creates the internal structure for any new variables in working_model.

InsertVarValueEncoding

Return type: void

Arguments: int literal, int ref, int64_t value

Inserts the given literal to encode ref == value. If an encoding already exists, it adds the two implications between the previous encoding and the new encoding. Important: This does not update the constraint<->variable graph, so ConstraintVariableGraphIsUpToDate() will be false until UpdateNewConstraintsVariableUsage() is called.

IntersectDomainWith

Return type: ABSL_MUST_USE_RESULT bool

Arguments: int ref, const Domain& domain, bool* domain_modified = nullptr

Returns false if the new domain is empty. Sets 'domain_modified' (if provided) to true iff the domain is modified otherwise does not change it.

IntervalUsage

Return type: int

Arguments: int c

IsFixed

Return type: bool

Arguments: int ref

LiteralIsFalse

Return type: bool

Arguments: int lit

LiteralIsTrue

Return type: bool

Arguments: int lit

logger

Return type: SolverLogger*

MarkVariableAsRemoved

Return type: void

Arguments: int ref

Functions to make sure that once we remove a variable, we no longer reuse it.

MaxOf

Return type: int64_t

Arguments: int ref

MaxOf

Return type: int64_t

Arguments: const LinearExpressionProto& expr

MinOf

Return type: int64_t

Arguments: int ref

MinOf

Return type: int64_t

Arguments: const LinearExpressionProto& expr

Helpers to query the current domain of a linear expression. This doesn't check for integer overflow, but our linear expression should be such that this cannot happen (tested at validation).

ModelIsUnsat

Return type: bool

NewBoolVar

Return type: int

NewIntVar

Return type: int

Arguments: const Domain& domain

Helpers to adds new variables to the presolved model.

NotifyThatModelIsUnsat

Return type: ABSL_MUST_USE_RESULT bool

Arguments: const std::string& message = ""

This function always return false. It is just a way to make a little bit more sure that we abort right away when infeasibility is detected.

NumAffineRelations

Return type: int

Used for statistics.

NumEquivRelations

Return type: int

ObjectiveDomain

Return type: const Domain&

Objective getters.

ObjectiveDomainIsConstraining

Return type: bool

ObjectiveMap

Return type: const absl::flat_hash_map<int, int64_t>&

params

Return type: const SatParameters&

PresolveContext

Return type: explicit

Arguments: Model* model, CpModelProto* cp_model, CpModelProto* mapping

PropagateAffineRelation

Return type: bool

Arguments: int ref

Makes sure the domain of ref and of its representative are in sync. Returns false on unsat.

random

Return type: ModelRandomGenerator*

ReadObjectiveFromProto

Return type: void

Objective handling functions. We load it at the beginning so that during presolve we can work on the more efficient hash_map representation. Note that ReadObjectiveFromProto() makes sure that var_to_constraints of all the variable that appear in the objective contains -1. This is later enforced by all the functions modifying the objective. Note(user): Because we process affine relation only on CanonicalizeObjective(), it is possible that when processing a canonicalized linear constraint, we don't detect that a variable in affine relation is in the objective. For now this is fine, because when this is the case, we also have an affine linear constraint, so we can't really do anything with that variable since it appear in at least two constraints.

RefDebugString

Return type: std::string

Arguments: int ref

To facilitate debugging.

RegisterVariablesUsedInAssumptions

Return type: void

Make sure we never delete an "assumption" literal by using a special constraint for that.

RemoveAllVariablesFromAffineRelationConstraint

Return type: void

RemoveVariableFromAffineRelation

Return type: void

Arguments: int var

Advanced usage. This should be called when a variable can be removed from the problem, so we don't count it as part of an affine relation anymore.

SetLiteralToFalse

Return type: ABSL_MUST_USE_RESULT bool

Arguments: int lit

Returns false if the 'lit' doesn't have the desired value in the domain.

SetLiteralToTrue

Return type: ABSL_MUST_USE_RESULT bool

Arguments: int lit

StoreAbsRelation

Return type: bool

Arguments: int target_ref, int ref

Stores/Get the relation target_ref = abs(ref); The first function returns false if it already exist and the second false if it is not present.

StoreAffineRelation

Return type: bool

Arguments: int ref_x, int ref_y, int64_t coeff, int64_t offset

Adds the relation (ref_x = coeff * ref_y + offset) to the repository. Once the relation is added, it doesn't need to be enforced by a constraint in the model proto, since we will propagate such relation directly and add them to the proto at the end of the presolve. Returns true if the relation was added. In some rare case, like if x = 3*z and y = 5*t are already added, we currently cannot add x = 2 * y and we will return false in these case. So when this returns false, the relation needs to be enforced by a separate constraint. If the relation was added, both variables will be marked to appear in the special kAffineRelationConstraint. This will allow to identify when a variable is no longer needed (only appear there and is not a representative).

StoreBooleanEqualityRelation

Return type: void

Arguments: int ref_a, int ref_b

Adds the fact that ref_a == ref_b using StoreAffineRelation() above. This should never fail, so the relation will always be added.

StoreLiteralImpliesVarEqValue

Return type: bool

Arguments: int literal, int var, int64_t value

Stores the fact that literal implies var == value. It returns true if that information is new.

StoreLiteralImpliesVarNEqValue

Return type: bool

Arguments: int literal, int var, int64_t value

Stores the fact that literal implies var != value. It returns true if that information is new.

SubstituteVariableInObjective

Return type: bool

Arguments: int var_in_equality, int64_t coeff_in_equality, const ConstraintProto& equality, std::vector<int>* new_vars_in_objective = nullptr

Given a variable defined by the given inequality that also appear in the objective, remove it from the objective by transferring its cost to other variables in the equality. If new_vars_in_objective is not nullptr, it will be filled with "new" variables that where not in the objective before and are after substitution. Returns false, if the substitution cannot be done. This is the case if the model become UNSAT or if doing it will result in an objective that do not satisfy our overflow preconditions. Note that this can only happen if the substitued variable is not implied free (i.e. if its domain is smaller than the implied domain from the equality).

time_limit

Return type: TimeLimit*

UpdateConstraintVariableUsage

Return type: void

Arguments: int c

Updates the constraints <-> variables graph. This needs to be called each time a constraint is modified.

UpdateNewConstraintsVariableUsage

Return type: void

Calls UpdateConstraintVariableUsage() on all newly created constraints.

UpdateRuleStats

Return type: void

Arguments: const std::string& name, int num_times = 1

Stores a description of a rule that was just applied to have a summary of what the presolve did at the end.

VariableIsNotUsedAnymore

Return type: bool

Arguments: int ref

Returns true if this ref no longer appears in the model.

VariableIsOnlyUsedInEncoding

Return type: bool

Arguments: int ref

Returns true if an integer variable is only appearing in the rhs of constraints of the form lit => var in domain. When this is the case, then we can usually remove this variable and replace these constraints with the proper constraints on the enforcement literals.

VariableIsUniqueAndRemovable

Return type: bool

Arguments: int ref

Returns true if this ref only appear in one constraint.

VariableWasRemoved

Return type: bool

Arguments: int ref

VariableWithCostIsUniqueAndRemovable

Return type: bool

Arguments: int ref

Same as VariableIsUniqueAndRemovable() except that in this case the variable also appear in the objective in addition to a single constraint.

VarToConstraints

Return type: const absl::flat_hash_set<int>&

Arguments: int var

WriteObjectiveToProto

Return type: void