22#include "absl/log/check.h"
23#include "absl/strings/string_view.h"
25#include "ortools/bop/bop_parameters.pb.h"
28#include "ortools/sat/boolean_problem.pb.h"
33#include "ortools/sat/sat_parameters.pb.h"
45 sat_solver_(model_.GetOrCreate<sat::SatSolver>()),
49 assumptions_already_added_(false) {
51 lower_bound_ = sat::Coefficient(0);
59 if (state_update_stamp_ == problem_state.
update_stamp()) {
74 for (
int i = 0; i < objective_proto.literals_size(); ++i) {
76 const sat::Coefficient coeff(objective_proto.coefficients(i));
86 offset_ -= objective_proto.coefficients(i);
92 stratified_lower_bound_ = sat::Coefficient(0);
93 for (
const sat::EncodingNode* n : encoder_.
nodes()) {
94 stratified_lower_bound_ = std::max(stratified_lower_bound_, n->weight());
109 stratified_lower_bound_, encoder_.
nodes(), sat_solver_);
123 CHECK(learned_info !=
nullptr);
125 learned_info->
Clear();
128 SynchronizeIfNeeded(problem_state);
133 int64_t conflict_limit =
parameters.max_number_of_conflicts_in_random_lns();
136 sat::SatParameters sat_params = sat_solver_->
parameters();
137 sat_params.set_max_time_in_seconds(
time_limit->GetTimeLeft());
138 sat_params.set_max_deterministic_time(
140 sat_params.set_random_seed(
parameters.random_seed());
141 sat_params.set_max_number_of_conflicts(conflict_limit);
144 const int64_t old_num_conflicts = sat_solver_->
num_failures();
146 ? sat_solver_->
Solve()
147 : SolveWithAssumptions();
149 deterministic_time_at_last_sync);
152 assumptions_already_added_ =
true;
153 conflict_limit -= sat_solver_->
num_failures() - old_num_conflicts;
154 learned_info->
lower_bound = lower_bound_.value() - offset_.value();
168 stratified_lower_bound_ =
169 MaxNodeWeightSmallerThan(encoder_.
nodes(), stratified_lower_bound_);
174 if (stratified_lower_bound_ > 0) {
175 assumptions_already_added_ =
false;
183 std::vector<sat::Literal> core =
187 const sat::Coefficient min_weight =
189 std::string info_str;
192 std::numeric_limits<sat::Coefficient::ValueType>::max(),
194 assumptions_already_added_ =
false;
int64_t update_stamp() const
const BopSolution & solution() const
const sat::LinearBooleanProblem & original_problem() const
bool ShouldBeRun(const ProblemState &problem_state) const override
Only run this if there is an objective.
~SatCoreBasedOptimizer() override
Status Optimize(const BopParameters ¶meters, const ProblemState &problem_state, LearnedInfo *learned_info, TimeLimit *time_limit) override
SatCoreBasedOptimizer(absl::string_view name)
static EncodingNode LiteralNode(Literal l, Coefficient weight)
std::vector< EncodingNode * > * mutable_nodes()
void AddBaseNode(EncodingNode node)
const std::vector< EncodingNode * > & nodes() const
bool ProcessCore(absl::Span< const Literal > core, Coefficient min_weight, Coefficient gap, std::string *info)
double deterministic_time() const
std::vector< Literal > GetLastIncompatibleDecisions()
int64_t num_failures() const
const SatParameters & parameters() const
Status ResetAndSolveWithGivenAssumptions(const std::vector< Literal > &assumptions, int64_t max_number_of_conflicts=-1)
const VariablesAssignment & Assignment() const
void SetParameters(const SatParameters ¶meters)
const std::string name
A name for logging purposes.
BopOptimizerBase::Status LoadStateProblemToSatSolver(const ProblemState &problem_state, sat::SatSolver *sat_solver)
void SatAssignmentToBopSolution(const sat::VariablesAssignment &assignment, BopSolution *solution)
void ExtractLearnedInfoFromSatSolver(sat::SatSolver *solver, LearnedInfo *info)
std::vector< Literal > ExtractAssumptions(Coefficient stratified_lower_bound, const std::vector< EncodingNode * > &nodes, SatSolver *solver)
void ReduceNodes(Coefficient upper_bound, Coefficient *lower_bound, std::vector< EncodingNode * > *nodes, SatSolver *solver)
Coefficient ComputeCoreMinWeight(const std::vector< EncodingNode * > &nodes, const std::vector< Literal > &core)
void MinimizeCore(SatSolver *solver, std::vector< Literal > *core)
const Coefficient kCoefficientMax(std::numeric_limits< Coefficient::ValueType >::max())
In SWIG mode, we don't want anything besides these top-level includes.
#define SCOPED_TIME_STAT(stats)
int64_t lower_bound
A lower bound (for multi-threading purpose).
BopSolution solution
New solution. Note that the solution might be infeasible.