14 #ifndef OR_TOOLS_SAT_CP_MODEL_PRESOLVE_H_
15 #define OR_TOOLS_SAT_CP_MODEL_PRESOLVE_H_
22 #include "absl/base/attributes.h"
23 #include "absl/container/flat_hash_map.h"
24 #include "absl/container/flat_hash_set.h"
25 #include "ortools/sat/cp_model.pb.h"
29 #include "ortools/sat/sat_parameters.pb.h"
47 const PresolveContext&
context);
73 std::vector<int>* postsolve_mapping);
90 CpSolverStatus InfeasibleStatus();
93 void PresolveToFixPoint();
109 bool PresolveAllDiff(ConstraintProto*
ct);
110 bool PresolveAutomaton(ConstraintProto*
ct);
111 bool PresolveElement(ConstraintProto*
ct);
112 bool PresolveIntAbs(ConstraintProto*
ct);
113 bool PresolveIntDiv(ConstraintProto*
ct);
114 bool PresolveIntMod(ConstraintProto*
ct);
115 bool PresolveIntProd(ConstraintProto*
ct);
116 bool PresolveInterval(
int c, ConstraintProto*
ct);
117 bool PresolveInverse(ConstraintProto*
ct);
118 bool PresolveLinMax(ConstraintProto*
ct);
119 bool PresolveLinMaxWhenAllBoolean(ConstraintProto*
ct);
120 bool PresolveTable(ConstraintProto*
ct);
121 void DetectDuplicateIntervals(
122 int c, google::protobuf::RepeatedField<int32_t>* intervals);
123 bool PresolveCumulative(ConstraintProto*
ct);
124 bool PresolveNoOverlap(ConstraintProto*
ct);
125 bool PresolveNoOverlap2D(
int c, ConstraintProto*
ct);
126 bool PresolveReservoir(ConstraintProto*
ct);
128 bool PresolveCircuit(ConstraintProto*
ct);
129 bool PresolveRoutes(ConstraintProto*
ct);
131 bool PresolveAtMostOrExactlyOne(ConstraintProto*
ct);
132 bool PresolveAtMostOne(ConstraintProto*
ct);
133 bool PresolveExactlyOne(ConstraintProto*
ct);
135 bool PresolveBoolAnd(ConstraintProto*
ct);
136 bool PresolveBoolOr(ConstraintProto*
ct);
137 bool PresolveBoolXor(ConstraintProto*
ct);
138 bool PresolveEnforcementLiteral(ConstraintProto*
ct);
142 template <
typename ProtoWithVarsAndCoeffs>
143 bool CanonicalizeLinearExpressionInternal(
const ConstraintProto&
ct,
144 ProtoWithVarsAndCoeffs*
proto,
146 bool CanonicalizeLinearExpression(
const ConstraintProto&
ct,
147 LinearExpressionProto* exp);
148 bool CanonicalizeLinearArgument(
const ConstraintProto&
ct,
149 LinearArgumentProto*
proto);
152 bool CanonicalizeLinear(ConstraintProto*
ct);
153 bool PropagateDomainsInLinear(
int ct_index, ConstraintProto*
ct);
154 bool RemoveSingletonInLinear(ConstraintProto*
ct);
155 bool PresolveSmallLinear(ConstraintProto*
ct);
156 bool PresolveLinearOfSizeOne(ConstraintProto*
ct);
157 bool PresolveLinearOfSizeTwo(ConstraintProto*
ct);
158 bool PresolveLinearOnBooleans(ConstraintProto*
ct);
159 bool PresolveDiophantine(ConstraintProto*
ct);
160 bool AddVarAffineRepresentativeFromLinearEquality(
int target_index,
161 ConstraintProto*
ct);
162 bool PresolveLinearEqualityWithModulo(ConstraintProto*
ct);
166 void DetectAndProcessAtMostOneInLinear(
int ct_index, ConstraintProto*
ct,
176 void TryToReduceCoefficientsOfLinearConstraint(
int c, ConstraintProto*
ct);
182 void ExtractEncodingFromLinear();
183 bool ProcessEncodingFromLinear(
int linear_encoding_ct_index,
184 const ConstraintProto& at_most_or_exactly_one,
185 int64_t* num_unique_terms,
186 int64_t* num_multiple_terms);
190 void DetectDuplicateConstraints();
194 void DetectDominatedLinearConstraints();
199 void ProcessSetPPC();
202 void DetectIncludedEnforcement();
206 bool ProcessSetPPCSubset(
int subset_c,
int superset_c,
207 absl::flat_hash_set<int>* tmp_set,
208 bool* remove_subset,
bool* remove_superset,
209 bool* stop_processing_superset);
212 void PresolvePureSatPart();
215 void ExtractAtMostOneFromLinear(ConstraintProto*
ct);
218 bool DivideLinearByGcd(ConstraintProto*
ct);
220 void ExtractEnforcementLiteralFromLinearConstraint(
int ct_index,
221 ConstraintProto*
ct);
222 void LowerThanCoeffStrengthening(
bool from_lower_bound, int64_t min_magnitude,
223 int64_t threshold, ConstraintProto*
ct);
227 void TransformIntoMaxCliques();
230 void ExtractBoolAnd();
235 void ExpandObjective();
243 void ShiftObjectiveWithExactlyOnes();
245 void ProcessVariableOnlyUsedInEncoding(
int var);
246 void TryToSimplifyDomain(
int var);
248 void LookAtVariableWithDegreeTwo(
int var);
249 void ProcessVariableInTwoAtMostOrExactlyOne(
int var);
251 void MergeNoOverlapConstraints();
255 void FindBigLinearOverlap();
272 void EncodeAllAffineRelations();
273 bool PresolveAffineRelationIfAny(
int var);
275 bool ExploitEquivalenceRelations(
int c, ConstraintProto*
ct);
277 ABSL_MUST_USE_RESULT
bool RemoveConstraint(ConstraintProto*
ct);
278 ABSL_MUST_USE_RESULT
bool MarkConstraintAsFalse(ConstraintProto*
ct);
280 std::vector<int>* postsolve_mapping_;
285 std::vector<std::pair<int, int64_t>> tmp_terms_;
288 std::vector<std::array<int64_t, 2>> conditional_mins_;
289 std::vector<std::array<int64_t, 2>> conditional_maxs_;
293 absl::flat_hash_map<int, int> temp_map_;
294 absl::flat_hash_set<int> temp_set_;
295 ConstraintProto temp_ct_;
329 const std::vector<int>& ignored_constraints,
330 bool first_copy =
false);
339 bool CreateUnsatModel();
341 void CopyEnforcementLiterals(
const ConstraintProto& orig,
342 ConstraintProto* dest);
343 bool OneEnforcementLiteralIsFalse(
const ConstraintProto&
ct)
const;
346 bool CopyBoolOr(
const ConstraintProto&
ct);
347 bool CopyBoolOrWithDupSupport(
const ConstraintProto&
ct);
348 bool CopyBoolAnd(
const ConstraintProto&
ct);
349 bool CopyLinear(
const ConstraintProto&
ct);
350 bool CopyAtMostOne(
const ConstraintProto&
ct);
351 bool CopyExactlyOne(
const ConstraintProto&
ct);
352 bool CopyInterval(
const ConstraintProto&
ct,
int c,
bool ignore_names);
357 void CopyAndMapNoOverlap(
const ConstraintProto&
ct);
358 void CopyAndMapNoOverlap2D(
const ConstraintProto&
ct);
359 void CopyAndMapCumulative(
const ConstraintProto&
ct);
362 int64_t skipped_non_zero_ = 0;
365 std::vector<int> non_fixed_variables_;
366 std::vector<int64_t> non_fixed_coefficients_;
367 absl::flat_hash_map<int, int> interval_mapping_;
368 int starting_constraint_index_ = 0;
369 std::vector<int> temp_enforcement_literals_;
371 std::vector<int> temp_literals_;
372 absl::flat_hash_set<int> tmp_literals_set_;
392 std::vector<int>* postsolve_mapping);
415 const CpModelProto&
model_proto,
bool ignore_enforcement =
false);
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
std::vector< std::pair< int, int > > FindDuplicateConstraints(const CpModelProto &model_proto, bool ignore_enforcement)
void CopyEverythingExceptVariablesAndConstraintsFieldsIntoContext(const CpModelProto &in_model, PresolveContext *context)
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.