Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model_search.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_SEARCH_H_
15#define OR_TOOLS_SAT_CP_MODEL_SEARCH_H_
16
17#include <cstdint>
18#include <functional>
19#include <string>
20#include <vector>
21
22#include "absl/container/flat_hash_map.h"
23#include "absl/random/bit_gen_ref.h"
24#include "absl/strings/string_view.h"
25#include "absl/types/span.h"
27#include "ortools/sat/cp_model.pb.h"
29#include "ortools/sat/integer.h"
32#include "ortools/sat/model.h"
34#include "ortools/sat/sat_parameters.pb.h"
35
36namespace operations_research {
37namespace sat {
38
39// This class allows to query information about the current bounds of the loaded
40// cp_model.proto variables during the search. It is a "view" of the current
41// solver state using the indices of the proto.
42//
43// TODO(user): For now it uses proto indices of the loaded model. We will need
44// to add a mapping to use proto indices of the non-presolved model to allow for
45// a client custom search with presolve. The main API shouldn't change though
46// and the change will be transparent.
48 public:
49 explicit CpModelView(Model* model);
50
51 // The valid indices for the calls below are in [0, num_variables).
52 int NumVariables() const;
53
54 // Getters about the current domain of the given variable.
55 bool IsFixed(int var) const;
56 int64_t Min(int var) const;
57 int64_t Max(int var) const;
58
59 // Helpers to generate a decision.
60 BooleanOrIntegerLiteral GreaterOrEqual(int var, int64_t value) const;
61 BooleanOrIntegerLiteral LowerOrEqual(int var, int64_t value) const;
63 BooleanOrIntegerLiteral RandomSplit(int var, int64_t lb, int64_t ub) const;
64
65 private:
66 const CpModelMapping& mapping_;
67 const VariablesAssignment& boolean_assignment_;
68 const IntegerTrail& integer_trail_;
69 const IntegerEncoder& integer_encoder_;
70 mutable absl::BitGenRef random_;
71};
72
73// Constructs the search strategy specified in the given CpModelProto.
75 const CpModelProto& cp_model_proto, Model* model);
76
77// Constructs a search strategy tailored for the current model.
79 const CpModelProto& cp_model_proto, Model* model);
80
81// Constructs an integer completion search strategy.
82std::function<BooleanOrIntegerLiteral()>
84 absl::Span<const IntegerVariable> variable_mapping,
85 IntegerVariable objective_var, Model* model);
86
87// Constructs a search strategy that follows the hints from the model.
89 const CpModelProto& cp_model_proto, CpModelMapping* mapping, Model* model);
90
91// Constructs our "fixed" search strategy which start with
92// ConstructUserSearchStrategy() but is completed by a couple of automatic
93// heuristics.
95 std::function<BooleanOrIntegerLiteral()> user_search,
96 std::function<BooleanOrIntegerLiteral()> heuristic_search,
97 std::function<BooleanOrIntegerLiteral()> integer_completion);
98
99// For debugging fixed-search: display information about the named variables
100// domain before taking each decision. Note that we copy the instrumented
101// strategy so it doesn't have to outlive the returned functions like the other
102// arguments.
104 const CpModelProto& cp_model_proto,
105 absl::Span<const IntegerVariable> variable_mapping,
106 std::function<BooleanOrIntegerLiteral()> instrumented_strategy,
107 Model* model);
108
109// Returns all the named set of parameters known to the solver. This include our
110// default strategies like "max_lp", "core", etc... It is visible here so that
111// this can be reused by parameter validation.
112//
113// Usually, named strategies just override a few field from the base_params.
114absl::flat_hash_map<std::string, SatParameters> GetNamedParameters(
115 SatParameters base_params);
116
117// Returns a list of full workers to run.
119std::vector<SatParameters> GetFullWorkerParameters(
120 const SatParameters& base_params, const CpModelProto& cp_model,
121 int num_already_present, SubsolverNameFilter* name_filter);
122
123// Given a base set of parameter, if non-empty, this repeat them (round-robbin)
124// until we get num_params_to_generate. Note that if we don't have a multiple,
125// the first base parameters will be repeated more than the others.
126//
127// Note that this will also change the random_seed of each of these parameters.
128std::vector<SatParameters> RepeatParameters(
129 absl::Span<const SatParameters> base_params, int num_params_to_generate);
130
131// Returns a vector of base parameters to specify solvers specialized to find a
132// initial solution. This is meant to be used with RepeatParameters() and
133// FilterParameters().
134std::vector<SatParameters> GetFirstSolutionBaseParams(
135 const SatParameters& base_params);
136
137// Simple class used to filter executed subsolver names.
139 public:
140 // Warning, params must outlive the class and be constant.
141 explicit SubsolverNameFilter(const SatParameters& params);
142
143 // Shall we keep a parameter with given name?
144 bool Keep(absl::string_view name);
145
146 // Applies Keep() to all the input list.
147 std::vector<SatParameters> Filter(absl::Span<const SatParameters> input) {
148 std::vector<SatParameters> result;
149 for (const SatParameters& param : input) {
150 if (Keep(param.name())) {
151 result.push_back(param);
152 }
153 }
154 return result;
155 }
156
157 // This is just a convenient function to follow the pattern
158 // if (filter.Keep("my_name")) subsovers.Add(.... filter.LastName() ... )
159 // And not repeat "my_name" twice.
160 std::string LastName() const { return last_name_; }
161
162 // Returns the list of all ignored subsolver for use in logs.
163 const std::vector<std::string>& AllIgnored() {
165 return ignored_;
166 }
167
168 private:
169 // Copy of absl::log_internal::FNMatch().
170 bool FNMatch(absl::string_view pattern, absl::string_view str);
171
172 std::vector<std::string> filter_patterns_;
173 std::vector<std::string> ignore_patterns_;
174 std::string last_name_;
175
176 std::vector<std::string> ignored_;
177};
178
179} // namespace sat
180} // namespace operations_research
181
182#endif // OR_TOOLS_SAT_CP_MODEL_SEARCH_H_
BooleanOrIntegerLiteral GreaterOrEqual(int var, int64_t value) const
Helpers to generate a decision.
BooleanOrIntegerLiteral LowerOrEqual(int var, int64_t value) const
BooleanOrIntegerLiteral MedianValue(int var) const
int NumVariables() const
The valid indices for the calls below are in [0, num_variables).
bool IsFixed(int var) const
Getters about the current domain of the given variable.
BooleanOrIntegerLiteral RandomSplit(int var, int64_t lb, int64_t ub) const
Simple class used to filter executed subsolver names.
SubsolverNameFilter(const SatParameters &params)
Warning, params must outlive the class and be constant.
std::vector< SatParameters > Filter(absl::Span< const SatParameters > input)
Applies Keep() to all the input list.
bool Keep(absl::string_view name)
Shall we keep a parameter with given name?
const std::vector< std::string > & AllIgnored()
Returns the list of all ignored subsolver for use in logs.
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
Definition stl_util.h:58
std::vector< SatParameters > GetFullWorkerParameters(const SatParameters &base_params, const CpModelProto &cp_model, int num_already_present, SubsolverNameFilter *filter)
std::function< BooleanOrIntegerLiteral()> ConstructIntegerCompletionSearchStrategy(absl::Span< const IntegerVariable > variable_mapping, IntegerVariable objective_var, Model *model)
Constructs an integer completion search strategy.
std::function< BooleanOrIntegerLiteral()> ConstructFixedSearchStrategy(std::function< BooleanOrIntegerLiteral()> user_search, std::function< BooleanOrIntegerLiteral()> heuristic_search, std::function< BooleanOrIntegerLiteral()> integer_completion)
std::function< BooleanOrIntegerLiteral()> ConstructHintSearchStrategy(const CpModelProto &cp_model_proto, CpModelMapping *mapping, Model *model)
Constructs a search strategy that follow the hint from the model.
std::function< BooleanOrIntegerLiteral()> ConstructHeuristicSearchStrategy(const CpModelProto &cp_model_proto, Model *model)
Constructs a search strategy tailored for the current model.
std::vector< SatParameters > GetFirstSolutionBaseParams(const SatParameters &base_params)
std::vector< SatParameters > RepeatParameters(absl::Span< const SatParameters > base_params, int num_params_to_generate)
std::function< BooleanOrIntegerLiteral()> ConstructUserSearchStrategy(const CpModelProto &cp_model_proto, Model *model)
Constructs the search strategy specified in the given CpModelProto.
absl::flat_hash_map< std::string, SatParameters > GetNamedParameters(SatParameters base_params)
std::function< BooleanOrIntegerLiteral()> InstrumentSearchStrategy(const CpModelProto &cp_model_proto, absl::Span< const IntegerVariable > variable_mapping, std::function< BooleanOrIntegerLiteral()> instrumented_strategy, Model *model)
In SWIG mode, we don't want anything besides these top-level includes.
static int input(yyscan_t yyscanner)