Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/solvers/logit/nfglogit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ void PointToProfile(MixedStrategyProfile<double> &p_profile, const Vector<double
}
}

void PointToLogProfile(MixedStrategyProfile<double> &p_profile, const Vector<double> &p_point)
{
for (size_t i = 1; i < p_point.size(); i++) {
p_profile[i] = p_point[i];
}
}

Vector<double> ProfileToPoint(const LogitQREMixedStrategyProfile &p_profile)
{
Vector<double> point(p_profile.size() + 1);
Expand Down Expand Up @@ -75,6 +82,7 @@ class Equation {
virtual ~Equation() = default;

virtual double Value(const MixedStrategyProfile<double> &p_profile,
const MixedStrategyProfile<double> &p_logProfile,
const Vector<double> &p_strategyValues, double p_lambda) const = 0;
virtual void Gradient(const MixedStrategyProfile<double> &p_profile,
const Vector<double> &p_strategyValues,
Expand Down Expand Up @@ -105,13 +113,15 @@ class SumToOneEquation final : public Equation {

~SumToOneEquation() override = default;
double Value(const MixedStrategyProfile<double> &p_profile,
const MixedStrategyProfile<double> &p_logProfile,
const Vector<double> &p_strategyValues, double p_lambda) const override;
void Gradient(const MixedStrategyProfile<double> &p_profile,
const Vector<double> &p_strategyValues, const Matrix<double> &p_strategyDerivs,
double p_lambda, Vector<double> &p_gradient) const override;
};

double SumToOneEquation::Value(const MixedStrategyProfile<double> &p_profile,
const MixedStrategyProfile<double> &p_logProfile,
const Vector<double> &p_strategyValues, double p_lambda) const
{
double value = -1.0;
Expand Down Expand Up @@ -157,17 +167,19 @@ class RatioEquation final : public Equation {

~RatioEquation() override = default;
double Value(const MixedStrategyProfile<double> &p_profile,
const MixedStrategyProfile<double> &p_logProfile,
const Vector<double> &p_strategyValues, double p_lambda) const override;
void Gradient(const MixedStrategyProfile<double> &p_profile,
const Vector<double> &p_strategyValues, const Matrix<double> &p_strategyDerivs,
double p_lambda, Vector<double> &p_gradient) const override;
};

double RatioEquation::Value(const MixedStrategyProfile<double> &p_profile,
const MixedStrategyProfile<double> &p_logProfile,
const Vector<double> &p_strategyValues, double p_lambda) const
{
return (std::log(p_profile[m_strategyIndex]) - std::log(p_profile[m_refStrategyIndex]) -
p_lambda * (p_strategyValues[m_strategyIndex] - p_strategyValues[m_refStrategyIndex]));
return p_logProfile[m_strategyIndex] - p_logProfile[m_refStrategyIndex] -
p_lambda * (p_strategyValues[m_strategyIndex] - p_strategyValues[m_refStrategyIndex]);
}

void RatioEquation::Gradient(const MixedStrategyProfile<double> &p_profile,
Expand Down Expand Up @@ -209,13 +221,14 @@ class EquationSystem {
private:
std::vector<std::shared_ptr<Equation>> m_equations;
const Game &m_game;
mutable MixedStrategyProfile<double> m_profile;
mutable MixedStrategyProfile<double> m_profile, m_logProfile;
mutable Vector<double> m_strategyValues;
mutable Matrix<double> m_strategyDerivs;
};

EquationSystem::EquationSystem(const Game &p_game)
: m_game(p_game), m_profile(p_game->NewMixedStrategyProfile(0.0)),
m_logProfile(p_game->NewMixedStrategyProfile(0.0)),
m_strategyValues(m_profile.MixedProfileLength()),
m_strategyDerivs(m_profile.MixedProfileLength(), m_profile.MixedProfileLength())
{
Expand All @@ -232,13 +245,15 @@ EquationSystem::EquationSystem(const Game &p_game)
void EquationSystem::GetValue(const Vector<double> &p_point, Vector<double> &p_lhs) const
{
PointToProfile(m_profile, p_point);
PointToLogProfile(m_logProfile, p_point);
const double lambda = p_point.back();
int col = 1;
for (const auto &strategy : m_game->GetStrategies()) {
m_strategyValues[col++] = m_profile.GetPayoff(strategy);
}
std::transform(m_equations.begin(), m_equations.end(), p_lhs.begin(),
[this, lambda](auto e) { return e->Value(m_profile, m_strategyValues, lambda); });
std::transform(m_equations.begin(), m_equations.end(), p_lhs.begin(), [this, lambda](auto e) {
return e->Value(m_profile, m_logProfile, m_strategyValues, lambda);
});
}

void EquationSystem::GetJacobian(const Vector<double> &p_point, Matrix<double> &p_jac) const
Expand Down
Loading