From 1557e142edac9230246a3a917a82e4b34ff8d68b Mon Sep 17 00:00:00 2001 From: Laio Oriel Seman Date: Fri, 27 Sep 2024 20:19:33 -0300 Subject: [PATCH] n-dimensional splay tree for fast bucket index computation --- bucket/BucketGraph.h | 54 +++-- bucket/BucketSolve.h | 38 +++- bucket/BucketUtils.h | 162 ++++++-------- examples/VRPTW.cpp | 4 +- examples/VRPTW.h | 2 +- external/hgs_vrptw/Individual.cpp | 4 +- external/hgs_vrptw/Individual.h | 15 +- external/hgs_vrptw/Population.cpp | 2 +- include/Definitions.h | 350 ++++++++++++++++++++++++++++++ include/HGS.h | 18 +- src/BucketGraph.cpp | 15 +- 11 files changed, 519 insertions(+), 145 deletions(-) diff --git a/bucket/BucketGraph.h b/bucket/BucketGraph.h index 4b3c48f7..27b7ddb5 100644 --- a/bucket/BucketGraph.h +++ b/bucket/BucketGraph.h @@ -267,6 +267,8 @@ class BucketGraph { std::thread rih_thread; std::mutex mtx; // For thread-safe access to merged_labels_improved + // + int redefine_counter = 0; std::vector> fw_ordered_sccs; std::vector> bw_ordered_sccs; @@ -338,6 +340,13 @@ class BucketGraph { double min_red_cost = std::numeric_limits::infinity(); bool first_reset = true; + std::vector dominance_checks_per_bucket; + int non_dominated_labels_per_bucket; + + // Interval tree to store bucket intervals + std::unordered_map fw_job_interval_trees; + std::unordered_map bw_job_interval_trees; + #ifdef SCHRODINGER /** * @brief Retrieves a list of paths with negative reduced costs. @@ -495,30 +504,30 @@ class BucketGraph { * * @param bucketInterval The new interval for the buckets. */ - [[maybe_unused]] void redefine(int bucketInterval) { + void redefine(int bucketInterval) { this->bucket_interval = bucketInterval; intervals.clear(); for (int i = 0; i < R_SIZE; ++i) { intervals.push_back(Interval(bucketInterval, 0)); } - define_buckets(); - define_buckets(); - - fixed_arcs.resize(getJobs().size()); - for (int i = 0; i < getJobs().size(); ++i) { fixed_arcs[i].resize(getJobs().size()); } - - // make every fixed_buckets also have size buckets.size() - fw_fixed_buckets.resize(fw_buckets.size()); - bw_fixed_buckets.resize(fw_buckets.size()); - - for (auto &fb : fw_fixed_buckets) { fb.resize(fw_buckets.size()); } - for (auto &bb : bw_fixed_buckets) { bb.resize(bw_buckets.size()); } - // set fixed_buckets to 0 - for (auto &fb : fw_fixed_buckets) { - for (std::size_t i = 0; i < fb.size(); ++i) { fb[i] = 0; } - } - for (auto &bb : bw_fixed_buckets) { - for (std::size_t i = 0; i < bb.size(); ++i) { bb[i] = 0; } - } + reset_fixed(); + reset_fixed_buckets(); + + PARALLEL_SECTIONS( + bi_sched, + SECTION { + // Section 1: Forward direction + define_buckets(); + fw_fixed_buckets.clear(); + fw_fixed_buckets.resize(fw_buckets_size); + for (auto &fb : fw_fixed_buckets) { fb.assign(fw_buckets_size, 0); } + }, + SECTION { + // Section 2: Backward direction + define_buckets(); + bw_fixed_buckets.clear(); + bw_fixed_buckets.resize(bw_buckets_size); + for (auto &bb : bw_fixed_buckets) { bb.assign(bw_buckets_size, 0); } + }); generate_arcs(); for (auto &VRPJob : jobs) { VRPJob.sort_arcs(); } @@ -575,6 +584,11 @@ class BucketGraph { for (auto &row : fixed_arcs) { std::fill(row.begin(), row.end(), 0); } } + void reset_fixed_buckets() { + for (auto &fb : fw_fixed_buckets) { std::fill(fb.begin(), fb.end(), 0); } + for (auto &bb : bw_fixed_buckets) { std::fill(bb.begin(), bb.end(), 0); } + } + /** * @brief Checks the feasibility of a given forward and backward label. * diff --git a/bucket/BucketSolve.h b/bucket/BucketSolve.h index bea4d257..8b18efaa 100644 --- a/bucket/BucketSolve.h +++ b/bucket/BucketSolve.h @@ -219,6 +219,12 @@ std::vector BucketGraph::labeling_algorithm() noexcept { const auto &to_bucket_labels = buckets[to_bucket].get_labels(); // Get existing labels in the destination bucket + if constexpr (S == Stage::Four) { + // Track dominance checks for this bucket + if constexpr (D == Direction::Forward) { + dominance_checks_per_bucket[to_bucket] += to_bucket_labels.size(); + } + } // Stage-specific dominance check if constexpr (S == Stage::One) { // If the new label has lower cost, remove dominated labels @@ -414,7 +420,10 @@ std::vector