Skip to content

Commit

Permalink
Streamline simple polynomial construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Sep 18, 2024
1 parent b6d4c5c commit 6c42be5
Showing 1 changed file with 37 additions and 47 deletions.
84 changes: 37 additions & 47 deletions src/solvers/enumpoly/efgpoly.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ class ProblemData {
~ProblemData();
};

gPoly<double> BuildSequenceVariable(ProblemData &p_data, const GamePlayer &p_player,
const GameAction &p_action)
{
if (!p_action) {
return gPoly<double>(p_data.Space, 1, p_data.Lex);
}
GameInfoset infoset = p_action->GetInfoset();
int seq = p_data.SF.GetSequenceNumber(p_player, p_action);
if (p_data.support.GetIndex(p_action) < p_data.support.NumActions(infoset)) {
return gPoly<double>(p_data.Space, p_data.var[p_player->GetNumber()][seq], 1, p_data.Lex);
}

int isetrow = p_data.SF.InfosetRowNumber(p_player, seq);
gPoly<double> equation(p_data.Space, p_data.Lex);
for (int j = 1; j < seq; j++) {
if (p_data.SF.GetConstraintEntry(p_player, isetrow, j) == Rational(-1)) {
equation -= BuildSequenceVariable(p_data, p_player, p_data.SF.GetSequence(p_player, j));
}
else if (p_data.SF.GetConstraintEntry(p_player, isetrow, j) == Rational(1)) {
equation += BuildSequenceVariable(p_data, p_player, p_data.SF.GetSequence(p_player, j));
}
}
return equation;
}

ProblemData::ProblemData(const BehaviorSupportProfile &p_support)
: support(p_support), SF(p_support),
nVars(SF.TotalNumSequences() - SF.NumPlayerInfosets() - SF.NumPlayers()),
Expand All @@ -73,6 +98,14 @@ ProblemData::ProblemData(const BehaviorSupportProfile &p_support)
}
}
}

for (auto player : p_support.GetGame()->GetPlayers()) {
for (auto action = SF.cbegin_sequences(player); action != SF.cend_sequences(player);
action++) {
variables.emplace(std::make_pair(player, *action),
BuildSequenceVariable(*this, player, *action));
}
}
}

ProblemData::~ProblemData()
Expand All @@ -92,49 +125,6 @@ ProblemData::~ProblemData()
// derivatives vanish, and that the sum of action probabilities at
// each information set be less than one.

gPoly<double> ProbOfSequence(ProblemData &p_data, const GamePlayer &p_player,
const GameAction &p_action)
{
auto var = p_data.variables.find({p_player, p_action});
if (var != p_data.variables.end()) {
return var->second;
}

Vector<int> exps(p_data.nVars);
if (!p_action) {
exps = 0;
exp_vect const_exp(p_data.Space, exps);
gMono<double> const_term(1.0, const_exp);
auto var = gPoly<double>(p_data.Space, const_term, p_data.Lex);
p_data.variables.emplace(std::make_pair(std::make_pair(p_player, p_action), var));
return var;
}
GameInfoset infoset = p_action->GetInfoset();
int seq = p_data.SF.GetSequenceNumber(p_player, p_action);
if (p_data.support.GetIndex(p_action) < p_data.support.NumActions(infoset)) {
exps = 0;
exps[p_data.var[p_player->GetNumber()][seq]] = 1;
exp_vect const_exp(p_data.Space, exps);
gMono<double> const_term(1.0, const_exp);
auto var = gPoly<double>(p_data.Space, const_term, p_data.Lex);
p_data.variables.emplace(std::make_pair(std::make_pair(p_player, p_action), var));
return var;
}

int isetrow = p_data.SF.InfosetRowNumber(p_player, seq);
gPoly<double> equation(p_data.Space, p_data.Lex);
for (int j = 1; j < seq; j++) {
if (p_data.SF.GetConstraintEntry(p_player, isetrow, j) == Rational(-1)) {
equation -= ProbOfSequence(p_data, p_player, p_data.SF.GetSequence(p_player, j));
}
else if (p_data.SF.GetConstraintEntry(p_player, isetrow, j) == Rational(1)) {
equation += ProbOfSequence(p_data, p_player, p_data.SF.GetSequence(p_player, j));
}
}
p_data.variables.emplace(std::make_pair(std::make_pair(p_player, p_action), equation));
return equation;
}

gPoly<double> GetPayoff(ProblemData &p_data, int pl)
{
gIndexOdometer index(p_data.SF.NumSequences());
Expand All @@ -145,9 +135,9 @@ gPoly<double> GetPayoff(ProblemData &p_data, int pl)
if (pay != Rational(0)) {
gPoly<double> term(p_data.Space, double(pay), p_data.Lex);
for (int k = 1; k <= p_data.support.GetGame()->NumPlayers(); k++) {
term *= ProbOfSequence(p_data, p_data.support.GetGame()->GetPlayer(k),
p_data.SF.GetSequence(p_data.support.GetGame()->GetPlayer(k),
index.CurrentIndices()[k]));
term *= p_data.variables.at({p_data.support.GetGame()->GetPlayer(k),
p_data.SF.GetSequence(p_data.support.GetGame()->GetPlayer(k),
index.CurrentIndices()[k])});
}
equation += term;
}
Expand Down Expand Up @@ -182,7 +172,7 @@ gPolyList<double> LastActionProbPositiveInequalities(ProblemData &p_data)
GameAction action = p_data.SF.GetSequence(player, j);
int act_num = p_data.support.GetIndex(action);
if (act_num == p_data.support.NumActions(action->GetInfoset()) && act_num > 1) {
equations += ProbOfSequence(p_data, player, action);
equations += p_data.variables.at({player, action});
}
}
}
Expand Down

0 comments on commit 6c42be5

Please sign in to comment.