Google OR-Tools v9.12
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 OR_TOOLS_SAT_CP_MODEL_SOLVER_HELPERS_H_
15#define OR_TOOLS_SAT_CP_MODEL_SOLVER_HELPERS_H_
16
17#include <cstdint>
18#include <memory>
19#include <string>
20#include <tuple>
21#include <utility>
22#include <vector>
23
24#include "absl/flags/declare.h"
25#include "absl/types/span.h"
26#include "ortools/base/timer.h"
27#include "ortools/sat/cp_model.pb.h"
29#include "ortools/sat/model.h"
30#include "ortools/sat/sat_parameters.pb.h"
33#include "ortools/sat/util.h"
36
37ABSL_DECLARE_FLAG(bool, cp_model_dump_models);
38ABSL_DECLARE_FLAG(std::string, cp_model_dump_prefix);
39ABSL_DECLARE_FLAG(bool, cp_model_dump_problematic_lns);
40ABSL_DECLARE_FLAG(bool, cp_model_dump_submodels);
41
42namespace operations_research {
43namespace sat {
44
45// Small wrapper containing all the shared classes between our subsolver
46// threads. Note that all these classes can also be retrieved with something
47// like global_model->GetOrCreate<Class>() but it is not thread-safe to do so.
48//
49// All the classes here should be thread-safe, or at least safe in the way they
50// are accessed. For instance the model_proto will be kept constant for the
51// whole duration of the solve.
53 SharedClasses(const CpModelProto* proto, Model* global_model);
54
55 // These are never nullptr.
56 const CpModelProto& model_proto;
65
66 // These can be nullptr depending on the options.
67 std::unique_ptr<SharedBoundsManager> bounds;
68 std::unique_ptr<SharedLPSolutionRepository> lp_solutions;
69 std::unique_ptr<SharedIncompleteSolutionManager> incomplete_solutions;
70 std::unique_ptr<SharedClausesManager> clauses;
71
72 // call local_model->Register() on most of the class here, this allow to
73 // more easily depends on one of the shared class deep within the solver.
75
76 bool SearchIsDone();
77};
78
79// Loads a CpModelProto inside the given model.
80// This should only be called once on a given 'Model' class.
81void LoadCpModel(const CpModelProto& model_proto, Model* model);
82
83// Solves an already loaded cp_model_proto.
84// The final CpSolverResponse must be read from the shared_response_manager.
85//
86// TODO(user): This should be transformed so that it can be called many times
87// and resume from the last search state as if it wasn't interrupted. That would
88// allow use to easily interleave different heuristics in the same thread.
89void SolveLoadedCpModel(const CpModelProto& model_proto, Model* model);
90
91// Registers a callback that will export variables bounds fixed at level 0 of
92// the search. This should not be registered to a LNS search.
94 const CpModelProto& /*model_proto*/,
95 SharedBoundsManager* shared_bounds_manager, Model* model);
96
97// Registers a callback to import new variables bounds stored in the
98// shared_bounds_manager. These bounds are imported at level 0 of the search
99// in the linear scan minimize function.
101 const CpModelProto& model_proto, SharedBoundsManager* shared_bounds_manager,
102 Model* model);
103
104// Registers a callback that will report improving objective best bound.
105// It will be called each time new objective bound are propagated at level zero.
107 IntegerVariable objective_var,
108 SharedResponseManager* shared_response_manager, Model* model);
109
110// Registers a callback to import new objective bounds. It will be called each
111// time the search main loop is back to level zero. Note that it the presence of
112// assumptions, this will not happen until the set of assumptions is changed.
114 SharedResponseManager* shared_response_manager, Model* model);
115
116// Registers a callback that will export good clauses discovered during search.
117void RegisterClausesExport(int id, SharedClausesManager* shared_clauses_manager,
118 Model* model);
119
120// Registers a callback to import new clauses stored in the
121// shared_clausess_manager. These clauses are imported at level 0 of the search
122// in the linear scan minimize function.
123// it returns the id of the worker in the shared clause manager.
124//
125// TODO(user): Can we import them in the core worker ?
127 SharedClausesManager* shared_clauses_manager,
128 Model* model);
129
130void PostsolveResponseWrapper(const SatParameters& params,
131 int num_variable_in_original_model,
132 const CpModelProto& mapping_proto,
133 absl::Span<const int> postsolve_mapping,
134 std::vector<int64_t>* solution);
135
136// Try to find a solution by following the hint and using a low conflict limit.
137// The CpModelProto must already be loaded in the Model.
138void QuickSolveWithHint(const CpModelProto& model_proto, Model* model);
139
140// Solve a model with a different objective consisting of minimizing the L1
141// distance with the provided hint. Note that this method creates an in-memory
142// copy of the model and loads a local Model object from the copied model.
143void MinimizeL1DistanceWithHint(const CpModelProto& model_proto, Model* model);
144
145void LoadFeasibilityPump(const CpModelProto& model_proto, Model* model);
146
147void AdaptGlobalParameters(const CpModelProto& model_proto, Model* model);
148
149// This should be called on the presolved model. It will read the file
150// specified by --cp_model_load_debug_solution and properly fill the
151// model->Get<DebugSolution>() proto vector.
152void LoadDebugSolution(const CpModelProto& model_proto, Model* model);
153
154} // namespace sat
155} // namespace operations_research
156
157#endif // OR_TOOLS_SAT_CP_MODEL_SOLVER_HELPERS_H_
The model "singleton" shared time limit.
Definition util.h:345
Contains the table we display after the solver is done.
Definition stat_tables.h:33
Simple class to add statistics by name and print them at the end.
ABSL_DECLARE_FLAG(bool, cp_model_dump_models)
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)
Registers a callback that will export good clauses discovered during search.
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)
int RegisterClausesLevelZeroImport(int id, SharedClausesManager *shared_clauses_manager, Model *model)
In SWIG mode, we don't want anything besides these top-level includes.
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
const CpModelProto & model_proto
These are never nullptr.
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< SharedBoundsManager > bounds
These can be nullptr depending on the options.