Google OR-Tools v9.15
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 ORTOOLS_SAT_CP_MODEL_SEARCH_H_
15#define ORTOOLS_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"
29#include "ortools/sat/integer.h"
32#include "ortools/sat/model.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() if present, but is completed by a couple of
93// automatic heuristics.
95
96// For debugging fixed-search: display information about the named variables
97// domain before taking each decision. Note that we copy the instrumented
98// strategy so it doesn't have to outlive the returned functions like the other
99// arguments.
101 const CpModelProto& cp_model_proto,
102 absl::Span<const IntegerVariable> variable_mapping,
103 std::function<BooleanOrIntegerLiteral()> instrumented_strategy,
104 Model* model);
105
106// Returns all the named set of parameters known to the solver. This include our
107// default strategies like "max_lp", "core", etc... It is visible here so that
108// this can be reused by parameter validation.
109//
110// Usually, named strategies just override a few field from the base_params.
111absl::flat_hash_map<std::string, SatParameters> GetNamedParameters(
112 SatParameters base_params);
113
114// Returns a list of full workers to run.
116std::vector<SatParameters> GetFullWorkerParameters(
117 const SatParameters& base_params, const CpModelProto& cp_model,
118 int num_already_present, SubsolverNameFilter* name_filter);
119
120// Given a base set of parameter, if non-empty, this repeat them (round-robbin)
121// until we get num_params_to_generate. Note that if we don't have a multiple,
122// the first base parameters will be repeated more than the others.
123//
124// Note that this will also change the random_seed of each of these parameters.
125std::vector<SatParameters> RepeatParameters(
126 absl::Span<const SatParameters> base_params, int num_params_to_generate);
127
128// Returns a vector of base parameters to specify solvers specialized to find a
129// initial solution. This is meant to be used with RepeatParameters() and
130// FilterParameters().
131std::vector<SatParameters> GetFirstSolutionBaseParams(
132 const SatParameters& base_params);
133
134// Simple class used to filter executed subsolver names.
136 public:
137 // Warning, params must outlive the class and be constant.
138 explicit SubsolverNameFilter(const SatParameters& params);
139
140 // Shall we keep a parameter with given name?
141 bool Keep(absl::string_view name);
142
143 // Applies Keep() to all the input list.
144 std::vector<SatParameters> Filter(absl::Span<const SatParameters> input) {
145 std::vector<SatParameters> result;
146 for (const SatParameters& param : input) {
147 if (Keep(param.name())) {
148 result.push_back(param);
149 }
150 }
151 return result;
152 }
153
154 // This is just a convenient function to follow the pattern
155 // if (filter.Keep("my_name")) subsovers.Add(.... filter.LastName() ... )
156 // And not repeat "my_name" twice.
157 std::string LastName() const { return last_name_; }
158
159 // Returns the list of all ignored subsolver for use in logs.
160 const std::vector<std::string>& AllIgnored() {
162 return ignored_;
163 }
164
165 private:
166 // Copy of absl::log_internal::FNMatch().
167 bool FNMatch(absl::string_view pattern, absl::string_view str);
168
169 std::vector<std::string> filter_patterns_;
170 std::vector<std::string> ignore_patterns_;
171 std::string last_name_;
172
173 std::vector<std::string> ignored_;
174};
175
176} // namespace sat
177} // namespace operations_research
178
179#endif // ORTOOLS_SAT_CP_MODEL_SEARCH_H_
BooleanOrIntegerLiteral GreaterOrEqual(int var, int64_t value) const
BooleanOrIntegerLiteral LowerOrEqual(int var, int64_t value) const
BooleanOrIntegerLiteral MedianValue(int var) const
BooleanOrIntegerLiteral RandomSplit(int var, int64_t lb, int64_t ub) const
std::vector< SatParameters > Filter(absl::Span< const SatParameters > input)
const std::vector< std::string > & AllIgnored()
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
Definition stl_util.h:55
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)
std::function< BooleanOrIntegerLiteral()> ConstructHintSearchStrategy(const CpModelProto &cp_model_proto, CpModelMapping *mapping, Model *model)
std::function< BooleanOrIntegerLiteral()> ConstructHeuristicSearchStrategy(const CpModelProto &cp_model_proto, Model *model)
std::vector< SatParameters > GetFirstSolutionBaseParams(const SatParameters &base_params)
void ConstructFixedSearchStrategy(SearchHeuristics *h, Model *model)
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)
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)
OR-Tools root namespace.
static int input(yyscan_t yyscanner)