@@ -1079,52 +1079,44 @@ Game GameTreeRep::SetChanceProbs(const GameInfoset &p_infoset, const Array<Numbe
10791079 throw DimensionException (" The number of probabilities given must match the number of actions" );
10801080 }
10811081 IncrementVersion ();
1082- Rational sum (0 );
1083- for (auto prob : p_probs) {
1084- if (static_cast <Rational>(prob) < Rational (0 )) {
1085- throw ValueException (" Probabilities must be non-negative numbers" );
1086- }
1087- sum += static_cast <Rational>(prob);
1082+ if (std::any_of (p_probs.begin (), p_probs.end (),
1083+ [](const Number &x) { return static_cast <Rational>(x) < Rational (0 ); })) {
1084+ throw ValueException (" Probabilities must be non-negative numbers" );
10881085 }
1086+ auto sum = std::accumulate (
1087+ p_probs.begin (), p_probs.end (), Rational (0 ),
1088+ [](const Rational &r, const Number &n) { return r + static_cast <Rational>(n); });
10891089 if (sum != Rational (1 )) {
10901090 throw ValueException (" Probabilities must sum to exactly one" );
10911091 }
1092- for (int act = 1 ; act <= p_infoset->NumActions (); act++) {
1093- dynamic_cast <GameTreeInfosetRep &>(*p_infoset).m_probs [act] = p_probs[act];
1094- }
1092+ std::copy (p_probs.begin (), p_probs.end (),
1093+ dynamic_cast <GameTreeInfosetRep &>(*p_infoset).m_probs .begin ());
10951094 ClearComputedValues ();
10961095 return this ;
10971096}
10981097
1099- Game GameTreeRep::NormalizeChanceProbs (const GameInfoset &m_infoset )
1098+ Game GameTreeRep::NormalizeChanceProbs (const GameInfoset &p_infoset )
11001099{
1101- if (m_infoset ->GetGame () != this ) {
1100+ if (p_infoset ->GetGame () != this ) {
11021101 throw MismatchException ();
11031102 }
1104- if (!m_infoset ->IsChanceInfoset ()) {
1103+ if (!p_infoset ->IsChanceInfoset ()) {
11051104 throw UndefinedException (
11061105 " Action probabilities can only be normalized for chance information sets" );
11071106 }
11081107 IncrementVersion ();
1109- Rational sum (0 );
1110- for (int act = 1 ; act <= m_infoset->NumActions (); act++) {
1111- Rational action_prob (m_infoset->GetActionProb (act));
1112- sum += action_prob;
1113- }
1114- Array<Number> m_probs (m_infoset->NumActions ());
1108+ auto &probs = dynamic_cast <GameTreeInfosetRep &>(*p_infoset).m_probs ;
1109+ auto sum = std::accumulate (
1110+ probs.begin (), probs.end (), Rational (0 ),
1111+ [](const Rational &s, const Number &n) { return s + static_cast <Rational>(n); });
11151112 if (sum == Rational (0 )) {
11161113 // all remaining moves have prob zero; split prob 1 equally among them
1117- for (int act = 1 ; act <= m_infoset->NumActions (); act++) {
1118- m_probs[act] = Rational (1 , m_infoset->NumActions ());
1119- }
1114+ std::fill (probs.begin (), probs.end (), Rational (1 , probs.size ()));
11201115 }
11211116 else {
1122- for (int act = 1 ; act <= m_infoset->NumActions (); act++) {
1123- Rational prob (m_infoset->GetActionProb (act));
1124- m_probs[act] = prob / sum;
1125- }
1117+ std::transform (probs.begin (), probs.end (), probs.begin (),
1118+ [&sum](const Number &n) { return static_cast <Rational>(n) / sum; });
11261119 }
1127- m_infoset->GetGame ()->SetChanceProbs (m_infoset, m_probs);
11281120 return this ;
11291121}
11301122
0 commit comments