Google OR-Tools v9.12
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-2025 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 <vector>
19
20#include "ortools/sat/cp_model.pb.h"
22#include "ortools/sat/model.h"
23
24namespace operations_research {
25namespace sat {
26
27// Extracts all the used variables in the CpModelProto and creates a
28// sat::Model representation for them. More precisely
29// - All Boolean variables will be mapped.
30// - All Interval variables will be mapped.
31// - All non-Boolean variable will have a corresponding IntegerVariable, and
32// depending on the view_all_booleans_as_integers, some or all of the
33// BooleanVariable will also have an IntegerVariable corresponding to its
34// "integer view".
35//
36// Note(user): We could create IntegerVariable on the fly as they are needed,
37// but that loose the original variable order which might be useful in
38// heuristics later.
39void LoadVariables(const CpModelProto& model_proto,
40 bool view_all_booleans_as_integers, Model* m);
41
42// Automatically detect optional variables.
43void DetectOptionalVariables(const CpModelProto& model_proto, Model* m);
44
45// Experimental. Loads the symmetry form the proto symmetry field, as long as
46// they only involve Booleans.
47//
48// TODO(user): We currently only have the code for Booleans, it is why we
49// currently ignore symmetries involving integer variables.
50void LoadBooleanSymmetries(const CpModelProto& model_proto, Model* m);
51
52// Extract the encodings (IntegerVariable <-> Booleans) present in the model.
53// This effectively load some linear constraints of size 1 that will be marked
54// as already loaded.
55void ExtractEncoding(const CpModelProto& model_proto, Model* m);
56
57// Extract element encodings from exactly_one constraints and
58// lit => var == value constraints.
59// This function must be called after ExtractEncoding() has been called.
60void ExtractElementEncoding(const CpModelProto& model_proto, Model* m);
61
62// Process all affine relations of the form a*X + b*Y == cte. For each
63// literals associated to (X >= bound) or (X == value) associate it to its
64// corresponding relation on Y. Also do the other side.
65//
66// TODO(user): In an ideal world, all affine relations like this should be
67// removed in the presolve.
68void PropagateEncodingFromEquivalenceRelations(const CpModelProto& model_proto,
69 Model* m);
70
71// Inspect the search strategy stored in the model, and adds a full encoding to
72// variables appearing in a SELECT_MEDIAN_VALUE search strategy if the search
73// branching is set to FIXED_SEARCH.
74void AddFullEncodingFromSearchBranching(const CpModelProto& model_proto,
75 Model* m);
76
77// Calls one of the functions below.
78// Returns false if we do not know how to load the given constraints.
79bool LoadConstraint(const ConstraintProto& ct, Model* m);
80
81void LoadBoolOrConstraint(const ConstraintProto& ct, Model* m);
82void LoadBoolAndConstraint(const ConstraintProto& ct, Model* m);
83void LoadAtMostOneConstraint(const ConstraintProto& ct, Model* m);
84void LoadExactlyOneConstraint(const ConstraintProto& ct, Model* m);
85void LoadBoolXorConstraint(const ConstraintProto& ct, Model* m);
86void LoadLinearConstraint(const ConstraintProto& ct, Model* m);
87void LoadAllDiffConstraint(const ConstraintProto& ct, Model* m);
88void LoadIntProdConstraint(const ConstraintProto& ct, Model* m);
89void LoadIntDivConstraint(const ConstraintProto& ct, Model* m);
90void LoadIntMinConstraint(const ConstraintProto& ct, Model* m);
91void LoadLinMaxConstraint(const ConstraintProto& ct, Model* m);
92void LoadIntMaxConstraint(const ConstraintProto& ct, Model* m);
93void LoadNoOverlapConstraint(const ConstraintProto& ct, Model* m);
94void LoadNoOverlap2dConstraint(const ConstraintProto& ct, Model* m);
95void LoadCumulativeConstraint(const ConstraintProto& ct, Model* m);
96void LoadCircuitConstraint(const ConstraintProto& ct, Model* m);
97void LoadReservoirConstraint(const ConstraintProto& ct, Model* m);
98void LoadRoutesConstraint(const ConstraintProto& ct, Model* m);
99void LoadCircuitCoveringConstraint(const ConstraintProto& ct, Model* m);
100
101// Part of LoadLinearConstraint() that we reuse to load the objective.
102//
103// We split large constraints into a square root number of parts.
104// This is to avoid a bad complexity while propagating them since our
105// algorithm is not in O(num_changes).
106//
107// TODO(user): Alternatively, we could use a O(num_changes) propagation (a
108// bit tricky to implement), or a decomposition into a tree with more than
109// one level. Both requires experimentations.
110void SplitAndLoadIntermediateConstraints(bool lb_required, bool ub_required,
111 std::vector<IntegerVariable>* vars,
112 std::vector<int64_t>* coeffs,
113 Model* m);
114
115} // namespace sat
116} // namespace operations_research
117
118#endif // OR_TOOLS_SAT_CP_MODEL_LOADER_H_
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.