Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model_solver_helpers.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_SOLVER_HELPERS_H_
15#define ORTOOLS_SAT_CP_MODEL_SOLVER_HELPERS_H_
16
17#include <cstdint>
18#include <memory>
19#include <tuple>
20#include <vector>
21
22#include "absl/types/span.h"
23#include "ortools/base/timer.h"
27#include "ortools/sat/model.h"
31#include "ortools/sat/util.h"
34
35namespace operations_research {
36namespace sat {
37
38// Small wrapper containing all the shared classes between our subsolver
39// threads. Note that all these classes can also be retrieved with something
40// like global_model->GetOrCreate<Class>() but it is not thread-safe to do so.
41//
42// All the classes here should be thread-safe, or at least safe in the way they
43// are accessed. For instance the model_proto will be kept constant for the
44// whole duration of the solve.
46 SharedClasses(const CpModelProto* proto, Model* global_model);
47
48 // These are never nullptr.
60
61 // These can be nullptr depending on the options.
62 std::unique_ptr<SharedBoundsManager> bounds;
63 std::unique_ptr<SharedLPSolutionRepository> lp_solutions;
64 std::unique_ptr<SharedIncompleteSolutionManager> incomplete_solutions;
65 std::unique_ptr<SharedClausesManager> clauses;
66 std::unique_ptr<SharedLinear2Bounds> linear2_bounds;
67
68 // call local_model->Register() on most of the class here, this allow to
69 // more easily depends on one of the shared class deep within the solver.
71
72 bool SearchIsDone();
73
74 void LogFinalStatistics();
75};
76
77// Loads a CpModelProto inside the given model.
78// This should only be called once on a given 'Model' class.
79void LoadCpModel(const CpModelProto& model_proto, Model* model);
80
81// Solves an already loaded cp_model_proto.
82// The final CpSolverResponse must be read from the shared_response_manager.
83//
84// TODO(user): This should be transformed so that it can be called many times
85// and resume from the last search state as if it wasn't interrupted. That would
86// allow use to easily interleave different heuristics in the same thread.
87void SolveLoadedCpModel(const CpModelProto& model_proto, Model* model);
88
89// Registers a callback that will export variables bounds fixed at level 0 of
90// the search. This should not be registered to a LNS search.
92 const CpModelProto& /*model_proto*/,
93 SharedBoundsManager* shared_bounds_manager, Model* model);
94
95// Registers a callback to import new variables bounds stored in the
96// shared_bounds_manager. These bounds are imported at level 0 of the search
97// in the linear scan minimize function.
99 const CpModelProto& model_proto, SharedBoundsManager* shared_bounds_manager,
100 Model* model);
101
102// Registers a callback that will report improving objective best bound.
103// It will be called each time new objective bound are propagated at level zero.
105 IntegerVariable objective_var,
106 SharedResponseManager* shared_response_manager, Model* model);
107
108// Registers a callback to import new objective bounds. It will be called each
109// time the search main loop is back to level zero. Note that it the presence of
110// assumptions, this will not happen until the set of assumptions is changed.
112 SharedResponseManager* shared_response_manager, Model* model);
113
114// Registers a callback that will export good clauses discovered during search.
115void RegisterClausesExport(int id, SharedClausesManager* shared_clauses_manager,
116 Model* model);
117
118// Registers a callback to import new clauses stored in the
119// shared_clausess_manager. These clauses are imported at level 0 of the search
120// in the linear scan minimize function.
121// it returns the id of the worker in the shared clause manager.
122//
123// TODO(user): Can we import them in the core worker ?
125 SharedClausesManager* shared_clauses_manager,
126 Model* model);
127
128// This will register a level zero callback to imports new linear2 from the
129// SharedLinear2Bounds.
130void RegisterLinear2BoundsImport(SharedLinear2Bounds* shared_linear2_bounds,
131 Model* model);
132
133void PostsolveResponseWrapper(const SatParameters& params,
134 int num_variable_in_original_model,
135 const CpModelProto& mapping_proto,
136 absl::Span<const int> postsolve_mapping,
137 std::vector<int64_t>* solution);
138
139// Try to find a solution by following the hint and using a low conflict limit.
140// The CpModelProto must already be loaded in the Model.
141void QuickSolveWithHint(const CpModelProto& model_proto, Model* model);
142
143// Solve a model with a different objective consisting of minimizing the L1
144// distance with the provided hint. Note that this method creates an in-memory
145// copy of the model and loads a local Model object from the copied model.
146void MinimizeL1DistanceWithHint(const CpModelProto& model_proto, Model* model);
147
148void LoadFeasibilityPump(const CpModelProto& model_proto, Model* model);
149
150void AdaptGlobalParameters(const CpModelProto& model_proto, Model* model);
151
152// This should be called on the presolved model. It will read the file
153// specified by --cp_model_load_debug_solution and properly fill the
154// model->Get<DebugSolution>() proto vector.
155void LoadDebugSolution(const CpModelProto& model_proto, Model* model);
156
157} // namespace sat
158} // namespace operations_research
159
160#endif // ORTOOLS_SAT_CP_MODEL_SOLVER_HELPERS_H_
void SolveLoadedCpModel(const CpModelProto &model_proto, Model *model)
void RegisterObjectiveBoundsImport(SharedResponseManager *shared_response_manager, Model *model)
void RegisterClausesExport(int id, SharedClausesManager *shared_clauses_manager, Model *model)
void MinimizeL1DistanceWithHint(const CpModelProto &model_proto, Model *model)
void LoadFeasibilityPump(const CpModelProto &model_proto, Model *model)
void AdaptGlobalParameters(const CpModelProto &model_proto, Model *model)
void QuickSolveWithHint(const CpModelProto &model_proto, Model *model)
void LoadDebugSolution(const CpModelProto &model_proto, Model *model)
void RegisterVariableBoundsLevelZeroExport(const CpModelProto &, SharedBoundsManager *shared_bounds_manager, Model *model)
void PostsolveResponseWrapper(const SatParameters &params, int num_variable_in_original_model, const CpModelProto &mapping_proto, absl::Span< const int > postsolve_mapping, std::vector< int64_t > *solution)
void RegisterVariableBoundsLevelZeroImport(const CpModelProto &model_proto, SharedBoundsManager *shared_bounds_manager, Model *model)
void LoadCpModel(const CpModelProto &model_proto, Model *model)
void RegisterObjectiveBestBoundExport(IntegerVariable objective_var, SharedResponseManager *shared_response_manager, Model *model)
void RegisterLinear2BoundsImport(SharedLinear2Bounds *shared_linear2_bounds, Model *model)
int RegisterClausesLevelZeroImport(int id, SharedClausesManager *shared_clauses_manager, Model *model)
OR-Tools root namespace.
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
SharedLsSolutionRepository *const ls_hints
std::unique_ptr< SharedIncompleteSolutionManager > incomplete_solutions
std::unique_ptr< SharedClausesManager > clauses
SharedClasses(const CpModelProto *proto, Model *global_model)
std::unique_ptr< SharedLPSolutionRepository > lp_solutions
std::unique_ptr< SharedLinear2Bounds > linear2_bounds
std::unique_ptr< SharedBoundsManager > bounds