14#ifndef OR_TOOLS_SAT_CP_MODEL_LNS_H_
15#define OR_TOOLS_SAT_CP_MODEL_LNS_H_
20#include "absl/container/flat_hash_map.h"
21#include "absl/random/bit_gen_ref.h"
22#include "absl/synchronization/mutex.h"
23#include "absl/types/span.h"
77 std::function<void()>
GenerateTask(int64_t task_id)
override {
return {}; }
84 const absl::flat_hash_set<int>& variables_to_fix)
const;
88 const std::vector<int>& constraints_to_remove)
const;
94 const std::vector<int>& relaxed_variables)
const;
116 const absl::flat_hash_set<int>& fixed_variables_set,
132 std::vector<int> result;
134 result = active_variables_;
140 return active_variables_.size();
145 const int target_size = std::ceil(difficulty * active_variables_.size());
146 return target_size == active_variables_.size();
153 return active_variables_;
160 return constraint_to_var_;
164 return var_to_constraint_;
170 if (type >= type_to_constraints_.size())
return {};
171 return absl::MakeSpan(type_to_constraints_[type]);
195 return *shared_response_;
212 void InitializeHelperData();
216 void RecomputeHelperData();
219 bool IsConstant(
int var)
const ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
223 int shared_bounds_id_;
234 CpModelProto model_proto_with_only_variables_ ABSL_GUARDED_BY(domain_mutex_);
237 std::vector<std::vector<int>> type_to_constraints_;
240 std::vector<std::vector<int>> constraint_to_var_
242 std::vector<std::vector<int>> var_to_constraint_
249 std::vector<bool> active_variables_set_ ABSL_GUARDED_BY(
graph_mutex_);
250 std::vector<int> active_variables_ ABSL_GUARDED_BY(
graph_mutex_);
252 mutable absl::Mutex domain_mutex_;
278 double difficulty, absl::BitGenRef random) = 0;
348 solve_data_.push_back(data);
367 return num_fully_solved_calls_;
373 return difficulty_.
value();
379 return deterministic_limit_;
385 return deterministic_time_;
399 std::vector<SolveData> solve_data_;
404 double deterministic_limit_ = 0.1;
408 int64_t num_calls_ = 0;
409 int64_t num_fully_solved_calls_ = 0;
410 int64_t num_consecutive_non_improving_calls_ = 0;
411 double deterministic_time_ = 0.0;
412 double current_average_ = 0.0;
422 double difficulty, absl::BitGenRef random)
final;
434 double difficulty, absl::BitGenRef random)
final;
447 double difficulty, absl::BitGenRef random)
final;
460 double difficulty, absl::BitGenRef random)
final;
467 const absl::Span<const int> intervals_to_relax,
468 const CpSolverResponse& initial_solution,
469 const NeighborhoodGeneratorHelper& helper);
483 double difficulty, absl::BitGenRef random)
final;
495 double difficulty, absl::BitGenRef random)
final;
503 const std::string&
name)
507 double difficulty, absl::BitGenRef random)
final;
515 const std::string&
name)
519 double difficulty, absl::BitGenRef random)
final;
536 double difficulty, absl::BitGenRef random)
final;
561 const std::string&
name)
563 response_manager_(response_manager),
567 CHECK(lp_solutions_ !=
nullptr || relaxation_solutions_ !=
nullptr ||
573 double difficulty, absl::BitGenRef random)
final;
595 double difficulty, absl::BitGenRef random)
final;
615 double difficulty, absl::BitGenRef random)
final;
623 void AdditionalProcessingOnSynchronize(
const SolveData& solve_data)
override
627 std::vector<double> constraint_weights_;
628 int num_removable_constraints_ = 0;
631 absl::flat_hash_map<int64_t, std::vector<int>> removed_constraints_
bool ReadyToGenerate() const override
ConsecutiveConstraintsRelaxationNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
bool IsRelaxationGenerator() const override
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
ConstraintGraphNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
NeighborhoodGeneratorHelper(CpModelProto const *model_proto, SatParameters const *parameters, SharedResponseManager *shared_response, SharedTimeLimit *shared_time_limit=nullptr, SharedBoundsManager *shared_bounds=nullptr)
Neighborhood FullNeighborhood() const
Neighborhood FixAllVariables(const CpSolverResponse &initial_solution) const
const CpModelProto & ModelProto() const
int NumActiveVariables() const
const SatParameters & Parameters() const
const absl::Span< const int > TypeToConstraints(ConstraintProto::ConstraintCase type) const
Neighborhood FixGivenVariables(const CpSolverResponse &initial_solution, const absl::flat_hash_set< int > &variables_to_fix) const
bool CopyAndFixVariables(const CpModelProto &source_model, const absl::flat_hash_set< int > &fixed_variables_set, const CpSolverResponse &initial_solution, CpModelProto *output_model) const
std::function< void()> GenerateTask(int64_t task_id) override
bool DifficultyMeansFullNeighborhood(double difficulty) const
Neighborhood NoNeighborhood() const
Neighborhood RelaxGivenVariables(const CpSolverResponse &initial_solution, const std::vector< int > &relaxed_variables) const
std::vector< int > GetActiveIntervals(const CpSolverResponse &initial_solution) const
const SharedResponseManager & shared_response() const
std::vector< int > ActiveVariables() const
bool TaskIsAvailable() override
const std::vector< std::vector< int > > & VarToConstraint() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
bool IsActive(int var) const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
const std::vector< int > & ActiveVariablesWhileHoldingLock() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
Neighborhood RemoveMarkedConstraints(const std::vector< int > &constraints_to_remove) const
void AddSolutionHinting(const CpSolverResponse &initial_solution, CpModelProto *model_proto) const
const std::vector< std::vector< int > > & ConstraintToVar() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
void Synchronize() override
std::vector< std::vector< int > > GetRoutingPaths(const CpSolverResponse &initial_solution) const
virtual Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random)=0
virtual bool IsRelaxationGenerator() const
absl::Mutex generator_mutex_
virtual ~NeighborhoodGenerator()
void AddSolveData(SolveData data)
virtual bool ReadyToGenerate() const
double difficulty() const
double GetUCBScore(int64_t total_num_calls) const
NeighborhoodGenerator(const std::string &name, NeighborhoodGeneratorHelper const *helper)
int64_t num_fully_solved_calls() const
int64_t num_calls() const
virtual void AdditionalProcessingOnSynchronize(const SolveData &solve_data)
const NeighborhoodGeneratorHelper & helper_
double deterministic_time() const
double deterministic_limit() const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
RelaxRandomConstraintsGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
RelaxRandomVariablesGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
RelaxationInducedNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const SharedResponseManager *response_manager, const SharedRelaxationSolutionRepository *relaxation_solutions, const SharedLPSolutionRepository *lp_solutions, SharedIncompleteSolutionManager *incomplete_solutions, const std::string &name)
bool ReadyToGenerate() const override
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
RoutingFullPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
RoutingPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
RoutingRandomNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
SchedulingNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
SchedulingTimeWindowNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
VariableGraphNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
bool ReadyToGenerate() const override
WeightedRandomRelaxationNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
bool IsRelaxationGenerator() const override
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
SharedRelaxationSolutionRepository * relaxation_solutions
SharedLPSolutionRepository * lp_solutions
CpModelProto const * model_proto
SharedIncompleteSolutionManager * incomplete_solutions
Neighborhood GenerateSchedulingNeighborhoodForRelaxation(const absl::Span< const int > intervals_to_relax, const CpSolverResponse &initial_solution, const NeighborhoodGeneratorHelper &helper)
Collection of objects used to extend the Constraint Solver library.
bool operator<(const SolveData &o) const
IntegerValue base_objective
IntegerValue initial_best_objective_bound
IntegerValue initial_best_objective
double deterministic_limit
IntegerValue new_objective
IntegerValue new_objective_bound
double deterministic_time
std::vector< int > constraints_to_ignore