From bb03c69eddbc3c3b3fca2ecf7d8891632f4b74a7 Mon Sep 17 00:00:00 2001 From: rlskoeser Date: Fri, 26 Jul 2024 16:54:44 -0400 Subject: [PATCH] Fix proportional scaling to always scale to size 8 neighborhood resolves #71 --- simulatingrisk/hawkdove/model.py | 13 +++++++++---- tests/test_hawkdove.py | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/simulatingrisk/hawkdove/model.py b/simulatingrisk/hawkdove/model.py index a7ede81..e4b4202 100644 --- a/simulatingrisk/hawkdove/model.py +++ b/simulatingrisk/hawkdove/model.py @@ -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) diff --git a/tests/test_hawkdove.py b/tests/test_hawkdove.py index 3cbce31..d837cd0 100644 --- a/tests/test_hawkdove.py +++ b/tests/test_hawkdove.py @@ -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():