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

Jormungandr: Best olympics choose higher attractivity for equal virtual duration #4085

Merged
merged 5 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ def _create_parameters(instance, request, isochrone_center, request_type):
boundary_duration=request.get("boundary_duration[]"),
)
else:
attractivities = copy.deepcopy(instance.stop_points_attractivities or {})
attractivities.update(request.get('_olympics_sites_attractivities[]') or [])
attractivities = request.get("attractivities", {})

return JourneyParameters(
max_duration=request['max_duration'],
Expand Down
37 changes: 23 additions & 14 deletions source/jormungandr/jormungandr/scenarios/journey_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,27 +753,35 @@ def get_journey_extremity_pt_section(journey, criteria):
sections = journey.sections
extremity_pt_section = next((s for s in sections if s.type == response_pb2.PUBLIC_TRANSPORT), None)

assert extremity_pt_section
return extremity_pt_section


def get_journey_pt_extremity(journey, criteria):
extemity_pt_section = get_journey_extremity_pt_section(journey, criteria)
extremity_pt_section = get_journey_extremity_pt_section(journey, criteria)

if extremity_pt_section is None:
return None, None

assert extemity_pt_section
if criteria == "arrival_stop_attractivity":
return extemity_pt_section, extemity_pt_section.destination
return extemity_pt_section, extemity_pt_section.origin
return extremity_pt_section, extremity_pt_section.destination
else:
return extremity_pt_section, extremity_pt_section.origin


def compute_journey_virtual_duration(journey, criteria, virtual_fallbacks):
def compute_journey_virtual_duration_and_attractivity(journey, criteria, virtual_fallbacks, attractivities):
extremity_pt_section, extremity = get_journey_pt_extremity(journey, criteria)
if extremity_pt_section is None:
return journey.duration, 0

virtual_fallback = virtual_fallbacks.get(extremity.uri) or 0
attractivity = attractivities.get(extremity.uri) or 0

if criteria == "arrival_stop_attractivity":
return extremity_pt_section.end_date_time - journey.departure_date_time + virtual_fallback

return journey.arrival_date_time - extremity_pt_section.begin_date_time + virtual_fallback
virtual_duration = extremity_pt_section.end_date_time - journey.departure_date_time + virtual_fallback
return virtual_duration, attractivity
else:
virtual_duration = journey.arrival_date_time - extremity_pt_section.begin_date_time + virtual_fallback
return virtual_duration, attractivity


class Interval:
Expand Down Expand Up @@ -856,18 +864,19 @@ def filter_olympics_journeys_v1(responses, request):
def filter_olympics_journeys_v2(responses, request):
virtual_fallback_durations = {}
virtual_fallback_durations.update(request.get("_olympics_sites_virtual_fallback[]") or [])
best = (None, float('inf'))
attractivities = request.get("attractivities", {})
best = (None, float('inf'), 0)
for r in responses:
for j in r.journeys:
if 'olympics' not in j.tags:
continue

virtual_duration = compute_journey_virtual_duration(
j, request.get('criteria'), virtual_fallback_durations
virtual_duration, attractivity = compute_journey_virtual_duration_and_attractivity(
j, request.get('criteria'), virtual_fallback_durations, attractivities
)

if virtual_duration < best[1]:
best = (j, virtual_duration)
if virtual_duration < best[1] or (virtual_duration == best[1] and attractivity > best[2]):
best = (j, virtual_duration, attractivity)

if best[0] is not None:
best[0].tags.append('best_olympics')
Expand Down
4 changes: 4 additions & 0 deletions source/jormungandr/jormungandr/scenarios/new_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,10 @@ def fill_journeys(self, request_type, api_request, instance):
# Return the possible combinations (origin_mode ,destination_mode, direct_path_type)
krakens_call = get_kraken_calls(api_request)

attractivities = deepcopy(instance.stop_points_attractivities or {})
attractivities.update(api_request.get('_olympics_sites_attractivities[]') or [])
api_request["attractivites"] = attractivities

# We need the original request (api_request) for filtering, but request
# is modified by create_next_kraken_request function.
request = deepcopy(api_request)
Expand Down
Loading