OR-Tools  8.1
bop_lns.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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_BOP_BOP_LNS_H_
15 #define OR_TOOLS_BOP_BOP_LNS_H_
16 
17 #include <string>
18 #include <vector>
19 
21 #include "ortools/base/int_type.h"
23 #include "ortools/base/logging.h"
24 #include "ortools/base/macros.h"
25 #include "ortools/base/random.h"
27 #include "ortools/bop/bop_base.h"
30 #include "ortools/bop/bop_types.h"
31 #include "ortools/bop/bop_util.h"
32 #include "ortools/glop/lp_solver.h"
34 #include "ortools/sat/sat_solver.h"
35 #include "ortools/util/stats.h"
37 
38 namespace operations_research {
39 namespace bop {
40 
41 // Uses SAT to solve the full problem under the constraint that the new solution
42 // should be under a given Hamming distance of the current solution.
44  public:
45  BopCompleteLNSOptimizer(const std::string& name,
46  const BopConstraintTerms& objective_terms);
48 
49  private:
50  bool ShouldBeRun(const ProblemState& problem_state) const final;
51  Status Optimize(const BopParameters& parameters,
52  const ProblemState& problem_state, LearnedInfo* learned_info,
53  TimeLimit* time_limit) final;
54 
55  BopOptimizerBase::Status SynchronizeIfNeeded(
56  const ProblemState& problem_state, int num_relaxed_vars);
57 
58  int64 state_update_stamp_;
59  std::unique_ptr<sat::SatSolver> sat_solver_;
60  const BopConstraintTerms& objective_terms_;
61 };
62 
63 // Interface of the different LNS neighborhood generation algorithm.
64 //
65 // NOTE(user): Using a sat_propagator as the output of the algorithm allows for
66 // a really simple and efficient interface for the generator that relies on it.
67 // However, if a generator doesn't rely on it at all, it may slow down a bit the
68 // code (to investigate). If this happens, we will probably need another
69 // function here and a way to select between which one to call.
71  public:
74 
75  // Interface for the neighborhood generation.
76  //
77  // The difficulty will be in [0, 1] and is related to the asked neighborhood
78  // size (and thus local problem difficulty). A difficulty of 0.0 means empty
79  // neighborhood and a difficulty of 1.0 means the full problem. The algorithm
80  // should try to generate an neighborhood according to this difficulty, which
81  // will be dynamically adjusted depending on whether or not we can solve the
82  // subproblem.
83  //
84  // The given sat_propagator will be reset and then configured so that all the
85  // variables propagated on its trail should be fixed. That is, the
86  // neighborhood will correspond to the unassigned variables in the
87  // sat_propagator. Note that sat_propagator_.IsModelUnsat() will be checked
88  // after this is called so it is okay to abort if this happens.
89  //
90  // Preconditions:
91  // - The given sat_propagator_ should have the current problem loaded (with
92  // the constraint to find a better solution that any current solution).
93  // - The problem state must contains a feasible solution.
94  virtual void GenerateNeighborhood(const ProblemState& problem_state,
95  double difficulty,
96  sat::SatSolver* sat_propagator) = 0;
97 };
98 
99 // A generic LNS optimizer which generates neighborhoods according to the given
100 // NeighborhoodGenerator and automatically adapt the neighborhood size depending
101 // on how easy it is to solve the associated problem.
103  public:
104  // Takes ownership of the given neighborhood_generator.
105  // The sat_propagator is assumed to contains the current problem.
106  BopAdaptiveLNSOptimizer(const std::string& name, bool use_lp_to_guide_sat,
107  NeighborhoodGenerator* neighborhood_generator,
108  sat::SatSolver* sat_propagator);
109  ~BopAdaptiveLNSOptimizer() final;
110 
111  private:
112  bool ShouldBeRun(const ProblemState& problem_state) const final;
113  Status Optimize(const BopParameters& parameters,
114  const ProblemState& problem_state, LearnedInfo* learned_info,
115  TimeLimit* time_limit) final;
116 
117  const bool use_lp_to_guide_sat_;
118  std::unique_ptr<NeighborhoodGenerator> neighborhood_generator_;
119  sat::SatSolver* const sat_propagator_;
120 
121  // Adaptive neighborhood size logic.
122  // The values are kept from one run to the next.
123  LubyAdaptiveParameterValue adaptive_difficulty_;
124 };
125 
126 // Generates a neighborhood by randomly fixing a subset of the objective
127 // variables that are currently at their lower cost.
129  public:
131  MTRandom* random)
132  : objective_terms_(*objective_terms), random_(random) {}
134 
135  private:
136  void GenerateNeighborhood(const ProblemState& problem_state,
137  double difficulty,
138  sat::SatSolver* sat_propagator) final;
139  const BopConstraintTerms& objective_terms_;
140  MTRandom* random_;
141 };
142 
143 // Generates a neighborhood by randomly selecting a subset of constraints and
144 // fixing the objective variables that are currently at their lower cost and
145 // not in the given subset of constraints.
147  public:
149  MTRandom* random)
150  : objective_terms_(*objective_terms), random_(random) {}
152 
153  private:
154  void GenerateNeighborhood(const ProblemState& problem_state,
155  double difficulty,
156  sat::SatSolver* sat_propagator) final;
157  const BopConstraintTerms& objective_terms_;
158  MTRandom* random_;
159 };
160 
161 // Generates a neighborhood by taking a random local neighborhood in an
162 // undirected graph where the nodes are the variables and two nodes are linked
163 // if they appear in the same constraint.
165  public:
166  RelationGraphBasedNeighborhood(const sat::LinearBooleanProblem& problem,
167  MTRandom* random);
169 
170  private:
171  void GenerateNeighborhood(const ProblemState& problem_state,
172  double difficulty,
173  sat::SatSolver* sat_propagator) final;
174 
175  // TODO(user): reuse by_variable_matrix_ from the LS? Note however than we
176  // don't need the coefficients here.
178  MTRandom* random_;
179 };
180 
181 } // namespace bop
182 } // namespace operations_research
183 #endif // OR_TOOLS_BOP_BOP_LNS_H_
operations_research::bop::ObjectiveBasedNeighborhood::~ObjectiveBasedNeighborhood
~ObjectiveBasedNeighborhood() final
Definition: bop_lns.h:133
operations_research::bop::RelationGraphBasedNeighborhood::RelationGraphBasedNeighborhood
RelationGraphBasedNeighborhood(const sat::LinearBooleanProblem &problem, MTRandom *random)
Definition: bop_lns.cc:508
integral_types.h
operations_research::bop::BopCompleteLNSOptimizer::~BopCompleteLNSOptimizer
~BopCompleteLNSOptimizer() final
Definition: bop_lns.cc:63
time_limit.h
operations_research::bop::BopAdaptiveLNSOptimizer::~BopAdaptiveLNSOptimizer
~BopAdaptiveLNSOptimizer() final
Definition: bop_lns.cc:225
operations_research::bop::BopOptimizerBase::name
const std::string & name() const
Definition: bop_base.h:46
operations_research::bop::LubyAdaptiveParameterValue
Definition: bop_util.h:65
operations_research::bop::RelationGraphBasedNeighborhood
Definition: bop_lns.h:164
logging.h
operations_research::sat::SatSolver
Definition: sat_solver.h:58
operations_research::bop::ObjectiveBasedNeighborhood
Definition: bop_lns.h:128
operations_research::bop::ConstraintBasedNeighborhood::~ConstraintBasedNeighborhood
~ConstraintBasedNeighborhood() final
Definition: bop_lns.h:151
macros.h
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
bop_solution.h
operations_research::bop::BopOptimizerBase
Definition: bop_base.h:40
int64
int64_t int64
Definition: integral_types.h:34
sat_solver.h
operations_research::TimeLimit
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
random.h
operations_research::bop::ConstraintBasedNeighborhood::ConstraintBasedNeighborhood
ConstraintBasedNeighborhood(const BopConstraintTerms *objective_terms, MTRandom *random)
Definition: bop_lns.h:148
stats.h
operations_research::MTRandom
Definition: random.h:55
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2103
operations_research::bop::LearnedInfo
Definition: bop_base.h:245
int_type.h
bop_types.h
operations_research::bop::ProblemState
Definition: bop_base.h:111
boolean_problem.pb.h
operations_research::bop::BopAdaptiveLNSOptimizer
Definition: bop_lns.h:102
operations_research::bop::NeighborhoodGenerator::~NeighborhoodGenerator
virtual ~NeighborhoodGenerator()
Definition: bop_lns.h:73
operations_research::bop::RelationGraphBasedNeighborhood::~RelationGraphBasedNeighborhood
~RelationGraphBasedNeighborhood() final
Definition: bop_lns.h:168
basictypes.h
bop_util.h
operations_research::bop::BopAdaptiveLNSOptimizer::BopAdaptiveLNSOptimizer
BopAdaptiveLNSOptimizer(const std::string &name, bool use_lp_to_guide_sat, NeighborhoodGenerator *neighborhood_generator, sat::SatSolver *sat_propagator)
Definition: bop_lns.cc:213
operations_research::bop::ObjectiveBasedNeighborhood::ObjectiveBasedNeighborhood
ObjectiveBasedNeighborhood(const BopConstraintTerms *objective_terms, MTRandom *random)
Definition: bop_lns.h:130
absl::StrongVector< SparseIndex, BopConstraintTerm >
operations_research::bop::BopCompleteLNSOptimizer::BopCompleteLNSOptimizer
BopCompleteLNSOptimizer(const std::string &name, const BopConstraintTerms &objective_terms)
Definition: bop_lns.cc:57
bop_parameters.pb.h
operations_research::bop::BopCompleteLNSOptimizer
Definition: bop_lns.h:43
operations_research::bop::ConstraintBasedNeighborhood
Definition: bop_lns.h:146
strong_vector.h
operations_research::bop::NeighborhoodGenerator::GenerateNeighborhood
virtual void GenerateNeighborhood(const ProblemState &problem_state, double difficulty, sat::SatSolver *sat_propagator)=0
operations_research::bop::NeighborhoodGenerator::NeighborhoodGenerator
NeighborhoodGenerator()
Definition: bop_lns.h:72
bop_base.h
operations_research::bop::NeighborhoodGenerator
Definition: bop_lns.h:70
operations_research::bop::BopOptimizerBase::Status
Status
Definition: bop_base.h:61
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:108
lp_solver.h