Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hawk/dove updating risk attitudes #36

Merged
merged 9 commits into from
Oct 25, 2023
Merged

Hawk/dove updating risk attitudes #36

merged 9 commits into from
Oct 25, 2023

Conversation

rlskoeser
Copy link
Contributor

@rlskoeser rlskoeser commented Oct 18, 2023

modify variable-risk hawk/dove game to add logic for updating risk levels based on neighbors

  • parameter for adjustment strategy: adopt, average, none = no adjustment
  • parameter for how often to adjust

adjustment logic is adapted from previous implementation in the risk bet simulation

@rlskoeser rlskoeser marked this pull request as draft October 18, 2023 20:52
@codecov
Copy link

codecov bot commented Oct 18, 2023

Codecov Report

Merging #36 (48a13b6) into main (47a1af7) will increase coverage by 5.05%.
Report is 1 commits behind head on main.
The diff coverage is 81.08%.

@@            Coverage Diff             @@
##             main      #36      +/-   ##
==========================================
+ Coverage   48.58%   53.64%   +5.05%     
==========================================
  Files          15       16       +1     
  Lines         566      645      +79     
==========================================
+ Hits          275      346      +71     
- Misses        291      299       +8     

@rlskoeser rlskoeser force-pushed the hawkdove-update-risk branch from e50342b to 0d5980c Compare October 19, 2023 17:14
@rlskoeser rlskoeser marked this pull request as ready for review October 19, 2023 17:18
@rlskoeser rlskoeser temporarily deployed to qa October 19, 2023 18:26 Inactive
@rlskoeser rlskoeser temporarily deployed to qa October 19, 2023 18:57 Inactive
@rlskoeser rlskoeser temporarily deployed to qa October 24, 2023 19:45 Inactive
@rlskoeser rlskoeser temporarily deployed to qa October 24, 2023 19:49 Inactive
@rlskoeser rlskoeser temporarily deployed to qa October 24, 2023 19:51 Inactive
@rlskoeser rlskoeser temporarily deployed to qa October 24, 2023 19:54 Inactive
Copy link

@quadrismegistus quadrismegistus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic!

Comment on lines 74 to 75
# You have to specify the dependencies as follows, so that the figure
# auto-updates when viz.model or viz.df is changed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment outdated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh, yes, thanks for catching

jupyterviz_params_var = jupyterviz_params.copy()
# remove parameter for agent risk level;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed? not clear from immediate context but perhaps clear elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I should clarify that in the comment at least - it's a parameter that's only relevant for the single-risk level simulation because in the variable simulation risk attitudes are generated randomly; I don't currently have base/common parameters (common params would probably be clearer)

@@ -13,18 +17,94 @@ def set_risk_level(self):
# generate a random risk level
self.risk_level = self.random.randint(0, num_neighbors)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.random presumably comes from the base mesa class? Otherwise wondering what it refers to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, mesa provides access to built-in random methods that works just like the main python library random module

It's in mentioned in their documentation best practices, it would allow using a random seed at model instantiation: https://mesa.readthedocs.io/en/stable/best-practices.html#randomization

Comment on lines +33 to +51
def adjust_risk(self):
# look at neighbors
# if anyone has more points
# either adopt their risk attitude or average theirs with yours

best = self.most_successful_neighbor
# if most successful neighbor has more points and a different
# risk attitude, adjust
if best.points > self.points and best.risk_level != self.risk_level:
# adjust risk based on model configuration
if self.model.risk_adjustment == "adopt":
# adopt neighbor's risk level
self.risk_level = best.risk_level
elif self.model.risk_adjustment == "average":
# average theirs with mine, then round to a whole number
# since this model uses discrete risk levels
self.risk_level = round(
statistics.mean([self.risk_level, best.risk_level])
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very, very cool, and slick. I remember talking about this and/or similar strategies of risk adjustment with David and Lara and you.

Comment on lines +69 to +72
assert (
repr(agent)
== f"<HawkDoveSingleRiskAgent id={agent_id} r={risk_level} points=0>"
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test

Comment on lines +30 to +41
assert model.risk_adjustment == "adopt"
assert model.adjust_round_n == 5
assert model.hawk_odds == 0.2
assert model.include_diagonals is False

# handle string none for solara app parameters
model = HawkDoveVariableRiskModel(5, risk_adjustment="none")
assert model.risk_adjustment is None

# complain about invalid adjustment type
with pytest.raises(ValueError, match="Unsupported risk adjustment 'bogus'"):
HawkDoveVariableRiskModel(3, risk_adjustment="bogus")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These easy access object property-methods really make using and testing quite easy and legible.

]


@pytest.mark.parametrize("params,expect_adjust_step", adjustment_testdata)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice, so it runs the test 4 times according to the number of tuples in the adjustment_testdata list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, isn't that cool? very useful sometimes

Comment on lines +120 to +121
neighbor = Mock(points=1500, risk_level=3)
with patch.object(HawkDoveVariableRiskAgent, "most_successful_neighbor", neighbor):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very elegant patching

@rlskoeser rlskoeser merged commit 3f0580d into main Oct 25, 2023
2 of 4 checks passed
@rlskoeser rlskoeser deleted the hawkdove-update-risk branch October 25, 2023 17:43
rlskoeser added a commit that referenced this pull request Feb 12, 2024
* Preliminary work for hawk/dove adaptive risk attitudes #20

* Clean up duplicate code; add comments about adjusting parameters

* Add tests for variable hawk/dove; improve error handling

* Add a label for round-adjustment parameter

* Fix typo in app about placeholder content

* Load about app content relative to app file

* Remove model dependency in matplotlib histogram (prevents from updating)

* Remove unused requirements text files/directories

* Clean up outdated comments and refactor common hawk/dove params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants