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

Query generator #79

Merged
merged 22 commits into from
Apr 11, 2024
Merged
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[geo]
url=git@github.com:motis-project/geo.git
branch=master
commit=f455b76d5894bb156e3bcb1f85c3e2915820fe3a
commit=ee76668f0f0454e4acd3c769e00c6868620e3490
[utl]
url=git@github.com:motis-project/utl.git
branch=master
Expand Down
113 changes: 113 additions & 0 deletions include/nigiri/query_generator/generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#pragma once

#include <ctime>
#include <random>

#include "geo/point_rtree.h"

#include "nigiri/query_generator/generator_settings.h"
#include "nigiri/query_generator/transport_mode.h"
#include "nigiri/routing/query.h"

namespace nigiri {
struct timetable;
} // namespace nigiri

namespace nigiri::query_generation {

constexpr auto const kMaxGenAttempts = 10000U;

constexpr auto const kTimeOfDayWeights = std::array<int, 24>{
1, // 01: 00:00 - 01:00
1, // 02: 01:00 - 02:00
1, // 03: 02:00 - 03:00
1, // 04: 03:00 - 04:00
1, // 05: 04:00 - 05:00
2, // 06: 05:00 - 06:00
3, // 07: 06:00 - 07:00
4, // 08: 07:00 - 08:00
4, // 09: 08:00 - 09:00
3, // 10: 09:00 - 10:00
2, // 11: 10:00 - 11:00
2, // 12: 11:00 - 12:00
2, // 13: 12:00 - 13:00
2, // 14: 13:00 - 14:00
3, // 15: 14:00 - 15:00
4, // 16: 15:00 - 16:00
4, // 17: 16:00 - 17:00
4, // 18: 17:00 - 18:00
4, // 19: 18:00 - 19:00
3, // 20: 19:00 - 20:00
2, // 21: 20:00 - 21:00
1, // 22: 21:00 - 22:00
1, // 23: 22:00 - 23:00
1 // 24: 23:00 - 24:00
};

struct generator {
explicit generator(timetable const&, generator_settings const&);

// randomize a point in time within the timetable
unixtime_t random_time();

// randomize a location that is active during the interval
// for the given event type
std::optional<location_idx_t> random_active_location(
interval<unixtime_t> const&, event_type);

// use start transport mode to randomize coordinates near a location
geo::latlng pos_near_start(location_idx_t);

// uses dest transport mode to randomize coordinates near a location
geo::latlng pos_near_dest(location_idx_t);

// randomize a transport and one of its stops that allows the given event type
std::pair<transport, stop_idx_t> random_transport_active_stop(event_type et);

// convenience functions for query generation inside nigiri
std::optional<routing::query> random_pretrip_query();
std::optional<routing::query> random_ontrip_query();

timetable const& tt_;
generator_settings const& s_;

private:
transport_idx_t random_transport_idx();
day_idx_t random_day();

std::optional<day_idx_t> random_active_day(transport_idx_t);
std::optional<stop_idx_t> random_active_stop(transport_idx_t, event_type);

geo::latlng random_point_in_range(
geo::latlng const&, std::uniform_int_distribution<std::uint32_t>&);

interval<day_idx_t> unix_to_day_interval(interval<unixtime_t> const&);
std::uint16_t tt_n_days();

routing::query make_query() const;

void add_offsets_for_pos(std::vector<routing::offset>&,
geo::latlng const&,
query_generation::transport_mode const&);

// R-Tree
geo::point_rtree locations_rtree_;

// RNG
std::random_device rd_;
std::mt19937 rng_{rd_()};

// Distributions
std::uniform_int_distribution<location_idx_t::value_t> location_d_;
std::uniform_int_distribution<date::sys_days::rep> date_d_;
std::uniform_int_distribution<transport_idx_t::value_t> transport_d_;
std::uniform_int_distribution<day_idx_t::value_t> day_d_;
std::uniform_int_distribution<std::uint32_t> start_mode_range_d_;
std::uniform_int_distribution<std::uint32_t> dest_mode_range_d_;
std::discrete_distribution<std::uint16_t> hours_d_{begin(kTimeOfDayWeights),
end(kTimeOfDayWeights)};
std::uniform_int_distribution<std::uint16_t> minutes_d_{0, 59};
std::uniform_int_distribution<std::uint16_t> bearing_d_{0, 359};
};

} // namespace nigiri::query_generation
29 changes: 29 additions & 0 deletions include/nigiri/query_generator/generator_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "nigiri/routing/clasz_mask.h"
#include "nigiri/routing/limits.h"
#include "nigiri/routing/location_match_mode.h"
#include "nigiri/timetable.h"

#include "transport_mode.h"

namespace nigiri::query_generation {

struct generator_settings {
mority marked this conversation as resolved.
Show resolved Hide resolved
duration_t interval_size_{60U};
routing::location_match_mode start_match_mode_{
routing::location_match_mode::kIntermodal};
routing::location_match_mode dest_match_mode_{
routing::location_match_mode::kIntermodal};
transport_mode start_mode_{kWalk};
transport_mode dest_mode_{kWalk};
bool use_start_footpaths_{true};
std::uint8_t max_transfers_{routing::kMaxTransfers};
unsigned min_connection_count_{0U};
bool extend_interval_earlier_{false};
bool extend_interval_later_{false};
profile_idx_t prf_idx_{0};
routing::clasz_mask_t allowed_claszes_{routing::all_clasz_allowed()};
};

} // namespace nigiri::query_generation
25 changes: 25 additions & 0 deletions include/nigiri/query_generator/transport_mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <cstdint>
#include "nigiri/types.h"

namespace nigiri::query_generation {

struct transport_mode {
constexpr std::uint32_t range() const {
return speed_ * max_duration_;
} // [m]

transport_mode_id_t mode_id_;
std::uint16_t speed_; // [m/minute]
std::uint16_t max_duration_; // [minutes]
};

constexpr auto const kWalk =
transport_mode{.mode_id_ = 1, .speed_ = 50U, .max_duration_ = 15U};
constexpr auto const kBicycle =
transport_mode{.mode_id_ = 2, .speed_ = 200U, .max_duration_ = 15U};
constexpr auto const kCar =
transport_mode{.mode_id_ = 3, .speed_ = 800U, .max_duration_ = 15U};

} // namespace nigiri::query_generation
10 changes: 6 additions & 4 deletions include/nigiri/routing/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ namespace nigiri::routing {
using start_type_t = std::int32_t;

struct offset {
offset(location_idx_t const l, duration_t const d, start_type_t const t)
: target_{l}, duration_{d}, type_{t} {}
offset(location_idx_t const l,
duration_t const d,
transport_mode_id_t const t)
: target_{l}, duration_{d}, transport_mode_id_{t} {}

location_idx_t target() const noexcept { return target_; }
duration_t duration() const noexcept { return duration_; }
start_type_t type() const noexcept { return type_; }
transport_mode_id_t type() const noexcept { return transport_mode_id_; }

location_idx_t target_;
duration_t duration_;
start_type_t type_;
transport_mode_id_t transport_mode_id_;
};

using start_time_t = std::variant<unixtime_t, interval<unixtime_t>>;
Expand Down
2 changes: 2 additions & 0 deletions include/nigiri/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ enum class location_type : std::uint8_t {
enum class event_type { kArr, kDep };
enum class direction { kForward, kBackward };

using transport_mode_id_t = std::int32_t;
felixguendling marked this conversation as resolved.
Show resolved Hide resolved

} // namespace nigiri

#include <iomanip>
Expand Down
Loading
Loading