Skip to content

Commit

Permalink
Bikesharing profile (#20)
Browse files Browse the repository at this point in the history
* add bike_sharing profile

* clang-format-18

* fix compile errors

* clang-format

* fix windows mimalloc build

* update pkg.cmake
  • Loading branch information
pablohoch authored Oct 25, 2024
1 parent 48ac3ab commit dc598c3
Show file tree
Hide file tree
Showing 19 changed files with 697 additions and 119 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996") # ignore deprecated fileno in tiles
endif ()

option(OSR_MIMALLOC "use mimalloc" OFF)

if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
if (OSR_MIMALLOC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
Expand All @@ -29,7 +31,6 @@ if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
endif ()
endif ()

option(OSR_MIMALLOC "use mimalloc" OFF)
if (OSR_MIMALLOC AND WIN32)
set(MI_BUILD_SHARED ON)
endif ()
Expand Down Expand Up @@ -155,9 +156,9 @@ if (OSR_MIMALLOC)
add_custom_command(
TARGET osr-extract POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_BINARY_DIR}/deps/mimalloc/mimalloc-redirect.dll"
"${CMAKE_BINARY_DIR}/deps/mimalloc/mimalloc-redirect.dll"
$<TARGET_FILE_DIR:osr>
COMMENT "Copy mimalloc-redirect.dll to output directory"
)
endif ()
endif ()
endif ()
95 changes: 54 additions & 41 deletions cmake/pkg.cmake
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
if (NOT DEFINED PROJECT_IS_TOP_LEVEL OR PROJECT_IS_TOP_LEVEL)
find_program(pkg-bin pkg HINTS /opt/pkg)
if (pkg-bin)
message(STATUS "found pkg ${pkg-bin}")
else ()
set(pkg-bin "${CMAKE_BINARY_DIR}/dl/pkg")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "aarch64")
set(pkg-url "pkg-linux-arm64")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(pkg-url "pkg")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(pkg-url "pkg.exe")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
set(pkg-url "pkgosx")
find_program(pkg-bin pkg HINTS /opt/pkg)
if (pkg-bin)
message(STATUS "found pkg ${pkg-bin}")
else ()
message(STATUS "Not downloading pkg tool. Using pkg from PATH.")
set(pkg-bin "pkg")
endif ()
set(pkg-bin "${CMAKE_BINARY_DIR}/dl/pkg")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "aarch64")
set(pkg-url "pkg-linux-arm64")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(pkg-url "pkg")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(pkg-url "pkg.exe")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
set(pkg-url "pkgosx")
else ()
message(STATUS "Not downloading pkg tool. Using pkg from PATH.")
set(pkg-bin "pkg")
endif ()

if (pkg-url)
if (NOT EXISTS ${pkg-bin})
message(STATUS "Downloading pkg binary from https://github.com/motis-project/pkg/releases/latest/download/${pkg-url}")
file(DOWNLOAD "https://github.com/motis-project/pkg/releases/latest/download/${pkg-url}" ${pkg-bin})
if (UNIX)
execute_process(COMMAND chmod +x ${pkg-bin})
if (pkg-url)
if (NOT EXISTS ${pkg-bin})
message(STATUS "Downloading pkg binary from https://github.com/motis-project/pkg/releases/latest/download/${pkg-url}")
file(DOWNLOAD "https://github.com/motis-project/pkg/releases/latest/download/${pkg-url}" ${pkg-bin})
if (UNIX)
execute_process(COMMAND chmod +x ${pkg-bin})
endif ()
else ()
message(STATUS "Pkg binary located in project.")
endif ()
endif ()
else ()
message(STATUS "Pkg binary located in project.")
endif ()
endif ()
endif ()

message(STATUS ${pkg-bin})
if (DEFINED ENV{GITHUB_ACTIONS})
message(STATUS "${pkg-bin} -l -h -f")
execute_process(
COMMAND ${pkg-bin} -l -h -f
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE pkg-result
)
else ()
message(STATUS "${pkg-bin} -l")
execute_process(
COMMAND ${pkg-bin} -l
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE pkg-result
)
endif ()

if (DEFINED ENV{GITHUB_ACTIONS})
execute_process(
COMMAND ${pkg-bin} -l -h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
else ()
execute_process(
COMMAND ${pkg-bin} -l
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif ()
endif ()
if (NOT pkg-result EQUAL 0)
message(FATAL_ERROR "pkg failed: ${pkg-result}")
endif ()

if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/deps")
add_subdirectory(deps)
if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/deps")
add_subdirectory(deps)
endif ()

set_property(
DIRECTORY
APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/.pkg"
)
endif ()
6 changes: 5 additions & 1 deletion exe/backend/src/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "osr/geojson.h"
#include "osr/lookup.h"
#include "osr/routing/profiles/bike.h"
#include "osr/routing/profiles/bike_sharing.h"
#include "osr/routing/profiles/car.h"
#include "osr/routing/profiles/car_parking.h"
#include "osr/routing/profiles/foot.h"
Expand Down Expand Up @@ -196,6 +197,9 @@ struct http_server::impl {
case search_profile::kCarParkingWheelchair:
send_graph_response<car_parking<true>>(req, cb, gj);
break;
case search_profile::kBikeSharing:
send_graph_response<bike_sharing>(req, cb, gj);
break;
default: throw utl::fail("not implemented");
}
}
Expand Down Expand Up @@ -369,4 +373,4 @@ void http_server::listen(std::string const& host, std::string const& port) {

void http_server::stop() { impl_->stop(); }

} // namespace osr::backend
} // namespace osr::backend
3 changes: 2 additions & 1 deletion exe/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ int main(int argc, char const* argv[]) {
d.reset(opt.max_dist_);
d.add_start(car::label{car::node{start, 0, direction::kForward}, 0U});
d.add_start(car::label{car::node{start, 0, direction::kBackward}, 0U});
d.run<direction::kForward, false>(*w.r_, opt.max_dist_, nullptr);
d.run<direction::kForward, false>(*w.r_, opt.max_dist_, nullptr,
nullptr);
}
});
}
Expand Down
14 changes: 14 additions & 0 deletions include/osr/routing/additional_edge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "osr/types.h"

namespace osr {

struct additional_edge {
friend bool operator==(additional_edge, additional_edge) = default;

node_idx_t node_{};
distance_t distance_{};
};

} // namespace osr
19 changes: 12 additions & 7 deletions include/osr/routing/dijkstra.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include "osr/routing/additional_edge.h"
#include "osr/routing/dial.h"
#include "osr/types.h"
#include "osr/ways.h"

namespace osr {

struct sharing_data;

template <typename Profile>
struct dijkstra {
using profile_t = Profile;
Expand Down Expand Up @@ -40,7 +43,8 @@ struct dijkstra {
template <direction SearchDir, bool WithBlocked>
void run(ways::routing const& r,
cost_t const max,
bitvec<node_idx_t> const* blocked) {
bitvec<node_idx_t> const* blocked,
sharing_data const* sharing) {
while (!pq_.empty()) {
auto l = pq_.pop();
if (get_cost(l.get_node()) < l.cost()) {
Expand All @@ -49,7 +53,7 @@ struct dijkstra {

auto const curr = l.get_node();
Profile::template adjacent<SearchDir, WithBlocked>(
r, curr, blocked,
r, curr, blocked, sharing,
[&](node const neighbor, std::uint32_t const cost, distance_t,
way_idx_t const way, std::uint16_t, std::uint16_t) {
auto const total = l.cost() + cost;
Expand All @@ -67,20 +71,21 @@ struct dijkstra {
void run(ways::routing const& r,
cost_t const max,
bitvec<node_idx_t> const* blocked,
sharing_data const* sharing,
direction const dir) {
if (blocked == nullptr) {
dir == direction::kForward
? run<direction::kForward, false>(r, max, blocked)
: run<direction::kBackward, false>(r, max, blocked);
? run<direction::kForward, false>(r, max, blocked, sharing)
: run<direction::kBackward, false>(r, max, blocked, sharing);
} else {
dir == direction::kForward
? run<direction::kForward, true>(r, max, blocked)
: run<direction::kBackward, true>(r, max, blocked);
? run<direction::kForward, true>(r, max, blocked, sharing)
: run<direction::kBackward, true>(r, max, blocked, sharing);
}
}

dial<label, get_bucket> pq_{get_bucket{}};
ankerl::unordered_dense::map<key, entry, hash> cost_;
};

} // namespace osr
} // namespace osr
17 changes: 17 additions & 0 deletions include/osr/routing/mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <cinttypes>
#include <string_view>

namespace osr {

enum class mode : std::uint8_t {
kFoot,
kWheelchair,
kBike,
kCar,
};

std::string_view to_str(mode);

} // namespace osr
5 changes: 3 additions & 2 deletions include/osr/routing/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ enum class search_profile : std::uint8_t {
kBike,
kCar,
kCarParking,
kCarParkingWheelchair
kCarParkingWheelchair,
kBikeSharing,
};

search_profile to_profile(std::string_view);

std::string_view to_str(search_profile);

} // namespace osr
} // namespace osr
8 changes: 7 additions & 1 deletion include/osr/routing/profiles/bike.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

#include "osr/routing/mode.h"
#include "osr/routing/route.h"
#include "osr/ways.h"

namespace osr {

struct sharing_data;

struct bike {
static constexpr auto const kMaxMatchDistance = 100U;
static constexpr auto const kOffroadPenalty = 1U;
Expand All @@ -20,6 +23,8 @@ struct bike {

constexpr node get_key() const noexcept { return *this; }

static constexpr mode get_mode() noexcept { return mode::kBike; }

std::ostream& print(std::ostream& out, ways const& w) const {
return out << w.node_to_osm_[n_];
}
Expand Down Expand Up @@ -101,6 +106,7 @@ struct bike {
static void adjacent(ways::routing const& w,
node const n,
bitvec<node_idx_t> const* blocked,
sharing_data const*,
Fn&& fn) {
for (auto const [way, i] :
utl::zip_unchecked(w.node_ways_[n.n_], w.node_in_way_idx_[n.n_])) {
Expand Down Expand Up @@ -154,4 +160,4 @@ struct bike {
}
};

} // namespace osr
} // namespace osr
Loading

0 comments on commit dc598c3

Please sign in to comment.