OR-Tools  9.1
cp_model_loader.h
Go to the documentation of this file.
1 // Copyright 2010-2021 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"
22 #include "ortools/base/int_type.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/base/map_util.h"
30 #include "ortools/sat/integer.h"
31 #include "ortools/sat/intervals.h"
33 #include "ortools/sat/model.h"
34 #include "ortools/sat/sat_base.h"
35 
36 namespace operations_research {
37 namespace sat {
38 
39 // Extracts all the used variables in the CpModelProto and creates a
40 // sat::Model representation for them. More precisely
41 // - All Boolean variables will be mapped.
42 // - All Interval variables will be mapped.
43 // - All non-Boolean variable will have a corresponding IntegerVariable, and
44 // depending on the view_all_booleans_as_integers, some or all of the
45 // BooleanVariable will also have an IntegerVariable corresponding to its
46 // "integer view".
47 //
48 // Note(user): We could create IntegerVariable on the fly as they are needed,
49 // but that loose the original variable order which might be useful in
50 // heuristics later.
51 void LoadVariables(const CpModelProto& model_proto,
52  bool view_all_booleans_as_integers, Model* m);
53 
54 // Automatically detect optional variables.
55 void DetectOptionalVariables(const CpModelProto& model_proto, Model* m);
56 
57 // Experimental. Loads the symmetry form the proto symmetry field, as long as
58 // they only involve Booleans.
59 //
60 // TODO(user): We currently only have the code for Booleans, it is why we
61 // currently ignore symmetries involving integer variables.
62 void LoadBooleanSymmetries(const CpModelProto& model_proto, Model* m);
63 
64 // Extract the encodings (IntegerVariable <-> Booleans) present in the model.
65 // This effectively load some linear constraints of size 1 that will be marked
66 // as already loaded.
67 void ExtractEncoding(const CpModelProto& model_proto, Model* m);
68 
69 // Process all affine relations of the form a*X + b*Y == cte. For each
70 // literals associated to (X >= bound) or (X == value) associate it to its
71 // corresponding relation on Y. Also do the other side.
72 //
73 // TODO(user): In an ideal world, all affine relations like this should be
74 // removed in the presolve.
76  Model* m);
77 
78 // Inspects the model and use some heuristic to decide which variable, if any,
79 // should be fully encoded. Note that some constraints like the element or table
80 // constraints require some of their variables to be fully encoded.
81 //
82 // TODO(user): This function exists so that we fully encode first all the
83 // variable that needs to be fully encoded so that at loading time we can adapt
84 // the algorithm used. Howeve it needs to duplicate the logic that decide what
85 // needs to be fully encoded. Try to come up with a more robust design.
86 void MaybeFullyEncodeMoreVariables(const CpModelProto& model_proto, Model* m);
87 
88 // Inspect the search strategy stored in the model, and adds a full encoding to
89 // variables appearing in a SELECT_MEDIAN_VALUE search strategy if the search
90 // branching is set to FIXED_SEARCH.
91 void AddFullEncodingFromSearchBranching(const CpModelProto& model_proto,
92  Model* m);
93 
94 // Calls one of the functions below.
95 // Returns false if we do not know how to load the given constraints.
96 bool LoadConstraint(const ConstraintProto& ct, Model* m);
97 
98 void LoadBoolOrConstraint(const ConstraintProto& ct, Model* m);
99 void LoadBoolAndConstraint(const ConstraintProto& ct, Model* m);
100 void LoadAtMostOneConstraint(const ConstraintProto& ct, Model* m);
101 void LoadExactlyOneConstraint(const ConstraintProto& ct, Model* m);
102 void LoadBoolXorConstraint(const ConstraintProto& ct, Model* m);
103 void LoadLinearConstraint(const ConstraintProto& ct, Model* m);
104 void LoadAllDiffConstraint(const ConstraintProto& ct, Model* m);
105 void LoadIntProdConstraint(const ConstraintProto& ct, Model* m);
106 void LoadIntDivConstraint(const ConstraintProto& ct, Model* m);
107 void LoadIntMinConstraint(const ConstraintProto& ct, Model* m);
108 void LoadLinMaxConstraint(const ConstraintProto& ct, Model* m);
109 void LoadIntMaxConstraint(const ConstraintProto& ct, Model* m);
110 void LoadNoOverlapConstraint(const ConstraintProto& ct, Model* m);
111 void LoadNoOverlap2dConstraint(const ConstraintProto& ct, Model* m);
112 void LoadCumulativeConstraint(const ConstraintProto& ct, Model* m);
113 void LoadReservoirConstraint(const ConstraintProto& ct, Model* m);
114 void LoadElementConstraintBounds(const ConstraintProto& ct, Model* m);
115 void LoadElementConstraintAC(const ConstraintProto& ct, Model* m);
116 void LoadElementConstraint(const ConstraintProto& ct, Model* m);
117 void LoadTableConstraint(const ConstraintProto& ct, Model* m);
118 void LoadAutomatonConstraint(const ConstraintProto& ct, Model* m);
119 void LoadCircuitConstraint(const ConstraintProto& ct, Model* m);
120 void LoadRoutesConstraint(const ConstraintProto& ct, Model* m);
121 void LoadCircuitCoveringConstraint(const ConstraintProto& ct, Model* m);
122 void LoadInverseConstraint(const ConstraintProto& ct, Model* m);
123 
124 } // namespace sat
125 } // namespace operations_research
126 
127 #endif // OR_TOOLS_SAT_CP_MODEL_LOADER_H_
void DetectOptionalVariables(const CpModelProto &model_proto, Model *m)
void LoadIntMinConstraint(const ConstraintProto &ct, Model *m)
void LoadReservoirConstraint(const ConstraintProto &ct, Model *m)
void LoadIntDivConstraint(const ConstraintProto &ct, Model *m)
void LoadLinMaxConstraint(const ConstraintProto &ct, Model *m)
void LoadTableConstraint(const ConstraintProto &ct, Model *m)
void ExtractEncoding(const CpModelProto &model_proto, Model *m)
bool LoadConstraint(const ConstraintProto &ct, Model *m)
void LoadRoutesConstraint(const ConstraintProto &ct, Model *m)
void LoadAtMostOneConstraint(const ConstraintProto &ct, Model *m)
void LoadAutomatonConstraint(const ConstraintProto &ct, Model *m)
void LoadCumulativeConstraint(const ConstraintProto &ct, Model *m)
void LoadElementConstraintAC(const ConstraintProto &ct, Model *m)
void MaybeFullyEncodeMoreVariables(const CpModelProto &model_proto, Model *m)
void LoadNoOverlap2dConstraint(const ConstraintProto &ct, Model *m)
void LoadNoOverlapConstraint(const ConstraintProto &ct, Model *m)
void LoadExactlyOneConstraint(const ConstraintProto &ct, Model *m)
void LoadCircuitCoveringConstraint(const ConstraintProto &ct, Model *m)
CpModelProto const * model_proto
void LoadIntProdConstraint(const ConstraintProto &ct, Model *m)
void LoadCircuitConstraint(const ConstraintProto &ct, Model *m)
void LoadElementConstraintBounds(const ConstraintProto &ct, Model *m)
void AddFullEncodingFromSearchBranching(const CpModelProto &model_proto, Model *m)
void LoadInverseConstraint(const ConstraintProto &ct, Model *m)
void PropagateEncodingFromEquivalenceRelations(const CpModelProto &model_proto, Model *m)
void LoadBoolXorConstraint(const ConstraintProto &ct, Model *m)
void LoadIntMaxConstraint(const ConstraintProto &ct, Model *m)
Collection of objects used to extend the Constraint Solver library.
void LoadBoolOrConstraint(const ConstraintProto &ct, Model *m)
void LoadElementConstraint(const ConstraintProto &ct, Model *m)
void LoadAllDiffConstraint(const ConstraintProto &ct, Model *m)
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
const Constraint * ct
void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
void LoadBoolAndConstraint(const ConstraintProto &ct, Model *m)
void LoadBooleanSymmetries(const CpModelProto &model_proto, Model *m)