Skip to content

Commit 8f0cbcd

Browse files
committed
Promote GameSequence to be a GameObject
1 parent 821ce92 commit 8f0cbcd

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

src/games/behavspt.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ int BehaviorSupportProfile::GetConstraintEntry(const GameInfoset &p_infoset,
288288
return GetSequenceForm()->GetConstraintEntry(p_infoset, p_action);
289289
}
290290

291-
const Rational &BehaviorSupportProfile::GetPayoff(
292-
const std::map<GamePlayer, std::shared_ptr<GameSequenceRep>> &p_profile,
293-
const GamePlayer &p_player) const
291+
const Rational &
292+
BehaviorSupportProfile::GetPayoff(const std::map<GamePlayer, GameSequence> &p_profile,
293+
const GamePlayer &p_player) const
294294
{
295295
return GetSequenceForm()->GetPayoff(p_profile, p_player);
296296
}

src/games/behavspt.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030
namespace Gambit {
3131

3232
class GameSequenceForm;
33-
class GameSequenceRep;
3433
class SequencesWrapper;
3534
class PlayerSequencesWrapper;
3635
class InfosetsWrapper;
3736
class ContingenciesWrapper;
38-
using GameSequence = std::shared_ptr<GameSequenceRep>;
3937

4038
/// This class represents a subset of the actions in an extensive game.
4139
/// It is enforced that each player has at least one action at each
@@ -48,6 +46,7 @@ class BehaviorSupportProfile {
4846
Game m_efg;
4947
std::map<GameInfoset, std::vector<GameAction>> m_actions;
5048
mutable std::shared_ptr<GameSequenceForm> m_sequenceForm;
49+
mutable std::shared_ptr<std::map<GameInfoset, bool>> m_reachableInfosets;
5150

5251
std::map<GameInfoset, bool> m_infosetReachable;
5352
std::map<GameNode, bool> m_nonterminalReachable;
@@ -248,16 +247,14 @@ class BehaviorSupportProfile {
248247
Sequences GetSequences() const;
249248
PlayerSequences GetSequences(GamePlayer &p_player) const;
250249
int GetConstraintEntry(const GameInfoset &p_infoset, const GameAction &p_action) const;
251-
const Rational &
252-
GetPayoff(const std::map<GamePlayer, std::shared_ptr<GameSequenceRep>> &p_profile,
253-
const GamePlayer &p_player) const;
250+
const Rational &GetPayoff(const std::map<GamePlayer, GameSequence> &p_profile,
251+
const GamePlayer &p_player) const;
254252
GameRep::Players GetPlayers() const { return GetGame()->GetPlayers(); }
255253
MixedBehaviorProfile<double>
256-
ToMixedBehaviorProfile(const std::map<std::shared_ptr<GameSequenceRep>, double> &) const;
254+
ToMixedBehaviorProfile(const std::map<GameSequence, double> &) const;
257255
Infosets GetInfosets() const { return {this}; };
258256
SequenceContingencies GetSequenceContingencies() const;
259257

260-
mutable std::shared_ptr<std::map<GameInfoset, bool>> m_reachableInfosets;
261258
void FindReachableInfosets(GameNode p_node) const;
262259
std::shared_ptr<std::map<GameInfoset, bool>> GetReachableInfosets() const;
263260
};

src/games/game.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ using GameInfoset = GameObjectPtr<GameInfosetRep>;
4848
class GameStrategyRep;
4949
using GameStrategy = GameObjectPtr<GameStrategyRep>;
5050

51+
class GameSequenceRep;
52+
using GameSequence = GameObjectPtr<GameSequenceRep>;
53+
5154
class GamePlayerRep;
5255
using GamePlayer = GameObjectPtr<GamePlayerRep>;
5356

@@ -316,6 +319,36 @@ class GameStrategyRep : public std::enable_shared_from_this<GameStrategyRep> {
316319
//@}
317320
};
318321

322+
class GameSequenceRep : public std::enable_shared_from_this<GameSequenceRep> {
323+
public:
324+
bool m_valid{true};
325+
GamePlayer player;
326+
GameAction action;
327+
size_t number;
328+
std::weak_ptr<GameSequenceRep> parent;
329+
330+
explicit GameSequenceRep(const GamePlayer &p_player, const GameAction &p_action, size_t p_number,
331+
std::weak_ptr<GameSequenceRep> p_parent)
332+
: player(p_player), action(p_action), number(p_number), parent(p_parent)
333+
{
334+
}
335+
336+
bool IsValid() const { return m_valid; }
337+
void Invalidate() { m_valid = false; }
338+
339+
Game GetGame() const;
340+
GameInfoset GetInfoset() const { return (action) ? action->GetInfoset() : nullptr; }
341+
342+
bool operator<(const GameSequenceRep &other) const
343+
{
344+
return player < other.player || (player == other.player && action < other.action);
345+
}
346+
bool operator==(const GameSequenceRep &other) const
347+
{
348+
return player == other.player && action == other.action;
349+
}
350+
};
351+
319352
/// A player in a game
320353
class GamePlayerRep : public std::enable_shared_from_this<GamePlayerRep> {
321354
friend class GameRep;
@@ -945,6 +978,8 @@ inline void GameOutcomeRep::SetPayoff(const GamePlayer &p_player, const Number &
945978
inline GamePlayer GameStrategyRep::GetPlayer() const { return m_player->shared_from_this(); }
946979
inline Game GameStrategyRep::GetGame() const { return m_player->GetGame(); }
947980

981+
inline Game GameSequenceRep::GetGame() const { return player->GetGame(); }
982+
948983
inline Game GameActionRep::GetGame() const { return m_infoset->GetGame(); }
949984

950985
inline Game GameInfosetRep::GetGame() const { return m_game->shared_from_this(); }

src/games/gameseq.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ void GameSequenceForm::BuildSequences(const GameNode &n,
4444
for (auto action : m_support.GetActions(n->GetInfoset())) {
4545
if (m_correspondence.find(action) == m_correspondence.end()) {
4646
m_sequences[n->GetPlayer()].push_back(std::make_shared<GameSequenceRep>(
47-
n->GetPlayer(), action, m_sequences[n->GetPlayer()].size() + 1, tmp_sequence));
47+
n->GetPlayer(), action, m_sequences[n->GetPlayer()].size() + 1,
48+
tmp_sequence.get_shared()));
4849
m_correspondence[action] = m_sequences[n->GetPlayer()].back();
4950
}
5051
p_currentSequences[n->GetPlayer()] = m_correspondence[action];

src/games/gameseq.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,6 @@
2828

2929
namespace Gambit {
3030

31-
class GameSequenceRep {
32-
public:
33-
GamePlayer player;
34-
GameAction action;
35-
size_t number;
36-
std::weak_ptr<GameSequenceRep> parent;
37-
38-
explicit GameSequenceRep(const GamePlayer &p_player, const GameAction &p_action, size_t p_number,
39-
std::weak_ptr<GameSequenceRep> p_parent)
40-
: player(p_player), action(p_action), number(p_number), parent(p_parent)
41-
{
42-
}
43-
44-
GameInfoset GetInfoset() const { return (action) ? action->GetInfoset() : nullptr; }
45-
46-
bool operator<(const GameSequenceRep &other) const
47-
{
48-
return player < other.player || (player == other.player && action < other.action);
49-
}
50-
bool operator==(const GameSequenceRep &other) const
51-
{
52-
return player == other.player && action == other.action;
53-
}
54-
};
55-
56-
using GameSequence = std::shared_ptr<GameSequenceRep>;
57-
5831
class GameSequenceForm {
5932
friend class BehaviorSupportProfile;
6033

0 commit comments

Comments
 (0)