Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
rins.cc
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#include "ortools/sat/rins.h"
15
16#include <algorithm>
17#include <cmath>
18#include <cstdint>
19#include <limits>
20#include <memory>
21#include <random>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include "absl/log/check.h"
27#include "absl/random/bit_gen_ref.h"
28#include "absl/random/distributions.h"
29#include "absl/strings/str_cat.h"
30#include "absl/types/span.h"
34#include "ortools/sat/model.h"
36
37namespace operations_research {
38namespace sat {
39
41 auto* lp_solutions = model->Mutable<SharedLPSolutionRepository>();
42 if (lp_solutions == nullptr) return;
43
44 auto* mapping = model->GetOrCreate<CpModelMapping>();
45 auto* lp_values = model->GetOrCreate<ModelLpValues>();
46
47 // TODO(user): The default of ::infinity() for variable for which we do not
48 // have any LP solution is weird and inconsistent with ModelLpValues default
49 // which is zero. Fix. Note that in practice, at linearization level 2, all
50 // variable will eventually have an lp relaxation value, so it shoulnd't
51 // matter much to just use zero in RINS/RENS.
52 std::vector<double> relaxation_values(
53 mapping->NumProtoVariables(), std::numeric_limits<double>::infinity());
54
55 // We only loop over the positive variables.
56 const int size = lp_values->size();
57 for (IntegerVariable var(0); var < size; var += 2) {
58 const int proto_var = mapping->GetProtoVariableFromIntegerVariable(var);
59 if (proto_var != -1) {
60 relaxation_values[proto_var] = (*lp_values)[var];
61 }
62 }
63
64 lp_solutions->NewLPSolution(std::move(relaxation_values));
65}
66
67namespace {
68
69std::vector<double> GetLPRelaxationValues(
70 const SharedLPSolutionRepository* lp_solutions, absl::BitGenRef random) {
71 std::vector<double> relaxation_values;
72
73 if (lp_solutions == nullptr || lp_solutions->NumSolutions() == 0) {
74 return relaxation_values;
75 }
76
77 std::shared_ptr<const SharedSolutionRepository<double>::Solution>
78 lp_solution = lp_solutions->GetRandomBiasedSolution(random);
79
80 for (int model_var = 0; model_var < lp_solution->variable_values.size();
81 ++model_var) {
82 relaxation_values.push_back(lp_solution->variable_values[model_var]);
83 }
84 return relaxation_values;
85}
86
87std::vector<double> GetIncompleteSolutionValues(
88 SharedIncompleteSolutionManager* incomplete_solutions) {
89 std::vector<double> empty_solution_values;
90
91 if (incomplete_solutions == nullptr || !incomplete_solutions->HasSolution()) {
92 return empty_solution_values;
93 }
94
95 return incomplete_solutions->PopLast();
96}
97
98static double kEpsilon = 1e-7;
99
100struct VarWeight {
101 int model_var;
102 // Variables with minimum weight will be fixed in the neighborhood.
103 double weight;
104
105 // Comparator with tolerance and random tie breaking.
106 bool operator<(const VarWeight& o) const { return weight < o.weight; }
107};
108
109void FillRinsNeighborhood(absl::Span<const int64_t> solution,
110 absl::Span<const double> relaxation_values,
111 double difficulty, absl::BitGenRef random,
112 ReducedDomainNeighborhood& reduced_domains) {
113 std::vector<VarWeight> var_lp_gap_pairs;
114 for (int model_var = 0; model_var < relaxation_values.size(); ++model_var) {
115 const double relaxation_value = relaxation_values[model_var];
116 if (relaxation_value == std::numeric_limits<double>::infinity()) continue;
117
118 const int64_t best_solution_value = solution[model_var];
119 const double pertubation = absl::Uniform(random, -kEpsilon, kEpsilon);
120 var_lp_gap_pairs.push_back({
121 model_var,
122 std::abs(relaxation_value - static_cast<double>(best_solution_value)) +
123 pertubation,
124 });
125 }
126 std::sort(var_lp_gap_pairs.begin(), var_lp_gap_pairs.end());
127
128 const int target_size = std::min(
129 static_cast<int>(std::round(
130 static_cast<double>(relaxation_values.size()) * (1.0 - difficulty))),
131 static_cast<int>(var_lp_gap_pairs.size()));
132 for (int i = 0; i < target_size; ++i) {
133 const int model_var = var_lp_gap_pairs[i].model_var;
134 reduced_domains.fixed_vars.push_back({model_var, solution[model_var]});
135 }
136}
137
138void FillRensNeighborhood(absl::Span<const double> relaxation_values,
139 double difficulty, absl::BitGenRef random,
140 ReducedDomainNeighborhood& reduced_domains) {
141 std::vector<VarWeight> var_fractionality_pairs;
142 for (int model_var = 0; model_var < relaxation_values.size(); ++model_var) {
143 const double relaxation_value = relaxation_values[model_var];
144 if (relaxation_value == std::numeric_limits<double>::infinity()) continue;
145
146 const double pertubation = absl::Uniform(random, -kEpsilon, kEpsilon);
147 var_fractionality_pairs.push_back(
148 {model_var, std::abs(std::round(relaxation_value) - relaxation_value) +
149 pertubation});
150 }
151 std::sort(var_fractionality_pairs.begin(), var_fractionality_pairs.end());
152 const int target_size = static_cast<int>(std::round(
153 static_cast<double>(relaxation_values.size()) * (1.0 - difficulty)));
154 for (int i = 0; i < var_fractionality_pairs.size(); ++i) {
155 const int model_var = var_fractionality_pairs[i].model_var;
156 const double relaxation_value = relaxation_values[model_var];
157 if (i < target_size) {
158 // Fix the variable.
159 reduced_domains.fixed_vars.push_back(
160 {model_var, static_cast<int64_t>(std::round(relaxation_value))});
161 } else {
162 // Important: the LP relaxation doesn't know about holes in the variable
163 // domains, so the intersection of [domain_lb, domain_ub] with the
164 // initial variable domain might be empty.
165 const int64_t domain_lb =
166 static_cast<int64_t>(std::floor(relaxation_value));
167 // TODO(user): Use the domain here.
168 reduced_domains.reduced_domain_vars.push_back(
169 {model_var, {domain_lb, domain_lb + 1}});
170 }
171 }
172}
173
174} // namespace
175
177 const SharedResponseManager* response_manager,
178 const SharedLPSolutionRepository* lp_solutions,
179 SharedIncompleteSolutionManager* incomplete_solutions, double difficulty,
180 absl::BitGenRef random) {
181 ReducedDomainNeighborhood reduced_domains;
182 CHECK(lp_solutions != nullptr);
183 CHECK(incomplete_solutions != nullptr);
184 const bool lp_solution_available = lp_solutions->NumSolutions() > 0;
185 const bool incomplete_solution_available =
186 incomplete_solutions->HasSolution();
187
188 if (!lp_solution_available && !incomplete_solution_available) {
189 return reduced_domains; // Not generated.
190 }
191
192 // Using a partial LP relaxation computed by feasibility_pump, and a full lp
193 // relaxation periodically dumped by linearization=2 workers is equiprobable.
194 std::bernoulli_distribution random_bool(0.5);
195
196 const bool use_lp_relaxation =
197 lp_solution_available && incomplete_solution_available
198 ? random_bool(random)
199 : lp_solution_available;
200
201 const std::vector<double> relaxation_values =
202 use_lp_relaxation ? GetLPRelaxationValues(lp_solutions, random)
203 : GetIncompleteSolutionValues(incomplete_solutions);
204 if (relaxation_values.empty()) return reduced_domains; // Not generated.
205
206 std::bernoulli_distribution three_out_of_four(0.75);
207
208 if (response_manager != nullptr &&
209 response_manager->SolutionsRepository().NumSolutions() > 0 &&
210 three_out_of_four(random)) { // Rins.
211 std::shared_ptr<const SharedSolutionRepository<int64_t>::Solution>
212 solution =
214 random);
215 FillRinsNeighborhood(solution->variable_values, relaxation_values,
216 difficulty, random, reduced_domains);
217 reduced_domains.source_info = "rins_";
218 } else { // Rens.
219 FillRensNeighborhood(relaxation_values, difficulty, random,
220 reduced_domains);
221 reduced_domains.source_info = "rens_";
222 }
223
224 absl::StrAppend(&reduced_domains.source_info,
225 use_lp_relaxation ? "lp" : "pump", "_lns");
226 return reduced_domains;
227}
228
229} // namespace sat
230} // namespace operations_research
const SharedSolutionRepository< int64_t > & SolutionsRepository() const
std::shared_ptr< const Solution > GetRandomBiasedSolution(absl::BitGenRef random) const
Returns a random solution biased towards good solutions.
constexpr double kEpsilon
Epsilon for type Fractional, i.e. the smallest e such that 1.0 + e != 1.0 .
Definition lp_types.h:90
ReducedDomainNeighborhood GetRinsRensNeighborhood(const SharedResponseManager *response_manager, const SharedLPSolutionRepository *lp_solutions, SharedIncompleteSolutionManager *incomplete_solutions, double difficulty, absl::BitGenRef random)
Definition rins.cc:176
void RecordLPRelaxationValues(Model *model)
Adds the current LP solution to the pool.
Definition rins.cc:40
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