Skip to content

Commit

Permalink
Draft implementation of SetChanceProbs that requires chance moves to …
Browse files Browse the repository at this point in the history
…have sum-to-one probs.
  • Loading branch information
tturocy committed Sep 7, 2023
1 parent 8e6bca0 commit 18d8df6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,12 @@ class GameRep : public BaseGameRep {
/// Returns the number of nodes in the game
virtual int NumNodes() const = 0;
//@}

/// @name Modification
//@{
/// Set the probability distribution of actions at a chance node
virtual Game SetChanceProbs(const GameInfoset &, const Array<Number> &) = 0;
//@}
};

typedef GameObjectPtr<GameRep> Game;
Expand Down
5 changes: 5 additions & 0 deletions src/games/gameagg.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class GameAggRep : public GameRep {

//@}

/// @name Modification
//@{
Game SetChanceProbs(const GameInfoset &, const Array<Number> &) override { throw UndefinedException(); }
//@}

/// @name Writing data files
//@{
/// Write the game to a savefile in the specified format.
Expand Down
5 changes: 5 additions & 0 deletions src/games/gamebagg.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ class GameBagentRep : public GameRep {
{ throw UndefinedException(); }
virtual void WriteBaggFile(std::ostream &) const;
//@}

/// @name Modification
//@{
Game SetChanceProbs(const GameInfoset &, const Array<Number> &) override { throw UndefinedException(); }
//@}
};

} // end namespace Gambit
Expand Down
4 changes: 4 additions & 0 deletions src/games/gametable.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class GameTableRep : public GameExplicitRep {
//@{
/// Write the game to a file in .nfg outcome format
void WriteNfgFile(std::ostream &) const override;

/// @name Modification
//@{
Game SetChanceProbs(const GameInfoset &, const Array<Number> &) override { throw UndefinedException(); }
//@}

PureStrategyProfile NewPureStrategyProfile() const override;
Expand Down
29 changes: 29 additions & 0 deletions src/games/gametree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//

#include <iostream>
#include <numeric>

#include "gambit.h"
#include "gametree.h"
Expand Down Expand Up @@ -1026,6 +1027,34 @@ int GameTreeRep::NumNodes() const
return CountNodes(m_root);
}


//------------------------------------------------------------------------
// GameTreeRep: Modification
//------------------------------------------------------------------------

Game GameTreeRep::SetChanceProbs(const GameInfoset &p_infoset, const Array<Number> &p_probs)
{
if (!p_infoset->IsChanceInfoset()) {
throw UndefinedException();
}
if (p_infoset->NumActions() != p_probs.size()) {
throw DimensionException();
}
Rational sum(0);
for (auto prob : p_probs) {
sum += static_cast<Rational>(prob);
}
if (sum != Rational(1)) {
throw ValueException();
}
for (int act = 1; act <= p_infoset->NumActions(); act++) {
dynamic_cast<GameTreeInfosetRep &>(*p_infoset).m_probs[act] = p_probs[act];
}
ClearComputedValues();
return this;
}


//------------------------------------------------------------------------
// GameTreeRep: Factory functions
//------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions src/games/gametree.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ class GameTreeRep : public GameExplicitRep {
GameAction GetAction(int act) const override;
//@}

/// @name Modification
//@{
Game SetChanceProbs(const GameInfoset &, const Array<Number> &) override;
//@}

PureStrategyProfile NewPureStrategyProfile() const override;
MixedStrategyProfile<double> NewMixedStrategyProfile(double) const override;
MixedStrategyProfile<Rational> NewMixedStrategyProfile(const Rational &) const override;
Expand Down

0 comments on commit 18d8df6

Please sign in to comment.