Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
shaving_solver.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_SHAVING_SOLVER_H_
15#define OR_TOOLS_SAT_SHAVING_SOLVER_H_
16
17#include <atomic>
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "absl/base/thread_annotations.h"
25#include "absl/synchronization/mutex.h"
26#include "ortools/sat/cp_model.pb.h"
29#include "ortools/sat/integer.h"
30#include "ortools/sat/model.h"
31#include "ortools/sat/sat_parameters.pb.h"
34
35namespace operations_research {
36namespace sat {
37
39 public:
40 ObjectiveShavingSolver(const SatParameters& local_parameters,
42 SharedClasses* shared);
43
44 ~ObjectiveShavingSolver() override;
45
46 bool TaskIsAvailable() override;
47
48 std::function<void()> GenerateTask(int64_t task_id) override;
49 void Synchronize() override;
50
51 private:
52 std::string Info();
53
54 bool ResetModel(int64_t task_id);
55
56 // This is fixed at construction.
57 SatParameters local_params_;
59 SharedClasses* shared_;
60
61 // Allow to control the local time limit in addition to a potential user
62 // defined external Boolean.
63 std::atomic<bool> stop_current_chunk_;
64
65 // Local singleton repository and presolved local model.
66 std::unique_ptr<Model> local_sat_model_;
67 CpModelProto local_proto_;
68
69 // For postsolving a feasible solution or improving objective lb.
70 std::vector<int> postsolve_mapping_;
71 CpModelProto mapping_proto_;
72
73 absl::Mutex mutex_;
74 IntegerValue objective_lb_ ABSL_GUARDED_BY(mutex_);
75 IntegerValue objective_ub_ ABSL_GUARDED_BY(mutex_);
76 IntegerValue current_objective_target_ub_ ABSL_GUARDED_BY(mutex_);
77 bool task_in_flight_ ABSL_GUARDED_BY(mutex_) = false;
78};
79
81 public:
87
88 VariablesShavingSolver(const SatParameters& local_parameters,
89 SharedClasses* shared);
90
91 ~VariablesShavingSolver() override;
92
93 bool TaskIsAvailable() override;
94
95 void ProcessLocalResponse(const CpSolverResponse& local_response,
96 const State& state);
97
98 std::function<void()> GenerateTask(int64_t task_id) override;
99
100 void Synchronize() override;
101
102 private:
103 std::string Info();
104
105 int64_t DomainSize(int var) const ABSL_SHARED_LOCKS_REQUIRED(mutex_);
106
107 bool VarIsFixed(int int_var) const ABSL_SHARED_LOCKS_REQUIRED(mutex_);
108
109 bool ConstraintIsInactive(int c) const ABSL_SHARED_LOCKS_REQUIRED(mutex_);
110
111 bool FindNextVar(State* state) ABSL_SHARED_LOCKS_REQUIRED(mutex_);
112
113 void CopyModelConnectedToVar(State* state, Model* local_sat_model,
114 CpModelProto* shaving_proto)
115 ABSL_SHARED_LOCKS_REQUIRED(mutex_);
116
117 bool ResetModel(int64_t task_id, State* state, Model* local_sat_model,
118 CpModelProto* shaving_proto);
119
120 // This is fixed at construction.
121 SatParameters local_params_;
122 SharedClasses* shared_;
123 int shared_bounds_id_ = -1;
124
125 // Allow to control the local time limit in addition to a potential user
126 // defined external Boolean.
127 std::atomic<bool> stop_current_chunk_;
128
129 const CpModelProto& model_proto_;
130
131 absl::Mutex mutex_;
132 int current_index_ = -1;
133 std::vector<Domain> var_domains_ ABSL_GUARDED_BY(mutex_);
134
135 // Stats.
136 int num_vars_tried_ ABSL_GUARDED_BY(mutex_) = 0;
137 int num_vars_shaved_ ABSL_GUARDED_BY(mutex_) = 0;
138 int num_infeasible_found_ ABSL_GUARDED_BY(mutex_) = 0;
139};
140
141} // namespace sat
142} // namespace operations_research
143
144#endif // OR_TOOLS_SAT_SHAVING_SOLVER_H_
ObjectiveShavingSolver(const SatParameters &local_parameters, NeighborhoodGeneratorHelper *helper, SharedClasses *shared)
std::function< void()> GenerateTask(int64_t task_id) override
VariablesShavingSolver(const SatParameters &local_parameters, SharedClasses *shared)
std::function< void()> GenerateTask(int64_t task_id) override
void ProcessLocalResponse(const CpSolverResponse &local_response, const State &state)
IntVar * var
In SWIG mode, we don't want anything besides these top-level includes.