Skip to content

Commit 68dd7db

Browse files
committed
Custom degradation and decision-making classes are served in a more pythonic way.
1 parent 2ffa166 commit 68dd7db

File tree

20 files changed

+192
-200
lines changed

20 files changed

+192
-200
lines changed

examples/custom-decision-making/decision_making.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@ class CustomDecisionMaking(DecisionMaking):
77
Methods related to agents' decision-making
88
"""
99

10+
is_custom = True
11+
1012
def decide_destination(self, agent_id: int, duration: float) -> None:
1113
"""
1214
Decide the destination
1315
"""
1416
destination_id = None
1517
critical_stay_length = duration
16-
action_queue = self.get_action_queue(id=agent_id)
18+
action_queue = self.society.get_action_queue(id=agent_id)
1719
# Find suitable market
1820
destinations = self.preasssumed_destinations(agent_id=agent_id)
1921
destinations = sorted(destinations, key=lambda x: x["score"], reverse=True)
2022
suitable_destination_found = False
2123
for destination in destinations:
22-
path = self.infrastructure.path(
23-
id_start=self.get_current_node(id=agent_id), id_end=destination["id"]
24+
path = self.society.infrastructure.path(
25+
id_start=self.society.get_current_node(id=agent_id), id_end=destination["id"]
2426
)
2527
move_go = Move(
2628
action_queue=action_queue,
2729
path=path,
28-
usage=self.transportation_degradation,
30+
usage=self.society.transportation_degradation,
2931
)
3032
# Stay (at the destination)
31-
stay_length = self.max_time_outside - (2 * move_go.total_duration)
33+
stay_length = self.society.max_time_outside - (2 * move_go.total_duration)
3234
if stay_length > critical_stay_length:
3335
suitable_destination_found = True
3436
destination_id = destination["id"]
@@ -39,17 +41,17 @@ def decide_destination(self, agent_id: int, duration: float) -> None:
3941
destinations = self.search_destinations(agent_id=agent_id)
4042
destinations = sorted(destinations, key=lambda x: x["score"], reverse=True)
4143
for destination in destinations:
42-
path = self.infrastructure.path(
43-
id_start=self.get_current_node(id=agent_id),
44+
path = self.society.infrastructure.path(
45+
id_start=self.society.get_current_node(id=agent_id),
4446
id_end=destination["id"],
4547
)
4648
move_go = Move(
4749
action_queue=action_queue,
4850
path=path,
49-
usage=self.transportation_degradation,
51+
usage=self.society.transportation_degradation,
5052
)
5153
# Stay (at the destination)
52-
stay_length = self.max_time_outside - (2 * move_go.total_duration)
54+
stay_length = self.society.max_time_outside - (2 * move_go.total_duration)
5355
if stay_length > critical_stay_length:
5456
suitable_destination_found = True
5557
destination_id = destination["id"]

examples/custom-decision-making/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import piperabm as pa
3+
from decision_making import CustomDecisionMaking
34

45

56
path = os.path.dirname(os.path.realpath(__file__))
@@ -23,6 +24,7 @@
2324
model.infrastructure.bake()
2425

2526
# Set up the society
27+
model.society.set_decision_making(CustomDecisionMaking) # Set the custom decision-making class
2628
model.society.average_income = 1
2729
agent_id = 1
2830
home_id = model.infrastructure.homes[0]
0 Bytes
Binary file not shown.

examples/custom-decision-making/result/final.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

examples/custom-decision-making/result/simulation.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
# Custom Degradation
22

3-
In this example, we see how the user can customize degradation behavior of the infrastrcture elements of the model by providing their own `degradation.py` file.
3+
In this example, we see how the user can customize degradation behavior of the infrastrcture elements of the model by providing a custom degradation class. Then, the impact of this change is compared to default model behavior.
4+
5+
This is how the custom degradation file is being served:
6+
7+
```python
8+
from piperabm.infrastructure.degradation import Degradation
9+
10+
class CustomDegradation(Degradation):
11+
12+
def calculate_adjustment_factor(
13+
self, usage_impact: float, age_impact: float
14+
) -> float:
15+
"""
16+
Calculate adjustment factor using a custom formula.
17+
"""
18+
return (
19+
1 + (self.infrastructure.coeff_usage * usage_impact) + (self.infrastructure.coeff_age * (age_impact ** 2))
20+
)
21+
22+
model.infrastructure.set_degradation(CustomDegradation)
23+
```
24+
25+
When edges degrade, they will become less efficient to be used. It is equivalent of having longer length, since it will take longer (and require more fuel) for the agents to pass through. This is called `adjusted_length` and using custom degradation, the formula to calculate it is customized.

examples/custom-degradation/degradation.py

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,69 @@
1-
import os
21
import piperabm as pa
2+
from piperabm.infrastructure.degradation import Degradation
33

44

5-
path = os.path.dirname(os.path.realpath(__file__))
6-
model = pa.Model(path=path, seed=2)
7-
8-
# Set up the infrastructure
9-
model.infrastructure.coeff_usage = 1
10-
model.infrastructure.coeff_age = 1
11-
model.infrastructure.add_street(pos_1=[0, 0], pos_2=[-60, 40], name="road")
12-
model.infrastructure.add_home(pos=[5, 0], id=1, name="home")
13-
model.infrastructure.add_market(
14-
pos=[-60, 45],
15-
id=2,
16-
name="market",
17-
resources={
18-
"food": 100,
19-
"water": 100,
20-
"energy": 100,
21-
},
22-
)
23-
model.infrastructure.bake()
24-
25-
# Set up the society
26-
model.society.average_income = 1
27-
agent_id = 1
28-
home_id = model.infrastructure.homes[0]
29-
model.society.add_agent(
30-
id=agent_id,
31-
home_id=home_id,
32-
socioeconomic_status=1,
33-
resources={
34-
"food": 1,
35-
"water": 1,
36-
"energy": 1,
37-
},
38-
balance=100,
39-
)
40-
41-
# Run the simulation
42-
model.run(n=80, step_size=1, save=True)
43-
44-
# Animate the result
45-
model.animate()
5+
class CustomDegradation(Degradation):
6+
7+
def calculate_adjustment_factor(
8+
self, usage_impact: float, age_impact: float
9+
) -> float:
10+
"""
11+
Calculate adjustment factor using a custom formula.
12+
"""
13+
return (
14+
1 + (self.infrastructure.coeff_usage * usage_impact) + (self.infrastructure.coeff_age * (age_impact ** 2))
15+
)
16+
17+
18+
def setup_model():
19+
model = pa.Model(seed=2)
20+
21+
# Set up the infrastructure
22+
model.infrastructure.coeff_usage = 1
23+
model.infrastructure.coeff_age = 1
24+
model.infrastructure.add_street(pos_1=[0, 0], pos_2=[-60, 40], name="road")
25+
model.infrastructure.add_home(pos=[5, 0], id=1, name="home")
26+
model.infrastructure.add_market(
27+
pos=[-60, 45],
28+
id=2,
29+
name="market",
30+
resources={
31+
"food": 100,
32+
"water": 100,
33+
"energy": 100,
34+
},
35+
)
36+
model.infrastructure.bake()
37+
38+
# Set up the society
39+
model.society.average_income = 1
40+
agent_id = 1
41+
home_id = model.infrastructure.homes[0]
42+
model.society.add_agent(
43+
id=agent_id,
44+
home_id=home_id,
45+
socioeconomic_status=1,
46+
resources={
47+
"food": 1,
48+
"water": 1,
49+
"energy": 1,
50+
},
51+
balance=100,
52+
)
53+
54+
return model
55+
56+
edge = (8042686386972756495, 478254495130285640)
57+
58+
# Run the simulation with default degradation
59+
model_default = setup_model()
60+
model_default.run(n=80, step_size=1)
61+
62+
# Run the simulation with custom degradation
63+
model_custom = setup_model()
64+
model_custom.infrastructure.set_degradation(CustomDegradation) # Set the custom degradation class
65+
model_custom.run(n=80, step_size=1)
66+
67+
# Compare the results
68+
print("Default adjusted length formula:", model_default.infrastructure.get_adjusted_length(ids=edge))
69+
print("Custom adjusted length formula:",model_custom.infrastructure.get_adjusted_length(ids=edge))
-10.8 KB
Binary file not shown.

examples/custom-degradation/result/final.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)