Skip to content

Commit d51360f

Browse files
committed
Implement computation of all payoff first derivatives for a player at once.
1 parent cefd1a3 commit d51360f

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/games/game.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,20 @@ template <class T> void MixedStrategyProfile<T>::ComputePayoffs() const
369369
Cache newCache;
370370
for (const auto &player : m_rep->GetSupport().GetPlayers()) {
371371
newCache.m_payoffs[player] = GetPayoff(player);
372-
for (const auto &strategy : m_rep->GetSupport().GetStrategies(player)) {
373-
newCache.m_strategyValues[player][strategy] = GetPayoff(strategy);
372+
const auto &strategies = m_rep->GetSupport().GetStrategies(player);
373+
Vector<T> values(strategies.size());
374+
if (m_rep->GetPayoffDerivs(player->GetNumber(), values)) {
375+
auto value_it = values.begin();
376+
for (const auto &strategy : strategies) {
377+
newCache.m_strategyValues[player][strategy] = *value_it;
378+
++value_it;
379+
;
380+
}
381+
}
382+
else {
383+
for (const auto &strategy : m_rep->GetSupport().GetStrategies(player)) {
384+
newCache.m_strategyValues[player][strategy] = GetPayoff(strategy);
385+
}
374386
}
375387
}
376388
newCache.m_valid = true;

src/games/gametable.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ template <class T> class TableMixedStrategyProfileRep : public MixedStrategyProf
282282
std::unique_ptr<MixedStrategyProfileRep<T>> Copy() const override;
283283
T GetPayoff(int pl) const override;
284284
T GetPayoffDeriv(int pl, const GameStrategy &) const override;
285+
bool GetPayoffDerivs(int pl, Vector<T> &p_derivs) const override;
285286
T GetPayoffDeriv(int pl, const GameStrategy &, const GameStrategy &) const override;
286287
};
287288

@@ -322,6 +323,26 @@ T TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &st
322323
return value;
323324
}
324325

326+
template <class T>
327+
bool TableMixedStrategyProfileRep<T>::GetPayoffDerivs(int pl, Vector<T> &p_derivs) const
328+
{
329+
const auto game = this->GetSupport().GetGame();
330+
auto &g = dynamic_cast<GameTableRep &>(*game);
331+
const auto player = game->GetPlayer(pl);
332+
p_derivs = T{0};
333+
auto segment = this->m_offsets.segment(pl);
334+
for (auto [index, prob] : ProductDistribution<T>(this->m_probs, this->m_offsets, pl)) {
335+
auto deriv_it = p_derivs.begin();
336+
for (const auto base_index : segment) {
337+
if (const auto outcome = g.m_results[base_index + index]) {
338+
*deriv_it += prob * outcome->template GetPayoff<T>(player);
339+
++deriv_it;
340+
}
341+
}
342+
}
343+
return true;
344+
}
345+
325346
template <class T>
326347
T TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &strategy1,
327348
const GameStrategy &strategy2) const

src/games/stratmixed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ template <class T> class MixedStrategyProfileRep {
9898
}
9999
virtual T GetPayoff(int pl) const = 0;
100100
virtual T GetPayoffDeriv(int pl, const GameStrategy &) const = 0;
101+
virtual bool GetPayoffDerivs(int pl, Vector<T> &p_derivs) const { return false; }
101102
virtual T GetPayoffDeriv(int pl, const GameStrategy &, const GameStrategy &) const = 0;
102103

103104
T GetPayoff(const GamePlayer &p_player) const { return GetPayoff(p_player->GetNumber()); }

0 commit comments

Comments
 (0)