Skip to content

Commit

Permalink
stop vehicle before closed segment
Browse files Browse the repository at this point in the history
  • Loading branch information
Michailidu authored and paulo308 committed Oct 30, 2023
1 parent aad42f1 commit 1e70344
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
13 changes: 6 additions & 7 deletions ruth/data/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ def get_travel_time(self, node_from: int, node_to: int, speed_kph: SpeedKph):
return float('inf') if speed_kph == 0 else float(
self.segment_lengths[(node_from, node_to)]) * 3.6 / float(speed_kph)

def get_current_travel_time(self, route: List[int]):
total_travel_time = 0
for node_from, node_to in zip(route[:-1], route[1:]):
total_travel_time += self.current_network[node_from][node_to]['current_travel_time']
return total_travel_time


def init_current_speeds(self):
speeds = nx.get_edge_attributes(self.current_network, name='speed_kph')
travel_times = {}
Expand All @@ -139,6 +132,12 @@ def get_current_max_speed(self, node_from: int, node_to: int):
def get_original_max_speed(self, node_from: int, node_to: int):
return self.original_network[node_from][node_to]['speed_kph']

def is_route_closed(self, route: List[int]):
for node_from, node_to in zip(route[:-1], route[1:]):
if self.get_current_max_speed(node_from, node_to) == 0:
return True
return False

def set_data_dir(self, path):
if self.data_dir is not None:
cl.warn(f"The data dir has changed from '{self.data_dir}' to '{path}.")
Expand Down
3 changes: 1 addition & 2 deletions ruth/simulator/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def remove_infinity_alternatives(self, alternatives: List[AlternativeRoutes], ro
for_vehicle = []
for alternative in alternatives_for_vehicle:
# calculate travel time for alternative
travel_time = routing_map.get_current_travel_time(alternative)
if travel_time != float("inf"):
if not routing_map.is_route_closed(alternative):
for_vehicle.append(alternative)
filtered_alternatives.append(for_vehicle)
return filtered_alternatives
Expand Down
14 changes: 8 additions & 6 deletions ruth/simulator/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ def move_on_segment(
current_segment = driving_route[segment_position.index]
assert segment_position.position <= current_segment.length

if vehicle.segment_position.index < len(driving_route):
if start_position == current_segment.length:
# if the car is at the end of a segment, we want to work with the next segment
start_position = LengthMeters(0.0)
segment_position = SegmentPosition(segment_position.index + 1, start_position)
current_segment = driving_route[segment_position.index]
if vehicle.segment_position.index < len(driving_route) and start_position == current_segment.length:
# if the vehicle is at the end of a segment and there are more segments in the route
if vehicle.has_next_segment_closed():
return current_time + vehicle.frequency, vehicle.segment_position, SpeedMps(0.0)
# if the vehicle can move to the next segment, work with the next segment
start_position = LengthMeters(0.0)
segment_position = SegmentPosition(segment_position.index + 1, start_position)
current_segment = driving_route[segment_position.index]

level_of_service = gv_db.gv.level_of_service_in_front_of_vehicle(current_time, current_segment, vehicle.id,
start_position, count_vehicles_tolerance)
Expand Down
5 changes: 5 additions & 0 deletions ruth/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def k_fastest_paths(self, k: int) -> Optional[List[List[int]]]:
print(f"vehicle: {self.id}: {ex}", file=sys.stderr)
return None

def has_next_segment_closed(self) -> bool:
next_segment_from, next_segment_to = self.osm_route[self.start_index + 1], self.osm_route[self.start_index + 2]
max_speed_on_next_segment = self.routing_map.get_current_max_speed(next_segment_from, next_segment_to)
return max_speed_on_next_segment == 0.0

def update_followup_route(self, osm_route: Route):
"""
Updates the route from the current node to the destination node.
Expand Down

0 comments on commit 1e70344

Please sign in to comment.