Skip to content

Commit

Permalink
NumStrategies now takes a player
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Oct 24, 2024
1 parent fa74687 commit 912c549
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ MixedStrategyProfileRep<T>::MixedStrategyProfileRep(const StrategySupportProfile
template <class T> void MixedStrategyProfileRep<T>::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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/games/stratpure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/games/stratspt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ bool StrategySupportProfile::Undominated(StrategySupportProfile &newS, const Gam
bool p_strict, bool p_external) const
{
std::vector<GameStrategy> 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());
Expand Down
7 changes: 5 additions & 2 deletions src/games/stratspt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> NumStrategies() const;
Expand Down
17 changes: 11 additions & 6 deletions src/gui/nfgtable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -1089,15 +1094,15 @@ 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;
}

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)
Expand All @@ -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;
}
Expand All @@ -1132,15 +1137,15 @@ 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;
}

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
Expand Down
3 changes: 1 addition & 2 deletions src/pygambit/gambit.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down
4 changes: 3 additions & 1 deletion src/pygambit/stratspt.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit 912c549

Please sign in to comment.