Skip to content

Commit

Permalink
Some STLisation of game representation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Apr 5, 2023
1 parent 3a7c7e1 commit b0743c8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
77 changes: 43 additions & 34 deletions src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//

#include <iostream>
#include <sstream>

#include "gambit.h"
#include "gametree.h"
Expand Down Expand Up @@ -70,9 +69,13 @@ GamePlayerRep::GamePlayerRep(GameRep *p_game, int p_id, int p_strats)
}

GamePlayerRep::~GamePlayerRep()
{
for (int j = 1; j <= m_infosets.Length(); m_infosets[j++]->Invalidate());
for (int j = 1; j <= m_strategies.Length(); m_strategies[j++]->Invalidate());
{
for (auto infoset : m_infosets) {
infoset->Invalidate();
}
for (auto strategy : m_strategies) {
strategy->Invalidate();
}
}


Expand All @@ -82,7 +85,7 @@ GameStrategy GamePlayerRep::NewStrategy()

auto *strategy = new GameStrategyRep(this);
m_strategies.push_back(strategy);
strategy->m_number = m_strategies.Length();
strategy->m_number = m_strategies.size();
strategy->m_offset = -1; // this flags this action as new
dynamic_cast<GameTableRep *>(m_game)->RebuildTable();
return strategy;
Expand All @@ -106,7 +109,7 @@ void GamePlayerRep::MakeStrategy()
strategy->m_label = "";

// We generate a default labeling -- probably should be changed in future
if (strategy->m_behav.Length() > 0) {
if (!strategy->m_behav.empty()) {
for (int iset = 1; iset <= strategy->m_behav.Length(); iset++) {
if (strategy->m_behav[iset] > 0) {
strategy->m_label += lexical_cast<std::string>(strategy->m_behav[iset]);
Expand Down Expand Up @@ -473,9 +476,12 @@ void GameRep::WriteNfgFile(std::ostream &p_file) const

GameExplicitRep::~GameExplicitRep()
{
for (int pl = 1; pl <= m_players.Length(); m_players[pl++]->Invalidate());
for (int outc = 1; outc <= m_outcomes.Length();
m_outcomes[outc++]->Invalidate());
for (auto player : m_players) {
player->Invalidate();
}
for (auto outcome : m_outcomes) {
outcome->Invalidate();
}
}

//------------------------------------------------------------------------
Expand All @@ -484,9 +490,11 @@ GameExplicitRep::~GameExplicitRep()

Rational GameExplicitRep::GetMinPayoff(int player) const
{
int index, p, p1, p2;
int p1, p2;

if (m_outcomes.Length() == 0) return Rational(0);
if (m_outcomes.empty()) {
return { 0 };
}

if (player) {
p1 = p2 = player;
Expand All @@ -495,23 +503,23 @@ Rational GameExplicitRep::GetMinPayoff(int player) const
p1 = 1;
p2 = NumPlayers();
}

Rational minpay = m_outcomes[1]->GetPayoff<Rational>(p1);
for (index = 1; index <= m_outcomes.Length(); index++) {
for (p = p1; p <= p2; p++) {
if (m_outcomes[index]->GetPayoff<Rational>(p) < minpay) {
minpay = m_outcomes[index]->GetPayoff<Rational>(p);
}

Rational minpay = m_outcomes.front()->GetPayoff<Rational>(p1);
for (auto outcome : m_outcomes) {
for (int p = p1; p <= p2; p++) {
minpay = std::min(minpay, outcome->GetPayoff<Rational>(p));
}
}
return minpay;
}

Rational GameExplicitRep::GetMaxPayoff(int player) const
{
int index, p, p1, p2;
int p1, p2;

if (m_outcomes.Length() == 0) return Rational(0);
if (m_outcomes.empty()) {
return { 0 };
}

if (player) {
p1 = p2 = player;
Expand All @@ -521,11 +529,11 @@ Rational GameExplicitRep::GetMaxPayoff(int player) const
p2 = NumPlayers();
}

Rational maxpay = m_outcomes[1]->GetPayoff<Rational>(p1);
for (index = 1; index <= m_outcomes.Length(); index++) {
for (p = p1; p <= p2; p++)
if (m_outcomes[index]->GetPayoff<Rational>(p) > maxpay)
maxpay = m_outcomes[index]->GetPayoff<Rational>(p);
Rational maxpay = m_outcomes.front()->GetPayoff<Rational>(p1);
for (auto outcome : m_outcomes) {
for (int p = p1; p <= p2; p++) {
maxpay = std::max(maxpay, outcome->GetPayoff<Rational>(p));
}
}
return maxpay;
}
Expand All @@ -537,9 +545,9 @@ Rational GameExplicitRep::GetMaxPayoff(int player) const
Array<int> GameExplicitRep::NumStrategies() const
{
const_cast<GameExplicitRep *>(this)->BuildComputedValues();
Array<int> dim(m_players.Length());
for (int pl = 1; pl <= m_players.Length(); pl++) {
dim[pl] = m_players[pl]->m_strategies.Length();
Array<int> dim(m_players.size());
for (int pl = 1; pl <= m_players.size(); pl++) {
dim[pl] = m_players[pl]->m_strategies.size();
}
return dim;
}
Expand All @@ -561,8 +569,8 @@ int GameExplicitRep::NumStrategyContingencies() const
{
const_cast<GameExplicitRep *>(this)->BuildComputedValues();
int ncont = 1;
for (int pl = 1; pl <= m_players.Length(); pl++) {
ncont *= m_players[pl]->m_strategies.Length();
for (auto player : m_players) {
ncont *= player->m_strategies.size();
}
return ncont;
}
Expand All @@ -571,8 +579,9 @@ int GameExplicitRep::MixedProfileLength() const
{
const_cast<GameExplicitRep *>(this)->BuildComputedValues();
int strats = 0;
for (int i = 1; i <= m_players.Length();
strats += m_players[i++]->m_strategies.Length());
for (auto player : m_players) {
strats += player->m_strategies.size();
}
return strats;
}

Expand All @@ -583,8 +592,8 @@ int GameExplicitRep::MixedProfileLength() const

GameOutcome GameExplicitRep::NewOutcome()
{
m_outcomes.push_back(new GameOutcomeRep(this, m_outcomes.Length() + 1));
return m_outcomes[m_outcomes.Last()];
m_outcomes.push_back(new GameOutcomeRep(this, m_outcomes.size() + 1));
return m_outcomes.back();
}

//------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ class GamePlayerRep : public GameObject {
/// @name Information sets
//@{
/// Returns the number of information sets at which the player makes a choice
int NumInfosets() const { return m_infosets.Length(); }
int NumInfosets() const { return m_infosets.size(); }
/// Returns the p_index'th information set
GameInfoset GetInfoset(int p_index) const;

Expand Down Expand Up @@ -821,7 +821,7 @@ inline GamePlayer GameStrategyRep::GetPlayer() const { return m_player; }

inline Game GamePlayerRep::GetGame() const { return m_game; }
inline int GamePlayerRep::NumStrategies() const
{ m_game->BuildComputedValues(); return m_strategies.Length(); }
{ m_game->BuildComputedValues(); return m_strategies.size(); }
inline GameStrategy GamePlayerRep::GetStrategy(int st) const
{ m_game->BuildComputedValues(); return m_strategies[st]; }
inline const GameStrategyArray &GamePlayerRep::Strategies() const
Expand Down
11 changes: 6 additions & 5 deletions src/games/gametable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ namespace {
int Product(const Array<int> &dim)
{
int accum = 1;
for (int i = 1; i <= dim.Length(); accum *= dim[i++]);
for (auto d : dim) {
accum *= d;
}
return accum;
}

Expand Down Expand Up @@ -290,11 +292,10 @@ void GameTableRep::WriteNfgFile(std::ostream &p_file) const

GamePlayer GameTableRep::NewPlayer()
{
GamePlayerRep *player = nullptr;
player = new GamePlayerRep(this, m_players.Length() + 1, 1);
GamePlayerRep *player = new GamePlayerRep(this, m_players.size() + 1, 1);
m_players.push_back(player);
for (int outc = 1; outc <= m_outcomes.Last(); outc++) {
m_outcomes[outc]->m_payoffs.push_back(Number());
for (auto outcome : m_outcomes) {
outcome->m_payoffs.push_back(Number());
}
ClearComputedValues();
return player;
Expand Down

0 comments on commit b0743c8

Please sign in to comment.