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"
97 std::function<void()>
GenerateTask(int64_t task_id)
override {
return {}; }
104 const absl::flat_hash_set<int>& variables_to_fix)
const;
108 const std::vector<int>& constraints_to_remove)
const;
114 const std::vector<int>& relaxed_variables)
const;
139 std::vector<int> result;
141 result = active_variables_;
147 return active_variables_.size();
152 const int target_size = std::ceil(difficulty * active_variables_.size());
153 return target_size == active_variables_.size();
160 return active_variables_;
167 return constraint_to_var_;
171 return var_to_constraint_;
177 if (type >= type_to_constraints_.size())
return {};
178 return absl::MakeSpan(type_to_constraints_[type]);
202 return *shared_response_;
219 void InitializeHelperData();
223 void RecomputeHelperData();
226 bool IsConstant(
int var)
const ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
230 bool ObjectiveDomainIsConstraining() const
231 ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
235 int shared_bounds_id_;
246 CpModelProto model_proto_with_only_variables_ ABSL_GUARDED_BY(domain_mutex_);
249 std::vector<
std::vector<
int>> type_to_constraints_;
253 std::vector<
bool> is_in_objective_;
265 std::vector<
std::vector<
int>> constraint_to_var_
267 std::vector<
std::vector<
int>> var_to_constraint_
274 std::vector<
int> var_to_component_index_ ABSL_GUARDED_BY(
graph_mutex_);
282 mutable
absl::Mutex domain_mutex_;
290 :
name_(
name), helper_(*helper), difficulty_(0.5) {}
308 double difficulty, absl::BitGenRef random) = 0;
311 virtual bool ReadyToGenerate()
const;
325 double GetUCBScore(int64_t total_num_calls)
const;
331 int64_t neighborhood_id = 0;
337 double difficulty = 0.0;
340 double deterministic_limit = 0.0;
354 IntegerValue initial_best_objective = IntegerValue(0);
355 IntegerValue base_objective = IntegerValue(0);
356 IntegerValue new_objective = IntegerValue(0);
359 IntegerValue initial_best_objective_bound = IntegerValue(0);
360 IntegerValue new_objective_bound = IntegerValue(0);
364 return std::tie(
status, difficulty, deterministic_limit,
366 base_objective, new_objective,
367 initial_best_objective_bound, new_objective_bound,
377 absl::MutexLock mutex_lock(&generator_mutex_);
378 solve_data_.push_back(data);
390 absl::MutexLock mutex_lock(&generator_mutex_);
396 absl::MutexLock mutex_lock(&generator_mutex_);
397 return num_fully_solved_calls_;
402 absl::MutexLock mutex_lock(&generator_mutex_);
403 return difficulty_.value();
408 absl::MutexLock mutex_lock(&generator_mutex_);
409 return deterministic_limit_;
414 absl::MutexLock mutex_lock(&generator_mutex_);
429 std::vector<SolveData> solve_data_;
434 double deterministic_limit_ = 0.1;
438 int64_t num_calls_ = 0;
439 int64_t num_fully_solved_calls_ = 0;
440 int64_t num_consecutive_non_improving_calls_ = 0;
442 double current_average_ = 0.0;
455 double difficulty, absl::BitGenRef random)
final;
470 double difficulty, absl::BitGenRef random)
final;
486 double difficulty, absl::BitGenRef random)
final;
499 double difficulty, absl::BitGenRef random)
final;
506 const absl::Span<const int> intervals_to_relax,
507 const CpSolverResponse& initial_solution,
522 double difficulty, absl::BitGenRef random)
final;
534 double difficulty, absl::BitGenRef random)
final;
542 const std::string&
name)
546 double difficulty, absl::BitGenRef random)
final;
554 const std::string&
name)
558 double difficulty, absl::BitGenRef random)
final;
575 double difficulty, absl::BitGenRef random)
final;
600 const std::string&
name)
602 response_manager_(response_manager),
606 CHECK(lp_solutions_ !=
nullptr || relaxation_solutions_ !=
nullptr ||
612 double difficulty, absl::BitGenRef random)
final;
615 bool ReadyToGenerate()
const override;
634 double difficulty, absl::BitGenRef random)
final;
654 double difficulty, absl::BitGenRef random)
final;
662 void AdditionalProcessingOnSynchronize(
const SolveData& solve_data)
override
663 ABSL_EXCLUSIVE_LOCKS_REQUIRED(generator_mutex_);
666 std::vector<double> constraint_weights_;
667 int num_removable_constraints_ = 0;
670 absl::flat_hash_map<int64_t, std::vector<int>> removed_constraints_
671 ABSL_GUARDED_BY(generator_mutex_);
675 int64_t next_available_id_ ABSL_GUARDED_BY(generator_mutex_) = 0;
bool ReadyToGenerate() const override
ConsecutiveConstraintsRelaxationNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
bool IsRelaxationGenerator() const override
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 &base_solution, const absl::flat_hash_set< int > &variables_to_fix) 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)
double difficulty() 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
RelaxRandomConstraintsGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
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)
RoutingFullPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
RoutingPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
RoutingRandomNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
SchedulingNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
SchedulingTimeWindowNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
double deterministic_time_
double deterministic_time() const
VariableGraphNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
bool ReadyToGenerate() const override
bool IsRelaxationGenerator() const override
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
int num_relaxed_variables
std::vector< int > variables_that_can_be_fixed_to_local_optimum
int num_relaxed_variables_in_objective
std::vector< int > constraints_to_ignore