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_
273 std::vector<std::vector<
int>> components_ ABSL_GUARDED_BY(
graph_mutex_);
274 std::vector<
int> var_to_component_index_ ABSL_GUARDED_BY(
graph_mutex_);
279 std::vector<
bool> active_variables_set_ ABSL_GUARDED_BY(
graph_mutex_);
280 std::vector<
int> active_variables_ 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;
681 #endif // OR_TOOLS_SAT_CP_MODEL_LNS_H_ SchedulingNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
std::vector< int > ActiveVariables() const
bool IsRelaxationGenerator() const override
int NumActiveVariables() const
VariableGraphNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
ConstraintGraphNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood GenerateSchedulingNeighborhoodForRelaxation(const absl::Span< const int > intervals_to_relax, const CpSolverResponse &initial_solution, const NeighborhoodGeneratorHelper &helper)
SharedLPSolutionRepository * lp_solutions
void AddSolveData(SolveData data)
Neighborhood RemoveMarkedConstraints(const std::vector< int > &constraints_to_remove) const
NeighborhoodGeneratorHelper(CpModelProto const *model_proto, SatParameters const *parameters, SharedResponseManager *shared_response, SharedTimeLimit *shared_time_limit=nullptr, SharedBoundsManager *shared_bounds=nullptr)
Neighborhood FixGivenVariables(const CpSolverResponse &base_solution, const absl::flat_hash_set< int > &variables_to_fix) const
bool IsActive(int var) const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
Neighborhood FixAllVariables(const CpSolverResponse &initial_solution) const
const absl::Span< const int > TypeToConstraints(ConstraintProto::ConstraintCase type) const
int num_relaxed_variables_in_objective
bool ReadyToGenerate() const override
SchedulingTimeWindowNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
std::function< void()> GenerateTask(int64_t task_id) override
double deterministic_time
Neighborhood NoNeighborhood() const
std::vector< int > constraints_to_ignore
RelaxRandomVariablesGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
bool DifficultyMeansFullNeighborhood(double difficulty) const
double deterministic_time_
double difficulty() const
RoutingPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
double deterministic_time() const
const std::vector< int > & ActiveVariablesWhileHoldingLock() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
void AddSolutionHinting(const CpSolverResponse &initial_solution, CpModelProto *model_proto) const
IntegerValue initial_best_objective_bound
SharedIncompleteSolutionManager * incomplete_solutions
const CpModelProto & ModelProto() const
bool ReadyToGenerate() const override
int num_relaxed_variables
absl::Mutex generator_mutex_
const SatParameters & Parameters() const
int64_t num_calls() const
const SharedResponseManager & shared_response() const
int64_t num_fully_solved_calls() const
void Synchronize() override
virtual void AdditionalProcessingOnSynchronize(const SolveData &solve_data)
const std::vector< std::vector< int > > & VarToConstraint() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
ConsecutiveConstraintsRelaxationNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
RoutingRandomNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood FullNeighborhood() const
CpModelProto const * model_proto
RoutingFullPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
bool IsRelaxationGenerator() const override
std::vector< int > variables_that_can_be_fixed_to_local_optimum
virtual bool IsRelaxationGenerator() const
IntegerValue base_objective
double deterministic_limit() const
double deterministic_time() const
virtual ~NeighborhoodGenerator()
const std::vector< std::vector< int > > & ConstraintToVar() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
NeighborhoodGenerator(const std::string &name, NeighborhoodGeneratorHelper const *helper)
RelaxRandomConstraintsGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
std::vector< std::vector< int > > GetRoutingPaths(const CpSolverResponse &initial_solution) const
Neighborhood RelaxGivenVariables(const CpSolverResponse &initial_solution, const std::vector< int > &relaxed_variables) const
SharedRelaxationSolutionRepository * relaxation_solutions
Collection of objects used to extend the Constraint Solver library.
IntegerValue new_objective_bound
bool operator<(const SolveData &o) const
double deterministic_limit
bool TaskIsAvailable() override
std::vector< int > GetActiveIntervals(const CpSolverResponse &initial_solution) const
IntegerValue initial_best_objective
const NeighborhoodGeneratorHelper & helper_
IntegerValue new_objective
RelaxationInducedNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const SharedResponseManager *response_manager, const SharedRelaxationSolutionRepository *relaxation_solutions, const SharedLPSolutionRepository *lp_solutions, SharedIncompleteSolutionManager *incomplete_solutions, const std::string &name)