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