Skip to content

Commit

Permalink
add trip mode TAXI
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchenplus committed Oct 23, 2024
1 parent 03d0480 commit 2d9d872
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
7 changes: 5 additions & 2 deletions examples/trip_generate_gravity_od.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import geopandas as gpd
import numpy as np
from mosstool.util.format_converter import pb2json

from mosstool.trip.generator import GravityGenerator
from mosstool.trip.generator.generate_from_od import TripGenerator
Expand Down Expand Up @@ -61,8 +62,10 @@ async def main():
seed=0,
)
pb = Persons(persons=od_persons)
with open("data/temp/beijing_OD_person.pb", "wb") as f:
f.write(pb.SerializeToString())
# with open("data/temp/beijing_OD_person.pb", "wb") as f:
# f.write(pb.SerializeToString())
with open("data/temp/persons.json", "w") as f:
f.write(pb2json(pb))

# # The generated trip of the person is not guaranteed to be reachable in the map. Preroute is required.
# # pre-route
Expand Down
1 change: 1 addition & 0 deletions mosstool/trip/generator/_util/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
}
BUS = tripv2.TRIP_MODE_BUS_WALK
CAR = tripv2.TRIP_MODE_DRIVE_ONLY
TAXI = tripv2.TRIP_MODE_TAXI
BIKE = tripv2.TRIP_MODE_BIKE_WALK
WALK = tripv2.TRIP_MODE_WALK_ONLY
LANE_TYPE_DRIVING = mapv2.LANE_TYPE_DRIVING
Expand Down
14 changes: 14 additions & 0 deletions mosstool/trip/generator/_util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
__all__ = [
"is_walking",
"gen_profiles",
"recalculate_trip_modes",
"recalculate_trip_mode_prob",
"gen_bus_drivers",
]
Expand Down Expand Up @@ -100,6 +101,19 @@ def gen_profiles(agent_num: int, workers: int) -> List[Dict]:
)
return profiles

def recalculate_trip_modes(profile: dict,trip_modes:List[int])->List[int]:
res_modes = np.array([m for m in trip_modes], dtype=np.uint8)
if (
profile.get("consumption",-1)
in {
personv2.CONSUMPTION_LOW,
personv2.CONSUMPTION_RELATIVELY_LOW,
}
or not _in_range(profile.get("age",24), 18, 70)
):
# no car to drive
res_modes[np.where(res_modes == CAR)] = TAXI
return [m for m in res_modes]

def recalculate_trip_mode_prob(profile: dict, V: np.ndarray):
"""
Expand Down
5 changes: 3 additions & 2 deletions mosstool/trip/generator/generate_from_od.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ...util.format_converter import dict2pb, pb2dict
from ._util.const import *
from ._util.utils import (extract_HWEO_from_od_matrix, gen_bus_drivers,
gen_departure_times, gen_profiles,
gen_departure_times, gen_profiles,recalculate_trip_modes,
recalculate_trip_mode_prob)
from .template import V1_DEFAULT_PERSON, V2_DEFAULT_PERSON

Expand Down Expand Up @@ -83,11 +83,12 @@ def _get_mode_with_distribution(
V_bicycle = -0.1185 * bicycle_duration / 60
V = np.array([V_bus, V_subway, V_fuel, V_elec, V_bicycle])
V = np.exp(V)
_all_trip_modes = recalculate_trip_modes(profile,ALL_TRIP_MODES)
V = recalculate_trip_mode_prob(profile, V)
V = V / sum(V)
rng = np.random.default_rng(seed)
choice_index = rng.choice(len(V), p=V)
return ALL_TRIP_MODES[choice_index]
return _all_trip_modes[choice_index]


def _match_aoi_unit(projector, aoi):
Expand Down
1 change: 1 addition & 0 deletions mosstool/trip/route/preroute.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

_TYPE_MAP = {
TripMode.TRIP_MODE_DRIVE_ONLY: RouteType.ROUTE_TYPE_DRIVING,
TripMode.TRIP_MODE_TAXI: RouteType.ROUTE_TYPE_DRIVING,
TripMode.TRIP_MODE_BIKE_WALK: RouteType.ROUTE_TYPE_WALKING,
TripMode.TRIP_MODE_BUS_WALK: RouteType.ROUTE_TYPE_BY_BUS,
TripMode.TRIP_MODE_WALK_ONLY: RouteType.ROUTE_TYPE_WALKING,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mosstool"
version = "0.2.43"
version = "0.2.44"
description = "MObility Simulation System toolbox "
authors = ["Jun Zhang <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 2d9d872

Please sign in to comment.