Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
bop_solver.h
Go to the documentation of this file.
1// Copyright 2010-2024 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
40#include "ortools/bop/bop_parameters.pb.h"
43#include "ortools/sat/boolean_problem.pb.h"
44#include "ortools/util/stats.h"
46
47namespace operations_research {
48namespace bop {
49// Solver of Boolean Optimization Problems based on Local Search.
50class BopSolver {
51 public:
52 explicit BopSolver(const sat::LinearBooleanProblem& problem);
53 virtual ~BopSolver();
54
55 // Parameters management.
56 void SetParameters(const BopParameters& parameters) {
57 parameters_ = parameters;
58 }
59
60 // Returns the status of the optimization.
62 BopSolveStatus Solve(const BopSolution& first_solution);
63
64 // Runs the solver with an external time limit.
66 BopSolveStatus SolveWithTimeLimit(const BopSolution& first_solution,
68
69 const BopSolution& best_solution() const { return problem_state_.solution(); }
70 bool GetSolutionValue(VariableIndex var_id) const {
71 return problem_state_.solution().Value(var_id);
72 }
73
74 // Returns the scaled best bound.
75 // In case of minimization (resp. maximization), the best bound is defined as
76 // the lower bound (resp. upper bound).
77 double GetScaledBestBound() const;
78 double GetScaledGap() const;
79
80 private:
81 void UpdateParameters();
82 BopSolveStatus InternalMonothreadSolver(TimeLimit* time_limit);
83 BopSolveStatus InternalMultithreadSolver(TimeLimit* time_limit);
84
85 const sat::LinearBooleanProblem& problem_;
86 ProblemState problem_state_;
87 BopParameters parameters_;
88
89 mutable StatsGroup stats_;
90};
91} // namespace bop
92} // namespace operations_research
93#endif // OR_TOOLS_BOP_BOP_SOLVER_H_
Base class to print a nice summary of a group of statistics.
Definition stats.h:128
bool Value(VariableIndex var) const
Solver of Boolean Optimization Problems based on Local Search.
Definition bop_solver.h:50
bool GetSolutionValue(VariableIndex var_id) const
Definition bop_solver.h:70
BopSolveStatus Solve()
Returns the status of the optimization.
Definition bop_solver.cc:85
void SetParameters(const BopParameters &parameters)
Parameters management.
Definition bop_solver.h:56
BopSolveStatus SolveWithTimeLimit(TimeLimit *time_limit)
Runs the solver with an external time limit.
Definition bop_solver.cc:91
const BopSolution & best_solution() const
Definition bop_solver.h:69
BopSolver(const sat::LinearBooleanProblem &problem)
Definition bop_solver.cc:75
const BopSolution & solution() const
Definition bop_base.h:205
SatParameters parameters
time_limit
Definition solve.cc:22
BopSolveStatus
Status of the solve of Bop.
Definition bop_types.h:34
In SWIG mode, we don't want anything besides these top-level includes.