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 {}; }
83 const CpSolverResponse& initial_solution,
84 const std::vector<int>& variables_to_fix)
const;
88 const std::vector<int>& constraints_to_remove)
const;
93 const CpSolverResponse& initial_solution,
94 const std::vector<int>& relaxed_variables)
const;
113 const absl::flat_hash_set<int>& fixed_variables_set,
114 const CpSolverResponse& initial_solution,
115 CpModelProto* output_model)
const;
129 std::vector<int> result;
131 result = active_variables_;
137 return active_variables_.size();
144 return constraint_to_var_;
148 return var_to_constraint_;
153 ConstraintProto::ConstraintCase type)
const {
154 if (type >= type_to_constraints_.size())
return {};
155 return absl::MakeSpan(type_to_constraints_[type]);
163 const CpSolverResponse& initial_solution)
const;
167 const CpModelProto&
ModelProto()
const {
return model_proto_; }
168 const SatParameters&
Parameters()
const {
return parameters_; }
171 return *shared_response_;
184 void RecomputeHelperData();
187 bool IsConstant(
int var)
const ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
189 const SatParameters& parameters_;
190 const CpModelProto& model_proto_;
191 int shared_bounds_id_;
202 CpModelProto model_proto_with_only_variables_ ABSL_GUARDED_BY(domain_mutex_);
205 std::vector<std::vector<int>> type_to_constraints_;
208 std::vector<std::vector<int>> constraint_to_var_
210 std::vector<std::vector<int>> var_to_constraint_
217 std::vector<bool> active_variables_set_ ABSL_GUARDED_BY(
graph_mutex_);
218 std::vector<int> active_variables_ ABSL_GUARDED_BY(
graph_mutex_);
220 mutable absl::Mutex domain_mutex_;
246 double difficulty, absl::BitGenRef random) = 0;
316 solve_data_.push_back(data);
335 return num_fully_solved_calls_;
341 return difficulty_.
value();
347 return deterministic_limit_;
353 return deterministic_time_;
367 std::vector<SolveData> solve_data_;
372 double deterministic_limit_ = 0.1;
376 int64_t num_calls_ = 0;
377 int64_t num_fully_solved_calls_ = 0;
378 int64_t num_consecutive_non_improving_calls_ = 0;
379 double deterministic_time_ = 0.0;
380 double current_average_ = 0.0;
390 double difficulty, absl::BitGenRef random)
final;
402 double difficulty, absl::BitGenRef random)
final;
415 double difficulty, absl::BitGenRef random)
final;
428 double difficulty, absl::BitGenRef random)
final;
435 const absl::Span<const int> intervals_to_relax,
436 const CpSolverResponse& initial_solution,
437 const NeighborhoodGeneratorHelper& helper);
451 double difficulty, absl::BitGenRef random)
final;
463 double difficulty, absl::BitGenRef random)
final;
488 const std::string&
name)
490 response_manager_(response_manager),
494 CHECK(lp_solutions_ !=
nullptr || relaxation_solutions_ !=
nullptr ||
500 double difficulty, absl::BitGenRef random)
final;
522 double difficulty, absl::BitGenRef random)
final;
542 double difficulty, absl::BitGenRef random)
final;
550 void AdditionalProcessingOnSynchronize(
const SolveData& solve_data)
override
554 std::vector<double> constraint_weights_;
555 int num_removable_constraints_ = 0;
558 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)
const std::vector< std::vector< int > > & VarToConstraint() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
NeighborhoodGeneratorHelper(CpModelProto const *model_proto, SatParameters const *parameters, SharedResponseManager *shared_response, SharedTimeLimit *shared_time_limit=nullptr, SharedBoundsManager *shared_bounds=nullptr)
const SharedResponseManager & shared_response() const
Neighborhood FullNeighborhood() const
std::vector< int > ActiveVariables() const
Neighborhood FixAllVariables(const CpSolverResponse &initial_solution) const
std::function< void()> GenerateTask(int64_t task_id) override
int NumActiveVariables() const
Neighborhood FixGivenVariables(const CpSolverResponse &initial_solution, const std::vector< int > &variables_to_fix) const
const absl::Span< const int > TypeToConstraints(ConstraintProto::ConstraintCase type) const
bool CopyAndFixVariables(const CpModelProto &source_model, const absl::flat_hash_set< int > &fixed_variables_set, const CpSolverResponse &initial_solution, CpModelProto *output_model) const
Neighborhood RelaxGivenVariables(const CpSolverResponse &initial_solution, const std::vector< int > &relaxed_variables) const
std::vector< int > GetActiveIntervals(const CpSolverResponse &initial_solution) const
const CpModelProto & ModelProto() const
const std::vector< std::vector< int > > & ConstraintToVar() const ABSL_SHARED_LOCKS_REQUIRED(graph_mutex_)
bool TaskIsAvailable() override
bool IsActive(int var) 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 SatParameters & Parameters() const
void Synchronize() override
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
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
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
SimpleConstraintNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
Neighborhood Generate(const CpSolverResponse &initial_solution, double difficulty, absl::BitGenRef random) final
SimpleNeighborhoodGenerator(NeighborhoodGeneratorHelper const *helper, const std::string &name)
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