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