14#ifndef OR_TOOLS_SAT_CP_MODEL_LNS_H_
15#define OR_TOOLS_SAT_CP_MODEL_LNS_H_
24#include "absl/base/thread_annotations.h"
25#include "absl/container/flat_hash_map.h"
26#include "absl/container/flat_hash_set.h"
27#include "absl/random/bit_gen_ref.h"
28#include "absl/synchronization/mutex.h"
29#include "absl/time/time.h"
30#include "absl/types/span.h"
33#include "ortools/sat/cp_model.pb.h"
37#include "ortools/sat/sat_parameters.pb.h"
109 std::function<void()>
GenerateTask(int64_t task_id)
override {
return {}; }
115 const CpSolverResponse& base_solution,
116 const absl::flat_hash_set<int>& variables_to_fix)
const;
120 const std::vector<int>& constraints_to_remove)
const;
125 const CpSolverResponse& initial_solution,
126 const std::vector<int>& relaxed_variables)
const;
151 std::vector<int> result;
153 result = active_variables_;
159 return active_variables_.size();
164 const int target_size = std::ceil(difficulty * active_variables_.size());
165 return target_size == active_variables_.size();
172 return active_variables_;
179 return constraint_to_var_;
183 return var_to_constraint_;
188 ConstraintProto::ConstraintCase type)
const {
189 if (type >= type_to_constraints_.size())
return {};
190 return absl::MakeSpan(type_to_constraints_[type]);
198 const CpSolverResponse& initial_solution)
const;
206 const CpSolverResponse& initial_solution)
const;
210 const CpModelProto&
ModelProto()
const {
return model_proto_; }
211 const SatParameters&
Parameters()
const {
return parameters_; }
214 return *shared_response_;
231 void InitializeHelperData();
235 void RecomputeHelperData();
238 bool IsConstant(
int var)
const ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
242 bool ObjectiveDomainIsConstraining() const
243 ABSL_SHARED_LOCKS_REQUIRED(domain_mutex_);
245 const SatParameters& parameters_;
246 const CpModelProto& model_proto_;
247 int shared_bounds_id_;
258 CpModelProto model_proto_with_only_variables_ ABSL_GUARDED_BY(domain_mutex_);
261 std::vector<
std::vector<
int>> type_to_constraints_;
265 std::vector<
bool> is_in_objective_;
270 CpModelProto simplied_model_proto_ ABSL_GUARDED_BY(
graph_mutex_);
277 std::vector<
std::vector<
int>> constraint_to_var_
279 std::vector<
std::vector<
int>> var_to_constraint_
286 std::vector<
int> var_to_component_index_ ABSL_GUARDED_BY(
graph_mutex_);
294 mutable
absl::Mutex domain_mutex_;
297 absl::Time last_logging_time_;
305 :
name_(
name), helper_(*helper), difficulty_(0.5) {}
323 double difficulty, absl::BitGenRef random) = 0;
326 virtual bool ReadyToGenerate()
const;
340 double GetUCBScore(int64_t total_num_calls)
const;
346 int64_t neighborhood_id = 0;
349 CpSolverStatus
status = CpSolverStatus::UNKNOWN;
352 double difficulty = 0.0;
355 double deterministic_limit = 0.0;
369 IntegerValue initial_best_objective = IntegerValue(0);
370 IntegerValue base_objective = IntegerValue(0);
371 IntegerValue new_objective = IntegerValue(0);
374 IntegerValue initial_best_objective_bound = IntegerValue(0);
375 IntegerValue new_objective_bound = IntegerValue(0);
379 return std::tie(
status, difficulty, deterministic_limit,
381 base_objective, new_objective,
382 initial_best_objective_bound, new_objective_bound,
392 absl::MutexLock mutex_lock(&generator_mutex_);
393 solve_data_.push_back(data);
405 absl::MutexLock mutex_lock(&generator_mutex_);
411 absl::MutexLock mutex_lock(&generator_mutex_);
412 return num_fully_solved_calls_;
417 absl::MutexLock mutex_lock(&generator_mutex_);
418 return difficulty_.value();
423 absl::MutexLock mutex_lock(&generator_mutex_);
424 return deterministic_limit_;
429 absl::MutexLock mutex_lock(&generator_mutex_);
444 std::vector<SolveData> solve_data_;
449 double deterministic_limit_ = 0.1;
453 int64_t num_calls_ = 0;
454 int64_t num_fully_solved_calls_ = 0;
455 int64_t num_consecutive_non_improving_calls_ = 0;
457 double current_average_ = 0.0;
469 Neighborhood Generate(
const CpSolverResponse& initial_solution,
470 double difficulty, absl::BitGenRef random)
final;
484 Neighborhood Generate(
const CpSolverResponse& initial_solution,
485 double difficulty, absl::BitGenRef random)
final;
500 Neighborhood Generate(
const CpSolverResponse& initial_solution,
501 double difficulty, absl::BitGenRef random)
final;
513 Neighborhood Generate(
const CpSolverResponse& initial_solution,
514 double difficulty, absl::BitGenRef random)
final;
521 const absl::Span<const int> intervals_to_relax,
522 const CpSolverResponse& initial_solution,
536 Neighborhood Generate(
const CpSolverResponse& initial_solution,
537 double difficulty, absl::BitGenRef random)
final;
548 Neighborhood Generate(
const CpSolverResponse& initial_solution,
549 double difficulty, absl::BitGenRef random)
final;
557 const std::string&
name)
560 Neighborhood Generate(
const CpSolverResponse& initial_solution,
561 double difficulty, absl::BitGenRef random)
final;
569 const std::string&
name)
572 Neighborhood Generate(
const CpSolverResponse& initial_solution,
573 double difficulty, absl::BitGenRef random)
final;
589 Neighborhood Generate(
const CpSolverResponse& initial_solution,
590 double difficulty, absl::BitGenRef random)
final;
615 const std::string&
name)
617 response_manager_(response_manager),
621 CHECK(lp_solutions_ !=
nullptr || relaxation_solutions_ !=
nullptr ||
626 Neighborhood Generate(
const CpSolverResponse& initial_solution,
627 double difficulty, absl::BitGenRef random)
final;
630 bool ReadyToGenerate()
const override;
648 Neighborhood Generate(
const CpSolverResponse& initial_solution,
649 double difficulty, absl::BitGenRef random)
final;
668 Neighborhood Generate(
const CpSolverResponse& initial_solution,
669 double difficulty, absl::BitGenRef random)
final;
677 void AdditionalProcessingOnSynchronize(
const SolveData& solve_data)
override
678 ABSL_EXCLUSIVE_LOCKS_REQUIRED(generator_mutex_);
681 std::vector<double> constraint_weights_;
682 int num_removable_constraints_ = 0;
685 absl::flat_hash_map<int64_t, std::vector<int>> removed_constraints_
686 ABSL_GUARDED_BY(generator_mutex_);
690 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