Skip to content

Commit b92aa0a

Browse files
committed
Slightly improve filling of gradient of sum-to-one equation
1 parent 04e1a9d commit b92aa0a

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/solvers/logit/nfglogit.cc

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,22 @@ class EquationSystem {
9797
class SumToOneEquation final : public Equation {
9898
Game m_game;
9999
GamePlayer m_player;
100+
int m_firstIndex, m_lastIndex;
100101

101102
public:
102103
explicit SumToOneEquation(const GamePlayer &p_player)
103-
: m_game(p_player->GetGame()), m_player(p_player)
104+
: m_game(p_player->GetGame()), m_player(p_player), m_firstIndex(0), m_lastIndex(0)
104105
{
106+
int col = 1;
107+
for (const auto &player : m_game->GetPlayers()) {
108+
if (player != m_player) {
109+
col += player->GetStrategies().size();
110+
continue;
111+
}
112+
m_firstIndex = col;
113+
m_lastIndex = col + player->GetStrategies().size();
114+
return;
115+
}
105116
}
106117

107118
~SumToOneEquation() override = default;
@@ -123,14 +134,11 @@ double SumToOneEquation::Value(const MixedStrategyProfile<double> &p_profile,
123134
void SumToOneEquation::Gradient(const MixedStrategyProfile<double> &p_profile, double p_lambda,
124135
Vector<double> &p_gradient) const
125136
{
126-
int col = 1;
127-
for (const auto &player : m_game->GetPlayers()) {
128-
for (const auto &strategy : player->GetStrategies()) {
129-
p_gradient[col++] = (player == m_player) ? p_profile[strategy] : 0.0;
130-
}
137+
p_gradient = 0.0;
138+
int col = m_firstIndex;
139+
for (const auto &strategy : m_player->GetStrategies()) {
140+
p_gradient[col++] = p_profile[strategy];
131141
}
132-
// Derivative wrt lambda is zero
133-
p_gradient[col] = 0.0;
134142
}
135143

136144
class RatioEquation final : public Equation {

0 commit comments

Comments
 (0)