From 0d54b9c1593e837e8010cb72160f7ee0f5c37137 Mon Sep 17 00:00:00 2001 From: Mizux Seiha Date: Sat, 30 Nov 2024 00:31:04 +0100 Subject: [PATCH] constraint_solver: Backport from main --- ortools/constraint_solver/BUILD.bazel | 1 + .../constraint_solver/constraint_solveri.h | 22 +++++++++---------- ortools/constraint_solver/local_search.cc | 10 ++++----- ortools/constraint_solver/search.cc | 10 ++++++--- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/ortools/constraint_solver/BUILD.bazel b/ortools/constraint_solver/BUILD.bazel index c68a447d45..39530a8284 100644 --- a/ortools/constraint_solver/BUILD.bazel +++ b/ortools/constraint_solver/BUILD.bazel @@ -217,6 +217,7 @@ cc_library( "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", "@com_google_absl//absl/random", "@com_google_absl//absl/random:distributions", diff --git a/ortools/constraint_solver/constraint_solveri.h b/ortools/constraint_solver/constraint_solveri.h index f85934d3e2..9992db7549 100644 --- a/ortools/constraint_solver/constraint_solveri.h +++ b/ortools/constraint_solver/constraint_solveri.h @@ -145,9 +145,9 @@ class SimpleRevFIFO { private: enum { CHUNK_SIZE = 16 }; // TODO(user): could be an extra template param struct Chunk { - T data_[CHUNK_SIZE]; - const Chunk* const next_; - explicit Chunk(const Chunk* next) : next_(next) {} + T data[CHUNK_SIZE]; + const Chunk* const next; + explicit Chunk(const Chunk* next) : next(next) {} }; public: @@ -160,9 +160,9 @@ class SimpleRevFIFO { T operator*() const { return *value_; } void operator++() { ++value_; - if (value_ == chunk_->data_ + CHUNK_SIZE) { - chunk_ = chunk_->next_; - value_ = chunk_ ? chunk_->data_ : nullptr; + if (value_ == chunk_->data + CHUNK_SIZE) { + chunk_ = chunk_->next; + value_ = chunk_ ? chunk_->data : nullptr; } } @@ -182,7 +182,7 @@ class SimpleRevFIFO { } else { pos_.Decr(s); } - chunks_->data_[pos_.Value()] = val; + chunks_->data[pos_.Value()] = val; } /// Pushes the var on top if is not a duplicate of the current top object. @@ -194,21 +194,21 @@ class SimpleRevFIFO { /// Returns the last item of the FIFO. const T* Last() const { - return chunks_ ? &chunks_->data_[pos_.Value()] : nullptr; + return chunks_ ? &chunks_->data[pos_.Value()] : nullptr; } - T* MutableLast() { return chunks_ ? &chunks_->data_[pos_.Value()] : nullptr; } + T* MutableLast() { return chunks_ ? &chunks_->data[pos_.Value()] : nullptr; } /// Returns the last value in the FIFO. const T& LastValue() const { DCHECK(chunks_); - return chunks_->data_[pos_.Value()]; + return chunks_->data[pos_.Value()]; } /// Sets the last value in the FIFO. void SetLastValue(const T& v) { DCHECK(Last()); - chunks_->data_[pos_.Value()] = v; + chunks_->data[pos_.Value()] = v; } private: diff --git a/ortools/constraint_solver/local_search.cc b/ortools/constraint_solver/local_search.cc index b05659e0fc..f43ed1dc66 100644 --- a/ortools/constraint_solver/local_search.cc +++ b/ortools/constraint_solver/local_search.cc @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -757,8 +756,9 @@ void PathOperator::InitializePathStarts() { base_sibling_alternatives_[j] = 0; if (IsInactive(base_nodes_[j]) || node_paths[base_nodes_[j]] == -1) { // Base node was made inactive or was moved to a new path, reposition - // the base node to the start of the path on which it was. - base_nodes_[j] = path_starts_[base_paths_[j]]; + // the base node to its restart position. + base_nodes_[j] = GetBaseNodeRestartPosition(j); + base_paths_[j] = node_paths[base_nodes_[j]]; } else { base_paths_[j] = node_paths[base_nodes_[j]]; } @@ -3990,8 +3990,8 @@ class LocalSearchProfiler : public LocalSearchMonitor { absl::string_view name, int64_t num_neighbors, int64_t num_filtered_neighbors, int64_t num_accepted_neighbors, double duration_seconds, - double make_next_neighbor_duration_seconds, - double accept_neighbor_duration_seconds) { + double /*make_next_neighbor_duration_seconds*/, + double /*accept_neighbor_duration_seconds*/) { // TODO(user): Add make_next_neighbor_duration_seconds and // accept_neighbor_duration_seconds to stats. absl::StrAppendFormat( diff --git a/ortools/constraint_solver/search.cc b/ortools/constraint_solver/search.cc index 9be936d458..0e8952fb3f 100644 --- a/ortools/constraint_solver/search.cc +++ b/ortools/constraint_solver/search.cc @@ -85,7 +85,10 @@ std::string SearchLog::DebugString() const { return "SearchLog"; } void SearchLog::EnterSearch() { const std::string buffer = - absl::StrFormat("Start search (%s)", MemoryUsage()); + (!display_on_new_solutions_only_ && display_callback_ != nullptr) + ? absl::StrFormat("Start search (%s, %s)", MemoryUsage(), + display_callback_()) + : absl::StrFormat("Start search (%s)", MemoryUsage()); OutputLine(buffer); timer_->Restart(); min_right_depth_ = std::numeric_limits::max(); @@ -367,7 +370,8 @@ SearchMonitor* Solver::MakeSearchLog( std::vector offsets(vars.size(), 0.0); return RevAlloc(new SearchLog( this, std::move(vars), opt_var->Name(), std::move(scaling_factors), - std::move(offsets), std::move(display_callback), true, branch_period)); + std::move(offsets), std::move(display_callback), + /*display_on_new_solutions_only=*/true, branch_period)); } SearchMonitor* Solver::MakeSearchLog(SearchLogParameters parameters) { @@ -4239,7 +4243,7 @@ BinaryGuidedLocalSearch

::BinaryGuidedLocalSearch( reset_penalties_on_new_best_solution), objective_function_(std::move(objective_function)) { solver->SetGuidedLocalSearchPenaltyCallback( - [this](int64_t i, int64_t j, int64_t k) { return PenalizedValue(i, j); }); + [this](int64_t i, int64_t j, int64_t) { return PenalizedValue(i, j); }); } template