Skip to content

Commit 8ee828b

Browse files
committed
Bit of tidying in TreeMixedStrategyProfileRep
1 parent 5c6f8d0 commit 8ee828b

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

src/games/gametree.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,34 @@ template <class T> MixedStrategyProfileRep<T> *TreeMixedStrategyProfileRep<T>::C
5050
return new TreeMixedStrategyProfileRep(*this);
5151
}
5252

53-
template <class T> T TreeMixedStrategyProfileRep<T>::GetPayoff(int pl) const
53+
template <class T> void TreeMixedStrategyProfileRep<T>::MakeBehavior() const
5454
{
5555
if (mixed_behav_profile_sptr.get() == nullptr) {
5656
MixedStrategyProfile<T> tmp(Copy());
5757
mixed_behav_profile_sptr = std::make_shared<MixedBehaviorProfile<T>>(tmp);
5858
}
59-
return mixed_behav_profile_sptr->GetPayoff(pl);
6059
}
6160

6261
template <class T> void TreeMixedStrategyProfileRep<T>::InvalidateCache() const
6362
{
6463
mixed_behav_profile_sptr = nullptr;
6564
}
6665

66+
template <class T> T TreeMixedStrategyProfileRep<T>::GetPayoff(int pl) const
67+
{
68+
MakeBehavior();
69+
return mixed_behav_profile_sptr->GetPayoff(pl);
70+
}
71+
6772
template <class T>
6873
T TreeMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &strategy) const
6974
{
70-
MixedStrategyProfile<T> foo(Copy());
71-
for (auto s : this->m_support.GetStrategies(this->m_support.GetGame()->GetPlayer(pl))) {
72-
foo[s] = static_cast<T>(0);
75+
auto foo = std::make_unique<TreeMixedStrategyProfileRep>(*this);
76+
for (auto s : this->m_support.GetStrategies(strategy->GetPlayer())) {
77+
(*foo)[s] = static_cast<T>(0);
7378
}
74-
foo[strategy] = static_cast<T>(1);
75-
return foo.GetPayoff(pl);
79+
(*foo)[strategy] = static_cast<T>(1);
80+
return foo->GetPayoff(pl);
7681
}
7782

7883
template <class T>
@@ -85,18 +90,18 @@ T TreeMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &str
8590
return T(0);
8691
}
8792

88-
MixedStrategyProfile<T> foo(Copy());
93+
auto foo = std::make_unique<TreeMixedStrategyProfileRep>(*this);
8994
for (auto strategy : this->m_support.GetStrategies(player1)) {
90-
foo[strategy] = T(0);
95+
(*foo)[strategy] = T(0);
9196
}
92-
foo[strategy1] = T(1);
97+
(*foo)[strategy1] = T(1);
9398

9499
for (auto strategy : this->m_support.GetStrategies(player2)) {
95-
foo[strategy] = T(0);
100+
(*foo)[strategy] = T(0);
96101
}
97-
foo[strategy2] = T(1);
102+
(*foo)[strategy2] = T(1);
98103

99-
return foo.GetPayoff(pl);
104+
return foo->GetPayoff(pl);
100105
}
101106

102107
template class TreeMixedStrategyProfileRep<double>;

src/games/gametree.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,11 @@ template <class T> class TreeMixedStrategyProfileRep : public MixedStrategyProfi
320320
T GetPayoffDeriv(int pl, const GameStrategy &) const override;
321321
T GetPayoffDeriv(int pl, const GameStrategy &, const GameStrategy &) const override;
322322

323-
void InvalidateCache() const override;
324-
325-
protected:
323+
private:
326324
mutable std::shared_ptr<MixedBehaviorProfile<T>> mixed_behav_profile_sptr;
325+
326+
void MakeBehavior() const;
327+
void InvalidateCache() const override;
327328
};
328329

329330
} // namespace Gambit

0 commit comments

Comments
 (0)