Skip to content

Commit

Permalink
remove person.v1
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchenplus committed Sep 28, 2024
1 parent 03d0480 commit 813f6dd
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 296 deletions.
9 changes: 4 additions & 5 deletions examples/random_persons.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from mosstool.trip.generator import (V1_DEFAULT_PERSON, V2_DEFAULT_PERSON,
PositionMode, RandomGenerator)
from mosstool.trip.generator import (DEFAULT_PERSON, PositionMode,
RandomGenerator)
from mosstool.trip.route import RoutingClient, pre_route
from mosstool.type import Map, TripMode, Person, Persons
from mosstool.type import Map, Person, Persons, TripMode
from mosstool.util.format_converter import pb2json


Expand All @@ -11,7 +11,7 @@ async def main():
m.ParseFromString(f.read())

template = Person()
template.CopyFrom(V1_DEFAULT_PERSON)
template.CopyFrom(DEFAULT_PERSON)
template.vehicle_attribute.model = "normal"
template.pedestrian_attribute.model = "normal"
rg = RandomGenerator(
Expand Down Expand Up @@ -43,7 +43,6 @@ async def main():
with open("data/temp/persons.json", "w") as f:
f.write(pb2json(pb))


if __name__ == "__main__":
import asyncio

Expand Down
5 changes: 2 additions & 3 deletions mosstool/trip/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from .generate_from_od import TripGenerator
from .gravity import GravityGenerator
from .random import PositionMode, RandomGenerator
from .template import V1_DEFAULT_PERSON,V2_DEFAULT_PERSON
from .template import DEFAULT_PERSON

__all__ = [
"V1_DEFAULT_PERSON",
"V2_DEFAULT_PERSON",
"DEFAULT_PERSON",
"RandomGenerator",
"PositionMode",
"GravityGenerator",
Expand Down
298 changes: 98 additions & 200 deletions mosstool/trip/generator/_util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
from typing import Dict, List, Literal, Optional, Set, Tuple, Union, cast

import numpy as np
from pycityproto.city.person.v2.person_pb2 import (BusAttribute, BusType,Person as v2Person,
PersonAttribute)
from pycityproto.city.person.v1.person_pb2 import (BusAttribute as v1BusAttribute, BusType as v1BusType,
PersonAttribute as v1PersonAttribute, Person as v1Person)
from pycityproto.city.person.v2.person_pb2 import (BusAttribute, BusType,
Person, PersonAttribute)
from pycityproto.city.routing.v2.routing_pb2 import (DrivingJourneyBody,
Journey, JourneyType)
from pycityproto.city.trip.v2.trip_pb2 import TripStop

from ....map._map_util.const import *
from ....type import (AoiPosition, LanePosition, Position, Schedule, Trip, TripMode)
from ....type import (AoiPosition, LanePosition, Position, Schedule, Trip,
TripMode)
from ....util.format_converter import dict2pb, pb2dict
from .const import *

Expand Down Expand Up @@ -243,13 +242,12 @@ def extract_HWEO_from_od_matrix(

def gen_bus_drivers(
person_id: int,
person_template: v2Person,
person_template: Person,
depart_times: List[float],
stop_duration_time: float,
road_aoi_id2d_pos: Dict[Tuple[int, int], geov2.LanePosition],
subline,
person_version:str,
) -> Tuple[int, List[v2Person]]:
) -> Tuple[int, List[Person]]:
def _transfer_conn_road_ids(
station_connection_road_ids: List[List[int]],
) -> List[int]:
Expand All @@ -270,204 +268,104 @@ def _aoi_road_ids(station_connection_road_ids) -> List[int]:
# road that connect to the end station
cur_road_ids.append(station_connection_road_ids[-1][-1])
return cur_road_ids
if person_version=="v1":
sl_id = subline.id
# basic attributes
sl_type = subline.type
bus_type = v1BusType.BUS_TYPE_UNSPECIFIED
if sl_type == mapv2.SUBLINE_TYPE_BUS:
sl_capacity = STATION_CAPACITY["BUS"]
sl_attributes = PT_DRIVER_ATTRIBUTES["BUS"]
bus_type = v1BusType.BUS_TYPE_BUS
elif sl_type == mapv2.SUBLINE_TYPE_SUBWAY:
sl_capacity = STATION_CAPACITY["SUBWAY"]
sl_attributes = PT_DRIVER_ATTRIBUTES["SUBWAY"]
bus_type = v1BusType.BUS_TYPE_SUBWAY
elif sl_type == mapv2.SUBLINE_TYPE_UNSPECIFIED:
sl_capacity = STATION_CAPACITY["UNSPECIFIED"]
sl_attributes = PT_DRIVER_ATTRIBUTES["UNSPECIFIED"]
bus_type = v1BusType.BUS_TYPE_UNSPECIFIED
else:
raise ValueError(f"Bad Subline Type {sl_type}")
# road ids
station_connection_road_ids = [
[rid for rid in rids.road_ids] for rids in subline.station_connection_road_ids
]
route_road_ids = _transfer_conn_road_ids(station_connection_road_ids)
trip_stop_road_ids = _aoi_road_ids(
station_connection_road_ids
) # stop road ids during the trip
# trip stops
aoi_ids = list(subline.aoi_ids)
home_aoi_id, end_aoi_id = aoi_ids[0], aoi_ids[-1]
trip_stop_aoi_ids = aoi_ids # stop aoi ids during the trip
trip_stop_lane_id_s = []
for cur_road_id, cur_aoi_id in zip(trip_stop_road_ids, trip_stop_aoi_ids):
road_aoi_key = (cur_road_id, cur_aoi_id)
if road_aoi_key not in road_aoi_id2d_pos:
raise ValueError(f"bad road and AOI pair {road_aoi_key}")
d_pos = road_aoi_id2d_pos[road_aoi_key]
d_lane_id, d_s = d_pos.lane_id, d_pos.s
trip_stop_lane_id_s.append((d_lane_id, d_s))
assert len(trip_stop_lane_id_s) == len(
trip_stop_aoi_ids
), f"Bad PublicTransport Route at {aoi_ids}"
p_trip_stops = []
# bus attribute
p_bus_attr = v1BusAttribute(
subline_id=sl_id, capacity=sl_capacity, type=bus_type, model=""
)
for (d_lane_id, d_s), aoi_id in zip(trip_stop_lane_id_s, trip_stop_aoi_ids):
trip_stop = TripStop()
trip_stop.lane_position.CopyFrom(LanePosition(lane_id=d_lane_id, s=d_s))
trip_stop.aoi_position.CopyFrom(AoiPosition(aoi_id=aoi_id))
trip_stop.duration = stop_duration_time
p_trip_stops.append(trip_stop)
# eta for bus journey
bus_eta = sum(subline.schedules.offset_times)
sl_drivers = []
if bus_type == BusType.BUS_TYPE_BUS:
for tm in depart_times:
p = v1Person()
p.CopyFrom(person_template)
p.id = person_id
if sl_attributes:
p.attribute.CopyFrom(dict2pb(sl_attributes, v1PersonAttribute()))
p.bus_attribute.CopyFrom(p_bus_attr)
p.home.CopyFrom(Position(aoi_position=AoiPosition(aoi_id=home_aoi_id)))
schedule = cast(Schedule, p.schedules.add())
schedule.departure_time = tm
schedule.loop_count = 1
trip = Trip(
mode=cast(
TripMode,
CAR,
),
end=Position(aoi_position=AoiPosition(aoi_id=end_aoi_id)),
activity="",
model="",
trip_stops=p_trip_stops,
routes=[
Journey(
driving=DrivingJourneyBody(
road_ids=route_road_ids, eta=bus_eta
),
type=JourneyType.JOURNEY_TYPE_DRIVING,
)
],
)
schedule.trips.append(trip)
person_id += 1
sl_drivers.append(p)
elif bus_type == BusType.BUS_TYPE_SUBWAY:
# empty schedule
p = v1Person()
p.CopyFrom(person_template)
p.id = person_id
if sl_attributes:
p.attribute.CopyFrom(dict2pb(sl_attributes, v1PersonAttribute()))
p.bus_attribute.CopyFrom(p_bus_attr)
p.home.CopyFrom(Position(aoi_position=AoiPosition(aoi_id=home_aoi_id)))
person_id += 1
sl_drivers.append(p)
else:
sl_id = subline.id
# basic attributes
sl_type = subline.type

sl_id = subline.id
# basic attributes
sl_type = subline.type
bus_type = BusType.BUS_TYPE_UNSPECIFIED
if sl_type == mapv2.SUBLINE_TYPE_BUS:
sl_capacity = STATION_CAPACITY["BUS"]
sl_attributes = PT_DRIVER_ATTRIBUTES["BUS"]
bus_type = BusType.BUS_TYPE_BUS
elif sl_type == mapv2.SUBLINE_TYPE_SUBWAY:
sl_capacity = STATION_CAPACITY["SUBWAY"]
sl_attributes = PT_DRIVER_ATTRIBUTES["SUBWAY"]
bus_type = BusType.BUS_TYPE_SUBWAY
elif sl_type == mapv2.SUBLINE_TYPE_UNSPECIFIED:
sl_capacity = STATION_CAPACITY["UNSPECIFIED"]
sl_attributes = PT_DRIVER_ATTRIBUTES["UNSPECIFIED"]
bus_type = BusType.BUS_TYPE_UNSPECIFIED
if sl_type == mapv2.SUBLINE_TYPE_BUS:
sl_capacity = STATION_CAPACITY["BUS"]
sl_attributes = PT_DRIVER_ATTRIBUTES["BUS"]
bus_type = BusType.BUS_TYPE_BUS
elif sl_type == mapv2.SUBLINE_TYPE_SUBWAY:
sl_capacity = STATION_CAPACITY["SUBWAY"]
sl_attributes = PT_DRIVER_ATTRIBUTES["SUBWAY"]
bus_type = BusType.BUS_TYPE_SUBWAY
elif sl_type == mapv2.SUBLINE_TYPE_UNSPECIFIED:
sl_capacity = STATION_CAPACITY["UNSPECIFIED"]
sl_attributes = PT_DRIVER_ATTRIBUTES["UNSPECIFIED"]
bus_type = BusType.BUS_TYPE_UNSPECIFIED
else:
raise ValueError(f"Bad Subline Type {sl_type}")
# road ids
station_connection_road_ids = [
[rid for rid in rids.road_ids] for rids in subline.station_connection_road_ids
]
route_road_ids = _transfer_conn_road_ids(station_connection_road_ids)
trip_stop_road_ids = _aoi_road_ids(
station_connection_road_ids
) # stop road ids during the trip
# trip stops
aoi_ids = list(subline.aoi_ids)
home_aoi_id, end_aoi_id = aoi_ids[0], aoi_ids[-1]
trip_stop_aoi_ids = aoi_ids # stop aoi ids during the trip
trip_stop_lane_id_s = []
for cur_road_id, cur_aoi_id in zip(trip_stop_road_ids, trip_stop_aoi_ids):
road_aoi_key = (cur_road_id, cur_aoi_id)
if road_aoi_key not in road_aoi_id2d_pos:
raise ValueError(f"bad road and AOI pair {road_aoi_key}")
d_pos = road_aoi_id2d_pos[road_aoi_key]
d_lane_id, d_s = d_pos.lane_id, d_pos.s
trip_stop_lane_id_s.append((d_lane_id, d_s))
assert len(trip_stop_lane_id_s) == len(
trip_stop_aoi_ids
), f"Bad PublicTransport Route at {aoi_ids}"
p_trip_stops = []
# bus attribute
p_bus_attr = BusAttribute(
subline_id=sl_id, capacity=sl_capacity, type=bus_type, model=""
)
for (d_lane_id, d_s), aoi_id in zip(trip_stop_lane_id_s, trip_stop_aoi_ids):
trip_stop = TripStop()
trip_stop.lane_position.CopyFrom(LanePosition(lane_id=d_lane_id, s=d_s))
trip_stop.aoi_position.CopyFrom(AoiPosition(aoi_id=aoi_id))
trip_stop.duration = stop_duration_time
p_trip_stops.append(trip_stop)
# eta for bus journey
bus_eta = sum(subline.schedules.offset_times)
sl_drivers = []
if bus_type == BusType.BUS_TYPE_BUS:
for tm in depart_times:
p = v2Person()
p.CopyFrom(person_template)
p.id = person_id
if sl_attributes:
p.attribute.CopyFrom(dict2pb(sl_attributes, PersonAttribute()))
p.bus_attribute.CopyFrom(p_bus_attr)
p.home.CopyFrom(Position(aoi_position=AoiPosition(aoi_id=home_aoi_id)))
schedule = cast(Schedule, p.schedules.add())
schedule.departure_time = tm
schedule.loop_count = 1
trip = Trip(
mode=cast(
TripMode,
CAR,
),
end=Position(aoi_position=AoiPosition(aoi_id=end_aoi_id)),
activity="",
model="",
trip_stops=p_trip_stops,
routes=[
Journey(
driving=DrivingJourneyBody(
road_ids=route_road_ids, eta=bus_eta
),
type=JourneyType.JOURNEY_TYPE_DRIVING,
)
],
)
schedule.trips.append(trip)
person_id += 1
sl_drivers.append(p)
elif bus_type == BusType.BUS_TYPE_SUBWAY:
# empty schedule
p = v2Person()
else:
raise ValueError(f"Bad Subline Type {sl_type}")
# road ids
station_connection_road_ids = [
[rid for rid in rids.road_ids] for rids in subline.station_connection_road_ids
]
route_road_ids = _transfer_conn_road_ids(station_connection_road_ids)
trip_stop_road_ids = _aoi_road_ids(
station_connection_road_ids
) # stop road ids during the trip
# trip stops
aoi_ids = list(subline.aoi_ids)
home_aoi_id, end_aoi_id = aoi_ids[0], aoi_ids[-1]
trip_stop_aoi_ids = aoi_ids # stop aoi ids during the trip
trip_stop_lane_id_s = []
for cur_road_id, cur_aoi_id in zip(trip_stop_road_ids, trip_stop_aoi_ids):
road_aoi_key = (cur_road_id, cur_aoi_id)
if road_aoi_key not in road_aoi_id2d_pos:
raise ValueError(f"bad road and AOI pair {road_aoi_key}")
d_pos = road_aoi_id2d_pos[road_aoi_key]
d_lane_id, d_s = d_pos.lane_id, d_pos.s
trip_stop_lane_id_s.append((d_lane_id, d_s))
assert len(trip_stop_lane_id_s) == len(
trip_stop_aoi_ids
), f"Bad PublicTransport Route at {aoi_ids}"
p_trip_stops = []
# bus attribute
p_bus_attr = BusAttribute(
subline_id=sl_id, capacity=sl_capacity, type=bus_type, model=""
)
for (d_lane_id, d_s), aoi_id in zip(trip_stop_lane_id_s, trip_stop_aoi_ids):
trip_stop = TripStop()
trip_stop.lane_position.CopyFrom(LanePosition(lane_id=d_lane_id, s=d_s))
trip_stop.aoi_position.CopyFrom(AoiPosition(aoi_id=aoi_id))
trip_stop.duration = stop_duration_time
p_trip_stops.append(trip_stop)
# eta for bus journey
bus_eta = sum(subline.schedules.offset_times)
sl_drivers = []
if bus_type == BusType.BUS_TYPE_BUS:
for tm in depart_times:
p = Person()
p.CopyFrom(person_template)
p.id = person_id
if sl_attributes:
p.attribute.CopyFrom(dict2pb(sl_attributes, PersonAttribute()))
p.bus_attribute.CopyFrom(p_bus_attr)
p.home.CopyFrom(Position(aoi_position=AoiPosition(aoi_id=home_aoi_id)))
schedule = cast(Schedule, p.schedules.add())
schedule.departure_time = tm
schedule.loop_count = 1
trip = Trip(
mode=cast(
TripMode,
CAR,
),
end=Position(aoi_position=AoiPosition(aoi_id=end_aoi_id)),
activity="",
model="",
trip_stops=p_trip_stops,
routes=[
Journey(
driving=DrivingJourneyBody(
road_ids=route_road_ids, eta=bus_eta
),
type=JourneyType.JOURNEY_TYPE_DRIVING,
)
],
)
schedule.trips.append(trip)
person_id += 1

sl_drivers.append(p)
elif bus_type == BusType.BUS_TYPE_SUBWAY:
# empty schedule
p = Person()
p.CopyFrom(person_template)
p.id = person_id
if sl_attributes:
p.attribute.CopyFrom(dict2pb(sl_attributes, PersonAttribute()))
p.bus_attribute.CopyFrom(p_bus_attr)
p.home.CopyFrom(Position(aoi_position=AoiPosition(aoi_id=home_aoi_id)))
person_id += 1

return (person_id, sl_drivers)
Loading

0 comments on commit 813f6dd

Please sign in to comment.