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