Skip to content

Commit 7b1aa19

Browse files
committed
Separate implementation of player-specific and overall min/max payoffs.
Closes #497
1 parent 8f93587 commit 7b1aa19

9 files changed

Lines changed: 64 additions & 60 deletions

File tree

.clang-tidy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Checks: |
1515
WarningsAsErrors: '*'
1616
# Don't bother with wxWidgets headers
1717
HeaderFilterRegex: '^((?!labenski).)*$'
18-
AnalyzeTemporaryDtors: false
1918
FormatStyle: none
2019
User: arbiter
2120
CheckOptions:

src/games/game.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,14 @@ class GameRep : public BaseGameRep {
444444

445445
/// Returns true if the game is constant-sum
446446
virtual bool IsConstSum() const = 0;
447-
/// Returns the smallest payoff in any outcome of the game
448-
virtual Rational GetMinPayoff(int pl = 0) const = 0;
449-
/// Returns the largest payoff in any outcome of the game
450-
virtual Rational GetMaxPayoff(int pl = 0) const = 0;
447+
/// Returns the smallest payoff to any player in any outcome of the game
448+
virtual Rational GetMinPayoff() const = 0;
449+
/// Returns the smallest payoff to the player in any outcome of the game
450+
virtual Rational GetMinPayoff(const GamePlayer &p_player) const = 0;
451+
/// Returns the largest payoff to any player in any outcome of the game
452+
virtual Rational GetMaxPayoff() const = 0;
453+
/// Returns the largest payoff to the player in any outcome of the game
454+
virtual Rational GetMaxPayoff(const GamePlayer &p_player) const = 0;
451455

452456
/// Returns true if the game is perfect recall. If not,
453457
/// a pair of violating information sets is returned in the parameters.

src/games/gameagg.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,14 @@ class GameAGGRep : public GameRep {
121121
bool IsAgg() const override { return true; }
122122
bool IsPerfectRecall(GameInfoset &, GameInfoset &) const override { return true; }
123123
bool IsConstSum() const override;
124-
/// Returns the smallest payoff in any outcome of the game
125-
Rational GetMinPayoff(int) const override { return Rational(aggPtr->getMinPayoff()); }
126-
/// Returns the largest payoff in any outcome of the game
127-
Rational GetMaxPayoff(int) const override { return Rational(aggPtr->getMaxPayoff()); }
128-
124+
/// Returns the smallest payoff to any player in any outcome of the game
125+
Rational GetMinPayoff() const override { return Rational(aggPtr->getMinPayoff()); }
126+
/// Returns the smallest payoff to the player in any outcome of the game
127+
Rational GetMinPayoff(const GamePlayer &) const override { throw UndefinedException(); }
128+
/// Returns the largest payoff to any player in any outcome of the game
129+
Rational GetMaxPayoff() const override { return Rational(aggPtr->getMaxPayoff()); }
130+
/// Returns the largest payoff to the player in any outcome of the game
131+
Rational GetMaxPayoff(const GamePlayer &) const override { throw UndefinedException(); }
129132
//@}
130133

131134
/// @name Modification

src/games/gamebagg.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ class GameBAGGRep : public GameRep {
122122
virtual bool IsBagg() const { return true; }
123123
bool IsPerfectRecall(GameInfoset &, GameInfoset &) const override { return true; }
124124
bool IsConstSum() const override { throw UndefinedException(); }
125-
/// Returns the smallest payoff in any outcome of the game
126-
Rational GetMinPayoff(int) const override { return Rational(baggPtr->getMinPayoff()); }
127-
/// Returns the largest payoff in any outcome of the game
128-
Rational GetMaxPayoff(int) const override { return Rational(baggPtr->getMaxPayoff()); }
125+
/// Returns the smallest payoff to any player in any outcome of the game
126+
Rational GetMinPayoff() const override { return Rational(baggPtr->getMinPayoff()); }
127+
/// Returns the smallest payoff to the player in any outcome of the game
128+
Rational GetMinPayoff(const GamePlayer &) const override { throw UndefinedException(); }
129+
/// Returns the largest payoff to any player in any outcome of the game
130+
Rational GetMaxPayoff() const override { return Rational(baggPtr->getMaxPayoff()); }
131+
/// Returns the largest payoff to the player in any outcome of the game
132+
Rational GetMaxPayoff(const GamePlayer &) const override { throw UndefinedException(); }
129133
//@}
130134

131135
/// @name Writing data files

src/games/gameexpl.cc

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,54 +50,42 @@ GameExplicitRep::~GameExplicitRep()
5050
// GameExplicitRep: General data access
5151
//------------------------------------------------------------------------
5252

53-
Rational GameExplicitRep::GetMinPayoff(int player) const
53+
Rational GameExplicitRep::GetMinPayoff() const
5454
{
55-
int p1, p2;
55+
return std::accumulate(
56+
std::next(m_players.begin()), m_players.end(), GetMinPayoff(m_players.front()),
57+
[this](const Rational &r, const GamePlayer &p) { return std::min(r, GetMinPayoff(p)); });
58+
}
5659

60+
Rational GameExplicitRep::GetMinPayoff(const GamePlayer &p_player) const
61+
{
5762
if (m_outcomes.empty()) {
5863
return Rational(0);
5964
}
60-
61-
if (player) {
62-
p1 = p2 = player;
63-
}
64-
else {
65-
p1 = 1;
66-
p2 = NumPlayers();
67-
}
68-
69-
Rational minpay = m_outcomes.front()->GetPayoff<Rational>(GetPlayer(p1));
70-
for (auto outcome : m_outcomes) {
71-
for (int p = p1; p <= p2; p++) {
72-
minpay = std::min(minpay, outcome->GetPayoff<Rational>(GetPlayer(p)));
73-
}
74-
}
75-
return minpay;
65+
return std::accumulate(std::next(m_outcomes.begin()), m_outcomes.end(),
66+
m_outcomes.front()->GetPayoff<Rational>(p_player),
67+
[&p_player](const Rational &r, const GameOutcomeRep *c) {
68+
return std::min(r, c->GetPayoff<Rational>(p_player));
69+
});
7670
}
7771

78-
Rational GameExplicitRep::GetMaxPayoff(int player) const
72+
Rational GameExplicitRep::GetMaxPayoff() const
7973
{
80-
int p1, p2;
74+
return std::accumulate(
75+
std::next(m_players.begin()), m_players.end(), GetMaxPayoff(m_players.front()),
76+
[this](const Rational &r, const GamePlayer &p) { return std::max(r, GetMaxPayoff(p)); });
77+
}
8178

79+
Rational GameExplicitRep::GetMaxPayoff(const GamePlayer &p_player) const
80+
{
8281
if (m_outcomes.empty()) {
8382
return Rational(0);
8483
}
85-
86-
if (player) {
87-
p1 = p2 = player;
88-
}
89-
else {
90-
p1 = 1;
91-
p2 = NumPlayers();
92-
}
93-
94-
Rational maxpay = m_outcomes.front()->GetPayoff<Rational>(GetPlayer(p1));
95-
for (auto outcome : m_outcomes) {
96-
for (int p = p1; p <= p2; p++) {
97-
maxpay = std::max(maxpay, outcome->GetPayoff<Rational>(GetPlayer(p)));
98-
}
99-
}
100-
return maxpay;
84+
return std::accumulate(std::next(m_outcomes.begin()), m_outcomes.end(),
85+
m_outcomes.front()->GetPayoff<Rational>(p_player),
86+
[&p_player](const Rational &r, const GameOutcomeRep *c) {
87+
return std::max(r, c->GetPayoff<Rational>(p_player));
88+
});
10189
}
10290

10391
//------------------------------------------------------------------------

src/games/gameexpl.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ class GameExplicitRep : public GameRep {
4343

4444
/// @name General data access
4545
//@{
46-
/// Returns the smallest payoff in any outcome of the game
47-
Rational GetMinPayoff(int pl = 0) const override;
48-
/// Returns the largest payoff in any outcome of the game
49-
Rational GetMaxPayoff(int pl = 0) const override;
46+
/// Returns the smallest payoff to any player in any outcome of the game
47+
virtual Rational GetMinPayoff() const override;
48+
/// Returns the smallest payoff to the player in any outcome of the game
49+
virtual Rational GetMinPayoff(const GamePlayer &) const override;
50+
/// Returns the largest payoff to any player in any outcome of the game
51+
virtual Rational GetMaxPayoff() const override;
52+
/// Returns the largest payoff to the player in any outcome of the game
53+
virtual Rational GetMaxPayoff(const GamePlayer &) const override;
5054
//@}
5155

5256
/// @name Dimensions of the game

src/pygambit/gambit.pxd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ cdef extern from "games/game.h":
218218
int BehavProfileLength() except +
219219

220220
bool IsConstSum() except +
221-
c_Rational GetMinPayoff(int) except +
222-
c_Rational GetMaxPayoff(int) except +
221+
c_Rational GetMinPayoff() except +
222+
c_Rational GetMinPayoff(c_GamePlayer) except +
223+
c_Rational GetMaxPayoff() except +
224+
c_Rational GetMaxPayoff(c_GamePlayer) except +
223225
bool IsPerfectRecall() except +
224226

225227
c_Game SetChanceProbs(c_GameInfoset, Array[c_Number]) except +

src/pygambit/game.pxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,12 @@ class Game:
704704
@property
705705
def min_payoff(self) -> typing.Union[decimal.Decimal, Rational]:
706706
"""The minimum payoff in the game."""
707-
return rat_to_py(self.game.deref().GetMinPayoff(0))
707+
return rat_to_py(self.game.deref().GetMinPayoff())
708708

709709
@property
710710
def max_payoff(self) -> typing.Union[decimal.Decimal, Rational]:
711711
"""The maximum payoff in the game."""
712-
return rat_to_py(self.game.deref().GetMaxPayoff(0))
712+
return rat_to_py(self.game.deref().GetMaxPayoff())
713713

714714
def set_chance_probs(self, infoset: typing.Union[Infoset, str], probs: typing.Sequence):
715715
"""Set the action probabilities at chance information set `infoset`.

src/pygambit/player.pxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ class Player:
227227
@property
228228
def min_payoff(self) -> Rational:
229229
"""Returns the smallest payoff for the player in any outcome of the game."""
230-
return rat_to_py(self.player.deref().GetGame().deref().GetMinPayoff(self.number + 1))
230+
return rat_to_py(self.player.deref().GetGame().deref().GetMinPayoff(self.player))
231231

232232
@property
233233
def max_payoff(self) -> Rational:
234234
"""Returns the largest payoff for the player in any outcome of the game."""
235-
return rat_to_py(self.player.deref().GetGame().deref().GetMaxPayoff(self.number + 1))
235+
return rat_to_py(self.player.deref().GetGame().deref().GetMaxPayoff(self.player))

0 commit comments

Comments
 (0)