Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model_loader.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_SAT_CP_MODEL_LOADER_H_
15#define OR_TOOLS_SAT_CP_MODEL_LOADER_H_
16
17#include <cstdint>
18#include <functional>
19#include <vector>
20
21#include "absl/container/flat_hash_set.h"
24#include "ortools/base/types.h"
25#include "ortools/sat/cp_model.pb.h"
28#include "ortools/sat/integer.h"
31#include "ortools/sat/model.h"
34
35namespace operations_research {
36namespace sat {
37
38// Extracts all the used variables in the CpModelProto and creates a
39// sat::Model representation for them. More precisely
40// - All Boolean variables will be mapped.
41// - All Interval variables will be mapped.
42// - All non-Boolean variable will have a corresponding IntegerVariable, and
43// depending on the view_all_booleans_as_integers, some or all of the
44// BooleanVariable will also have an IntegerVariable corresponding to its
45// "integer view".
46//
47// Note(user): We could create IntegerVariable on the fly as they are needed,
48// but that loose the original variable order which might be useful in
49// heuristics later.
50void LoadVariables(const CpModelProto& model_proto,
51 bool view_all_booleans_as_integers, Model* m);
52
53// Automatically detect optional variables.
54void DetectOptionalVariables(const CpModelProto& model_proto, Model* m);
55
56// Experimental. Loads the symmetry form the proto symmetry field, as long as
57// they only involve Booleans.
58//
59// TODO(user): We currently only have the code for Booleans, it is why we
60// currently ignore symmetries involving integer variables.
61void LoadBooleanSymmetries(const CpModelProto& model_proto, Model* m);
62
63// Extract the encodings (IntegerVariable <-> Booleans) present in the model.
64// This effectively load some linear constraints of size 1 that will be marked
65// as already loaded.
66void ExtractEncoding(const CpModelProto& model_proto, Model* m);
67
68// Extract element encodings from exactly_one constraints and
69// lit => var == value constraints.
70// This function must be called after ExtractEncoding() has been called.
71void ExtractElementEncoding(const CpModelProto& model_proto, Model* m);
72
73// Process all affine relations of the form a*X + b*Y == cte. For each
74// literals associated to (X >= bound) or (X == value) associate it to its
75// corresponding relation on Y. Also do the other side.
76//
77// TODO(user): In an ideal world, all affine relations like this should be
78// removed in the presolve.
79void PropagateEncodingFromEquivalenceRelations(const CpModelProto& model_proto,
80 Model* m);
81
82// Inspect the search strategy stored in the model, and adds a full encoding to
83// variables appearing in a SELECT_MEDIAN_VALUE search strategy if the search
84// branching is set to FIXED_SEARCH.
85void AddFullEncodingFromSearchBranching(const CpModelProto& model_proto,
86 Model* m);
87
88// Calls one of the functions below.
89// Returns false if we do not know how to load the given constraints.
90bool LoadConstraint(const ConstraintProto& ct, Model* m);
91
92void LoadBoolOrConstraint(const ConstraintProto& ct, Model* m);
93void LoadBoolAndConstraint(const ConstraintProto& ct, Model* m);
94void LoadAtMostOneConstraint(const ConstraintProto& ct, Model* m);
95void LoadExactlyOneConstraint(const ConstraintProto& ct, Model* m);
96void LoadBoolXorConstraint(const ConstraintProto& ct, Model* m);
97void LoadLinearConstraint(const ConstraintProto& ct, Model* m);
98void LoadAllDiffConstraint(const ConstraintProto& ct, Model* m);
99void LoadIntProdConstraint(const ConstraintProto& ct, Model* m);
100void LoadIntDivConstraint(const ConstraintProto& ct, Model* m);
101void LoadIntMinConstraint(const ConstraintProto& ct, Model* m);
102void LoadLinMaxConstraint(const ConstraintProto& ct, Model* m);
103void LoadIntMaxConstraint(const ConstraintProto& ct, Model* m);
104void LoadNoOverlapConstraint(const ConstraintProto& ct, Model* m);
105void LoadNoOverlap2dConstraint(const ConstraintProto& ct, Model* m);
106void LoadCumulativeConstraint(const ConstraintProto& ct, Model* m);
107void LoadCircuitConstraint(const ConstraintProto& ct, Model* m);
108void LoadReservoirConstraint(const ConstraintProto& ct, Model* m);
109void LoadRoutesConstraint(const ConstraintProto& ct, Model* m);
110void LoadCircuitCoveringConstraint(const ConstraintProto& ct, Model* m);
111
112// Part of LoadLinearConstraint() that we reuse to load the objective.
113//
114// We split large constraints into a square root number of parts.
115// This is to avoid a bad complexity while propagating them since our
116// algorithm is not in O(num_changes).
117//
118// TODO(user): Alternatively, we could use a O(num_changes) propagation (a
119// bit tricky to implement), or a decomposition into a tree with more than
120// one level. Both requires experimentations.
121void SplitAndLoadIntermediateConstraints(bool lb_required, bool ub_required,
122 std::vector<IntegerVariable>* vars,
123 std::vector<int64_t>* coeffs,
124 Model* m);
125
126} // namespace sat
127} // namespace operations_research
128
129#endif // OR_TOOLS_SAT_CP_MODEL_LOADER_H_
const Constraint * ct
void LoadBoolXorConstraint(const ConstraintProto &ct, Model *m)
void LoadCircuitCoveringConstraint(const ConstraintProto &ct, Model *m)
void LoadCumulativeConstraint(const ConstraintProto &ct, Model *m)
void LoadLinMaxConstraint(const ConstraintProto &ct, Model *m)
void LoadIntProdConstraint(const ConstraintProto &ct, Model *m)
void SplitAndLoadIntermediateConstraints(bool lb_required, bool ub_required, std::vector< IntegerVariable > *vars, std::vector< int64_t > *coeffs, Model *m)
void DetectOptionalVariables(const CpModelProto &model_proto, Model *m)
Automatically detect optional variables.
void LoadBoolOrConstraint(const ConstraintProto &ct, Model *m)
void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
void LoadBooleanSymmetries(const CpModelProto &model_proto, Model *m)
void LoadRoutesConstraint(const ConstraintProto &ct, Model *m)
void LoadAtMostOneConstraint(const ConstraintProto &ct, Model *m)
void AddFullEncodingFromSearchBranching(const CpModelProto &model_proto, Model *m)
void LoadCircuitConstraint(const ConstraintProto &ct, Model *m)
void ExtractElementEncoding(const CpModelProto &model_proto, Model *m)
void LoadIntMinConstraint(const ConstraintProto &ct, Model *m)
void LoadReservoirConstraint(const ConstraintProto &ct, Model *m)
void LoadNoOverlapConstraint(const ConstraintProto &ct, Model *m)
void LoadAllDiffConstraint(const ConstraintProto &ct, Model *m)
void LoadNoOverlap2dConstraint(const ConstraintProto &ct, Model *m)
void LoadBoolAndConstraint(const ConstraintProto &ct, Model *m)
void LoadExactlyOneConstraint(const ConstraintProto &ct, Model *m)
void LoadIntMaxConstraint(const ConstraintProto &ct, Model *m)
bool LoadConstraint(const ConstraintProto &ct, Model *m)
void PropagateEncodingFromEquivalenceRelations(const CpModelProto &model_proto, Model *m)
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
void ExtractEncoding(const CpModelProto &model_proto, Model *m)
void LoadIntDivConstraint(const ConstraintProto &ct, Model *m)
In SWIG mode, we don't want anything besides these top-level includes.