Skip to content

Commit

Permalink
Remove deprecated versions of operator() in MixedBehaviorProfile
Browse files Browse the repository at this point in the history
This removes two redundant/deprecated versions of operator():
* operator()(int, int, int), which is a deprecated way of indexing profiles;
* operator()(GameAction), which is identical to operator[](GameAction).
  • Loading branch information
tturocy committed Feb 14, 2024
1 parent 6027476 commit 8518f48
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 45 deletions.
21 changes: 11 additions & 10 deletions src/games/behavmixed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ template <class T> void MixedBehaviorProfile<T>::SetCentroid()
if (infoset->NumActions() > 0) {
T center = (T(1) / T(infoset->NumActions()));
for (auto act : infoset->GetActions()) {
(*this)(act) = center;
(*this)[act] = center;
}
}
}
Expand All @@ -186,7 +186,7 @@ template <class T> void MixedBehaviorProfile<T>::UndefinedToCentroid()
[this](T total, GameAction act) { return total + GetActionProb(act); });
if (total == T(0)) {
for (auto act : infoset->GetActions()) {
(*this)(act) = T(1) / T(infoset->NumActions());
(*this)[act] = T(1) / T(infoset->NumActions());
}
}
}
Expand All @@ -208,7 +208,7 @@ template <class T> MixedBehaviorProfile<T> MixedBehaviorProfile<T>::Normalize()
continue;
}
for (auto act : infoset->GetActions()) {
norm(act) /= total;
norm[act] /= total;
}
}
return norm;
Expand All @@ -224,13 +224,13 @@ template <> void MixedBehaviorProfile<double>::Randomize()
// renormalize at the end (this is a special case of the Dirichlet distribution).
for (auto infoset : game->GetInfosets()) {
for (auto act : infoset->GetActions()) {
(*this)(act) = -std::log(((double)std::rand()) / ((double)RAND_MAX));
(*this)[act] = -std::log(((double)std::rand()) / ((double)RAND_MAX));
}
}
MixedBehaviorProfile<double> norm(Normalize());
for (auto infoset : game->GetInfosets()) {
for (auto act : infoset->GetActions()) {
(*this)(act) = norm(act);
(*this)[act] = norm[act];
}
}
}
Expand All @@ -245,8 +245,9 @@ template <> void MixedBehaviorProfile<Rational>::Randomize()
template <class T> void MixedBehaviorProfile<T>::Randomize(int p_denom)
{
CheckVersion();
InvalidateCache();
Game game = m_support.GetGame();
*this = T(0);
m_probs = T(0);

for (int pl = 1; pl <= game->NumPlayers(); pl++) {
GamePlayer player = game->GetPlayer(pl);
Expand All @@ -261,10 +262,10 @@ template <class T> void MixedBehaviorProfile<T>::Randomize(int p_denom)
cutoffs.push_back(p_denom);
T sum = T(0);
for (int act = 1; act < infoset->NumActions(); act++) {
(*this)(pl, iset, act) = T(cutoffs[act] - cutoffs[act - 1]) / T(p_denom);
sum += (*this)(pl, iset, act);
m_probs(pl, iset, act) = T(cutoffs[act] - cutoffs[act - 1]) / T(p_denom);
sum += m_probs(pl, iset, act);
}
(*this)(pl, iset, infoset->NumActions()) = T(1) - sum;
m_probs(pl, iset, infoset->NumActions()) = T(1) - sum;
}
}
}
Expand Down Expand Up @@ -353,7 +354,7 @@ template <class T> T MixedBehaviorProfile<T>::GetActionProb(const GameAction &ac
return T(0);
}
else {
return (*this)(action->GetInfoset()->GetPlayer()->GetNumber(),
return m_probs(action->GetInfoset()->GetPlayer()->GetNumber(),
action->GetInfoset()->GetNumber(), m_support.GetIndex(action));
}
}
Expand Down
22 changes: 3 additions & 19 deletions src/games/behavmixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,16 @@ template <class T> class MixedBehaviorProfile {

const T &operator[](const GameAction &p_action) const
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
return m_probs(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}
T &operator[](const GameAction &p_action)
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}

const T &operator()(const GameAction &p_action) const
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}
T &operator()(const GameAction &p_action)
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
InvalidateCache();
return m_probs(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}

const T &operator()(int a, int b, int c) const { return m_probs(a, b, c); }
T &operator()(int a, int b, int c)
{
InvalidateCache();
return m_probs(a, b, c);
}
const T &operator[](int a) const { return m_probs[a]; }
T &operator[](int a)
{
Expand Down
2 changes: 1 addition & 1 deletion src/games/behavpure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ MixedBehaviorProfile<Rational> PureBehaviorProfile::ToMixedBehaviorProfile() con
temp = Rational(0);
for (auto player : m_efg->GetPlayers()) {
for (auto infoset : player->GetInfosets()) {
temp(GetAction(infoset)) = Rational(1);
temp[GetAction(infoset)] = Rational(1);
}
}
return temp;
Expand Down
7 changes: 5 additions & 2 deletions src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,15 @@ MixedStrategyProfile<T>::MixedStrategyProfile(const MixedBehaviorProfile<T> &p_p
Game game = p_profile.GetGame();
auto *efg = dynamic_cast<GameTreeRep *>(game.operator->());
for (int pl = 1; pl <= m_rep->m_support.GetGame()->NumPlayers(); pl++) {
for (int st = 1; st <= m_rep->m_support.GetGame()->GetPlayer(pl)->NumStrategies(); st++) {
GamePlayer player = m_rep->m_support.GetGame()->GetPlayer(pl);
for (int st = 1; st <= player->NumStrategies(); st++) {
T prob = (T)1;

for (int iset = 1; iset <= efg->GetPlayer(pl)->NumInfosets(); iset++) {
if (efg->m_players[pl]->m_strategies[st]->m_behav[iset] > 0) {
prob *= p_profile(pl, iset, efg->m_players[pl]->m_strategies[st]->m_behav[iset]);
GameInfoset infoset = player->GetInfoset(iset);
prob *=
p_profile[infoset->GetAction(efg->m_players[pl]->m_strategies[st]->m_behav[iset])];
}
}
(*this)[m_rep->m_support.GetGame()->GetPlayer(pl)->GetStrategy(st)] = prob;
Expand Down
5 changes: 2 additions & 3 deletions src/games/nash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ void BehavStrategyDetailRenderer<T>::Render(const MixedBehaviorProfile<T> &p_pro
m_stream << std::setw(7) << action->GetNumber() << " ";
}
m_stream << std::setw(11);
m_stream << lexical_cast<std::string>(p_profile(player->GetNumber(), iset, act),
m_numDecimals);
m_stream << lexical_cast<std::string>(p_profile[action], m_numDecimals);
m_stream << " ";
m_stream << std::setw(11);
m_stream << lexical_cast<std::string>(p_profile.GetPayoff(infoset->GetAction(act)),
Expand Down Expand Up @@ -341,7 +340,7 @@ void SubgameBehavSolver<T>::SolveSubgames(const Game &p_game, const DVector<T> &
int id = atoi(subinfoset->GetLabel().c_str());
for (int act = 1; act <= subsupport.NumActions(pl, iset); act++) {
int actno = subsupport.GetAction(pl, iset, act)->GetNumber();
solns[solns.Length()](pl, id, actno) = sol[solno](pl, iset, act);
solns[solns.Length()](pl, id, actno) = sol[solno][subinfoset->GetAction(act)];
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pygambit/gambit.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ cdef extern from "games/behavmixed.h":
void Randomize() except +TypeError
void Randomize(int) except +
double getitem "operator[]"(int) except +IndexError
double getaction "operator()"(c_GameAction) except +IndexError
double getaction "operator[]"(c_GameAction) except +IndexError
double GetPayoff(int) except +
double GetBeliefProb(c_GameNode) except +
double GetRealizProb(c_GameNode) except +
Expand Down Expand Up @@ -318,7 +318,7 @@ cdef extern from "games/behavmixed.h":
void Randomize() except +
void Randomize(int) except +
c_Rational getitem "operator[]"(int) except +IndexError
c_Rational getaction "operator()"(c_GameAction) except +IndexError
c_Rational getaction "operator[]"(c_GameAction) except +IndexError
c_Rational GetPayoff(int) except +
c_Rational GetBeliefProb(c_GameNode) except +
c_Rational GetRealizProb(c_GameNode) except +
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/enumpoly/efgpoly.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ MixedBehaviorProfile<double> ToFullSupport(const MixedBehaviorProfile<double> &p
GameInfoset infoset = player->GetInfoset(iset);
for (int act = 1; act <= infoset->NumActions(); act++) {
if (support.Contains(infoset->GetAction(act))) {
fullProfile(pl, iset, act) = p_profile[index++];
fullProfile[infoset->GetAction(act)] = p_profile[index++];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/enumpoly/sfg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Gambit::MixedBehaviorProfile<double> Sfg::ToBehav(const Gambit::PVector<double>
value = Gambit::Rational(0);
}

b(i, sij->GetInfoset()->GetNumber(), efsupp.GetIndex(sij->GetAction())) = value;
b[sij->GetAction()] = value;
}
}
return b;
Expand Down
8 changes: 4 additions & 4 deletions src/solvers/lcp/efglcp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ void NashLcpBehaviorSolver<T>::GetProfile(const linalg::LemkeTableau<T> &tab,
int snew = p_solution.infosetOffset.at(n->GetInfoset());
for (const auto &action : n->GetInfoset()->GetActions()) {
snew++;
v(action) = static_cast<T>(0);
v[action] = static_cast<T>(0);
if (tab.Member(s1)) {
int ind = tab.Find(s1);
if (sol[ind] > p_solution.eps && tab.Member(snew)) {
int ind2 = tab.Find(snew);
if (sol[ind2] > p_solution.eps) {
v(action) = sol[ind2] / sol[ind];
v[action] = sol[ind2] / sol[ind];
}
}
}
Expand All @@ -302,13 +302,13 @@ void NashLcpBehaviorSolver<T>::GetProfile(const linalg::LemkeTableau<T> &tab,
int snew = p_solution.infosetOffset.at(n->GetInfoset());
for (const auto &action : n->GetInfoset()->GetActions()) {
snew++;
v(action) = static_cast<T>(0);
v[action] = static_cast<T>(0);
if (tab.Member(ns1 + s2)) {
int ind = tab.Find(ns1 + s2);
if (sol[ind] > p_solution.eps && tab.Member(ns1 + snew)) {
int ind2 = tab.Find(ns1 + snew);
if (sol[ind2] > p_solution.eps) {
v(action) = sol[ind2] / sol[ind];
v[action] = sol[ind2] / sol[ind];
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/solvers/lp/efglp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ void NashLpBehavSolver<T>::GameData::GetBehavior(MixedBehaviorProfile<T> &v,
int snew = infosetOffset.at(n->GetInfoset());
for (const auto &action : n->GetInfoset()->GetActions()) {
snew++;
v(action) = (p_primal[s1] > (T)0) ? p_primal[snew] / p_primal[s1] : (T)0;
v[action] = (p_primal[s1] > (T)0) ? p_primal[snew] / p_primal[s1] : (T)0;
GetBehavior(v, p_primal, p_dual, n->GetChild(action), snew, s2);
}
}
else {
int snew = infosetOffset.at(n->GetInfoset());
for (const auto &action : n->GetInfoset()->GetActions()) {
snew++;
v(action) = (p_dual[s2] > (T)0) ? p_dual[snew] / p_dual[s2] : (T)0;
v[action] = (p_dual[s2] > (T)0) ? p_dual[snew] / p_dual[s2] : (T)0;
GetBehavior(v, p_primal, p_dual, n->GetChild(action), s1, snew);
}
}
Expand Down

0 comments on commit 8518f48

Please sign in to comment.