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