Permalink
Browse files

Convert GameActionRep to abstract base class.

  • Loading branch information...
1 parent 2eaf1d5 commit a3f9ac601825fdadef784f39da64d1347d85fa60 @tturocy tturocy committed Feb 15, 2012
Showing with 63 additions and 44 deletions.
  1. +2 −2 src/libgambit/behav.h
  2. +9 −8 src/libgambit/behav.imp
  3. +9 −8 src/libgambit/game.cc
  4. +9 −19 src/libgambit/game.h
  5. +32 −5 src/libgambit/gametree.h
  6. +2 −2 src/tools/enumpure/enumpure.cc
@@ -51,11 +51,11 @@ template <class T> class MixedBehavProfile : public DVector<T> {
const T &ActionValue(const GameAction &act) const
{ return m_actionValues(act->GetInfoset()->GetPlayer()->GetNumber(),
act->GetInfoset()->GetNumber(),
- act->m_number); }
+ act->GetNumber()); }
T &ActionValue(const GameAction &act)
{ return m_actionValues(act->GetInfoset()->GetPlayer()->GetNumber(),
act->GetInfoset()->GetNumber(),
- act->m_number); }
+ act->GetNumber()); }
/// @name Auxiliary functions for computation of interesting values
//@{
@@ -249,7 +249,7 @@ T MixedBehavProfile<T>::GetLiapValue(bool p_definedOnly) const
x = GetActionProb(action);
avg += x * m_actionValues(action->GetInfoset()->GetPlayer()->GetNumber(),
action->GetInfoset()->GetNumber(),
- action->m_number);
+ action->GetNumber());
sum += x;
if (x > (T)0) x = (T)0;
result += BIG1 * x * x; // add penalty for neg probabilities
@@ -331,15 +331,15 @@ const T &MixedBehavProfile<T>::GetActionValue(const GameAction &act) const
ComputeSolutionData();
return m_actionValues(act->GetInfoset()->GetPlayer()->GetNumber(),
act->GetInfoset()->GetNumber(),
- act->m_number);
+ act->GetNumber());
}
template <class T>
const T &MixedBehavProfile<T>::GetRegret(const GameAction &act) const
{
ComputeSolutionData();
return m_gripe(act->GetInfoset()->GetPlayer()->GetNumber(),
- act->GetInfoset()->GetNumber(), act->m_number);
+ act->GetInfoset()->GetNumber(), act->GetNumber());
}
template <class T>
@@ -397,16 +397,16 @@ T MixedBehavProfile<T>::DiffActionValue(const GameAction &p_action,
for (int i = 1; i <= infoset->NumMembers(); i++) {
GameNode member = infoset->GetMember(i);
- GameNode child = member->GetChild(p_action->m_number);
+ GameNode child = member->GetChild(p_action->GetNumber());
deriv += DiffRealizProb(member, p_oppAction) *
(m_nodeValues(child->GetNumber(), player->GetNumber()) -
m_actionValues(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(),
- p_action->m_number));
+ p_action->GetNumber()));
deriv += m_realizProbs[member->GetNumber()] *
- DiffNodeValue(member->GetChild(p_action->m_number), player, p_oppAction);
+ DiffNodeValue(member->GetChild(p_action->GetNumber()), player, p_oppAction);
}
return deriv / GetInfosetProb(p_action->GetInfoset());
@@ -517,7 +517,7 @@ void MixedBehavProfile<T>::ComputeSolutionDataPass2(const GameNode &node) const
if (!iset->IsChanceInfoset()) {
T &cpay = m_actionValues(act->GetInfoset()->GetPlayer()->GetNumber(),
act->GetInfoset()->GetNumber(),
- act->m_number);
+ act->GetNumber());
if (infosetProb != infosetProb * (T) 0) {
cpay += m_beliefs[node->GetNumber()] * m_nodeValues(childNode->GetNumber(), iset->GetPlayer()->GetNumber());
}
@@ -576,7 +576,8 @@ void MixedBehavProfile<T>::ComputeSolutionData(void) const
for (int act = 1; act <= infoset->NumActions(); act++) {
GameAction action = infoset->GetAction(act);
m_gripe(action->GetInfoset()->GetPlayer()->GetNumber(),
- action->GetInfoset()->GetNumber(), action->m_number) =
+ action->GetInfoset()->GetNumber(),
+ action->GetNumber()) =
(ActionValue(action) - GetInfosetValue(infoset)) * GetInfosetProb(infoset);
}
}
View
@@ -39,15 +39,15 @@ GameOutcomeRep::GameOutcomeRep(GameRep *p_game, int p_number)
{ }
//========================================================================
-// class GameActionRep
+// class GameTreeActionRep
//========================================================================
-bool GameActionRep::Precedes(const GameNode &n) const
+bool GameTreeActionRep::Precedes(const GameNode &n) const
{
GameNode node = n;
while (node != node->GetGame()->GetRoot()) {
- if (node->GetPriorAction() == this) {
+ if (node->GetPriorAction() == GameAction(const_cast<GameTreeActionRep *>(this))) {
return true;
}
else {
@@ -57,7 +57,7 @@ bool GameActionRep::Precedes(const GameNode &n) const
return false;
}
-void GameActionRep::DeleteAction(void)
+void GameTreeActionRep::DeleteAction(void)
{
if (m_infoset->NumActions() == 1) throw UndefinedException();
@@ -75,19 +75,20 @@ void GameActionRep::DeleteAction(void)
m_infoset->m_efg->ClearComputedValues();
}
-GameInfoset GameActionRep::GetInfoset(void) const { return m_infoset; }
+GameInfoset GameTreeActionRep::GetInfoset(void) const { return m_infoset; }
//========================================================================
// class GameTreeInfosetRep
//========================================================================
GameTreeInfosetRep::GameTreeInfosetRep(GameTreeRep *p_efg, int p_number,
- GamePlayerRep *p_player, int p_actions)
+ GamePlayerRep *p_player,
+ int p_actions)
: m_efg(p_efg), m_number(p_number), m_player(p_player),
m_actions(p_actions), flag(0)
{
while (p_actions) {
- m_actions[p_actions] = new GameActionRep(p_actions, "", this);
+ m_actions[p_actions] = new GameTreeActionRep(p_actions, "", this);
p_actions--;
}
@@ -141,7 +142,7 @@ GameAction GameTreeInfosetRep::InsertAction(GameAction p_action /* =0 */)
for (where = 1; m_actions[where] != p_action; where++);
}
- GameActionRep *action = new GameActionRep(where, "", this);
+ GameTreeActionRep *action = new GameTreeActionRep(where, "", this);
m_actions.Insert(action, where);
if (m_player->IsChance()) {
m_probs.Insert(Number("0"), where);
View
@@ -273,29 +273,19 @@ typedef GameObjectPtr<GameOutcomeRep> GameOutcome;
/// An action at an information set in an extensive game
class GameActionRep : public GameObject {
- friend class GameTreeInfosetRep;
- template <class T> friend class MixedBehavProfile;
-
-private:
- int m_number;
- std::string m_label;
- GameTreeInfosetRep *m_infoset;
-
- GameActionRep(int p_number, const std::string &p_label,
- GameTreeInfosetRep *p_infoset)
- : m_number(p_number), m_label(p_label), m_infoset(p_infoset) { }
- ~GameActionRep() { }
+protected:
+ virtual ~GameActionRep() { }
public:
- int GetNumber(void) const { return m_number; }
- GameInfoset GetInfoset(void) const;
+ virtual int GetNumber(void) const = 0;
+ virtual GameInfoset GetInfoset(void) const = 0;
- const std::string &GetLabel(void) const { return m_label; }
- void SetLabel(const std::string &p_label) { m_label = p_label; }
+ virtual const std::string &GetLabel(void) const = 0;
+ virtual void SetLabel(const std::string &p_label) = 0;
- bool Precedes(const GameNode &) const;
+ virtual bool Precedes(const GameNode &) const = 0;
- void DeleteAction(void);
+ virtual void DeleteAction(void) = 0;
};
/// An information set in an extensive game
@@ -324,7 +314,7 @@ class GameInfosetRep : public GameObject {
/// Returns the p_index'th action at the information set
virtual GameAction GetAction(int p_index) const = 0;
/// Returns a forward iterator over the available actions
- virtual GameActionIterator Actions(void) const = 0;
+ //virtual GameActionIterator Actions(void) const = 0;
//@}
virtual int NumMembers(void) const = 0;
@@ -29,9 +29,36 @@ namespace Gambit {
class GameTreeRep;
+class GameTreeActionRep : public GameActionRep {
+ friend class GameTreeRep;
+ friend class GameTreeInfosetRep;
+ template <class T> friend class MixedBehavProfile;
+
+private:
+ int m_number;
+ std::string m_label;
+ GameTreeInfosetRep *m_infoset;
+
+ GameTreeActionRep(int p_number, const std::string &p_label,
+ GameTreeInfosetRep *p_infoset)
+ : m_number(p_number), m_label(p_label), m_infoset(p_infoset) { }
+ ~GameTreeActionRep() { }
+
+public:
+ int GetNumber(void) const { return m_number; }
+ GameInfoset GetInfoset(void) const;
+
+ const std::string &GetLabel(void) const { return m_label; }
+ void SetLabel(const std::string &p_label) { m_label = p_label; }
+
+ bool Precedes(const GameNode &) const;
+
+ void DeleteAction(void);
+};
+
class GameTreeInfosetRep : public GameInfosetRep {
friend class GameTreeRep;
- friend class GameActionRep;
+ friend class GameTreeActionRep;
friend class GamePlayerRep;
friend class GameTreeNodeRep;
template <class T> friend class MixedBehavProfile;
@@ -41,7 +68,7 @@ class GameTreeInfosetRep : public GameInfosetRep {
int m_number;
std::string m_label;
GamePlayerRep *m_player;
- Array<GameActionRep *> m_actions;
+ Array<GameTreeActionRep *> m_actions;
Array<GameTreeNodeRep *> m_members;
int flag, whichbranch;
Array<Number> m_probs;
@@ -78,8 +105,8 @@ class GameTreeInfosetRep : public GameInfosetRep {
/// Returns the p_index'th action at the information set
virtual GameAction GetAction(int p_index) const { return m_actions[p_index]; }
/// Returns a forward iterator over the available actions
- virtual GameActionIterator Actions(void) const
- { return GameActionIterator(m_actions); }
+ //virtual GameActionIterator Actions(void) const
+ // { return GameActionIterator(m_actions); }
//@}
virtual int NumMembers(void) const { return m_members.Length(); }
@@ -101,7 +128,7 @@ class GameTreeInfosetRep : public GameInfosetRep {
class GameTreeNodeRep : public GameNodeRep {
friend class GameTreeRep;
- friend class GameActionRep;
+ friend class GameTreeActionRep;
friend class GameTreeInfosetRep;
friend class GamePlayerRep;
friend class PureBehavProfile;
@@ -59,8 +59,8 @@ List<MixedBehavProfile<Rational> > SolveBehav(const BehavSupport &p_support,
for (int iset = 1; isNash && iset <= player->NumInfosets(); iset++) {
GameInfoset infoset = player->GetInfoset(iset);
- for (GameActionIterator action = infoset->Actions();
- !action.AtEnd(); action++) {
+ for (int act = 1; act <= infoset->NumActions(); act++) {
+ GameAction action = infoset->GetAction(act);
if (citer->GetActionValue<Rational>(action) > current) {
isNash = false;
break;

0 comments on commit a3f9ac6

Please sign in to comment.