14#ifndef OR_TOOLS_SAT_CP_MODEL_PRESOLVE_H_
15#define OR_TOOLS_SAT_CP_MODEL_PRESOLVE_H_
21#include "absl/base/attributes.h"
22#include "absl/container/flat_hash_map.h"
23#include "absl/container/flat_hash_set.h"
24#include "ortools/sat/cp_model.pb.h"
28#include "ortools/sat/sat_parameters.pb.h"
46 const PresolveContext&
context);
72 std::vector<int>* postsolve_mapping);
89 CpSolverStatus InfeasibleStatus();
92 void PresolveToFixPoint();
108 bool PresolveAllDiff(ConstraintProto*
ct);
109 bool PresolveAutomaton(ConstraintProto*
ct);
110 bool PresolveElement(ConstraintProto*
ct);
111 bool PresolveIntAbs(ConstraintProto*
ct);
112 bool PresolveIntDiv(ConstraintProto*
ct);
113 bool PresolveIntMod(ConstraintProto*
ct);
114 bool PresolveIntProd(ConstraintProto*
ct);
115 bool PresolveInterval(
int c, ConstraintProto*
ct);
116 bool PresolveInverse(ConstraintProto*
ct);
117 bool PresolveLinMax(ConstraintProto*
ct);
118 bool PresolveTable(ConstraintProto*
ct);
120 bool PresolveCumulative(ConstraintProto*
ct);
121 bool PresolveNoOverlap(ConstraintProto*
ct);
122 bool PresolveNoOverlap2D(
int c, ConstraintProto*
ct);
123 bool PresolveReservoir(ConstraintProto*
ct);
125 bool PresolveCircuit(ConstraintProto*
ct);
126 bool PresolveRoutes(ConstraintProto*
ct);
128 bool PresolveAtMostOrExactlyOne(ConstraintProto*
ct);
129 bool PresolveAtMostOne(ConstraintProto*
ct);
130 bool PresolveExactlyOne(ConstraintProto*
ct);
132 bool PresolveBoolAnd(ConstraintProto*
ct);
133 bool PresolveBoolOr(ConstraintProto*
ct);
134 bool PresolveBoolXor(ConstraintProto*
ct);
135 bool PresolveEnforcementLiteral(ConstraintProto*
ct);
139 template <
typename ProtoWithVarsAndCoeffs>
140 bool CanonicalizeLinearExpressionInternal(
const ConstraintProto&
ct,
141 ProtoWithVarsAndCoeffs*
proto,
143 bool CanonicalizeLinearExpression(
const ConstraintProto&
ct,
144 LinearExpressionProto* exp);
145 bool CanonicalizeLinearArgument(
const ConstraintProto&
ct,
146 LinearArgumentProto*
proto);
149 bool CanonicalizeLinear(ConstraintProto*
ct);
150 bool PropagateDomainsInLinear(
int c, ConstraintProto*
ct);
151 bool RemoveSingletonInLinear(ConstraintProto*
ct);
152 bool PresolveSmallLinear(ConstraintProto*
ct);
153 bool PresolveLinearOfSizeOne(ConstraintProto*
ct);
154 bool PresolveLinearOfSizeTwo(ConstraintProto*
ct);
155 bool PresolveLinearOnBooleans(ConstraintProto*
ct);
156 bool AddVarAffineRepresentativeFromLinearEquality(
int target_index,
157 ConstraintProto*
ct);
158 bool PresolveLinearEqualityWithModulo(ConstraintProto*
ct);
159 bool DetectAndProcessOneSidedLinearConstraint(
int c, ConstraintProto*
ct);
168 void TryToReduceCoefficientsOfLinearConstraint(
int c, ConstraintProto*
ct);
174 void ExtractEncodingFromLinear();
175 bool ProcessEncodingFromLinear(
int linear_encoding_ct_index,
176 const ConstraintProto& at_most_or_exactly_one,
177 int64_t* num_unique_terms,
178 int64_t* num_multiple_terms);
182 void DetectDuplicateConstraints();
186 void DetectDominatedLinearConstraints();
192 bool IsImpliedFree(
const std::vector<int>& constraints,
int x,
198 void DetectOverlappingColumns();
203 void PerformFreeColumnSubstitution(
204 const std::vector<std::pair<int, int64_t>>& constraints_with_x_coeff,
205 int x,
int y, int64_t factor);
210 void ProcessSetPPC();
214 bool ProcessSetPPCSubset(
int subset_c,
int superset_c,
215 absl::flat_hash_set<int>* tmp_set,
216 bool* remove_subset,
bool* remove_superset);
219 void PresolvePureSatPart();
222 void ExtractAtMostOneFromLinear(ConstraintProto*
ct);
225 bool DivideLinearByGcd(ConstraintProto*
ct);
227 void ExtractEnforcementLiteralFromLinearConstraint(
int ct_index,
228 ConstraintProto*
ct);
232 void TransformIntoMaxCliques();
235 void ExtractBoolAnd();
237 void ExpandObjective();
239 void LookAtVariableWithDegreeTwo(
int var);
240 void ProcessVariableOnlyUsedInEncoding(
int var);
241 void TryToSimplifyDomain(
int var);
243 void MergeNoOverlapConstraints();
247 void EncodeAllAffineRelations();
248 bool PresolveAffineRelationIfAny(
int var);
250 bool ExploitEquivalenceRelations(
int c, ConstraintProto*
ct);
252 ABSL_MUST_USE_RESULT
bool RemoveConstraint(ConstraintProto*
ct);
253 ABSL_MUST_USE_RESULT
bool MarkConstraintAsFalse(ConstraintProto*
ct);
255 std::vector<int>* postsolve_mapping_;
260 std::vector<std::pair<int, int64_t>> tmp_terms_;
288 const std::vector<int>& ignored_constraints,
289 bool first_copy =
false);
298 bool CreateUnsatModel();
300 void CopyEnforcementLiterals(
const ConstraintProto& orig,
301 ConstraintProto* dest);
302 bool OneEnforcementLiteralIsFalse(
const ConstraintProto&
ct)
const;
305 bool CopyBoolOr(
const ConstraintProto&
ct);
306 bool CopyBoolOrWithDupSupport(
const ConstraintProto&
ct);
307 bool CopyBoolAnd(
const ConstraintProto&
ct);
308 bool CopyLinear(
const ConstraintProto&
ct);
309 bool CopyAtMostOne(
const ConstraintProto&
ct);
310 bool CopyExactlyOne(
const ConstraintProto&
ct);
311 bool CopyInterval(
const ConstraintProto&
ct,
int c,
bool ignore_names);
316 void CopyAndMapNoOverlap(
const ConstraintProto&
ct);
317 void CopyAndMapNoOverlap2D(
const ConstraintProto&
ct);
318 void CopyAndMapCumulative(
const ConstraintProto&
ct);
321 int64_t skipped_non_zero_ = 0;
324 std::vector<int> non_fixed_variables_;
325 std::vector<int64_t> non_fixed_coefficients_;
326 absl::flat_hash_map<int, int> interval_mapping_;
327 int starting_constraint_index_ = 0;
328 std::vector<int> temp_enforcement_literals_;
330 std::vector<int> temp_literals_;
331 absl::flat_hash_set<int> tmp_literals_set_;
351 std::vector<int>* postsolve_mapping);
CpSolverStatus Presolve()
void RemoveEmptyConstraints()
CpModelPresolver(PresolveContext *context, std::vector< int > *postsolve_mapping)
bool PresolveOneConstraint(int c)
ModelCopy(PresolveContext *context)
bool ImportAndSimplifyConstraints(const CpModelProto &in_model, const std::vector< int > &ignored_constraints, bool first_copy=false)
void ImportVariablesAndMaybeIgnoreNames(const CpModelProto &in_model)
CpModelProto const * model_proto
GurobiMPCallbackContext * context
void CopyEverythingExceptVariablesAndConstraintsFieldsIntoContext(const CpModelProto &in_model, PresolveContext *context)
std::vector< std::pair< int, int > > FindDuplicateConstraints(const CpModelProto &model_proto)
CpSolverStatus PresolveCpModel(PresolveContext *context, std::vector< int > *postsolve_mapping)
bool ImportModelWithBasicPresolveIntoContext(const CpModelProto &in_model, PresolveContext *context)
void ApplyVariableMapping(const std::vector< int > &mapping, const PresolveContext &context)
Collection of objects used to extend the Constraint Solver library.