OR-Tools  9.6
rins.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 <cmath>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <limits>
20 #include <utility>
21 #include <vector>
22 
23 #include "absl/random/bit_gen_ref.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/sat/integer.h"
27 #include "ortools/sat/model.h"
29 
30 namespace operations_research {
31 namespace sat {
32 
34  auto* lp_solutions = model->Mutable<SharedLPSolutionRepository>();
35  if (lp_solutions == nullptr) return;
36 
37  const LPVariables& lp_vars = *model->GetOrCreate<LPVariables>();
38  std::vector<double> relaxation_values(
39  lp_vars.model_vars_size, std::numeric_limits<double>::infinity());
40 
41  auto* integer_trail = model->GetOrCreate<IntegerTrail>();
42  for (const LPVariable& lp_var : lp_vars.vars) {
43  const IntegerVariable positive_var = lp_var.positive_var;
44  if (integer_trail->IsCurrentlyIgnored(positive_var)) continue;
45 
46  LinearProgrammingConstraint* lp = lp_var.lp;
47  if (lp == nullptr || !lp->HasSolution()) continue;
48 
49  relaxation_values[lp_var.model_var] = lp->GetSolutionValue(positive_var);
50  }
51  lp_solutions->NewLPSolution(std::move(relaxation_values));
52 }
53 
54 namespace {
55 
56 std::vector<double> GetLPRelaxationValues(
57  const SharedLPSolutionRepository* lp_solutions, absl::BitGenRef random) {
58  std::vector<double> relaxation_values;
59 
60  if (lp_solutions == nullptr || lp_solutions->NumSolutions() == 0) {
61  return relaxation_values;
62  }
63 
64  // TODO(user): Experiment with random biased solutions.
65  const SharedSolutionRepository<double>::Solution lp_solution =
67 
68  for (int model_var = 0; model_var < lp_solution.variable_values.size();
69  ++model_var) {
70  relaxation_values.push_back(lp_solution.variable_values[model_var]);
71  }
72  return relaxation_values;
73 }
74 
75 std::vector<double> GetGeneralRelaxationValues(
76  const SharedRelaxationSolutionRepository* relaxation_solutions,
77  absl::BitGenRef random) {
78  std::vector<double> relaxation_values;
79 
80  if (relaxation_solutions == nullptr ||
82  return relaxation_values;
83  }
84  const SharedSolutionRepository<int64_t>::Solution relaxation_solution =
86 
87  for (int model_var = 0;
88  model_var < relaxation_solution.variable_values.size(); ++model_var) {
89  relaxation_values.push_back(relaxation_solution.variable_values[model_var]);
90  }
91  return relaxation_values;
92 }
93 
94 std::vector<double> GetIncompleteSolutionValues(
95  SharedIncompleteSolutionManager* incomplete_solutions) {
96  std::vector<double> empty_solution_values;
97 
98  if (incomplete_solutions == nullptr ||
100  return empty_solution_values;
101  }
102 
104 }
105 } // namespace
106 
108  const SharedResponseManager* response_manager,
112  absl::BitGenRef random) {
113  RINSNeighborhood rins_neighborhood;
114 
115  const bool use_only_relaxation_values =
116  (response_manager == nullptr ||
117  response_manager->SolutionsRepository().NumSolutions() == 0);
118 
119  if (use_only_relaxation_values && lp_solutions == nullptr &&
120  incomplete_solutions == nullptr) {
121  // As of now RENS doesn't generate good neighborhoods from integer
122  // relaxation solutions.
123  return rins_neighborhood;
124  }
125 
126  std::vector<double> relaxation_values;
127  if (incomplete_solutions != nullptr) {
128  relaxation_values = GetIncompleteSolutionValues(incomplete_solutions);
129  } else if (lp_solutions != nullptr) {
130  relaxation_values = GetLPRelaxationValues(lp_solutions, random);
131  } else {
132  CHECK(relaxation_solutions != nullptr)
133  << "No relaxation solutions repository or lp solutions repository "
134  "provided.";
135  relaxation_values =
136  GetGeneralRelaxationValues(relaxation_solutions, random);
137  }
138  if (relaxation_values.empty()) return rins_neighborhood;
139 
140  const double tolerance = 1e-6;
142  use_only_relaxation_values
144  : response_manager->SolutionsRepository().GetRandomBiasedSolution(
145  random);
146  for (int model_var = 0; model_var < relaxation_values.size(); ++model_var) {
147  const double relaxation_value = relaxation_values[model_var];
148 
149  if (relaxation_value == std::numeric_limits<double>::infinity()) {
150  continue;
151  }
152 
153  if (use_only_relaxation_values) {
154  // The tolerance make sure that if the relaxation_value is close to an
155  // integer, then we fix the variable to this integer value.
156  //
157  // Important: the LP relaxation doesn't know about holes in the variable
158  // domains, so the intersection of [domain_lb, domain_ub] with the
159  // initial variable domain might be empty.
160  const int64_t domain_lb =
161  static_cast<int64_t>(std::floor(relaxation_value + tolerance));
162  const int64_t domain_ub =
163  static_cast<int64_t>(std::ceil(relaxation_value - tolerance));
164  if (domain_lb == domain_ub) {
165  rins_neighborhood.fixed_vars.push_back({model_var, domain_lb});
166  } else {
167  rins_neighborhood.reduced_domain_vars.push_back(
168  {model_var, {domain_lb, domain_ub}});
169  }
170 
171  } else {
172  const IntegerValue best_solution_value =
173  IntegerValue(solution.variable_values[model_var]);
174  if (std::abs(best_solution_value.value() - relaxation_value) < 1e-4) {
175  rins_neighborhood.fixed_vars.push_back(
176  {model_var, best_solution_value.value()});
177  }
178  }
179  }
180 
181  return rins_neighborhood;
182 }
183 
184 } // namespace sat
185 } // namespace operations_research
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
void NewLPSolution(std::vector< double > lp_solution)
const SharedSolutionRepository< int64_t > & SolutionsRepository() const
Solution GetRandomBiasedSolution(absl::BitGenRef random) const
SharedRelaxationSolutionRepository * relaxation_solutions
SharedLPSolutionRepository * lp_solutions
SharedIncompleteSolutionManager * incomplete_solutions
GRBmodel * model
void RecordLPRelaxationValues(Model *model)
Definition: rins.cc:33
RINSNeighborhood GetRINSNeighborhood(const SharedResponseManager *response_manager, const SharedRelaxationSolutionRepository *relaxation_solutions, const SharedLPSolutionRepository *lp_solutions, SharedIncompleteSolutionManager *incomplete_solutions, absl::BitGenRef random)
Definition: rins.cc:107
Collection of objects used to extend the Constraint Solver library.
IntegerVariable positive_var
Definition: rins.h:36
LinearProgrammingConstraint * lp
Definition: rins.h:37
std::vector< LPVariable > vars
Definition: rins.h:48
std::vector< std::pair< int, int64_t > > fixed_vars
Definition: rins.h:61
std::vector< std::pair< int, std::pair< int64_t, int64_t > > > reduced_domain_vars
Definition: rins.h:64