From a6fcf5a7d6fdf24abf8b0013d1691a57a60459ac Mon Sep 17 00:00:00 2001 From: Ted Turocy Date: Thu, 21 Nov 2024 13:17:19 +0000 Subject: [PATCH] Remove some unneeded or unused classes and functions. This removes the DVector container class, which is no longer needed now that we have re-implemented MixedBehaviorProfile. A follow-on from this is that we can remove the NumActions() and NumMembers() functions on games and behavior supports, as those were only providing the shapes to create the underlying DVectors. --- Makefile.am | 3 - src/core/dvector.cc | 27 -------- src/core/dvector.h | 69 -------------------- src/core/dvector.imp | 115 --------------------------------- src/games/behavmixed.cc | 21 ++---- src/games/behavmixed.h | 15 +++-- src/games/behavspt.cc | 13 ---- src/games/behavspt.h | 2 - src/games/game.h | 6 +- src/games/gameagg.h | 4 -- src/games/gamebagg.h | 4 -- src/games/gametable.h | 4 -- src/games/gametree.cc | 36 ----------- src/games/gametree.h | 4 -- src/solvers/liap/efgliap.cc | 9 ++- src/solvers/logit/logbehav.imp | 10 +-- 16 files changed, 28 insertions(+), 314 deletions(-) delete mode 100644 src/core/dvector.cc delete mode 100644 src/core/dvector.h delete mode 100644 src/core/dvector.imp diff --git a/Makefile.am b/Makefile.am index 00e65a661..bc8c65ff2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -251,8 +251,6 @@ core_SOURCES = \ src/core/vector.imp \ src/core/pvector.h \ src/core/pvector.imp \ - src/core/dvector.h \ - src/core/dvector.imp \ src/core/recarray.h \ src/core/matrix.h \ src/core/matrix.imp \ @@ -264,7 +262,6 @@ core_SOURCES = \ src/core/rational.h \ src/core/vector.cc \ src/core/pvector.cc \ - src/core/dvector.cc \ src/core/matrix.cc \ src/core/sqmatrix.cc \ src/core/function.cc \ diff --git a/src/core/dvector.cc b/src/core/dvector.cc deleted file mode 100644 index 969622ba8..000000000 --- a/src/core/dvector.cc +++ /dev/null @@ -1,27 +0,0 @@ -// -// This file is part of Gambit -// Copyright (c) 1994-2024, The Gambit Project (http://www.gambit-project.org) -// -// FILE: src/libgambit/dvector.cc -// Instantiation of doubly partitioned vector types -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// - -#include "gambit.h" -#include "dvector.imp" - -template class Gambit::DVector; -template class Gambit::DVector; diff --git a/src/core/dvector.h b/src/core/dvector.h deleted file mode 100644 index 449e4f8eb..000000000 --- a/src/core/dvector.h +++ /dev/null @@ -1,69 +0,0 @@ -// -// This file is part of Gambit -// Copyright (c) 1994-2024, The Gambit Project (http://www.gambit-project.org) -// -// FILE: src/libgambit/dvector.h -// Doubly-partitioned vector class -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// - -#ifndef LIBGAMBIT_DVECTOR_H -#define LIBGAMBIT_DVECTOR_H - -#include "pvector.h" - -namespace Gambit { - -template class DVector : public PVector { -private: - void setindex(); - -protected: - T ***dvptr; - Array dvlen, dvidx; - -public: - explicit DVector(const PVector &shape); - DVector(const DVector &v); - ~DVector() override; - - T &operator()(int a, int b, int c); - const T &operator()(int a, int b, int c) const; - - DVector &operator=(const DVector &v) - { - if (this == &v) { - return *this; - } - if (dvlen != v.dvlen || dvidx != v.dvidx) { - throw DimensionException(); - } - static_cast &>(*this) = static_cast &>(v); - return *this; - } - - DVector &operator=(const Vector &v) - { - static_cast &>(*this) = v; - return *this; - } - - DVector &operator=(const T &c); -}; - -} // end namespace Gambit - -#endif // LIBGAMBIT_DVECTOR_H diff --git a/src/core/dvector.imp b/src/core/dvector.imp deleted file mode 100644 index 5a62afb39..000000000 --- a/src/core/dvector.imp +++ /dev/null @@ -1,115 +0,0 @@ -// -// This file is part of Gambit -// Copyright (c) 1994-2024, The Gambit Project (http://www.gambit-project.org) -// -// FILE: src/libgambit/dvector.imp -// Implementation of doubly-partitioned vector class -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// - -#include "dvector.h" - -namespace Gambit { - -//-------------------------------------------------------------------------- -// DVector: Private and protected member functions -//-------------------------------------------------------------------------- - -template void DVector::setindex() -{ - int index = 1; - - for (int i = 1; i <= dvlen.Length(); i++) { - dvptr[i] = this->svptr + index - 1; - dvidx[i] = index; - index += dvlen[i]; - } -} - -//-------------------------------------------------------------------------- -// DVector: Constructors, destructor, and constructive operators -//-------------------------------------------------------------------------- - -template -DVector::DVector(const PVector &shape) - : PVector(static_cast &>(shape)), dvlen(shape.Lengths().Length()), - dvidx(shape.Lengths().Length()) -{ - dvptr = new T **[dvlen.Length()]; - dvptr -= 1; - - for (int i = 1; i <= dvlen.Length(); i++) { - dvlen[i] = shape.Lengths()[i]; - } - - setindex(); -} - -template -DVector::DVector(const DVector &v) : PVector(v), dvlen(v.dvlen), dvidx(v.dvidx) -{ - dvptr = new T **[dvlen.Length()]; - dvptr -= 1; - - setindex(); -} - -template DVector::~DVector() -{ - if (dvptr) { - delete[] (dvptr + 1); - } -} - -template DVector &DVector::operator=(const T &c) -{ - PVector::operator=(c); - return *this; -} - -//-------------------------------------------------------------------------- -// DVector: Operator definitions -//-------------------------------------------------------------------------- - -template T &DVector::operator()(int a, int b, int c) -{ - if (dvlen.First() > a || a > dvlen.Last()) { - throw IndexException(); - } - if (1 > b || b > dvlen[a]) { - throw IndexException(); - } - if (1 > c || c > this->svlen[dvidx[a] + b - 1]) { - throw IndexException(); - } - return dvptr[a][b][c]; -} - -template const T &DVector::operator()(int a, int b, int c) const -{ - if (dvlen.First() > a || a > dvlen.Last()) { - throw IndexException(); - } - if (1 > b || b > dvlen[a]) { - throw IndexException(); - } - if (1 > c || c > this->svlen[dvidx[a] + b - 1]) { - throw IndexException(); - } - return dvptr[a][b][c]; -} - -} // end namespace Gambit diff --git a/src/games/behavmixed.cc b/src/games/behavmixed.cc index f7f7f3e12..940b5a06a 100644 --- a/src/games/behavmixed.cc +++ b/src/games/behavmixed.cc @@ -35,7 +35,7 @@ namespace Gambit { template MixedBehaviorProfile::MixedBehaviorProfile(const Game &p_game) - : m_probs(p_game->NumActions()), m_support(BehaviorSupportProfile(p_game)), + : m_probs(p_game->BehavProfileLength()), m_support(BehaviorSupportProfile(p_game)), m_gameversion(p_game->GetVersion()) { int index = 1; @@ -51,7 +51,7 @@ MixedBehaviorProfile::MixedBehaviorProfile(const Game &p_game) template MixedBehaviorProfile::MixedBehaviorProfile(const BehaviorSupportProfile &p_support) - : m_probs(p_support.NumActions()), m_support(p_support), + : m_probs(p_support.BehaviorProfileLength()), m_support(p_support), m_gameversion(p_support.GetGame()->GetVersion()) { int index = 1; @@ -127,20 +127,19 @@ void MixedBehaviorProfile::RealizationProbs(const MixedStrategyProfile &mp template MixedBehaviorProfile::MixedBehaviorProfile(const MixedStrategyProfile &p_profile) - : m_probs(p_profile.GetGame()->NumActions()), m_support(p_profile.GetGame()), + : m_probs(p_profile.GetGame()->BehavProfileLength()), m_support(p_profile.GetGame()), m_gameversion(p_profile.GetGame()->GetVersion()) { int index = 1; for (const auto &player : p_profile.GetGame()->GetPlayers()) { for (const auto &infoset : player->GetInfosets()) { for (const auto &action : infoset->GetActions()) { - m_profileIndex[action] = index++; + m_profileIndex[action] = index; + m_probs[index++] = static_cast(0); } } } - static_cast &>(m_probs) = T(0); - GameTreeNodeRep *root = dynamic_cast(m_support.GetGame()->GetRoot().operator->()); @@ -184,16 +183,6 @@ MixedBehaviorProfile::operator=(const MixedBehaviorProfile &p_profile) return *this; } -//======================================================================== -// MixedBehaviorProfile: Operator overloading -//======================================================================== - -template -bool MixedBehaviorProfile::operator==(const MixedBehaviorProfile &p_profile) const -{ - return (m_support == p_profile.m_support && (DVector &)*this == (DVector &)p_profile); -} - //======================================================================== // MixedBehaviorProfile: General data access //======================================================================== diff --git a/src/games/behavmixed.h b/src/games/behavmixed.h index c54aa2253..9ec3135a3 100644 --- a/src/games/behavmixed.h +++ b/src/games/behavmixed.h @@ -33,7 +33,7 @@ namespace Gambit { /// template class MixedBehaviorProfile { protected: - DVector m_probs; + Vector m_probs; BehaviorSupportProfile m_support; /// The index into the action profile for a action (-1 if not in support) std::map m_profileIndex; @@ -101,11 +101,14 @@ template class MixedBehaviorProfile { /// @name Operator overloading //@{ - bool operator==(const MixedBehaviorProfile &) const; - bool operator!=(const MixedBehaviorProfile &x) const { return !(*this == x); } - - bool operator==(const DVector &x) const { return m_probs == x; } - bool operator!=(const DVector &x) const { return m_probs != x; } + bool operator==(const MixedBehaviorProfile &p_profile) const + { + return (m_support == p_profile.m_support && m_probs == p_profile.m_probs); + } + bool operator!=(const MixedBehaviorProfile &p_profile) const + { + return (m_support != p_profile.m_support || m_probs != p_profile.m_probs); + } const T &operator[](const GameAction &p_action) const { diff --git a/src/games/behavspt.cc b/src/games/behavspt.cc index 0c4d238ce..582f31f5c 100644 --- a/src/games/behavspt.cc +++ b/src/games/behavspt.cc @@ -54,19 +54,6 @@ BehaviorSupportProfile::BehaviorSupportProfile(const Game &p_efg) : m_efg(p_efg) // BehaviorSupportProfile: General information //======================================================================== -PVector BehaviorSupportProfile::NumActions() const -{ - PVector answer(m_efg->NumInfosets()); - for (const auto &player : m_efg->GetPlayers()) { - for (const auto &infoset : player->GetInfosets()) { - answer(player->GetNumber(), infoset->GetNumber()) = - static_cast(m_actions.at(infoset).size()); - } - } - - return answer; -} - size_t BehaviorSupportProfile::BehaviorProfileLength() const { size_t answer = 0; diff --git a/src/games/behavspt.h b/src/games/behavspt.h index 3717283d5..4bad1748c 100644 --- a/src/games/behavspt.h +++ b/src/games/behavspt.h @@ -97,8 +97,6 @@ class BehaviorSupportProfile { /// Returns the game on which the support is defined. Game GetGame() const { return m_efg; } - /// Returns the number of actions in the support for all information sets - PVector NumActions() const; /// Returns the total number of actions in the support size_t BehaviorProfileLength() const; diff --git a/src/games/game.h b/src/games/game.h index 289ab78b1..a0cc39898 100644 --- a/src/games/game.h +++ b/src/games/game.h @@ -28,7 +28,7 @@ #include #include -#include "core/dvector.h" +#include "core/pvector.h" #include "number.h" #include "gameobject.h" @@ -485,10 +485,6 @@ class GameRep : public BaseGameRep { /// @name Dimensions of the game //@{ - /// The number of actions in each information set - virtual PVector NumActions() const = 0; - /// The number of members in each information set - virtual PVector NumMembers() const = 0; /// The number of strategies for each player virtual Array NumStrategies() const = 0; /// Gets the i'th strategy in the game, numbered globally diff --git a/src/games/gameagg.h b/src/games/gameagg.h index b1b10204d..3a770e6d1 100644 --- a/src/games/gameagg.h +++ b/src/games/gameagg.h @@ -54,10 +54,6 @@ class GameAGGRep : public GameRep { std::shared_ptr GetUnderlyingAGG() const { return aggPtr; } /// @name Dimensions of the game //@{ - /// The number of actions in each information set - PVector NumActions() const override { throw UndefinedException(); } - /// The number of members in each information set - PVector NumMembers() const override { throw UndefinedException(); } /// The number of strategies for each player Array NumStrategies() const override; /// Gets the i'th strategy in the game, numbered globally diff --git a/src/games/gamebagg.h b/src/games/gamebagg.h index aa6cbf475..ec59af05a 100644 --- a/src/games/gamebagg.h +++ b/src/games/gamebagg.h @@ -54,10 +54,6 @@ class GameBAGGRep : public GameRep { /// @name Dimensions of the game //@{ - /// The number of actions in each information set - PVector NumActions() const override { throw UndefinedException(); } - /// The number of members in each information set - PVector NumMembers() const override { throw UndefinedException(); } /// The number of strategies for each player Array NumStrategies() const override; /// Gets the i'th strategy in the game, numbered globally diff --git a/src/games/gametable.h b/src/games/gametable.h index 7bd8fc2cc..629f7da0a 100644 --- a/src/games/gametable.h +++ b/src/games/gametable.h @@ -62,10 +62,6 @@ class GameTableRep : public GameExplicitRep { /// @name Dimensions of the game //@{ - /// The number of actions in each information set - PVector NumActions() const override { throw UndefinedException(); } - /// The number of members in each information set - PVector NumMembers() const override { throw UndefinedException(); } /// Returns the total number of actions in the game int BehavProfileLength() const override { throw UndefinedException(); } //@} diff --git a/src/games/gametree.cc b/src/games/gametree.cc index bf738f993..e1e564f22 100644 --- a/src/games/gametree.cc +++ b/src/games/gametree.cc @@ -982,42 +982,6 @@ void GameTreeRep::WriteNfgFile(std::ostream &p_file) const // GameTreeRep: Dimensions of the game //------------------------------------------------------------------------ -PVector GameTreeRep::NumActions() const -{ - Array foo(m_players.Length()); - int i; - for (i = 1; i <= m_players.Length(); i++) { - foo[i] = m_players[i]->m_infosets.Length(); - } - - PVector bar(foo); - for (i = 1; i <= m_players.Length(); i++) { - for (int j = 1; j <= m_players[i]->m_infosets.Length(); j++) { - bar(i, j) = m_players[i]->m_infosets[j]->NumActions(); - } - } - - return bar; -} - -PVector GameTreeRep::NumMembers() const -{ - Array foo(m_players.Length()); - - for (int i = 1; i <= m_players.Length(); i++) { - foo[i] = m_players[i]->NumInfosets(); - } - - PVector bar(foo); - for (int i = 1; i <= m_players.Length(); i++) { - for (int j = 1; j <= m_players[i]->NumInfosets(); j++) { - bar(i, j) = m_players[i]->m_infosets[j]->NumMembers(); - } - } - - return bar; -} - int GameTreeRep::BehavProfileLength() const { int sum = 0; diff --git a/src/games/gametree.h b/src/games/gametree.h index 5d0e66f63..46a1df6d3 100644 --- a/src/games/gametree.h +++ b/src/games/gametree.h @@ -277,10 +277,6 @@ class GameTreeRep : public GameExplicitRep { /// @name Dimensions of the game //@{ - /// The number of actions in each information set - PVector NumActions() const override; - /// The number of members in each information set - PVector NumMembers() const override; /// Returns the total number of actions in the game int BehavProfileLength() const override; //@} diff --git a/src/solvers/liap/efgliap.cc b/src/solvers/liap/efgliap.cc index a2d22d9bc..827910cad 100644 --- a/src/solvers/liap/efgliap.cc +++ b/src/solvers/liap/efgliap.cc @@ -45,6 +45,12 @@ class AgentLyapunovFunction : public FunctionOnSimplices { else { m_scale = 1.0 / m_scale; } + + for (const auto &player : m_game->GetPlayers()) { + for (const auto &infoset : player->GetInfosets()) { + m_shape.push_back(infoset->NumActions()); + } + } } ~AgentLyapunovFunction() override = default; @@ -54,6 +60,7 @@ class AgentLyapunovFunction : public FunctionOnSimplices { private: Game m_game; mutable MixedBehaviorProfile m_profile; + Array m_shape; double m_scale, m_penalty{100.0}; double Value(const Vector &x) const override; @@ -116,7 +123,7 @@ bool AgentLyapunovFunction::Gradient(const Vector &x, Vector &gr m_profile[i] += DELTA; grad[i] = value / (2.0 * DELTA); } - Project(grad, static_cast &>(m_game->NumActions())); + Project(grad, m_shape); return true; } diff --git a/src/solvers/logit/logbehav.imp b/src/solvers/logit/logbehav.imp index a10bf0e13..6683887ce 100644 --- a/src/solvers/logit/logbehav.imp +++ b/src/solvers/logit/logbehav.imp @@ -30,19 +30,19 @@ template LogBehavProfile::LogBehavProfile(const Game &p_game) - : m_game(p_game), m_probs(m_game->BehavProfileLength()), m_logProbs(m_game->BehavProfileLength()), - m_cacheValid(false) + : m_game(p_game), m_probs(m_game->BehavProfileLength()), + m_logProbs(m_game->BehavProfileLength()), m_cacheValid(false) { int index = 1; for (const auto &player : p_game->GetPlayers()) { - for (const auto &infoset: player->GetInfosets()) { - for (const auto &action: infoset->GetActions()) { + for (const auto &infoset : player->GetInfosets()) { + for (const auto &action : infoset->GetActions()) { m_profileIndex[action] = index++; } } } - for (auto infoset : m_game->GetInfosets()) { + for (auto infoset : m_game->GetInfosets()) { if (infoset->NumActions() > 0) { T center = (T(1) / T(infoset->NumActions())); for (auto act : infoset->GetActions()) {