OR-Tools  8.1
bop_solver.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_SOLVER_H_
15 #define OR_TOOLS_BOP_BOP_SOLVER_H_
16 
17 // Solver for Boolean Optimization Problems built on top of the SAT solver.
18 // To optimize a problem the solver uses several optimization strategies like
19 // Local Search (LS), Large Neighborhood Search (LNS), and Linear
20 // Programming (LP). See bop_parameters.proto to tune the strategies.
21 //
22 // Note that the BopSolver usage is limited to:
23 // - Boolean variables,
24 // - Linear constraints and linear optimization objective,
25 // - Integral weights for both constraints and objective,
26 // - Minimization.
27 // To deal with maximization, integral variables and floating weights, one can
28 // use the bop::IntegralSolver.
29 //
30 // Usage example:
31 // const LinearBooleanProblem problem = BuildProblem();
32 // BopSolver bop_solver(problem);
33 // BopParameters bop_parameters;
34 // bop_parameters.set_max_deterministic_time(10);
35 // bop_solver.SetParameters(bop_parameters);
36 // const BopSolveStatus solve_status = bop_solver.Solve();
37 // if (solve_status == BopSolveStatus::OPTIMAL_SOLUTION_FOUND) { ... }
38 
39 #include <string>
40 #include <vector>
41 
43 #include "ortools/base/int_type.h"
45 #include "ortools/base/logging.h"
46 #include "ortools/base/macros.h"
48 #include "ortools/bop/bop_base.h"
51 #include "ortools/bop/bop_types.h"
52 #include "ortools/glop/lp_solver.h"
54 #include "ortools/sat/sat_solver.h"
55 #include "ortools/util/stats.h"
57 
58 namespace operations_research {
59 namespace bop {
60 // Solver of Boolean Optimization Problems based on Local Search.
61 class BopSolver {
62  public:
63  explicit BopSolver(const sat::LinearBooleanProblem& problem);
64  virtual ~BopSolver();
65 
66  // Parameters management.
67  void SetParameters(const BopParameters& parameters) {
68  parameters_ = parameters;
69  }
70 
71  // Returns the status of the optimization.
73  BopSolveStatus Solve(const BopSolution& first_solution);
74 
75  // Runs the solver with an external time limit.
77  BopSolveStatus SolveWithTimeLimit(const BopSolution& first_solution,
79 
80  const BopSolution& best_solution() const { return problem_state_.solution(); }
81  bool GetSolutionValue(VariableIndex var_id) const {
82  return problem_state_.solution().Value(var_id);
83  }
84 
85  // Returns the scaled best bound.
86  // In case of minimization (resp. maximization), the best bound is defined as
87  // the lower bound (resp. upper bound).
88  double GetScaledBestBound() const;
89  double GetScaledGap() const;
90 
91  private:
92  void UpdateParameters();
93  BopSolveStatus InternalMonothreadSolver(TimeLimit* time_limit);
94  BopSolveStatus InternalMultithreadSolver(TimeLimit* time_limit);
95 
96  const sat::LinearBooleanProblem& problem_;
97  ProblemState problem_state_;
98  BopParameters parameters_;
99 
100  mutable StatsGroup stats_;
101 };
102 } // namespace bop
103 } // namespace operations_research
104 #endif // OR_TOOLS_BOP_BOP_SOLVER_H_
operations_research::bop::BopSolver::GetScaledGap
double GetScaledGap() const
Definition: bop_solver.cc:180
integral_types.h
time_limit.h
operations_research::bop::ProblemState::solution
const BopSolution & solution() const
Definition: bop_base.h:193
operations_research::StatsGroup
Definition: stats.h:131
operations_research::bop::BopSolver::BopSolver
BopSolver(const sat::LinearBooleanProblem &problem)
Definition: bop_solver.cc:70
operations_research::bop::BopSolver::Solve
BopSolveStatus Solve()
Definition: bop_solver.cc:80
logging.h
operations_research::bop::BopSolver::SetParameters
void SetParameters(const BopParameters &parameters)
Definition: bop_solver.h:67
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::BopSolver::GetSolutionValue
bool GetSolutionValue(VariableIndex var_id) const
Definition: bop_solver.h:81
sat_solver.h
operations_research::bop::BopSolution::Value
bool Value(VariableIndex var) const
Definition: bop_solution.h:43
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::BopSolution
Definition: bop_solution.h:31
stats.h
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2103
int_type.h
bop_types.h
operations_research::bop::ProblemState
Definition: bop_base.h:111
boolean_problem.pb.h
operations_research::bop::BopSolver::best_solution
const BopSolution & best_solution() const
Definition: bop_solver.h:80
basictypes.h
operations_research::bop::BopSolver::GetScaledBestBound
double GetScaledBestBound() const
Definition: bop_solver.cc:175
operations_research::bop::BopSolver
Definition: bop_solver.h:61
bop_parameters.pb.h
strong_vector.h
operations_research::bop::BopSolver::~BopSolver
virtual ~BopSolver()
Definition: bop_solver.cc:78
operations_research::bop::BopSolver::SolveWithTimeLimit
BopSolveStatus SolveWithTimeLimit(TimeLimit *time_limit)
Definition: bop_solver.cc:86
bop_base.h
operations_research::bop::BopSolveStatus
BopSolveStatus
Definition: bop_types.h:31
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:108
lp_solver.h