Browse files

Implement get_max_payoff and get_min_payoff in the Python API with tests

  • Loading branch information...
1 parent fa2bcf4 commit 2fb712d17fad19e55dce7113cf4420a5fc4e33da @andrioni andrioni committed with tturocy Apr 5, 2012
View
8 src/python/gambit/lib/game.pxi
@@ -112,6 +112,14 @@ cdef class Game:
def __get__(self):
return self.game.deref().IsConstSum()
+ property min_payoff:
+ def __get__(self):
+ return fractions.Fraction(rat_str(self.game.deref().GetMinPayoff(0)).c_str())
+
+ property max_payoff:
+ def __get__(self):
+ return fractions.Fraction(rat_str(self.game.deref().GetMaxPayoff(0)).c_str())
+
def _get_contingency(self, *args):
cdef c_PureStrategyProfile *psp
cdef Outcome outcome
View
2 src/python/gambit/lib/libgambit.pyx
@@ -162,6 +162,8 @@ cdef extern from "libgambit/game.h":
c_GameNode GetRoot()
bool IsConstSum()
+ c_Rational GetMinPayoff(int)
+ c_Rational GetMaxPayoff(int)
ctypedef struct c_PureStrategyProfile "PureStrategyProfile":
c_GameStrategy GetStrategy(c_GamePlayer)
View
26 src/python/gambit/lib/player.pxi
@@ -40,7 +40,7 @@ cdef class Player:
def __repr__(self):
if self.is_chance:
return "<Player [CHANCE] in game '%s'>" % self.player.deref().GetGame().deref().GetTitle().c_str()
- return "<Player [%d] '%s' in game '%s'>" % (self.player.deref().GetNumber()-1,
+ return "<Player [%d] '%s' in game '%s'>" % (self.number,
self.label,
self.player.deref().GetGame().deref().GetTitle().c_str())
@@ -82,6 +82,10 @@ cdef class Player:
s.assign(value)
self.player.deref().SetLabel(s)
+ property number:
+ def __get__(self):
+ return self.player.deref().GetNumber() - 1;
+
property is_chance:
def __get__(self):
return True if self.player.deref().IsChance() != 0 else False
@@ -98,4 +102,22 @@ cdef class Player:
cdef Infosets s
s = Infosets()
s.player = self.player
- return s
+ return s
+
+ property min_payoff:
+ def __get__(self):
+ cdef Game g
+ cdef cxx_string s
+ g = Game()
+ g.game = self.player.deref().GetGame()
+ s = rat_str(g.game.deref().GetMinPayoff(self.number + 1))
+ return fractions.Fraction(s.c_str())
+
+ property max_payoff:
+ def __get__(self):
+ cdef Game g
+ cdef cxx_string s
+ g = Game()
+ g.game = self.player.deref().GetGame()
+ s = rat_str(g.game.deref().GetMaxPayoff(self.number + 1))
+ return fractions.Fraction(s.c_str())
View
11 src/python/gambit/tests/test_game.py
@@ -1,6 +1,7 @@
import gambit
from nose.tools import assert_raises
import warnings
+import fractions
class TestGambitGame(object):
def setUp(self):
@@ -61,3 +62,13 @@ def test_game_is_not_const_sum(self):
"To test checking if the game is not constant sum"
game = gambit.read_game("test_games/non_const_sum_game.nfg")
assert not game.is_const_sum
+
+ def test_game_get_min_payoff(self):
+ "To test getting the minimum payoff of the game"
+ game = gambit.read_game("test_games/payoff_game.nfg")
+ assert game.min_payoff == fractions.Fraction(1,1)
+
+ def test_game_get_max_payoff(self):
+ "To test getting the maximum payoff of the game"
+ game = gambit.read_game("test_games/payoff_game.nfg")
+ assert game.max_payoff == fractions.Fraction(10,1)
View
4 src/python/gambit/tests/test_games/payoff_game.nfg
@@ -0,0 +1,4 @@
+NFG 1 R "Test payoff game"
+{ "Player 1" "Player 2" } { 2 2 }
+
+7 3 4 1 5 4 10 8
View
17 src/python/gambit/tests/test_players.py
@@ -1,5 +1,6 @@
import gambit
import warnings
+import fractions
from nose.tools import assert_raises
class TestGambitPlayers(object):
@@ -95,3 +96,19 @@ def test_game_add_strategies_to_player_extensive_game(self):
"Test to ensure that an exception is raised when attempting to add a strategy to a player in an extensive game"
self.extensive_game.players.add("Alice")
assert_raises(TypeError, self.extensive_game.players[0].strategies.add, "Test")
+
+ def test_player_get_min_payoff(self):
+ "To test getting the minimum payoff for the players"
+ game = gambit.read_game("test_games/payoff_game.nfg")
+ assert game.players[0].min_payoff == fractions.Fraction(4,1)
+ assert game.players["Player 1"].min_payoff == fractions.Fraction(4,1)
+ assert game.players[1].min_payoff == fractions.Fraction(1,1)
+ assert game.players["Player 2"].min_payoff == fractions.Fraction(1,1)
+
+ def test_player_get_max_payoff(self):
+ "To test getting the maximum payoff for the players"
+ game = gambit.read_game("test_games/payoff_game.nfg")
+ assert game.players[0].max_payoff == fractions.Fraction(10,1)
+ assert game.players["Player 1"].max_payoff == fractions.Fraction(10,1)
+ assert game.players[1].max_payoff == fractions.Fraction(8,1)
+ assert game.players["Player 2"].max_payoff == fractions.Fraction(8,1)

0 comments on commit 2fb712d

Please sign in to comment.