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;
113 const absl::flat_hash_set<int>& fixed_variables_set,
129 std::vector<int> result;
131 result = active_variables_;
137 return active_variables_.size();
142 const int target_size = std::ceil(difficulty * active_variables_.size());
143 return target_size == active_variables_.size();
150 return active_variables_;
157 return constraint_to_var_;
161 return var_to_constraint_;
167 if (type >= type_to_constraints_.size())
return {};
168 return absl::MakeSpan(type_to_constraints_[type]);
192 return *shared_response_;
209 void InitializeHelperData();
213 void RecomputeHelperData();
216 bool IsConstant(
int var)
const ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
220 int shared_bounds_id_;
231 CpModelProto model_proto_with_only_variables_ ABSL_GUARDED_BY(domain_mutex_);
234 std::vector<std::vector<int>> type_to_constraints_;
237 std::vector<std::vector<int>> constraint_to_var_
239 std::vector<std::vector<int>> var_to_constraint_
246 std::vector<bool> active_variables_set_ ABSL_GUARDED_BY(
graph_mutex_);
247 std::vector<int> active_variables_ ABSL_GUARDED_BY(
graph_mutex_);
249 mutable absl::Mutex domain_mutex_;
275 double difficulty, absl::BitGenRef random) = 0;
345 solve_data_.push_back(data);
364 return num_fully_solved_calls_;
370 return difficulty_.
value();
376 return deterministic_limit_;
382 return deterministic_time_;
396 std::vector<SolveData> solve_data_;
401 double deterministic_limit_ = 0.1;
405 int64_t num_calls_ = 0;
406 int64_t num_fully_solved_calls_ = 0;
407 int64_t num_consecutive_non_improving_calls_ = 0;
408 double deterministic_time_ = 0.0;
409 double current_average_ = 0.0;
419 double difficulty, absl::BitGenRef random)
final;
431 double difficulty, absl::BitGenRef random)
final;
444 double difficulty, absl::BitGenRef random)
final;
457 double difficulty, absl::BitGenRef random)
final;
464 const absl::Span<const int> intervals_to_relax,
465 const CpSolverResponse& initial_solution,
466 const NeighborhoodGeneratorHelper& helper);
480 double difficulty, absl::BitGenRef random)
final;
492 double difficulty, absl::BitGenRef random)
final;
500 const std::string&
name)
504 double difficulty, absl::BitGenRef random)
final;
512 const std::string&
name)
516 double difficulty, absl::BitGenRef random)
final;
533 double difficulty, absl::BitGenRef random)
final;
558 const std::string&
name)
560 response_manager_(response_manager),
564 CHECK(lp_solutions_ !=
nullptr || relaxation_solutions_ !=
nullptr ||
570 double difficulty, absl::BitGenRef random)
final;
592 double difficulty, absl::BitGenRef random)
final;
612 double difficulty, absl::BitGenRef random)
final;
620 void AdditionalProcessingOnSynchronize(
const SolveData& solve_data)
override 624 std::vector<double> constraint_weights_;
625 int num_removable_constraints_ = 0;
628 absl::flat_hash_map<int64_t, std::vector<int>> removed_constraints_
639 #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)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
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 Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
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
bool ReadyToGenerate() const override
bool ReadyToGenerate() const override
SchedulingTimeWindowNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
std::function< void()> GenerateTask(int64_t task_id) override
double deterministic_time
double GetUCBScore(int64_t total_num_calls) const
std::vector< int > constraints_to_ignore
RelaxRandomVariablesGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
bool DifficultyMeansFullNeighborhood(double difficulty) const
double difficulty() const
RoutingPathNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
const std::vector< int > & ActiveVariablesWhileHoldingLock() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
void AddSolutionHinting(const CpSolverResponse &initial_solution, CpModelProto *model_proto) const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
IntegerValue initial_best_objective_bound
SharedIncompleteSolutionManager * incomplete_solutions
const CpModelProto & ModelProto() const
bool CopyAndFixVariables(const CpModelProto &source_model, const absl::flat_hash_set< int > &fixed_variables_set, const CpSolverResponse &initial_solution, CpModelProto *output_model) const
bool ReadyToGenerate() const override
absl::Mutex generator_mutex_
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
const SatParameters & Parameters() const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
int64_t num_calls() const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
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)
WeightedRandomRelaxationNeighborhoodGenerator(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
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)
virtual Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random)=0
RelaxRandomConstraintsGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
std::vector< std::vector< int > > GetRoutingPaths(const CpSolverResponse &initial_solution) const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
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.
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
Neighborhood FixGivenVariables(const CpSolverResponse &initial_solution, const absl::flat_hash_set< int > &variables_to_fix) const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
IntegerValue new_objective_bound
virtual bool ReadyToGenerate() const
bool operator<(const SolveData &o) const
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
double deterministic_limit
bool TaskIsAvailable() override
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
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)