Skip to content

Commit

Permalink
caching in TreeMixedStrategyProfileRep<T>::GetPayoff(int pl)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsavani committed Mar 14, 2024
1 parent 468e8a9 commit 963625c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ template <class T>
MixedStrategyProfile<T>::MixedStrategyProfile(const MixedStrategyProfile<T> &p_profile)
: m_rep(p_profile.m_rep->Copy())
{
// NEW
InvalidateCache();
}

Expand Down Expand Up @@ -537,7 +536,6 @@ template <class T> void MixedStrategyProfile<T>::ComputePayoffs() const
return;
}

std::cout << "TESTING TESTING 123" << std::endl;
for (auto player : m_rep->m_support.GetPlayers()) {
map_profile_payoffs[player] = GetPayoff(player);
// values of the player's strategies
Expand Down
22 changes: 15 additions & 7 deletions src/games/gametree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ template <class T> MixedStrategyProfileRep<T> *TreeMixedStrategyProfileRep<T>::C

template <class T> T TreeMixedStrategyProfileRep<T>::GetPayoff(int pl) const
{
MixedStrategyProfile<T> profile(Copy());
return MixedBehaviorProfile<T>(profile).GetPayoff(pl);
if (mixed_behav_profile_sptr.get() == nullptr) {
MixedStrategyProfile<T> tmp(Copy());
mixed_behav_profile_sptr = std::make_shared<MixedBehaviorProfile<T>>(tmp);
}
return mixed_behav_profile_sptr->GetPayoff(pl);
}

template <class T> void TreeMixedStrategyProfileRep<T>::InvalidateCache() const
{
mixed_behav_profile_sptr = nullptr;
}

template <class T>
Expand All @@ -73,19 +81,19 @@ T TreeMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &str
GamePlayerRep *player1 = strategy1->GetPlayer();
GamePlayerRep *player2 = strategy2->GetPlayer();
if (player1 == player2) {
return (T)0;
return T(0);
}

MixedStrategyProfile<T> foo(Copy());
for (auto strategy : this->m_support.GetStrategies(player1)) {
foo[strategy] = (T)0;
foo[strategy] = T(0);
}
foo[strategy1] = (T)1;
foo[strategy1] = T(1);

for (auto strategy : this->m_support.GetStrategies(player2)) {
foo[strategy] = (T)0;
foo[strategy] = T(0);
}
foo[strategy2] = (T)1;
foo[strategy2] = T(1);

return foo.GetPayoff(pl);
}
Expand Down
5 changes: 5 additions & 0 deletions src/games/gametree.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ template <class T> class TreeMixedStrategyProfileRep : public MixedStrategyProfi
T GetPayoff(int pl) const override;
T GetPayoffDeriv(int pl, const GameStrategy &) const override;
T GetPayoffDeriv(int pl, const GameStrategy &, const GameStrategy &) const override;

void InvalidateCache() const override;

protected:
mutable std::shared_ptr<MixedBehaviorProfile<T>> mixed_behav_profile_sptr;
};

} // namespace Gambit
Expand Down
7 changes: 5 additions & 2 deletions src/games/stratmixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ template <class T> class MixedStrategyProfileRep {
T GetRegret(const GameStrategy &) const;
T GetRegret(const GamePlayer &) const;
T GetMaxRegret() const;

virtual void InvalidateCache() const {};
};

/// \brief A probability distribution over strategies in a game
Expand Down Expand Up @@ -141,7 +143,7 @@ template <class T> class MixedStrategyProfile {
T &operator[](int i)
{
CheckVersion();
InvalidateCache(); // NEW
InvalidateCache();
return m_rep->m_probs[i];
}

Expand Down Expand Up @@ -230,10 +232,11 @@ template <class T> class MixedStrategyProfile {
mutable std::map<GamePlayer, std::map<GameStrategy, T>> map_strategy_payoffs;
mutable std::map<GamePlayer, T> map_profile_payoffs;
/// Reset cache for payoffs and strategy values
void InvalidateCache() const
virtual void InvalidateCache() const
{
map_strategy_payoffs.clear();
map_profile_payoffs.clear();
m_rep->InvalidateCache();
}

/// Computes the payoff of the profile to player 'pl'
Expand Down

0 comments on commit 963625c

Please sign in to comment.