Skip to content

Commit

Permalink
Change parameter for chance probability setting from std::string to N…
Browse files Browse the repository at this point in the history
…umber.
  • Loading branch information
tturocy committed Apr 14, 2023
1 parent 7cb331a commit 35b2c15
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/games/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ void ParseChanceNode(GameParserState &p_state,
infoset->SetLabel(label);
for (int act = 1; act <= actions.Length(); act++) {
infoset->GetAction(act)->SetLabel(actions[act]);
infoset->SetActionProb(act, probs[act]);
infoset->SetActionProb(act, Number(probs[act]));
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class GameInfosetRep : public GameObject {

virtual bool Precedes(GameNode) const = 0;

virtual void SetActionProb(int i, const std::string &p_value) = 0;
virtual void SetActionProb(int i, const Number &p_value) = 0;
virtual double GetActionProb(int pl, double) const = 0;
virtual Rational GetActionProb(int pl, const Rational &) const = 0;
virtual std::string GetActionProb(int pl, const std::string &) const = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/games/gametree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void GameTreeInfosetRep::RemoveAction(int which)
}
}

void GameTreeInfosetRep::SetActionProb(int act, const std::string &p_value)
void GameTreeInfosetRep::SetActionProb(int act, const Number &p_value)
{
m_probs[act] = p_value;
m_efg->ClearComputedValues();
Expand Down
2 changes: 1 addition & 1 deletion src/games/gametree.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class GameTreeInfosetRep : public GameInfosetRep {

bool Precedes(GameNode) const override;

void SetActionProb(int i, const std::string &p_value) override;
void SetActionProb(int i, const Number &p_value) override;
double GetActionProb(int pl, double) const override
{ return (double) m_probs[pl]; }
Rational GetActionProb(int pl, const Rational &) const override
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gamedoc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ void gbtGameDocument::DoSetActionProb(GameInfoset p_infoset,
unsigned int p_action,
const wxString &p_prob)
{
p_infoset->SetActionProb(p_action, static_cast<const char *>(p_prob.mb_str()));
p_infoset->SetActionProb(p_action, Number(p_prob.ToStdString()));
UpdateViews(GBT_DOC_MODIFIED_PAYOFFS);
}

Expand Down
12 changes: 3 additions & 9 deletions src/pygambit/lib/action.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ cdef class Action:
return Rational(py_string.decode('ascii'))

def __set__(self, value):
cdef string s
if isinstance(value, (int, decimal.Decimal, fractions.Fraction)):
v = str(value)
s = v.encode('ascii')
self.action.deref().GetInfoset().deref().SetActionProb(
self.action.deref().GetNumber(), s)
else:
raise TypeError("numeric argument required for action probability")

self.action.deref().GetInfoset().deref().SetActionProb(
self.action.deref().GetNumber(), _to_number(value)
)
4 changes: 2 additions & 2 deletions src/pygambit/lib/libgambit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cdef extern from "core/number.h":
c_Number(string)
string as_string "operator const string &"()

cdef c_Number _to_number(value):
cdef c_Number _to_number(value) except *:
"""Convert a value into a game Number representation."""
if isinstance(value, (int, Decimal, Rational)):
value = str(value)
Expand Down Expand Up @@ -165,7 +165,7 @@ cdef extern from "games/game.h":
c_GameAction InsertAction(c_GameAction) except +ValueError

string GetActionProb(int, string) except +IndexError
void SetActionProb(int, string) except +IndexError
void SetActionProb(int, c_Number) except +IndexError

int NumMembers()
c_GameNode GetMember(int) except +IndexError
Expand Down
23 changes: 10 additions & 13 deletions src/pygambit/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def test_action_probability(self):
decimal.Decimal('0.500000')
)

self.extensive_game.root.infoset.actions[0].prob = 2.0
assert(self.extensive_game.root.infoset.actions[0].prob == fractions.Fraction(2))

self.extensive_game.root.infoset.actions[0].prob = (
decimal.Decimal('0.97300')
)
Expand All @@ -50,22 +53,16 @@ def test_action_probability(self):
self.extensive_game.root.infoset.actions[0].prob = 2
assert self.extensive_game.root.infoset.actions[0].prob == 2

self.extensive_game.root.infoset.actions[0].prob = "1/7"
assert (self.extensive_game.root.infoset.actions[0].prob == fractions.Fraction('1/7'))

self.extensive_game.root.infoset.actions[0].prob = "2.7"
assert (self.extensive_game.root.infoset.actions[0].prob == decimal.Decimal('2.7'))

self.assertRaises(
TypeError, setattr, self.extensive_game.root.infoset.actions[0],
"prob", 2.0
)
self.assertRaises(
TypeError, setattr, self.extensive_game.root.infoset.actions[0],
ValueError, setattr, self.extensive_game.root.infoset.actions[0],
"prob", "test"
)
self.assertRaises(
TypeError, setattr, self.extensive_game.root.infoset.actions[0],
"prob", "1/7"
)
self.assertRaises(
TypeError, setattr, self.extensive_game.root.infoset.actions[0],
"prob", "2.7"
)

def test_action_precedes(self):
"Test to ensure precedes is working"
Expand Down

0 comments on commit 35b2c15

Please sign in to comment.