Skip to content

Commit

Permalink
Ajout de la classe CarpoolTravelCosts
Browse files Browse the repository at this point in the history
Premier push pour créer CarpoolTravelCosts. Modification uniquement des distances et temps pour le moment
  • Loading branch information
matt-gau committed Jul 26, 2024
1 parent 5ea46a4 commit 391e1cc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added examples/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/trip_localizer_detailed_steps/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MOBILITY_PACKAGE_DATA_FOLDER = "/Users/matthieugautrot/Documents/mobility_data/data_all"
MOBILITY_PROJECT_DATA_FOLDER = "/Users/matthieugautrot/Documents/mobility_data/data_geneve"
Binary file added mobility/.DS_Store
Binary file not shown.
87 changes: 87 additions & 0 deletions mobility/carpool_travel_costs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import pathlib
import logging
import pandas as pd

from mobility.asset import Asset
from mobility.travel_costs import TravelCosts

class CarpoolTravelCosts(Asset):
"""
A class for computing carpooling travel cost using car travel costs, inheriting from the Asset class.
This class is responsible for creating, caching, and retrieving carpool travel costs based on specified transport zones and a number of persons in the vehicule.
Attributes:
car_travel_costs (MultimodalTravelCosts): The travel costs for the different modes (excluding )
nb_occupant: number of persons in the vehicule.
Methods:
get_cached_asset: Retrieve a cached DataFrame of travel costs.
create_and_get_asset: Calculate and retrieve travel costs based on the current inputs.
"""

def __init__(self, car_travel_costs: TravelCosts, nb_occupant: int):
"""
Initializes a CarpoolTravelCosts object with the given transport zones and travel mode.
"""

inputs = {"car_travel_costs": car_travel_costs, "nb_occupant": nb_occupant}

file_name = "carpool" + str(nb_occupant) + "_travel_costs.parquet"
cache_path = pathlib.Path(os.environ["MOBILITY_PROJECT_DATA_FOLDER"]) / file_name

super().__init__(inputs, cache_path)

def get_cached_asset(self) -> pd.DataFrame:
"""
Retrieves the travel costs DataFrame from the cache.
Returns:
pd.DataFrame: The cached DataFrame of travel costs.
"""

logging.info("Travel costs already prepared. Reusing the file : " + str(self.cache_path))
costs = pd.read_parquet(self.cache_path)

return costs

def create_and_get_asset(self) -> pd.DataFrame:
"""
Creates and retrieves carpool travel costs based on the current inputs.
Returns:
pd.DataFrame: A DataFrame of calculated carpool travel costs.
"""
car_travel_costs = self.inputs["car_travel_costs"].get()
nb_occupant = self.inputs["nb_occupant"]

logging.info("Preparing carpool travel costs for " + str(nb_occupant) + " occupants...")

costs = self.compute_carpool_costs(car_travel_costs, nb_occupant)

return costs

def compute_carpool_costs(self, car_travel_costs: pd.DataFrame, nb_occupant: int) -> pd.DataFrame:
"""
Calculates carpool travel costs for the specified number of occupants in the vehicule.
Args:
car_travel_costs (MultimodalTravelCosts): The travel costs for the different modes (excluding )
nb_occupant: number of persons in the vehicule.
Returns:
pd.DataFrame: A DataFrame containing calculated travel costs.
"""

logging.info("Computing carpool travel costs for " + str(nb_occupant) + " occupants...")

costs = car_travel_costs.copy()

# Adding a delay of 5min (1km) and 5% of the travel time (resp. distance) per passenger
costs["time"] += (nb_occupant-1)*(0.05*costs["time"] + 5/60)
costs["distance"] += (nb_occupant-1)*(0.05*costs["distance"] + 1)

return costs

12 changes: 10 additions & 2 deletions mobility/multimodal_travel_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from mobility.travel_costs import TravelCosts
from mobility.public_transport_travel_costs import PublicTransportTravelCosts
from mobility.carpool_travel_costs import CarpoolTravelCosts

class MultimodalTravelCosts(Asset):

Expand All @@ -31,12 +32,19 @@ def __init__(
public_transport_max_traveltime,
public_transport_additional_gtfs_files
)


carpool2_travel_costs = CarpoolTravelCosts(car_travel_costs, 2)
carpool3_travel_costs = CarpoolTravelCosts(car_travel_costs, 3)
carpool4_travel_costs = CarpoolTravelCosts(car_travel_costs, 4)

inputs = {
"car_travel_costs": car_travel_costs,
"walk_travel_costs": walk_travel_costs,
"bicycle_travel_costs": bicycle_travel_costs,
"pub_trans_travel_costs": pub_trans_travel_costs
"pub_trans_travel_costs": pub_trans_travel_costs,
"carpool2_travel_costs": carpool2_travel_costs,
"carpool3_travel_costs": carpool3_travel_costs,
"carpool4_travel_costs": carpool4_travel_costs
}

file_name = "multimodal_travel_costs.parquet"
Expand Down

0 comments on commit 391e1cc

Please sign in to comment.