Skip to content

Commit

Permalink
Use a slightly faster rounding procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
acco93 committed Mar 11, 2024
1 parent 51eb9a6 commit 42276b7
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions instance/Instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#include "Parser.hpp"

namespace cobra {
namespace {
inline double fastround(double value) {
return static_cast<int>(value + 0.5);
}
} // namespace

// Manages a CVRP instance by providing a set of methods to query its properties.
class Instance : private NonCopyable<Instance> {
Expand Down Expand Up @@ -64,8 +69,11 @@ namespace cobra {
assert(i >= get_vertices_begin() && i < get_vertices_end());
assert(j >= get_vertices_begin() && j < get_vertices_end());

return std::round(
std::sqrt((xcoords[i] - xcoords[j]) * (xcoords[i] - xcoords[j]) + (ycoords[i] - ycoords[j]) * (ycoords[i] - ycoords[j])));
const double sqrt = std::sqrt((xcoords[i] - xcoords[j]) * (xcoords[i] - xcoords[j]) +
(ycoords[i] - ycoords[j]) * (ycoords[i] - ycoords[j]));
assert(static_cast<int>(std::round(sqrt)) == static_cast<int>(fastround(sqrt)));

return fastround(sqrt);
}

// Returns the demand of vertex `i`. The demand is 0 for the depot.
Expand Down

0 comments on commit 42276b7

Please sign in to comment.