Skip to content
1 change: 1 addition & 0 deletions src/core/segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ template <class Storage> class Segmented {

void SetFlattened(const Storage &v) { m_values = v; }
const Storage &GetFlattened() const { return m_values; }
Storage &GetFlattened() { return m_values; }
};

template <class T> using SegmentedArray = Segmented<Array<T>>;
Expand Down
22 changes: 18 additions & 4 deletions src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,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_probs(p_support.GetShape()), m_offsets(p_support.GetShape()), m_support(p_support),
m_gameversion(p_support.GetGame()->GetVersion())
{
int index = 1;
for (auto player : p_support.GetGame()->GetPlayers()) {
for (auto strategy : player->GetStrategies()) {
if (p_support.Contains(strategy)) {
m_profileIndex[strategy] = index++;
m_offsets.GetFlattened()[index] = StrategyOffset(strategy);
m_profileIndex[strategy] = index;
index++;
}
else {
m_profileIndex[strategy] = -1;
Expand Down Expand Up @@ -367,8 +369,20 @@ template <class T> void MixedStrategyProfile<T>::ComputePayoffs() const
Cache newCache;
for (const auto &player : m_rep->GetSupport().GetPlayers()) {
newCache.m_payoffs[player] = GetPayoff(player);
for (const auto &strategy : m_rep->GetSupport().GetStrategies(player)) {
newCache.m_strategyValues[player][strategy] = GetPayoff(strategy);
const auto &strategies = m_rep->GetSupport().GetStrategies(player);
Vector<T> values(strategies.size());
if (m_rep->GetPayoffDerivs(player->GetNumber(), values)) {
auto value_it = values.begin();
for (const auto &strategy : strategies) {
newCache.m_strategyValues[player][strategy] = *value_it;
++value_it;
;
}
}
else {
for (const auto &strategy : m_rep->GetSupport().GetStrategies(player)) {
newCache.m_strategyValues[player][strategy] = GetPayoff(strategy);
}
}
}
newCache.m_valid = true;
Expand Down
1 change: 1 addition & 0 deletions src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
friend class StrategySupportProfile;
template <class T> friend class MixedBehaviorProfile;
template <class T> friend class MixedStrategyProfile;
template <class T> friend class MixedStrategyProfileRep;
template <class T> friend class TableMixedStrategyProfileRep;

protected:
Expand Down
6 changes: 3 additions & 3 deletions src/games/gameagg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ template <class T> T AGGMixedStrategyProfileRep<T>::GetPayoff(int pl) const
const GameStrategy strategy =
this->m_support.GetGame()->GetPlayer(i + 1)->GetStrategy(j + 1);
const int ind = this->m_profileIndex.at(strategy);
s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs[ind];
s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs.GetFlattened()[ind];
}
}
return (T)g.aggPtr->getMixedPayoff(pl - 1, s);
Expand All @@ -125,7 +125,7 @@ T AGGMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &ps)
const GameStrategy strategy =
this->m_support.GetGame()->GetPlayer(i + 1)->GetStrategy(j + 1);
const int &ind = this->m_profileIndex.at(strategy);
s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs[ind];
s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs.GetFlattened()[ind];
}
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ T AGGMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &ps1,
const GameStrategy strategy =
this->m_support.GetGame()->GetPlayer(i + 1)->GetStrategy(j + 1);
const int ind = this->m_profileIndex.at(strategy);
s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs[ind];
s[g.aggPtr->firstAction(i) + j] = (ind == -1) ? (T)0 : this->m_probs.GetFlattened()[ind];
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/games/gamebagg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ template <class T> T BAGGMixedStrategyProfileRep<T>::GetPayoff(int pl) const
->GetPlayer(g.baggPtr->typeOffset[i] + tp + 1)
->GetStrategy(j + 1);
const int ind = this->m_profileIndex.at(strategy);
s.at(offs) = (ind == -1) ? (T)0 : this->m_probs[ind];
s.at(offs) = (ind == -1) ? (T)0 : this->m_probs.GetFlattened()[ind];
}
}
}
Expand Down Expand Up @@ -144,7 +144,8 @@ T BAGGMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &ps)
->GetPlayer(g.baggPtr->typeOffset[i] + tp + 1)
->GetStrategy(j + 1);
const int ind = this->m_profileIndex.at(strategy);
s.at(g.baggPtr->firstAction(i, tp) + j) = (ind == -1) ? Rational(0) : this->m_probs[ind];
s.at(g.baggPtr->firstAction(i, tp) + j) =
(ind == -1) ? Rational(0) : this->m_probs.GetFlattened()[ind];
}
}
}
Expand Down Expand Up @@ -191,7 +192,7 @@ T BAGGMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &ps1
->GetStrategy(j + 1);
const 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]);
static_cast<T>((ind == -1) ? T(0) : this->m_probs.GetFlattened()[ind]);
}
}
}
Expand Down
Loading