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