Skip to content

Commit cf5ce62

Browse files
Merge pull request #85 from alexanderthclark/zn87ia-codex/add-bach-or-stravinsky-to-game-classmethods
Add Bach or Stravinsky helper
2 parents 94a6f01 + 54cfb14 commit cf5ce62

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

freeride/games.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,22 @@ def battle_of_the_sexes(cls) -> "Game":
528528
action_names=(("Opera", "Boxing Match"), ("Opera", "Boxing Match")),
529529
)
530530

531+
@classmethod
532+
def bach_or_stravinsky(cls) -> "Game":
533+
"""Return the Bach or Stravinsky coordination game."""
534+
535+
p1 = [[2, 0], [0, 1]]
536+
p2 = [[1, 0], [0, 2]]
537+
return cls(
538+
p1,
539+
p2,
540+
player_names=("Anna", "Boris"),
541+
action_names=(
542+
("Bach", "Stravinsky"),
543+
("Bach", "Stravinsky"),
544+
),
545+
)
546+
531547
@classmethod
532548
def pure_coordination(cls) -> "Game":
533549
"""Return a simple pure coordination game."""
@@ -584,4 +600,3 @@ def rock_paper_scissors(cls) -> "Game":
584600

585601
# Backwards compatibility
586602
NormalFormGame = Game
587-

tests/test_games.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ def test_class_helpers(self):
210210
# Test nash equilibrium with proper action names
211211
self.assertEqual(set(bos.nash_equilibria()), {('Opera', 'Opera'), ('Boxing Match', 'Boxing Match')})
212212

213+
bos_alt = Game.bach_or_stravinsky()
214+
self.assertEqual(bos_alt.payoffs1.tolist(), [[2, 0], [0, 1]])
215+
self.assertEqual(bos_alt.payoffs2.tolist(), [[1, 0], [0, 2]])
216+
self.assertEqual(bos_alt.player_names, ("Anna", "Boris"))
217+
self.assertEqual(
218+
bos_alt.action_names,
219+
(("Bach", "Stravinsky"), ("Bach", "Stravinsky")),
220+
)
221+
self.assertEqual(
222+
set(bos_alt.nash_equilibria()),
223+
{("Bach", "Bach"), ("Stravinsky", "Stravinsky")},
224+
)
225+
213226
sh = Game.stag_hunt()
214227
self.assertEqual(sh.payoffs1.tolist(), [[2, 0], [1, 1]])
215228
self.assertEqual(sh.payoffs2.tolist(), [[2, 1], [0, 1]])

0 commit comments

Comments
 (0)