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