Skip to content

Commit

Permalink
constraint_solver: Backport from main
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Nov 29, 2024
1 parent 348f4e4 commit 0d54b9c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions ortools/constraint_solver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 11 additions & 11 deletions ortools/constraint_solver/constraint_solveri.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
}

Expand All @@ -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.
Expand All @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions ortools/constraint_solver/local_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <functional>
#include <limits>
#include <memory>
#include <numeric>
#include <optional>
#include <random>
#include <string>
Expand Down Expand Up @@ -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]];
}
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions ortools/constraint_solver/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t>::max();
Expand Down Expand Up @@ -367,7 +370,8 @@ SearchMonitor* Solver::MakeSearchLog(
std::vector<double> 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) {
Expand Down Expand Up @@ -4239,7 +4243,7 @@ BinaryGuidedLocalSearch<P>::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 <typename P>
Expand Down

0 comments on commit 0d54b9c

Please sign in to comment.