OR-Tools  8.1
bop_fs.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_FS_H_
15 #define OR_TOOLS_BOP_BOP_FS_H_
16 
17 #include <string>
18 
20 #include "ortools/base/int_type.h"
22 #include "ortools/base/logging.h"
23 #include "ortools/base/macros.h"
24 #include "ortools/base/random.h"
26 #include "ortools/bop/bop_base.h"
29 #include "ortools/bop/bop_types.h"
30 #include "ortools/bop/bop_util.h"
31 #include "ortools/glop/lp_solver.h"
33 #include "ortools/sat/sat_solver.h"
35 
36 namespace operations_research {
37 namespace bop {
38 
39 // Tries to find a first solution using SAT and a given assignment preference.
40 // This optimizer will never run again once it has found a solution except if
41 // the policy is kNotGuided in which case it will be ran again.
43  public:
44  // The different guiding heuristics
45  enum class Policy {
46  kNotGuided, // The default SAT solver.
47  kLpGuided, // Guided by the values of the linear relaxation.
48  kObjectiveGuided, // Guided by the objective coefficient.
49  kUserGuided, // Guided by the problem assignment_preference().
50  };
51  GuidedSatFirstSolutionGenerator(const std::string& name, Policy policy);
53 
54  bool ShouldBeRun(const ProblemState& problem_state) const override;
55 
56  // Note that if the last call to Optimize() returned CONTINUE and if the
57  // problem didn't change, calling this will resume the solve from its last
58  // position.
59  Status Optimize(const BopParameters& parameters,
60  const ProblemState& problem_state, LearnedInfo* learned_info,
61  TimeLimit* time_limit) override;
62 
63  private:
64  BopOptimizerBase::Status SynchronizeIfNeeded(
65  const ProblemState& problem_state);
66 
67  const Policy policy_;
68  bool abort_;
69  int64 state_update_stamp_;
70  std::unique_ptr<sat::SatSolver> sat_solver_;
71 };
72 
73 // This class implements an optimizer that tries various random search
74 // strategies, each with a really low conflict limit. It can be used to generate
75 // a first solution or to improve an existing one.
76 //
77 // By opposition to all the other optimizers, this one doesn't return right away
78 // when a new solution is found. Instead, it continues to improve it as long as
79 // it has time.
80 //
81 // TODO(user): Coupled with some Local Search it might be used to diversify
82 // the solutions. To try.
84  public:
85  BopRandomFirstSolutionGenerator(const std::string& name,
86  const BopParameters& parameters,
87  sat::SatSolver* sat_propagator,
88  MTRandom* random);
90 
91  bool ShouldBeRun(const ProblemState& problem_state) const override;
92  Status Optimize(const BopParameters& parameters,
93  const ProblemState& problem_state, LearnedInfo* learned_info,
94  TimeLimit* time_limit) override;
95 
96  private:
97  BopOptimizerBase::Status SynchronizeIfNeeded(
98  const ProblemState& problem_state);
99 
100  int random_seed_;
101  MTRandom* random_;
102  sat::SatSolver* sat_propagator_;
103 };
104 
105 // This class computes the linear relaxation of the state problem.
106 // Optimize() fills the relaxed values (possibly floating values) that can be
107 // used by other optimizers as BopSatLpFirstSolutionGenerator for instance,
108 // and the lower bound.
110  public:
111  LinearRelaxation(const BopParameters& parameters, const std::string& name);
112  ~LinearRelaxation() override;
113 
114  bool ShouldBeRun(const ProblemState& problem_state) const override;
115  Status Optimize(const BopParameters& parameters,
116  const ProblemState& problem_state, LearnedInfo* learned_info,
117  TimeLimit* time_limit) override;
118 
119  private:
120  BopOptimizerBase::Status SynchronizeIfNeeded(
121  const ProblemState& problem_state);
122 
123  // Runs Glop to solve the current lp_model_.
124  // Updates the time limit and returns the status of the solve.
125  // Note that when the solve is incremental, the preprocessor is deactivated,
126  // and the dual simplex is used.
127  glop::ProblemStatus Solve(bool incremental_solve, TimeLimit* time_limit);
128 
129  // Computes and returns a better best bound using strong branching, i.e.
130  // doing a what-if analysis on each variable v: compute the best bound when
131  // v is assigned to true, compute the best bound when v is assigned to false,
132  // and then use those best bounds to improve the overall best bound.
133  // As a side effect, it might fix some variables.
134  double ComputeLowerBoundUsingStrongBranching(LearnedInfo* learned_info,
136 
137  // Returns true when the cost is worse than the cost of the current solution.
138  // If they are within the given tolerance, returns false.
139  bool CostIsWorseThanSolution(double scaled_cost, double tolerance) const;
140 
141  const BopParameters parameters_;
142  int64 state_update_stamp_;
143  bool lp_model_loaded_;
144  int num_full_solves_;
145  glop::LinearProgram lp_model_;
146  glop::LPSolver lp_solver_;
147  double scaling_;
148  double offset_;
149  int num_fixed_variables_;
150  bool problem_already_solved_;
151  double scaled_solution_cost_;
152 };
153 
154 } // namespace bop
155 } // namespace operations_research
156 #endif // OR_TOOLS_BOP_BOP_FS_H_
operations_research::bop::BopRandomFirstSolutionGenerator::Optimize
Status Optimize(const BopParameters &parameters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
Definition: bop_fs.cc:226
scaled_cost
Fractional scaled_cost
Definition: preprocessor.cc:425
integral_types.h
time_limit.h
operations_research::bop::BopOptimizerBase::name
const std::string & name() const
Definition: bop_base.h:46
operations_research::bop::GuidedSatFirstSolutionGenerator::Policy::kUserGuided
@ kUserGuided
operations_research::bop::LinearRelaxation::Optimize
Status Optimize(const BopParameters &parameters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
Definition: bop_fs.cc:442
operations_research::bop::LinearRelaxation::LinearRelaxation
LinearRelaxation(const BopParameters &parameters, const std::string &name)
Definition: bop_fs.cc:338
operations_research::bop::GuidedSatFirstSolutionGenerator::ShouldBeRun
bool ShouldBeRun(const ProblemState &problem_state) const override
Definition: bop_fs.cc:145
operations_research::bop::GuidedSatFirstSolutionGenerator::Policy::kLpGuided
@ kLpGuided
logging.h
operations_research::sat::SatSolver
Definition: sat_solver.h:58
operations_research::bop::BopRandomFirstSolutionGenerator::ShouldBeRun
bool ShouldBeRun(const ProblemState &problem_state) const override
Definition: bop_fs.cc:221
operations_research::bop::LinearRelaxation
Definition: bop_fs.h:109
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::bop::BopRandomFirstSolutionGenerator
Definition: bop_fs.h:83
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
operations_research::bop::GuidedSatFirstSolutionGenerator::~GuidedSatFirstSolutionGenerator
~GuidedSatFirstSolutionGenerator() override
Definition: bop_fs.cc:85
random.h
operations_research::MTRandom
Definition: random.h:55
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2103
operations_research::bop::BopRandomFirstSolutionGenerator::BopRandomFirstSolutionGenerator
BopRandomFirstSolutionGenerator(const std::string &name, const BopParameters &parameters, sat::SatSolver *sat_propagator, MTRandom *random)
Definition: bop_fs.cc:211
operations_research::bop::LearnedInfo
Definition: bop_base.h:245
int_type.h
bop_types.h
operations_research::bop::ProblemState
Definition: bop_base.h:111
operations_research::bop::GuidedSatFirstSolutionGenerator::GuidedSatFirstSolutionGenerator
GuidedSatFirstSolutionGenerator(const std::string &name, Policy policy)
Definition: bop_fs.cc:77
boolean_problem.pb.h
operations_research::bop::GuidedSatFirstSolutionGenerator
Definition: bop_fs.h:42
operations_research::bop::LinearRelaxation::~LinearRelaxation
~LinearRelaxation() override
Definition: bop_fs.cc:353
basictypes.h
bop_util.h
operations_research::bop::GuidedSatFirstSolutionGenerator::Policy::kObjectiveGuided
@ kObjectiveGuided
operations_research::bop::GuidedSatFirstSolutionGenerator::Policy::kNotGuided
@ kNotGuided
operations_research::bop::BopRandomFirstSolutionGenerator::~BopRandomFirstSolutionGenerator
~BopRandomFirstSolutionGenerator() override
Definition: bop_fs.cc:218
operations_research::bop::GuidedSatFirstSolutionGenerator::Policy
Policy
Definition: bop_fs.h:45
operations_research::glop::ProblemStatus
ProblemStatus
Definition: lp_types.h:101
operations_research::glop::LinearProgram
Definition: lp_data.h:55
bop_parameters.pb.h
operations_research::glop::LPSolver
Definition: lp_solver.h:29
strong_vector.h
operations_research::bop::LinearRelaxation::ShouldBeRun
bool ShouldBeRun(const ProblemState &problem_state) const override
Definition: bop_fs.cc:437
operations_research::bop::GuidedSatFirstSolutionGenerator::Optimize
Status Optimize(const BopParameters &parameters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
Definition: bop_fs.cc:158
bop_base.h
operations_research::bop::BopOptimizerBase::Status
Status
Definition: bop_base.h:61
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:108
lp_solver.h