Skip to content

Commit

Permalink
Fix proportional scaling to always scale to size 8 neighborhood
Browse files Browse the repository at this point in the history
resolves #71
  • Loading branch information
rlskoeser committed Jul 26, 2024
1 parent b198c51 commit bb03c69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 9 additions & 4 deletions simulatingrisk/hawkdove/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,15 @@ def num_dove_neighbors(self):

@property
def proportional_num_dove_neighbors(self):
"""adjust the number of dove neighbors based on ratio between
play neighborhood and observed neighborhood, to scale observations
to the range of agent risk level."""
ratio = self.model.max_risk_level / self.model.observed_neighborhood
"""adjust the number of dove neighbors to scale observations
to a standard range of risk levels."""
# for convenience and simplicity, we scale the neighborhood so
# that agent risk levels can always be defined as 0-9 no matter
# the neighborhood size. (Can also be thought of as always
# scaling to a neighborhood size of 8.).
# Agent risk levels are 0-9 but scaling is 1-8, since 0 and 9
# always play hawk and dove respectively.
ratio = 8 / self.model.observed_neighborhood
# always round to an integer
return round(ratio * self.num_dove_neighbors)

Expand Down
16 changes: 14 additions & 2 deletions tests/test_hawkdove.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,31 @@ def test_proportional_num_dove_neighbors():
model = HawkDoveSingleRiskModel(4, agent_risk_level=3)
agent = HawkDoveSingleRiskAgent(1, model)

## equal play/observed; scales to 9 (risk level range)
## equal play/observed; scales to 8 (risk level range)
model.observed_neighborhood = 4
with patch.object(HawkDoveAgent, "num_dove_neighbors", 4):
assert agent.proportional_num_dove_neighbors == 8
with patch.object(HawkDoveAgent, "num_dove_neighbors", 3):
assert agent.proportional_num_dove_neighbors == 7
assert agent.proportional_num_dove_neighbors == 6
with patch.object(HawkDoveAgent, "num_dove_neighbors", 2):
assert agent.proportional_num_dove_neighbors == 4

model.observed_neighborhood = 8
with patch.object(HawkDoveAgent, "num_dove_neighbors", 5):
assert agent.proportional_num_dove_neighbors == 5
with patch.object(HawkDoveAgent, "num_dove_neighbors", 6):
assert agent.proportional_num_dove_neighbors == 6
with patch.object(HawkDoveAgent, "num_dove_neighbors", 7):
assert agent.proportional_num_dove_neighbors == 7

# observe more than 8
model.observed_neighborhood = 24
with patch.object(HawkDoveAgent, "num_dove_neighbors", 20):
assert agent.proportional_num_dove_neighbors == 7
with patch.object(HawkDoveAgent, "num_dove_neighbors", 23):
assert agent.proportional_num_dove_neighbors == 8
with patch.object(HawkDoveAgent, "num_dove_neighbors", 6):
assert agent.proportional_num_dove_neighbors == 2


def test_agent_choose_when_observe_play_differ():
Expand Down

0 comments on commit bb03c69

Please sign in to comment.