Skip to content

Commit

Permalink
add few more attributes to activities
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jun 7, 2024
1 parent d70193d commit 69e87e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
5 changes: 4 additions & 1 deletion matsim/scenariogen/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,15 @@ class Trip:

@dataclass
class Activity:
""" Activity information (including leg) """
""" Activity information (including some leg information as well) """
a_id: str
a_weight: float
p_id: str
n: int
type: Purpose
duration: int
leg_dist: float
leg_duration: float
leg_mode: TripMode
location: str = pd.NA
zone: str = pd.NA
34 changes: 25 additions & 9 deletions matsim/scenariogen/data/preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,28 @@ def augment_persons(pp, factor=1, permute_age=0.5):
"""
pp = pp.reset_index()

duplicated = pp.loc[pp.index.repeat(np.maximum(1, np.rint(pp.p_weight * factor)))]
repeats = np.maximum(1, np.rint(pp.p_weight * factor)).to_numpy(int)
duplicated = pp.loc[pp.index.repeat(repeats)]

# Each sample has a unique sequence number
seq = np.zeros(len(duplicated), dtype=int)

i = 0
for r in repeats:
for s in range(r):
seq[i] = s
i += 1

duplicated["seq"] = seq

if permute_age > 0:
np.random.seed(0)

age_permute = np.rint(np.random.normal(0, permute_age, len(duplicated))).astype(np.int32)

# Non duplicated person is not permuted
age_permute[seq == 0] = 0

duplicated.age += age_permute

# 0 age is minimum
Expand Down Expand Up @@ -264,11 +280,11 @@ def a_id(t_i):
return "%s_%d" % (p.Index, t_i)

if len(trips) == 0:
acts.append(Activity(a_id(0), p.Index, 0, Purpose.HOME, 1440, 0, 0, TripMode.OTHER))
acts.append(Activity(a_id(0), p.p_weight, p.Index, 0, Purpose.HOME, 1440, 0, 0, TripMode.OTHER))
else:
acts.append(
Activity(a_id(0), p.Index, 0, trips.iloc[0].sd_group.source(), trips.iloc[0].departure, 0, 0,
TripMode.OTHER))
trip_0 = trips.iloc[0]
acts.append(Activity(a_id(0), p.p_weight, p.Index, 0, trip_0.sd_group.source(), trip_0.departure, 0, 0,
TripMode.OTHER, trip_0.from_location, trip_0.from_zone))

for i in range(len(trips) - 1):
t0 = trips.iloc[i]
Expand All @@ -279,8 +295,8 @@ def a_id(t_i):
if duration < 0 or t0.gis_length < 0:
valid = False

acts.append(Activity(a_id(i + 1), p.Index, i + 1, t0.purpose,
duration, t0.gis_length, t0.duration, t0.main_mode))
acts.append(Activity(a_id(i + 1), p.p_weight, p.Index, i + 1, t0.purpose,duration, t0.gis_length,
t0.duration, t0.main_mode, t0.to_location, t0.to_zone))

if len(trips) > 1:
i += 1
Expand All @@ -291,8 +307,8 @@ def a_id(t_i):
valid = False

# Duration is set to rest of day
acts.append(
Activity(a_id(i + 1), p.Index, i + 1, tl.purpose, 1440, tl.gis_length, tl.duration, tl.main_mode))
acts.append(Activity(a_id(i + 1), p.p_weight, p.Index, i + 1, tl.purpose, 1440, tl.gis_length, tl.duration, tl.main_mode,
tl.to_location, tl.to_zone))

if valid:
res.extend(acts)
Expand Down

0 comments on commit 69e87e3

Please sign in to comment.