Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid solution "shadowing" when comparing indicators #1205

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Crash when input is valid JSON but not an object (#1172)
- Capacity array check consistency (#1086)
- Segfault when using the C++ API with empty vehicles (#1187)
- Solution "shadowing" when only comparing indicators (#1199)

#### Internals

Expand Down
43 changes: 29 additions & 14 deletions src/structures/vroom/solution_indicators.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All rights reserved (see LICENSE).

*/

#include <algorithm>
#include <tuple>

#include "structures/typedefs.h"
Expand All @@ -23,6 +24,8 @@ struct SolutionIndicators {
unsigned assigned{0};
Eval eval;
unsigned used_vehicles{0};
// Hash based on the ordered sizes of routes in the solution.
uint32_t routes_hash;

SolutionIndicators() = default;

Expand All @@ -41,6 +44,14 @@ struct SolutionIndicators {
used_vehicles += 1;
}
}

std::vector<uint32_t> routes_sizes;
routes_sizes.reserve(sol.size());
std::ranges::transform(sol,
std::back_inserter(routes_sizes),
[](const auto& r) { return std::size(r); });
std::ranges::sort(routes_sizes);
routes_hash = get_vector_hash(routes_sizes);
}

friend bool operator<(const SolutionIndicators& lhs,
Expand All @@ -50,28 +61,32 @@ struct SolutionIndicators {
lhs.eval.cost,
lhs.used_vehicles,
lhs.eval.duration,
lhs.eval.distance) < std::tie(lhs.priority_sum,
lhs.assigned,
rhs.eval.cost,
rhs.used_vehicles,
rhs.eval.duration,
rhs.eval.distance);
lhs.eval.distance,
lhs.routes_hash) < std::tie(lhs.priority_sum,
lhs.assigned,
rhs.eval.cost,
rhs.used_vehicles,
rhs.eval.duration,
rhs.eval.distance,
rhs.routes_hash);
}

#ifdef LOG_LS
friend bool operator==(const SolutionIndicators& lhs,
const SolutionIndicators& rhs) {
return std::tie(rhs.priority_sum,
rhs.assigned,
return std::tie(lhs.priority_sum,
lhs.assigned,
lhs.eval.cost,
lhs.used_vehicles,
lhs.eval.duration,
lhs.eval.distance) == std::tie(lhs.priority_sum,
lhs.assigned,
rhs.eval.cost,
rhs.used_vehicles,
rhs.eval.duration,
rhs.eval.distance);
lhs.eval.distance,
lhs.routes_hash) == std::tie(rhs.priority_sum,
rhs.assigned,
rhs.eval.cost,
rhs.used_vehicles,
rhs.eval.duration,
rhs.eval.distance,
rhs.routes_hash);
}
#endif
};
Expand Down
12 changes: 12 additions & 0 deletions src/utils/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ inline UserCost add_without_overflow(UserCost a, UserCost b) {
return a + b;
}

// Taken from https://stackoverflow.com/a/72073933.
inline uint32_t get_vector_hash(const std::vector<uint32_t>& vec) {
uint32_t seed = vec.size();
for (auto x : vec) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}

inline unsigned get_depth(unsigned exploration_level) {
return exploration_level;
}
Expand Down