From c9a7f8c6a7a8193ce1aa21fdf8e5338288be635a Mon Sep 17 00:00:00 2001 From: Ted Turocy Date: Thu, 7 Nov 2024 11:07:37 +0000 Subject: [PATCH] Use a map to point to strategy probabilities in MixedStrategyProfileRep. --- src/games/game.cc | 7 +++---- src/games/game.h | 7 ++----- src/games/gameagg.cc | 11 +++-------- src/games/gamebagg.cc | 11 +++-------- src/games/gametable.cc | 7 ------- src/games/gametree.cc | 6 ------ src/games/stratmixed.h | 9 +++------ 7 files changed, 14 insertions(+), 44 deletions(-) diff --git a/src/games/game.cc b/src/games/game.cc index b4ad82b97..000d316f5 100644 --- a/src/games/game.cc +++ b/src/games/game.cc @@ -312,17 +312,16 @@ void GameRep::WriteNfgFile(std::ostream &p_file) const template MixedStrategyProfileRep::MixedStrategyProfileRep(const StrategySupportProfile &p_support) : m_probs(p_support.MixedProfileLength()), m_support(p_support), - m_profileIndex(p_support.GetGame()->MixedProfileLength()), m_gameversion(p_support.GetGame()->GetVersion()) { - int index = 1, stnum = 1; + int index = 1; for (auto player : p_support.GetGame()->GetPlayers()) { for (auto strategy : player->GetStrategies()) { if (p_support.Contains(strategy)) { - m_profileIndex[stnum++] = index++; + m_profileIndex[strategy] = index++; } else { - m_profileIndex[stnum++] = -1; + m_profileIndex[strategy] = -1; } } } diff --git a/src/games/game.h b/src/games/game.h index 1fb664e5f..289ab78b1 100644 --- a/src/games/game.h +++ b/src/games/game.h @@ -239,7 +239,7 @@ class GameStrategyRep : public GameObject { template friend class MixedBehaviorProfile; private: - int m_number, m_id; + int m_number; GamePlayerRep *m_player; long m_offset; std::string m_label; @@ -248,8 +248,7 @@ class GameStrategyRep : public GameObject { /// @name Lifecycle //@{ /// Creates a new strategy for the given player. - explicit GameStrategyRep(GamePlayerRep *p_player) - : m_number(0), m_id(0), m_player(p_player), m_offset(0L) + explicit GameStrategyRep(GamePlayerRep *p_player) : m_number(0), m_player(p_player), m_offset(0L) { } //@} @@ -266,8 +265,6 @@ class GameStrategyRep : public GameObject { GamePlayer GetPlayer() const; /// Returns the index of the strategy for its player int GetNumber() const { return m_number; } - /// Returns the global number of the strategy in the game - int GetId() const { return m_id; } /// Remove this strategy from the game void DeleteStrategy(); diff --git a/src/games/gameagg.cc b/src/games/gameagg.cc index 494a145d2..1ef4b753a 100644 --- a/src/games/gameagg.cc +++ b/src/games/gameagg.cc @@ -103,7 +103,7 @@ template T AGGMixedStrategyProfileRep::GetPayoff(int pl) const for (int i = 0; i < g.aggPtr->getNumPlayers(); ++i) { for (int j = 0; j < g.aggPtr->getNumActions(i); ++j) { GameStrategy strategy = this->m_support.GetGame()->GetPlayer(i + 1)->GetStrategy(j + 1); - int ind = this->m_profileIndex[strategy->GetId()]; + int ind = this->m_profileIndex.at(strategy); s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs[ind]; } } @@ -125,7 +125,7 @@ T AGGMixedStrategyProfileRep::GetPayoffDeriv(int pl, const GameStrategy &ps) else { for (int j = 0; j < g.aggPtr->getNumActions(i); ++j) { GameStrategy strategy = this->m_support.GetGame()->GetPlayer(i + 1)->GetStrategy(j + 1); - const int &ind = this->m_profileIndex[strategy->GetId()]; + const int &ind = this->m_profileIndex.at(strategy); s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs[ind]; } } @@ -161,7 +161,7 @@ T AGGMixedStrategyProfileRep::GetPayoffDeriv(int pl, const GameStrategy &ps1, else { for (int j = 0; j < g.aggPtr->getNumActions(i); ++j) { GameStrategy strategy = this->m_support.GetGame()->GetPlayer(i + 1)->GetStrategy(j + 1); - const int &ind = this->m_profileIndex[strategy->GetId()]; + int ind = this->m_profileIndex.at(strategy); s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs[ind]; } } @@ -185,11 +185,6 @@ GameAGGRep::GameAGGRep(std::shared_ptr p_aggPtr) : aggPtr(p_aggPtr) m_players[pl]->m_strategies[st]->SetLabel(lexical_cast(st)); } } - for (int pl = 1, id = 1; pl <= m_players.Length(); pl++) { - for (int st = 1; st <= m_players[pl]->m_strategies.Length(); - m_players[pl]->m_strategies[st++]->m_id = id++) - ; - } } Game GameAGGRep::Copy() const diff --git a/src/games/gamebagg.cc b/src/games/gamebagg.cc index 7a1deefb2..3b931d7a9 100644 --- a/src/games/gamebagg.cc +++ b/src/games/gamebagg.cc @@ -115,7 +115,7 @@ template T BAGGMixedStrategyProfileRep::GetPayoff(int pl) const GameStrategy strategy = this->m_support.GetGame() ->GetPlayer(g.baggPtr->typeOffset[i] + tp + 1) ->GetStrategy(j + 1); - int ind = this->m_profileIndex[strategy->GetId()]; + int ind = this->m_profileIndex.at(strategy); s.at(offs) = (ind == -1) ? (T)0 : this->m_probs[ind]; } } @@ -146,7 +146,7 @@ T BAGGMixedStrategyProfileRep::GetPayoffDeriv(int pl, const GameStrategy &ps) GameStrategy strategy = this->m_support.GetGame() ->GetPlayer(g.baggPtr->typeOffset[i] + tp + 1) ->GetStrategy(j + 1); - int ind = this->m_profileIndex[strategy->GetId()]; + int ind = this->m_profileIndex.at(strategy); s.at(g.baggPtr->firstAction(i, tp) + j) = (ind == -1) ? Rational(0) : this->m_probs[ind]; } } @@ -192,7 +192,7 @@ T BAGGMixedStrategyProfileRep::GetPayoffDeriv(int pl, const GameStrategy &ps1 GameStrategy strategy = this->m_support.GetGame() ->GetPlayer(g.baggPtr->typeOffset[i] + tp + 1) ->GetStrategy(j + 1); - int ind = this->m_profileIndex[strategy->GetId()]; + int ind = this->m_profileIndex.at(strategy); s.at(g.baggPtr->firstAction(i, tp) + j) = static_cast((ind == -1) ? T(0) : this->m_probs[ind]); } @@ -223,11 +223,6 @@ GameBAGGRep::GameBAGGRep(std::shared_ptr _baggPtr) } } } - for (int pl = 1, id = 1; pl <= m_players.Length(); pl++) { - for (int st = 1; st <= m_players[pl]->m_strategies.Length(); - m_players[pl]->m_strategies[st++]->m_id = id++) - ; - } } Game GameBAGGRep::Copy() const diff --git a/src/games/gametable.cc b/src/games/gametable.cc index 9d4c5d3b6..4c0e12520 100644 --- a/src/games/gametable.cc +++ b/src/games/gametable.cc @@ -498,13 +498,6 @@ void GameTableRep::IndexStrategies() } offset *= player->m_strategies.size(); } - - int id = 1; - for (auto player : m_players) { - for (auto strategy : player->m_strategies) { - strategy->m_id = id++; - } - } } } // end namespace Gambit diff --git a/src/games/gametree.cc b/src/games/gametree.cc index a54b1bf5f..d887e8a88 100644 --- a/src/games/gametree.cc +++ b/src/games/gametree.cc @@ -907,12 +907,6 @@ void GameTreeRep::BuildComputedValues() m_players[pl]->MakeReducedStrats(m_root, nullptr); } - for (int pl = 1, id = 1; pl <= m_players.Length(); pl++) { - for (int st = 1; st <= m_players[pl]->m_strategies.Length(); - m_players[pl]->m_strategies[st++]->m_id = id++) - ; - } - m_computedValues = true; } diff --git a/src/games/stratmixed.h b/src/games/stratmixed.h index 1b6f010a5..6bf55fa7e 100644 --- a/src/games/stratmixed.h +++ b/src/games/stratmixed.h @@ -34,7 +34,7 @@ template class MixedStrategyProfileRep { Vector m_probs; StrategySupportProfile m_support; /// The index into the strategy profile for a strategy (-1 if not in support) - Array m_profileIndex; + std::map m_profileIndex; unsigned int m_gameversion; explicit MixedStrategyProfileRep(const StrategySupportProfile &); @@ -46,13 +46,10 @@ template class MixedStrategyProfileRep { /// Returns the probability the strategy is played const T &operator[](const GameStrategy &p_strategy) const { - return m_probs[m_profileIndex[p_strategy->GetId()]]; + return m_probs[m_profileIndex.at(p_strategy)]; } /// Returns the probability the strategy is played - T &operator[](const GameStrategy &p_strategy) - { - return m_probs[m_profileIndex[p_strategy->GetId()]]; - } + T &operator[](const GameStrategy &p_strategy) { return m_probs[m_profileIndex.at(p_strategy)]; } virtual T GetPayoff(int pl) const = 0; virtual T GetPayoffDeriv(int pl, const GameStrategy &) const = 0;