Skip to content

Commit

Permalink
Use a map to point to strategy probabilities in MixedStrategyProfileRep.
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Nov 7, 2024
1 parent 4cfd909 commit c9a7f8c
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 44 deletions.
7 changes: 3 additions & 4 deletions src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,16 @@ void GameRep::WriteNfgFile(std::ostream &p_file) const
template <class T>
MixedStrategyProfileRep<T>::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;
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class GameStrategyRep : public GameObject {
template <class T> friend class MixedBehaviorProfile;

private:
int m_number, m_id;
int m_number;
GamePlayerRep *m_player;
long m_offset;
std::string m_label;
Expand All @@ -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)
{
}
//@}
Expand All @@ -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();
Expand Down
11 changes: 3 additions & 8 deletions src/games/gameagg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ template <class T> T AGGMixedStrategyProfileRep<T>::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];
}
}
Expand All @@ -125,7 +125,7 @@ T AGGMixedStrategyProfileRep<T>::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];
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ T AGGMixedStrategyProfileRep<T>::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];
}
}
Expand All @@ -185,11 +185,6 @@ GameAGGRep::GameAGGRep(std::shared_ptr<agg::AGG> p_aggPtr) : aggPtr(p_aggPtr)
m_players[pl]->m_strategies[st]->SetLabel(lexical_cast<std::string>(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
Expand Down
11 changes: 3 additions & 8 deletions src/games/gamebagg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ template <class T> T BAGGMixedStrategyProfileRep<T>::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];
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ T BAGGMixedStrategyProfileRep<T>::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];
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ T BAGGMixedStrategyProfileRep<T>::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<T>((ind == -1) ? T(0) : this->m_probs[ind]);
}
Expand Down Expand Up @@ -223,11 +223,6 @@ GameBAGGRep::GameBAGGRep(std::shared_ptr<agg::BAGG> _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
Expand Down
7 changes: 0 additions & 7 deletions src/games/gametable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions src/games/gametree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 3 additions & 6 deletions src/games/stratmixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ template <class T> class MixedStrategyProfileRep {
Vector<T> m_probs;
StrategySupportProfile m_support;
/// The index into the strategy profile for a strategy (-1 if not in support)
Array<int> m_profileIndex;
std::map<GameStrategy, int> m_profileIndex;
unsigned int m_gameversion;

explicit MixedStrategyProfileRep(const StrategySupportProfile &);
Expand All @@ -46,13 +46,10 @@ template <class T> 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;
Expand Down

0 comments on commit c9a7f8c

Please sign in to comment.