Google OR-Tools v9.15
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-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_SHAVING_SOLVER_H_
15#define ORTOOLS_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 "google/protobuf/arena.h"
31#include "ortools/sat/model.h"
35
36namespace operations_research {
37namespace sat {
38
40 public:
41 ObjectiveShavingSolver(const SatParameters& local_parameters,
43 SharedClasses* shared);
44
45 ~ObjectiveShavingSolver() override;
46
47 bool TaskIsAvailable() override;
48
49 std::function<void()> GenerateTask(int64_t task_id) override;
50 void Synchronize() override;
51
52 private:
53 std::string Info();
54
55 void ResetModel();
56 bool ResetAndSolveModel(int64_t task_id);
57
58 // This is fixed at construction.
59 SatParameters local_params_;
61 SharedClasses* shared_;
62
63 // Allow to control the local time limit in addition to a potential user
64 // defined external Boolean.
65 std::atomic<bool> stop_current_chunk_;
66
67 // Local singleton repository and presolved local model.
68 std::unique_ptr<Model> local_sat_model_;
69 std::unique_ptr<google::protobuf::Arena> arena_;
70 CpModelProto* local_proto_;
71
72 // For postsolving a feasible solution or improving objective lb.
73 std::vector<int> postsolve_mapping_;
74 CpModelProto mapping_proto_;
75
76 absl::Mutex mutex_;
77 IntegerValue objective_lb_ ABSL_GUARDED_BY(mutex_);
78 IntegerValue objective_ub_ ABSL_GUARDED_BY(mutex_);
79 IntegerValue current_objective_target_ub_ ABSL_GUARDED_BY(mutex_);
80 bool task_in_flight_ ABSL_GUARDED_BY(mutex_) = false;
81};
82
84 public:
85 struct State {
88
89 // We have two modes:
90 // - When "shave_using_objective" is true, we shave by minimizing the value
91 // of a variable.
92 // - When false, we restrict its domain and detect feasible/infeasible.
95 };
96
97 VariablesShavingSolver(const SatParameters& local_parameters,
99 SharedClasses* shared);
100
101 ~VariablesShavingSolver() override;
102
103 bool TaskIsAvailable() override;
104
105 void ProcessLocalResponse(const CpSolverResponse& local_response,
106 const State& state);
107
108 std::function<void()> GenerateTask(int64_t task_id) override;
109
110 void Synchronize() override;
111
112 private:
113 std::string Info();
114
115 int64_t DomainSize(int var) const ABSL_SHARED_LOCKS_REQUIRED(mutex_);
116
117 bool VarIsFixed(int int_var) const ABSL_SHARED_LOCKS_REQUIRED(mutex_);
118
119 bool ConstraintIsInactive(int c) const ABSL_SHARED_LOCKS_REQUIRED(mutex_);
120
121 bool FindNextVar(State* state) ABSL_SHARED_LOCKS_REQUIRED(mutex_);
122
123 void CopyModelConnectedToVar(State* state, Model* local_model,
124 CpModelProto* shaving_proto,
125 bool* has_no_overlap_2d)
126 ABSL_SHARED_LOCKS_REQUIRED(mutex_);
127
128 bool ResetAndSolveModel(int64_t task_id, State* state, Model* local_model,
129 CpModelProto* shaving_proto);
130
131 // This is fixed at construction.
132 SatParameters local_params_;
133 SharedClasses* shared_;
134 int shared_bounds_id_ = -1;
135
136 // Allow to control the local time limit in addition to a potential user
137 // defined external Boolean.
138 std::atomic<bool> stop_current_chunk_;
139
140 const CpModelProto& model_proto_;
141
142 absl::Mutex mutex_;
143 int64_t current_index_ = -1;
144 std::vector<Domain> var_domains_ ABSL_GUARDED_BY(mutex_);
145
146 // Stats.
147 int num_vars_tried_ ABSL_GUARDED_BY(mutex_) = 0;
148 int num_vars_shaved_ ABSL_GUARDED_BY(mutex_) = 0;
149 int num_infeasible_found_ ABSL_GUARDED_BY(mutex_) = 0;
150};
151
152} // namespace sat
153} // namespace operations_research
154
155#endif // ORTOOLS_SAT_SHAVING_SOLVER_H_
ObjectiveShavingSolver(const SatParameters &local_parameters, NeighborhoodGeneratorHelper *helper, SharedClasses *shared)
std::function< void()> GenerateTask(int64_t task_id) override
SubSolver(absl::string_view name, SubsolverType type)
Definition subsolver.h:48
std::function< void()> GenerateTask(int64_t task_id) override
VariablesShavingSolver(const SatParameters &local_parameters, NeighborhoodGeneratorHelper *helper, SharedClasses *shared)
void ProcessLocalResponse(const CpSolverResponse &local_response, const State &state)
OR-Tools root namespace.