-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
168 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from collections import namedtuple, defaultdict | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from scipy.stats import truncnorm | ||
|
||
# Cost parameter from current berlin model | ||
daily_costs = defaultdict(lambda: 0.0, car=-14.30, pt=-3) | ||
km_costs = defaultdict(lambda: 0.0, car=-0.149, ride=-0.149) | ||
|
||
TN = truncnorm(0, np.inf) | ||
|
||
PlanChoice = namedtuple("PlanChoice", ["df", "modes", "varying", "k"]) | ||
|
||
|
||
def read_plan_choices(input_file: str, sample: float = 1, seed: int = 42) -> PlanChoice: | ||
""" Read plan choices from input file """ | ||
|
||
df_wide = pd.read_csv(input_file, comment="#") | ||
|
||
modes = list(df_wide.columns.str.extract(r"_([a-zA-z]+)_usage", expand=False).dropna().unique()) | ||
print("Modes: ", modes) | ||
|
||
k = df_wide.columns.str.extract(r"plan_(\d+)", expand=False).dropna().to_numpy(int).max() | ||
print("Number of plans: ", len(df_wide)) | ||
print("Number of choices for plan: ", k) | ||
|
||
# df_wide["p_id"] = df_wide["p_id"].str.replace(r"_\d+$", "", regex=True) | ||
# df_wide["person"] = df_wide["person"].astype('category').cat.codes | ||
|
||
# sample = set(df_wide.person.sample(frac=0.2)) | ||
# df_wide = df_wide[df_wide.person.isin(sample)] | ||
if sample < 1: | ||
df_wide = df_wide.sample(frac=sample, random_state=seed) | ||
|
||
print("Modes:", modes) | ||
print("Number of choices:", len(df_wide)) | ||
|
||
df_wide['custom_id'] = np.arange(len(df_wide)) # Add unique identifier | ||
df_wide['choice'] = df_wide['choice'].map({1: "plan_1"}) | ||
|
||
df_wide = calc_plan_variables(df_wide, k, modes) | ||
|
||
varying = list(df_wide.columns.str.extract(r"plan_1_([a-zA-z_]+)", expand=False).dropna().unique()) | ||
|
||
return PlanChoice(df_wide, modes, varying, k) | ||
|
||
|
||
def tn_generator(sample_size: int, number_of_draws: int) -> np.ndarray: | ||
""" | ||
User-defined random number generator to the database. | ||
See the numpy.random documentation to obtain a list of other distributions. | ||
""" | ||
return TN.rvs((sample_size, number_of_draws)) | ||
|
||
|
||
def calc_plan_variables(df, k, modes, use_util_money=False): | ||
""" Calculate utility and costs variables for all alternatives in the dataframe""" | ||
|
||
util_performing = -6.88 | ||
|
||
# Normalize activity utilities to be near zero | ||
# columns = [f"plan_{i}_act_util" for i in range(1, k + 1)] | ||
# for t in df.itertuples(): | ||
# utils = df.loc[t.Index, columns] | ||
# df.loc[t.Index, columns] -= utils.max() | ||
|
||
# Marginal utility of money as factor | ||
util_money = df.util_money if use_util_money else 1 | ||
|
||
for i in range(1, k + 1): | ||
|
||
# Price is only monetary costs | ||
df[f"plan_{i}_price"] = 0 | ||
|
||
# Costs will also include time costs | ||
df[f"plan_{i}_utils"] = 0 | ||
|
||
df[f"plan_{i}_tt_hours"] = 0 | ||
|
||
for mode in modes: | ||
|
||
fixed_costs = (df[f"plan_{i}_{mode}_usage"] > 0) * daily_costs[mode] | ||
distance_costs = df[f"plan_{i}_{mode}_km"] * km_costs[mode] | ||
|
||
df[f"plan_{i}_{mode}_fixed_cost"] = fixed_costs | ||
df[f"plan_{i}_price"] += fixed_costs + distance_costs | ||
|
||
df[f"plan_{i}_{mode}_used"] = (df[f"plan_{i}_{mode}_usage"] > 0) * 1 | ||
df[f"plan_{i}_tt_hours"] += df[f"plan_{i}_{mode}_hours"] | ||
|
||
# Add configured time costs | ||
df[f"plan_{i}_utils"] += (fixed_costs + distance_costs) * util_money | ||
|
||
# Add time costs the overall costs | ||
df[f"plan_{i}_utils"] += util_performing * df[f"plan_{i}_{mode}_hours"] | ||
|
||
# Add additional ride time utils for the driver | ||
if mode == "ride": | ||
df[f"plan_{i}_utils"] += util_performing * df[f"plan_{i}_{mode}_hours"] | ||
|
||
# Defragment df | ||
df = df.copy() | ||
|
||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
|
||
CONFIG="./input/v6.3/berlin-v6.3.config.xml" | ||
JVM_ARGS="-Xmx22G -Xms22G -XX:+AlwaysPreTouch -XX:+UseParallelGC" | ||
|
||
run_eval() { | ||
echo "Running evaluation with $1" | ||
java $JVM_ARGS -cp matsim-berlin-*.jar org.matsim.prepare.choices.ComputePlanChoices --config $CONFIG\ | ||
--scenario org.matsim.run.OpenBerlinScenario\ | ||
--args --10pct\ | ||
--modes walk,pt,car,bike,ride\ | ||
$1 | ||
} | ||
|
||
|
||
run_eval "--plan-candidates bestK --top-k 3" | ||
run_eval "--plan-candidates bestK --top-k 5" | ||
run_eval "--plan-candidates bestK --top-k 9" | ||
run_eval "--plan-candidates bestK --top-k 3 --time-util-only" | ||
run_eval "--plan-candidates bestK --top-k 5 --time-util-only" | ||
run_eval "--plan-candidates bestK --top-k 9 --time-util-only" | ||
run_eval "--plan-candidates random --top-k 3" | ||
run_eval "--plan-candidates random --top-k 5" | ||
run_eval "--plan-candidates random --top-k 9" | ||
run_eval "--plan-candidates diverse --top-k 9" |