From 912c54922c0af33a54bddbd11d7b7d11b8d7957a Mon Sep 17 00:00:00 2001 From: Ted Turocy Date: Thu, 24 Oct 2024 15:57:34 +0100 Subject: [PATCH] NumStrategies now takes a player --- src/games/game.cc | 2 +- src/games/stratpure.cc | 2 +- src/games/stratspt.cc | 2 +- src/games/stratspt.h | 7 +++++-- src/gui/nfgtable.cc | 17 +++++++++++------ src/pygambit/gambit.pxd | 3 +-- src/pygambit/stratspt.pxi | 4 +++- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/games/game.cc b/src/games/game.cc index 042e533f1..dfe2aa19f 100644 --- a/src/games/game.cc +++ b/src/games/game.cc @@ -332,7 +332,7 @@ MixedStrategyProfileRep::MixedStrategyProfileRep(const StrategySupportProfile template void MixedStrategyProfileRep::SetCentroid() { for (auto player : m_support.GetGame()->GetPlayers()) { - T center = ((T)1) / ((T)m_support.NumStrategies(player->GetNumber())); + T center = T(1) / T(m_support.NumStrategies(player)); for (auto strategy : m_support.GetStrategies(player)) { (*this)[strategy] = center; } diff --git a/src/games/stratpure.cc b/src/games/stratpure.cc index ab74c0949..0c80b5928 100644 --- a/src/games/stratpure.cc +++ b/src/games/stratpure.cc @@ -175,7 +175,7 @@ void StrategyProfileIterator::operator++() } auto player = m_support.GetGame()->GetPlayer(pl); - if (m_currentStrat[pl] < m_support.NumStrategies(pl)) { + if (m_currentStrat[pl] < m_support.NumStrategies(player)) { m_profile->SetStrategy(m_support.GetStrategies(player)[++(m_currentStrat[pl])]); return; } diff --git a/src/games/stratspt.cc b/src/games/stratspt.cc index ecd40c6e5..be3878be2 100644 --- a/src/games/stratspt.cc +++ b/src/games/stratspt.cc @@ -244,7 +244,7 @@ bool StrategySupportProfile::Undominated(StrategySupportProfile &newS, const Gam bool p_strict, bool p_external) const { std::vector set((p_external) ? p_player->NumStrategies() - : NumStrategies(p_player->GetNumber())); + : NumStrategies(p_player)); if (p_external) { auto strategies = p_player->GetStrategies(); std::copy(strategies.begin(), strategies.end(), set.begin()); diff --git a/src/games/stratspt.h b/src/games/stratspt.h index d9570d323..dddf58993 100644 --- a/src/games/stratspt.h +++ b/src/games/stratspt.h @@ -74,8 +74,11 @@ class StrategySupportProfile { /// Returns the game on which the support is defined. Game GetGame() const { return m_nfg; } - /// Returns the number of strategies in the support for player pl. - int NumStrategies(int pl) const { return m_support[pl].size(); } + /// Returns the number of strategies in the support for the player + int NumStrategies(const GamePlayer &p_player) const + { + return m_support[p_player->GetNumber()].size(); + } /// Returns the number of strategies in the support for all players. Array NumStrategies() const; diff --git a/src/gui/nfgtable.cc b/src/gui/nfgtable.cc index 64bebcd5c..b2b76e549 100644 --- a/src/gui/nfgtable.cc +++ b/src/gui/nfgtable.cc @@ -1076,11 +1076,16 @@ void gbtTableWidget::SetRowPlayer(int index, int pl) OnUpdate(); } +int NumStrategies(const StrategySupportProfile &p_profile, int p_player) +{ + return p_profile.NumStrategies(p_profile.GetGame()->GetPlayer(p_player)); +} + int gbtTableWidget::NumRowContingencies() const { int ncont = 1; const Gambit::StrategySupportProfile &support = m_doc->GetNfgSupport(); - for (int i = 1; i <= NumRowPlayers(); ncont *= support.NumStrategies(GetRowPlayer(i++))) + for (int i = 1; i <= NumRowPlayers(); ncont *= NumStrategies(support, GetRowPlayer(i++))) ; return ncont; } @@ -1089,7 +1094,7 @@ int gbtTableWidget::NumRowsSpanned(int index) const { int ncont = 1; const Gambit::StrategySupportProfile &support = m_doc->GetNfgSupport(); - for (int i = index + 1; i <= NumRowPlayers(); ncont *= support.NumStrategies(GetRowPlayer(i++))) + for (int i = index + 1; i <= NumRowPlayers(); ncont *= NumStrategies(support, GetRowPlayer(i++))) ; return ncont; } @@ -1097,7 +1102,7 @@ int gbtTableWidget::NumRowsSpanned(int index) const int gbtTableWidget::RowToStrategy(int player, int row) const { int strat = row / NumRowsSpanned(player); - return (strat % m_doc->GetNfgSupport().NumStrategies(GetRowPlayer(player)) + 1); + return (strat % NumStrategies(m_doc->GetNfgSupport(), GetRowPlayer(player)) + 1); } void gbtTableWidget::SetColPlayer(int index, int pl) @@ -1123,7 +1128,7 @@ int gbtTableWidget::NumColContingencies() const { int ncont = 1; const Gambit::StrategySupportProfile &support = m_doc->GetNfgSupport(); - for (int i = 1; i <= NumColPlayers(); ncont *= support.NumStrategies(GetColPlayer(i++))) + for (int i = 1; i <= NumColPlayers(); ncont *= NumStrategies(support, GetColPlayer(i++))) ; return ncont; } @@ -1132,7 +1137,7 @@ int gbtTableWidget::NumColsSpanned(int index) const { int ncont = 1; const Gambit::StrategySupportProfile &support = m_doc->GetNfgSupport(); - for (int i = index + 1; i <= NumColPlayers(); ncont *= support.NumStrategies(GetColPlayer(i++))) + for (int i = index + 1; i <= NumColPlayers(); ncont *= NumStrategies(support, GetColPlayer(i++))) ; return ncont; } @@ -1140,7 +1145,7 @@ int gbtTableWidget::NumColsSpanned(int index) const int gbtTableWidget::ColToStrategy(int player, int col) const { int strat = col / m_doc->NumPlayers() / NumColsSpanned(player); - return (strat % m_doc->GetNfgSupport().NumStrategies(GetColPlayer(player)) + 1); + return (strat % NumStrategies(m_doc->GetNfgSupport(), GetColPlayer(player)) + 1); } Gambit::PureStrategyProfile gbtTableWidget::CellToProfile(const wxSheetCoords &p_coords) const diff --git a/src/pygambit/gambit.pxd b/src/pygambit/gambit.pxd index 7b3a05d6a..2e359513f 100644 --- a/src/pygambit/gambit.pxd +++ b/src/pygambit/gambit.pxd @@ -348,10 +348,9 @@ cdef extern from "games/stratspt.h": Array[int] NumStrategies() except + int MixedProfileLength() except + int GetIndex(c_GameStrategy) except + - int NumStrategiesPlayer "NumStrategies"(int) except +IndexError + int NumStrategiesPlayer "NumStrategies"(c_GamePlayer) except +IndexError bool IsSubsetOf(c_StrategySupportProfile) except + bool RemoveStrategy(c_GameStrategy) except + - c_GameStrategy GetStrategy(int, int) except +IndexError Array[c_GameStrategy] GetStrategies(c_GamePlayer) except + bool Contains(c_GameStrategy) except + bool IsDominated(c_GameStrategy, bool, bool) except + diff --git a/src/pygambit/stratspt.pxi b/src/pygambit/stratspt.pxi index f7c4ce52a..2fd8431d2 100644 --- a/src/pygambit/stratspt.pxi +++ b/src/pygambit/stratspt.pxi @@ -160,7 +160,9 @@ class StrategySupportProfile: raise MismatchError( "remove(): strategy is not part of the game on which the profile is defined." ) - if deref(self.support).NumStrategiesPlayer(strategy.player.number + 1) == 1: + if deref(self.support).NumStrategiesPlayer( + cython.cast(Player, strategy.player).player + ) == 1: raise UndefinedOperationError( "remove(): cannot remove last strategy of a player" )