OR-Tools  9.0
cp_model_search.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_SEARCH_H_
15 #define OR_TOOLS_SAT_CP_MODEL_SEARCH_H_
16 
17 #include <functional>
18 #include <vector>
19 
23 #include "ortools/sat/integer.h"
25 #include "ortools/sat/model.h"
26 
27 namespace operations_research {
28 namespace sat {
29 
30 // This class allows to query information about the current bounds of the loaded
31 // cp_model.proto variables during the search. It is a "view" of the current
32 // solver state using the indices of the proto.
33 //
34 // TODO(user): For now it uses proto indices of the loaded model. We will need
35 // to add a mapping to use proto indices of the non-presolved model to allow for
36 // a client custom search with presolve. The main API shouldn't change though
37 // and the change will be transparent.
38 class CpModelView {
39  public:
40  explicit CpModelView(Model* model);
41 
42  // The valid indices for the calls below are in [0, num_variables).
43  int NumVariables() const;
44 
45  // Getters about the current domain of the given variable.
46  bool IsFixed(int var) const;
47  int64_t Min(int var) const;
48  int64_t Max(int var) const;
49 
50  // If under a given partial assignment, the value of a variable has no impact,
51  // this might returns true, and there is no point trying to branch on this
52  // variable.
53  //
54  // This might for example be the case for the start of an unperformed interval
55  // which will not impact the rest of the problem in any way. Note that it is
56  // still possible to branch on ignored variable, this will just not change
57  // anything.
58  bool IsCurrentlyFree(int var) const;
59 
60  // Helpers to generate a decision.
61  BooleanOrIntegerLiteral GreaterOrEqual(int var, int64_t value) const;
62  BooleanOrIntegerLiteral LowerOrEqual(int var, int64_t value) const;
63 
64  private:
65  const CpModelMapping& mapping_;
66  const VariablesAssignment& boolean_assignment_;
67  const IntegerTrail& integer_trail_;
68 };
69 
70 // Constructs the search strategy specified in the given CpModelProto. A
71 // positive variable ref in the proto is mapped to variable_mapping[ref] in the
72 // model. All the variables referred in the search strategy must be correctly
73 // mapped, the other entries can be set to kNoIntegerVariable.
75  const CpModelProto& cp_model_proto,
76  const std::vector<IntegerVariable>& variable_mapping,
77  IntegerVariable objective_var, Model* model);
78 
79 // For debugging fixed-search: display information about the named variables
80 // domain before taking each decision. Note that we copy the instrumented
81 // stategy so it doesn't have to outlive the returned functions like the other
82 // arguments.
84  const CpModelProto& cp_model_proto,
85  const std::vector<IntegerVariable>& variable_mapping,
86  const std::function<BooleanOrIntegerLiteral()>& instrumented_strategy,
87  Model* model);
88 
89 // Returns up to "num_workers" different parameters. We do not always return
90 // num_worker parameters to leave room for strategies like LNS that do not
91 // consume a full worker and can always be interleaved.
92 std::vector<SatParameters> GetDiverseSetOfParameters(
93  const SatParameters& base_params, const CpModelProto& cp_model,
94  const int num_workers);
95 
96 } // namespace sat
97 } // namespace operations_research
98 
99 #endif // OR_TOOLS_SAT_CP_MODEL_SEARCH_H_
BooleanOrIntegerLiteral GreaterOrEqual(int var, int64_t value) const
BooleanOrIntegerLiteral LowerOrEqual(int var, int64_t value) const
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
int64_t value
IntVar * var
Definition: expr_array.cc:1874
GRBmodel * model
std::vector< SatParameters > GetDiverseSetOfParameters(const SatParameters &base_params, const CpModelProto &cp_model, const int num_workers)
std::function< BooleanOrIntegerLiteral()> ConstructSearchStrategy(const CpModelProto &cp_model_proto, const std::vector< IntegerVariable > &variable_mapping, IntegerVariable objective_var, Model *model)
std::function< BooleanOrIntegerLiteral()> InstrumentSearchStrategy(const CpModelProto &cp_model_proto, const std::vector< IntegerVariable > &variable_mapping, const std::function< BooleanOrIntegerLiteral()> &instrumented_strategy, Model *model)
Collection of objects used to extend the Constraint Solver library.