EPEC solve
Solving Equilibrium Problems with Equilibrium Constraints (EPECs)
Public Member Functions | Private Member Functions | Private Attributes | Friends
Game::NashGame Class Reference

Class to model Nash-cournot games with each player playing a QP. More...

#include <games.h>

Public Member Functions

 NashGame (GRBEnv *e) noexcept
 To be used only when NashGame is being loaded from a file. More...
 
 NashGame (GRBEnv *e, std::vector< std::shared_ptr< QP_Param >> Players, arma::sp_mat MC, arma::vec MCRHS, unsigned int n_LeadVar=0, arma::sp_mat LeadA={}, arma::vec LeadRHS={})
 
 NashGame (const NashGame &N)
 
 ~NashGame ()
 
unsigned int getNprimals () const
 Return the number of primal variables. More...
 
unsigned int getNshadow () const
 Gets the number of Market clearing Shadow prices. More...
 
unsigned int getNleaderVars () const
 Gets the number of leader variables. More...
 
unsigned int getNduals () const
 Gets the number of dual variables in the problem. More...
 
unsigned int getPrimalLoc (unsigned int i=0) const
 Gets the position of the primal variable of i th player. More...
 
unsigned int getMCdualLoc () const
 Gets the positin where the Market-clearing dual variables start. More...
 
unsigned int getLeaderLoc () const
 Gets the positin where the Leader variables start. More...
 
unsigned int getDualLoc (unsigned int i=0) const
 Gets the location where the dual variables start. More...
 
const NashGameFormulateLCP (arma::sp_mat &M, arma::vec &q, perps &Compl, bool writeToFile=false, std::string M_name="dat/LCP.txt", std::string q_name="dat/q.txt") const
 
arma::sp_mat RewriteLeadCons () const
 Rewrites leader constraint adjusting for dual variables. Rewrites leader constraints given earlier with added empty columns and spaces corresponding to Market clearing duals and other equation duals. More...
 
arma::vec getLeadRHS () const
 
arma::vec getMCLeadRHS () const
 
std::unique_ptr< GRBModel > Respond (unsigned int player, const arma::vec &x, bool fullvec=true) const
 Given the decision of other players, find the optimal response for player in position player. More...
 
double RespondSol (arma::vec &sol, unsigned int player, const arma::vec &x, bool fullvec=true) const
 
arma::vec ComputeQPObjvals (const arma::vec &x, bool checkFeas=false) const
 
bool isSolved (const arma::vec &sol, unsigned int &violPlayer, arma::vec &violSol, double tol=1e-4) const
 
NashGameaddDummy (unsigned int par=0, int position=-1)
 Add dummy variables in a NashGame object. More...
 
NashGameaddLeadCons (const arma::vec &a, double b)
 Adds Leader constraint to a NashGame object. More...
 
void write (std::string filename, bool append=true, bool KKT=false) const
 
void save (std::string filename, bool erase=true) const
 Saves the Game::NashGame object in a loadable file. More...
 
long int load (std::string filename, long int pos=0)
 Loads the Game::NashGame object stored in a file. More...
 

Private Member Functions

void set_positions ()
 

Private Attributes

GRBEnv * env = nullptr
 
arma::sp_mat LeaderConstraints
 Upper level leader constraints LHS. More...
 
arma::vec LeaderConsRHS
 Upper level leader constraints RHS. More...
 
unsigned int Nplayers
 Number of players in the Nash Game. More...
 
std::vector< std::shared_ptr< QP_Param > > Players
 The QP that each player solves. More...
 
arma::sp_mat MarketClearing
 Market clearing constraints. More...
 
arma::vec MCRHS
 RHS to the Market Clearing constraints. More...
 
std::vector< unsigned int > primal_position
 
std::vector< unsigned int > dual_position
 
unsigned int MC_dual_position
 
unsigned int Leader_position
 
unsigned int n_LeadVar
 

Friends

std::ostream & operator<< (std::ostream &os, const NashGame &N)
 

Detailed Description

Class to model Nash-cournot games with each player playing a QP.

Stores a vector of QPs with each player's optimization problem. Potentially common (leader) constraints can be stored too.

Helpful in rewriting the Nash-Cournot game as an LCP Helpful in rewriting leader constraints after incorporating dual variables etc

Warning
This has public fields which if accessed and changed can cause undefined behavior!

Definition at line 249 of file games.h.

Constructor & Destructor Documentation

◆ NashGame() [1/3]

Game::NashGame::NashGame ( GRBEnv *  e)
inlineexplicitnoexcept

To be used only when NashGame is being loaded from a file.

Definition at line 278 of file games.h.

◆ NashGame() [2/3]

Game::NashGame::NashGame ( GRBEnv *  e,
std::vector< std::shared_ptr< QP_Param >>  Players,
arma::sp_mat  MC,
arma::vec  MCRHS,
unsigned int  n_LeadVar = 0,
arma::sp_mat  LeadA = {},
arma::vec  LeadRHS = {} 
)
explicit

Constructing a NashGame from a set of Game::QP_Param, Market clearing constraints

◆ NashGame() [3/3]

Game::NashGame::NashGame ( const NashGame N)

Definition at line 599 of file Games.cpp.

◆ ~NashGame()

Game::NashGame::~NashGame ( )
inline

Definition at line 287 of file games.h.

Member Function Documentation

◆ addDummy()

Game::NashGame & Game::NashGame::addDummy ( unsigned int  par = 0,
int  position = -1 
)

Add dummy variables in a NashGame object.

Add extra variables at the end of the problem. These are just zero columns that don't feature in the problem anywhere. They are of importance only where the NashGame gets converted into an LCP and gets parametrized. Typically, they appear in the upper level objective in such a case.

Definition at line 917 of file Games.cpp.

◆ addLeadCons()

Game::NashGame & Game::NashGame::addLeadCons ( const arma::vec &  a,
double  b 
)

Adds Leader constraint to a NashGame object.

In case common constraint to all followers is to be added (like a leader constraint in an MPEC), this function can be used. It adds a single constraint $ a^Tx \leq b$

Definition at line 967 of file Games.cpp.

◆ ComputeQPObjvals()

arma::vec Game::NashGame::ComputeQPObjvals ( const arma::vec &  x,
bool  checkFeas = false 
) const

Computes players' objective

Computes the objective value of each player in the Game::NashGame object.

Returns
An arma::vec with the objective values.

Definition at line 1127 of file Games.cpp.

◆ FormulateLCP()

const Game::NashGame & Game::NashGame::FormulateLCP ( arma::sp_mat &  M,
arma::vec &  q,
perps Compl,
bool  writeToFile = false,
std::string  M_name = "dat/LCP.txt",
std::string  q_name = "dat/q.txt" 
) const

Formulates the LCP corresponding to the Nash game.

Warning
Does not return the leader constraints. Use NashGame::RewriteLeadCons() to handle them

Computes the KKT conditions for each Player, calling QP_Param::KKT. Arranges them systematically to return M, q as an LCP $0\leq q \perp Mx+q \geq 0 $. The way the variables of the players get distributed is shown in the image below

FormulateLCP.png

Definition at line 723 of file Games.cpp.

◆ getDualLoc()

unsigned int Game::NashGame::getDualLoc ( unsigned int  i = 0) const
inline

Gets the location where the dual variables start.

Definition at line 352 of file games.h.

◆ getLeaderLoc()

unsigned int Game::NashGame::getLeaderLoc ( ) const
inline

Gets the positin where the Leader variables start.

Definition at line 350 of file games.h.

◆ getLeadRHS()

arma::vec Game::NashGame::getLeadRHS ( ) const
inline

Definition at line 362 of file games.h.

◆ getMCdualLoc()

unsigned int Game::NashGame::getMCdualLoc ( ) const
inline

Gets the positin where the Market-clearing dual variables start.

Definition at line 348 of file games.h.

◆ getMCLeadRHS()

arma::vec Game::NashGame::getMCLeadRHS ( ) const
inline

Definition at line 363 of file games.h.

◆ getNduals()

unsigned int Game::NashGame::getNduals ( ) const
inline

Gets the number of dual variables in the problem.

This is the count of number of dual variables and that is indeed the sum of the number dual variables each player has. And the number of dual variables for any player is equal to the number of linear constraints they have which is given by the number of rows in the player's Game::QP_Param::A

Definition at line 331 of file games.h.

◆ getNleaderVars()

unsigned int Game::NashGame::getNleaderVars ( ) const
inline

Gets the number of leader variables.

Leader variables are variables which do not have a complementarity relation with any equation.

Definition at line 329 of file games.h.

◆ getNprimals()

unsigned int Game::NashGame::getNprimals ( ) const
inline

Return the number of primal variables.

Definition at line 311 of file games.h.

◆ getNshadow()

unsigned int Game::NashGame::getNshadow ( ) const
inline

Gets the number of Market clearing Shadow prices.

Number of shadow price variables is equal to the number of Market clearing constraints.

Definition at line 323 of file games.h.

◆ getPrimalLoc()

unsigned int Game::NashGame::getPrimalLoc ( unsigned int  i = 0) const
inline

Gets the position of the primal variable of i th player.

Definition at line 344 of file games.h.

◆ isSolved()

bool Game::NashGame::isSolved ( const arma::vec &  sol,
unsigned int &  violPlayer,
arma::vec &  violSol,
double  tol = 1e-4 
) const

Checks if the Nash game is solved.

Checks if the Nash game is solved, if not provides a proof of deviation

Parameters
[in]sol- The vector of pure strategies for the Nash Game
[out]violPlayer- Index of the player with profitable deviation
[out]violSol- The pure strategy for that player - which gives a profitable deviation
[in]tol- If the additional profit is smaller than this, then it is not considered a profitable deviation.

Definition at line 1166 of file Games.cpp.

◆ load()

long int Game::NashGame::load ( std::string  filename,
long int  pos = 0 
)

Loads the Game::NashGame object stored in a file.

Loads the NashGame object stored in a file. Before calling this function, use the constructor NashGame::NashGame(GRBEnv *env) to initialize.

Loads the NashGame object stored in a file. Before calling this function, use the constructor NashGame::NashGame(GRBEnv *env) to initialize. Example usage:

int main()
{
GRBEnv env;
Game::NashGame N(&env);
N.load("./dat/Ndata.dat");
std::cout<<N<<'\n';
return 0;
}

Definition at line 627 of file Games.cpp.

◆ Respond()

unique_ptr< GRBModel > Game::NashGame::Respond ( unsigned int  player,
const arma::vec &  x,
bool  fullvec = true 
) const

Given the decision of other players, find the optimal response for player in position player.

Given the strategy of each player, returns a Gurobi Model that has the optimal strategy of the player at position player.

Returns
A unique_ptr to GRBModel
Parameters
playerPlayer whose optimal response is to be computed
xA vector of pure strategies (either for all players or all other players)
fullvecIs x strategy of all players? (including player player)

Definition at line 1051 of file Games.cpp.

◆ RespondSol()

double Game::NashGame::RespondSol ( arma::vec &  sol,
unsigned int  player,
const arma::vec &  x,
bool  fullvec = true 
) const

Returns the optimal objective value that is obtainable for the player player given the decision x of all other players.

Calls Game::NashGame::Respond and obtains the unique_ptr to GRBModel of best response by player player. Then solves the model and returns the appropriate objective value.

Returns
The optimal objective value for the player player.
Parameters
[out]solOptimal response
playerPlayer whose optimal response is to be computed
xA vector of pure strategies (either for all players or all other players)
fullvecIs x strategy of all players? (including player player)

Definition at line 1094 of file Games.cpp.

◆ RewriteLeadCons()

arma::sp_mat Game::NashGame::RewriteLeadCons ( ) const

Rewrites leader constraint adjusting for dual variables. Rewrites leader constraints given earlier with added empty columns and spaces corresponding to Market clearing duals and other equation duals.

This becomes important if the Lower level complementarity problem is passed to LCP with upper level constraints.

Definition at line 866 of file Games.cpp.

◆ save()

void Game::NashGame::save ( std::string  filename,
bool  erase = true 
) const

Saves the Game::NashGame object in a loadable file.

Definition at line 609 of file Games.cpp.

◆ set_positions()

void Game::NashGame::set_positions ( )
private

Stores the position of each players' primal and dual variables. Also allocates Leader's position appropriately. The ordering is according to the columns of

FormulateLCP.png

Definition at line 692 of file Games.cpp.

◆ write()

void Game::NashGame::write ( std::string  filename,
bool  append = true,
bool  KKT = false 
) const

Definition at line 990 of file Games.cpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const NashGame N 
)
friend

Definition at line 290 of file games.h.

Field Documentation

◆ dual_position

std::vector<unsigned int> Game::NashGame::dual_position
private

In the vector of variables of all players, which position do the DUAL variable corrresponding to this player starts.

Definition at line 265 of file games.h.

◆ env

GRBEnv* Game::NashGame::env = nullptr
private

Definition at line 251 of file games.h.

◆ Leader_position

unsigned int Game::NashGame::Leader_position
private

Manages the position of where the leader's variables start

Definition at line 269 of file games.h.

◆ LeaderConsRHS

arma::vec Game::NashGame::LeaderConsRHS
private

Upper level leader constraints RHS.

Definition at line 253 of file games.h.

◆ LeaderConstraints

arma::sp_mat Game::NashGame::LeaderConstraints
private

Upper level leader constraints LHS.

Definition at line 252 of file games.h.

◆ MarketClearing

arma::sp_mat Game::NashGame::MarketClearing
private

Market clearing constraints.

Definition at line 257 of file games.h.

◆ MC_dual_position

unsigned int Game::NashGame::MC_dual_position
private

Manages the position of Market clearing constraints' duals

Definition at line 267 of file games.h.

◆ MCRHS

arma::vec Game::NashGame::MCRHS
private

RHS to the Market Clearing constraints.

Definition at line 258 of file games.h.

◆ n_LeadVar

unsigned int Game::NashGame::n_LeadVar
private

Number of leader variables. These many variables will not have a matching complementary equation.

Definition at line 272 of file games.h.

◆ Nplayers

unsigned int Game::NashGame::Nplayers
private

Number of players in the Nash Game.

Definition at line 254 of file games.h.

◆ Players

std::vector<std::shared_ptr<QP_Param> > Game::NashGame::Players
private

The QP that each player solves.

Definition at line 256 of file games.h.

◆ primal_position

std::vector<unsigned int> Game::NashGame::primal_position
private

In the vector of variables of all players, which position does the variable corrresponding to this player starts.

Definition at line 262 of file games.h.


The documentation for this class was generated from the following files: