Skip to content

Commit

Permalink
Write StrategyProfileIterator using iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Oct 31, 2024
1 parent 24f67b9 commit 0ce1f8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
52 changes: 21 additions & 31 deletions src/games/stratpure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,16 @@ MixedStrategyProfile<Rational> PureStrategyProfileRep::ToMixedStrategyProfile()
//---------------------------------------------------------------------------

StrategyProfileIterator::StrategyProfileIterator(const StrategySupportProfile &p_support)
: m_atEnd(false), m_support(p_support), m_profile(m_support.GetGame()->NewPureStrategyProfile()),
m_frozen1(0), m_frozen2(0)
: m_support(p_support), m_profile(p_support.GetGame()->NewPureStrategyProfile())
{
First();
}

StrategyProfileIterator::StrategyProfileIterator(const StrategySupportProfile &p_support,
const GameStrategy &p_strategy)
: m_atEnd(false), m_support(p_support), m_profile(p_support.GetGame()->NewPureStrategyProfile()),
m_frozen1(p_strategy->GetPlayer()), m_frozen2(0)
: m_support(p_support), m_profile(p_support.GetGame()->NewPureStrategyProfile())
{
m_currentStrat[m_frozen1] = p_strategy->GetNumber();
m_frozen.push_back(p_strategy);
m_profile->SetStrategy(p_strategy);
First();
}
Expand All @@ -132,12 +130,11 @@ StrategyProfileIterator::StrategyProfileIterator(const StrategySupportProfile &p
const GameStrategy &p_strategy1,
const GameStrategy &p_strategy2)

: m_atEnd(false), m_support(p_support), m_profile(m_support.GetGame()->NewPureStrategyProfile()),
m_frozen1(p_strategy1->GetPlayer()), m_frozen2(p_strategy2->GetPlayer())
: m_support(p_support), m_profile(p_support.GetGame()->NewPureStrategyProfile())
{
m_currentStrat[m_frozen1] = p_strategy1->GetNumber();
m_frozen.push_back(p_strategy1);
m_profile->SetStrategy(p_strategy1);
m_currentStrat[m_frozen2] = p_strategy2->GetNumber();
m_frozen.push_back(p_strategy2);
m_profile->SetStrategy(p_strategy2);
First();
}
Expand All @@ -148,38 +145,31 @@ StrategyProfileIterator::StrategyProfileIterator(const StrategySupportProfile &p

void StrategyProfileIterator::First()
{
m_unfrozen = {};
for (auto player : m_support.GetGame()->GetPlayers()) {
if (player == m_frozen1 || player == m_frozen2) {
continue;
auto frozen = std::find_if(m_frozen.begin(), m_frozen.end(), [player](const GameStrategy &s) {
return s->GetPlayer() == player;
});
if (frozen == m_frozen.end()) {
m_unfrozen.push_back(player);
m_currentStrat[player] = m_support.GetStrategies(player).begin();
m_profile->SetStrategy(*m_currentStrat[player]);
}
m_profile->SetStrategy(m_support.GetStrategies(player).front());
m_currentStrat[player] = 1;
}
}

void StrategyProfileIterator::operator++()
{
auto players = m_support.GetGame()->GetPlayers();
auto player = players.begin();

while (true) {
if (player == players.end()) {
m_atEnd = true;
return;
}
if (*player == m_frozen1 || *player == m_frozen2) {
++player;
continue;
}

if (m_currentStrat[*player] < m_support.GetStrategies(*player).size()) {
m_profile->SetStrategy(m_support.GetStrategies(*player)[++(m_currentStrat[*player])]);
for (auto player : m_unfrozen) {
++m_currentStrat[player];
if (m_currentStrat[player] != m_support.GetStrategies(player).end()) {
m_profile->SetStrategy(*m_currentStrat[player]);
return;
}
m_profile->SetStrategy(m_support.GetStrategies(*player).front());
m_currentStrat[*player] = 1;
++player;
m_currentStrat[player] = m_support.GetStrategies(player).begin();
m_profile->SetStrategy(*m_currentStrat[player]);
}
m_atEnd = true;
}

} // namespace Gambit
7 changes: 4 additions & 3 deletions src/games/stratpure.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ class StrategyProfileIterator {
friend class GameTableRep;

private:
bool m_atEnd;
bool m_atEnd{false};
StrategySupportProfile m_support;
std::map<GamePlayer, int> m_currentStrat;
std::map<GamePlayer, StrategySupportProfile::Support::const_iterator> m_currentStrat;
PureStrategyProfile m_profile;
GamePlayer m_frozen1, m_frozen2;
std::vector<GamePlayer> m_unfrozen;
std::vector<GameStrategy> m_frozen;

/// Reset the iterator to the first contingency (this is called by ctors)
void First();
Expand Down

0 comments on commit 0ce1f8e

Please sign in to comment.