Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
bop_fs.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_FS_H_
15#define OR_TOOLS_BOP_BOP_FS_H_
16
17#include <cstdint>
18#include <memory>
19
20#include "absl/random/bit_gen_ref.h"
21#include "absl/strings/string_view.h"
23#include "ortools/bop/bop_parameters.pb.h"
29
30namespace operations_research {
31namespace bop {
32
33// Tries to find a first solution using SAT and a given assignment preference.
34// This optimizer will never run again once it has found a solution except if
35// the policy is kNotGuided in which case it will be ran again.
37 public:
38 // The different guiding heuristics
39 enum class Policy {
40 kNotGuided, // The default SAT solver.
41 kLpGuided, // Guided by the values of the linear relaxation.
42 kObjectiveGuided, // Guided by the objective coefficient.
43 kUserGuided, // Guided by the problem assignment_preference().
44 };
45 GuidedSatFirstSolutionGenerator(absl::string_view name, Policy policy);
47
48 bool ShouldBeRun(const ProblemState& problem_state) const override;
49
50 // Note that if the last call to Optimize() returned CONTINUE and if the
51 // problem didn't change, calling this will resume the solve from its last
52 // position.
53 Status Optimize(const BopParameters& parameters,
54 const ProblemState& problem_state, LearnedInfo* learned_info,
55 TimeLimit* time_limit) override;
56
57 private:
58 BopOptimizerBase::Status SynchronizeIfNeeded(
59 const ProblemState& problem_state);
60
61 const Policy policy_;
62 bool abort_;
63 int64_t state_update_stamp_;
64 std::unique_ptr<sat::SatSolver> sat_solver_;
65};
66
67// This class implements an optimizer that tries various random search
68// strategies, each with a really low conflict limit. It can be used to generate
69// a first solution or to improve an existing one.
70//
71// By opposition to all the other optimizers, this one doesn't return right away
72// when a new solution is found. Instead, it continues to improve it as long as
73// it has time.
74//
75// TODO(user): Coupled with some Local Search it might be used to diversify
76// the solutions. To try.
78 public:
79 BopRandomFirstSolutionGenerator(absl::string_view name,
80 const BopParameters& parameters,
81 sat::SatSolver* sat_propagator,
82 absl::BitGenRef random);
84
85 bool ShouldBeRun(const ProblemState& problem_state) const override;
86 Status Optimize(const BopParameters& parameters,
87 const ProblemState& problem_state, LearnedInfo* learned_info,
88 TimeLimit* time_limit) override;
89
90 private:
91 BopOptimizerBase::Status SynchronizeIfNeeded(
92 const ProblemState& problem_state);
93
94 int random_seed_;
95 absl::BitGenRef random_;
96 sat::SatSolver* sat_propagator_;
97};
98
99// This class computes the linear relaxation of the state problem.
100// Optimize() fills the relaxed values (possibly floating values) that can be
101// used by other optimizers as BopSatLpFirstSolutionGenerator for instance,
102// and the lower bound.
104 public:
105 LinearRelaxation(const BopParameters& parameters, absl::string_view name);
107
108 bool ShouldBeRun(const ProblemState& problem_state) const override;
109 Status Optimize(const BopParameters& parameters,
110 const ProblemState& problem_state, LearnedInfo* learned_info,
111 TimeLimit* time_limit) override;
112
113 private:
114 BopOptimizerBase::Status SynchronizeIfNeeded(
115 const ProblemState& problem_state);
116
117 // Runs Glop to solve the current lp_model_.
118 // Updates the time limit and returns the status of the solve.
119 // Note that when the solve is incremental, the preprocessor is deactivated,
120 // and the dual simplex is used.
121 glop::ProblemStatus Solve(bool incremental_solve, TimeLimit* time_limit);
122
123 // Computes and returns a better best bound using strong branching, i.e.
124 // doing a what-if analysis on each variable v: compute the best bound when
125 // v is assigned to true, compute the best bound when v is assigned to false,
126 // and then use those best bounds to improve the overall best bound.
127 // As a side effect, it might fix some variables.
128 double ComputeLowerBoundUsingStrongBranching(LearnedInfo* learned_info,
130
131 // Returns true when the cost is worse than the cost of the current solution.
132 // If they are within the given tolerance, returns false.
133 bool CostIsWorseThanSolution(double scaled_cost, double tolerance) const;
134
135 const BopParameters parameters_;
136 int64_t state_update_stamp_;
137 bool lp_model_loaded_;
138 int num_full_solves_;
139 glop::LinearProgram lp_model_;
140 glop::LPSolver lp_solver_;
141 double scaling_;
142 double offset_;
143 int num_fixed_variables_;
144 bool problem_already_solved_;
145 double scaled_solution_cost_;
146};
147
148} // namespace bop
149} // namespace operations_research
150#endif // OR_TOOLS_BOP_BOP_FS_H_
const std::string & name() const
Returns the name given at construction.
Definition bop_base.h:54
BopRandomFirstSolutionGenerator(absl::string_view name, const BopParameters &parameters, sat::SatSolver *sat_propagator, absl::BitGenRef random)
Definition bop_fs.cc:236
Status Optimize(const BopParameters &parameters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
Definition bop_fs.cc:251
bool ShouldBeRun(const ProblemState &problem_state) const override
Only run the RandomFirstSolution when there is an objective to minimize.
Definition bop_fs.cc:246
Status Optimize(const BopParameters &parameters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
Definition bop_fs.cc:181
bool ShouldBeRun(const ProblemState &problem_state) const override
Definition bop_fs.cc:168
GuidedSatFirstSolutionGenerator(absl::string_view name, Policy policy)
Definition bop_fs.cc:100
Status Optimize(const BopParameters &parameters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
Definition bop_fs.cc:475
LinearRelaxation(const BopParameters &parameters, absl::string_view name)
Definition bop_fs.cc:371
bool ShouldBeRun(const ProblemState &problem_state) const override
Definition bop_fs.cc:470
A full-fledged linear programming solver.
Definition lp_solver.h:31
SatParameters parameters
time_limit
Definition solve.cc:22
ProblemStatus
Different statuses for a given problem.
Definition lp_types.h:107
In SWIG mode, we don't want anything besides these top-level includes.
Fractional scaled_cost