Skip to content

Commit

Permalink
clang-tidy improvements principally in algorithms.
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Apr 9, 2023
1 parent e6dd90a commit 501c3bb
Show file tree
Hide file tree
Showing 39 changed files with 411 additions and 513 deletions.
12 changes: 6 additions & 6 deletions src/games/nash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ BehavViaStrategySolver<T>::Solve(const BehaviorSupportProfile &p_support) const
{
List<MixedStrategyProfile<T> > output = m_solver->Solve(p_support.GetGame());
List<MixedBehaviorProfile<T> > solutions;
for (auto profile : output) {
for (const auto &profile : output) {
solutions.push_back(MixedBehaviorProfile<T>(profile));
}
return solutions;
Expand Down Expand Up @@ -261,12 +261,12 @@ void ChildSubgames(const GameNode &p_node, List<GameNode> &p_list)
// at the end of the computation
//

template <class T>
template<class T>
void SubgameBehavSolver<T>::SolveSubgames(const BehaviorSupportProfile &p_support,
const DVector<T> &p_templateSolution,
GameNode n,
List<DVector<T> > &solns,
List<GameOutcome> &values) const
const DVector<T> &p_templateSolution,
const GameNode &n,
List<DVector<T> > &solns,
List<GameOutcome> &values) const
{
Game efg = p_support.GetGame();

Expand Down
8 changes: 4 additions & 4 deletions src/games/nash.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ template <class T> class SubgameBehavSolver : public BehavSolver<T> {

private:
void SolveSubgames(const BehaviorSupportProfile &p_support,
const DVector<T> &p_templateSolution,
GameNode n,
List<DVector<T> > &solns,
List<GameOutcome> &values) const;
const DVector<T> &p_templateSolution,
const GameNode &n,
List<DVector<T> > &solns,
List<GameOutcome> &values) const;
};

//
Expand Down
4 changes: 2 additions & 2 deletions src/pygambit/lib/nash.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ logit_atlambda(const Game &p_game, double p_lambda)
StrategicQREPathTracer alg;
NullBuffer null_buffer;
std::ostream null_stream(&null_buffer);
return new LogitQREMixedStrategyProfile(alg.SolveAtLambda(p_game, null_stream,
return new LogitQREMixedStrategyProfile(alg.SolveAtLambda(start, null_stream,
p_lambda, 1.0));
}

Expand All @@ -61,6 +61,6 @@ logit_principal_branch(const Game &p_game, double p_maxLambda=1000000.0)
StrategicQREPathTracer alg;
NullBuffer null_buffer;
std::ostream null_stream(&null_buffer);
return alg.TraceStrategicPath(p_game, null_stream, p_maxLambda, 1.0);
return alg.TraceStrategicPath(start, null_stream, p_maxLambda, 1.0);
}

14 changes: 7 additions & 7 deletions src/solvers/enummixed/clique.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@
namespace Gambit {
namespace Nash {

#define MAXM 700 // max. no of left nodes for incidence matrix
#define MAXN MAXM // max. no of right nodes for incidence matrix
const int MAXM = 700; // max. no of left nodes for incidence matrix
const int MAXN = MAXM; // max. no of right nodes for incidence matrix

#define MAXINP1 5000 // max. no of left nodes in input
#define MAXINP2 MAXINP1 // max. no of right nodes in input
#define MAXEDGES 50000 // max. no of edges in input
#define MAXCO MIN(MAXINP1, MAXINP2) + 1
// #define MAXINP1 5000 // max. no of left nodes in input
// #define MAXINP2 MAXINP1 // max. no of right nodes in input
// #define MAXEDGES 50000 // max. no of edges in input
// #define MAXCO MIN(MAXINP1, MAXINP2) + 1
// max. no of connected components; on the smaller side,
// each node could be in different component
#define STKSIZE (MAXM + 1) * (MAXN + 1)
const int STKSIZE = (MAXM + 1) * (MAXN + 1);
// largest stack usage for full graph


Expand Down
83 changes: 41 additions & 42 deletions src/solvers/enummixed/enummixed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,59 +135,58 @@ EnumMixedStrategySolver<T>::SolveDetailed(const Game &p_game) const
int id1 = 0, id2 = 0;

for (int i2 = 2; i2 <= solution->m_v2; i2++) {
BFS<T> bfs1 = verts2[i2];
const BFS<T> &bfs1 = verts2[i2];
i++;
for (int i1 = 2; i1 <= solution->m_v1; i1++) {
BFS<T> bfs2 = verts1[i1];
const BFS<T> &bfs2 = verts1[i1];

// check if solution is nash
// need only check complementarity, since it is feasible
bool nash = true;
for (size_t k = 1; nash && k <= p_game->Players()[1]->Strategies().size(); k++) {
if (bfs1.count(k) && bfs2.count(-k)) {
nash = nash && EqZero(bfs1[k] * bfs2[-k]);
}
if (bfs1.count(k) && bfs2.count(-k)) {
nash = EqZero(bfs1[k] * bfs2[-k]);
}
}

for (size_t k = 1; nash && k <= p_game->Players()[2]->Strategies().size(); k++) {
if (bfs2.count(k) && bfs1.count(-k)) {
nash = nash && EqZero(bfs2[k] * bfs1[-k]);
}
if (bfs2.count(k) && bfs1.count(-k)) {
nash = EqZero(bfs2[k] * bfs1[-k]);
}
}

if (nash) {
MixedStrategyProfile<T> profile(p_game->NewMixedStrategyProfile(static_cast<T>(0)));
static_cast<Vector<T> &>(profile) = static_cast<T>(0);
for (size_t k = 1; k <= p_game->Players()[1]->Strategies().size(); k++) {
if (bfs1.count(k)) {
profile[p_game->Players()[1]->Strategies()[k]] = -bfs1[k];
}
}
for (size_t k = 1; k <= p_game->Players()[2]->Strategies().size(); k++) {
if (bfs2.count(k)) {
profile[p_game->Players()[2]->Strategies()[k]] = -bfs2[k];
}
}
profile = profile.Normalize();
solution->m_extremeEquilibria.push_back(profile);
this->m_onEquilibrium->Render(profile);
// note: The keys give the mixed strategy associated with each node.
// The keys should also keep track of the basis
// As things stand now, two different bases could lead to
// the same key... BAD!
if (vert1id[i1] == 0) {
id1++;
vert1id[i1] = id1;
solution->m_key2.push_back(profile[p_game->GetPlayer(2)]);
}
if (vert2id[i2] == 0) {
id2++;
vert2id[i2] = id2;
solution->m_key1.push_back(profile[p_game->GetPlayer(1)]);
}
solution->m_node1.push_back(vert2id[i2]);
solution->m_node2.push_back(vert1id[i1]);
MixedStrategyProfile<T> eqm(p_game->NewMixedStrategyProfile(static_cast<T>(0)));
static_cast<Vector<T> &>(eqm) = static_cast<T>(0);
for (size_t k = 1; k <= p_game->Players()[1]->Strategies().size(); k++) {
if (bfs1.count(k)) {
eqm[p_game->Players()[1]->Strategies()[k]] = -bfs1[k];
}
}
for (size_t k = 1; k <= p_game->Players()[2]->Strategies().size(); k++) {
if (bfs2.count(k)) {
eqm[p_game->Players()[2]->Strategies()[k]] = -bfs2[k];
}
}
eqm = eqm.Normalize();
solution->m_extremeEquilibria.push_back(eqm);
this->m_onEquilibrium->Render(eqm);

// note: The keys give the mixed strategy associated with each node.
// The keys should also keep track of the basis
// As things stand now, two different bases could lead to
// the same key... BAD!
if (vert1id[i1] == 0) {
id1++;
vert1id[i1] = id1;
solution->m_key2.push_back(eqm[p_game->GetPlayer(2)]);
}
if (vert2id[i2] == 0) {
id2++;
vert2id[i2] = id2;
solution->m_key1.push_back(eqm[p_game->GetPlayer(1)]);
}
solution->m_node1.push_back(vert2id[i2]);
solution->m_node2.push_back(vert1id[i1]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/solvers/enumpure/enumpure.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Nash {
///
class EnumPureStrategySolver : public StrategySolver<Rational> {
public:
EnumPureStrategySolver(std::shared_ptr<StrategyProfileRenderer<Rational> > p_onEquilibrium = nullptr)
explicit EnumPureStrategySolver(std::shared_ptr<StrategyProfileRenderer<Rational> > p_onEquilibrium = nullptr)
: StrategySolver<Rational>(p_onEquilibrium) { }
~EnumPureStrategySolver() override = default;

Expand Down Expand Up @@ -67,7 +67,7 @@ EnumPureStrategySolver::Solve(const Game &p_game) const
///
class EnumPureAgentSolver : public BehavSolver<Rational> {
public:
EnumPureAgentSolver(std::shared_ptr<StrategyProfileRenderer<Rational> > p_onEquilibrium = nullptr)
explicit EnumPureAgentSolver(std::shared_ptr<StrategyProfileRenderer<Rational> > p_onEquilibrium = nullptr)
: BehavSolver<Rational>(p_onEquilibrium) { }
~EnumPureAgentSolver() override = default;

Expand Down
4 changes: 2 additions & 2 deletions src/solvers/ipa/ipa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ NashIPAStrategySolver::Solve(const Game &p_game,
List<MixedStrategyProfile<double> > solutions;

if (p_game->IsAgg()) {
A.reset(new aggame(dynamic_cast<GameAggRep &>(*p_game)));
A = std::make_shared<aggame>(dynamic_cast<GameAggRep &>(*p_game));
}
else {
std::vector<int> actions(p_game->NumPlayers());
Expand All @@ -62,7 +62,7 @@ NashIPAStrategySolver::Solve(const Game &p_game,
}
cvector payoffs(veclength);

A.reset(new nfgame(p_game->NumPlayers(), actions, payoffs));
A = std::make_shared<nfgame>(p_game->NumPlayers(), actions, payoffs);

std::vector<int> profile(p_game->NumPlayers());
for (StrategyProfileIterator iter(p_game); !iter.AtEnd(); iter++) {
Expand Down
6 changes: 3 additions & 3 deletions src/solvers/ipa/ipa.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ namespace Nash {

class NashIPAStrategySolver : public StrategySolver<double> {
public:
explicit NashIPAStrategySolver(std::shared_ptr<StrategyProfileRenderer<double> > p_onEquilibrium = 0)
explicit NashIPAStrategySolver(std::shared_ptr<StrategyProfileRenderer<double> > p_onEquilibrium = nullptr)
: StrategySolver<double>(p_onEquilibrium)
{ }
virtual ~NashIPAStrategySolver() { }
~NashIPAStrategySolver() override = default;

List<MixedStrategyProfile<double> > Solve(const Game &p_game) const;
List<MixedStrategyProfile<double> > Solve(const Game &p_game) const override;
List<MixedStrategyProfile<double> > Solve(const Game &p_game,
const Array<double> &p_pert) const;
};
Expand Down
5 changes: 0 additions & 5 deletions src/solvers/linalg/basis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,5 @@ void Basis::CheckBasis()
IsBasisIdent = check;
}

bool Basis::IsIdent()
{
return IsBasisIdent;
}



4 changes: 3 additions & 1 deletion src/solvers/linalg/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class Basis {
// Check if Basis is Ident
virtual void CheckBasis();
// returns whether the basis is the identity matrix
bool IsIdent();
bool IsIdent() const { return IsBasisIdent; }


};

} // end namespace Gambit::linalg
Expand Down
56 changes: 28 additions & 28 deletions src/solvers/linalg/lemketab.imp
Original file line number Diff line number Diff line change
Expand Up @@ -152,58 +152,58 @@ template <class T> int LemkeTableau<T>::ExitIndex(int inlabel)
T ratio, tempmax;
Vector<T> incol(this->MinRow(), this->MaxRow());
Vector<T> col(this->MinRow(), this->MaxRow());
this->SolveColumn(inlabel,incol);

this->SolveColumn(inlabel, incol);
// gout << "\nincol = " << incol;
// Find all row indices for which column col has positive entries.
// Find all row indices for which column col has positive entries.
for (i = this->MinRow(); i <= this->MaxRow(); i++)
if (incol[i] > this->eps2)
BestSet.push_back(i);
// Is this really needed?
if(BestSet.Length()==0)
if (BestSet.Length() == 0)
// gout << "\nBestSet.Length() == 0, Find(0):\n" << Find(0);
if(BestSet.Length()==0
&& incol[this->Find(0)]<=this->eps2 && incol[this->Find(0)] >= (-this->eps2) )
return this->Find(0);
if(BestSet.Length() <= 0) throw BadExitIndex();
// If there are multiple candidates, break ties by
// looking at ratios with other columns,
// eliminating nonmaximizers of
// a similar ratio, until only one candidate remains.
c = this->MinRow()-1;
if (BestSet.Length() == 0
&& incol[this->Find(0)] <= this->eps2 && incol[this->Find(0)] >= (-this->eps2))
return this->Find(0);
if (BestSet.Length() <= 0) throw BadExitIndex();

// If there are multiple candidates, break ties by
// looking at ratios with other columns,
// eliminating nonmaximizers of
// a similar ratio, until only one candidate remains.
c = this->MinRow() - 1;
this->BasisVector(col);
// gout << "\nLength = " << BestSet.Length();
// gout << "\n x = " << col << "\n";
while (BestSet.Length() > 1) {
while (BestSet.Length() > 1) {
// this is where ITEM 001 is failing
if(c > this->MaxRow()) throw BadExitIndex();
if(c>=this->MinRow()) {
this->SolveColumn(-c,col);
if (c > this->MaxRow()) throw BadExitIndex();
if (c >= this->MinRow()) {
this->SolveColumn(-c, col);
// gout << "\n-c = " << -c << " col = " << col;
}
// Initialize tempmax.
// Initialize tempmax.
tempmax = col[BestSet[1]] / incol[BestSet[1]];
// Find the maximum ratio.
for (i = 2; i <= BestSet.Length(); i++) {
// Find the maximum ratio.
for (i = 2; i <= BestSet.Length(); i++) {
ratio = col[BestSet[i]] / incol[BestSet[i]];
if (ratio > tempmax) tempmax = ratio;
if (ratio > tempmax) tempmax = ratio;
}
// if(tempmax <= (T 2)*eps1) throw BadExitIndex();
// Remove nonmaximizers from the list of candidate columns.
for (i = BestSet.Length(); i >= 1; i--) {

// Remove nonmaximizers from the list of candidate columns.
for (i = BestSet.Length(); i >= 1; i--) {
ratio = col[BestSet[i]] / incol[BestSet[i]];
if (ratio < tempmax -this->eps1)
BestSet.Remove(i);
if (ratio < tempmax - this->eps1)
BestSet.Remove(i);
}
// else {
// if(!Member(FindColumn(c))) throw BadExitIndex();
// if (BestSet.Contains(c_row)) return c_row;
// }
c++;
}
if(BestSet.Length() <= 0) throw BadExitIndex();
if (BestSet.Length() <= 0) throw BadExitIndex();
return BestSet[1];
}

Expand Down
12 changes: 3 additions & 9 deletions src/solvers/linalg/lhtab.imp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,11 @@ int LHTableau<T>::Find(int i) const
template <class T>
bool LHTableau<T>::CanPivot(int outlabel, int inlabel) const
{
if (T1.ValidIndex(outlabel) &&
T1.CanPivot(outlabel, inlabel)) {
return true;
}
else if (T2.ValidIndex(outlabel) &&
T2.CanPivot(outlabel, inlabel)) {
return true;
}
return false;
return ((T1.ValidIndex(outlabel) && T1.CanPivot(outlabel, inlabel)) ||
(T2.ValidIndex(outlabel) && T2.CanPivot(outlabel, inlabel)));
}


template <class T>
void LHTableau<T>::Pivot(int outrow, int inlabel)
{
Expand Down

0 comments on commit 501c3bb

Please sign in to comment.