9 #include <gurobi_c++.h> 18 std::ostream &operator<<(std::ostream &ost, std::vector<T> v) {
25 template <
class T,
class S>
26 std::ostream &operator<<(std::ostream &ost, std::pair<T, S> p) {
27 ost <<
"<" << p.first <<
", " << p.second <<
">";
32 bool isZero(arma::mat M,
double tol = 1e-6) noexcept;
34 bool isZero(arma::sp_mat M,
double tol = 1e-6) noexcept;
55 arma::sp_mat
Q, A, B,
C;
58 unsigned int Nx,
Ny, Ncons;
60 bool dataCheck(
bool forcesymm =
true)
const;
63 return this->dataCheck();
71 void bound(
double bigM,
unsigned int primals);
74 arma::sp_mat
getQ()
const {
77 arma::sp_mat
getC()
const {
80 arma::sp_mat
getA()
const {
83 arma::sp_mat
getB()
const {
125 virtual MP_Param &
set(
const arma::sp_mat &
Q,
const arma::sp_mat &
C,
126 const arma::sp_mat &A,
const arma::sp_mat &B,
129 virtual MP_Param &
set(arma::sp_mat &&
Q, arma::sp_mat &&
C, arma::sp_mat &&A,
130 arma::sp_mat &&B, arma::vec &&
c,
136 virtual MP_Param &addDummy(
unsigned int pars,
unsigned int vars = 0,
139 void write(std::string filename,
bool append =
true)
const;
142 bool checkObj =
true,
bool checkCons =
true);
170 : env{env}, QuadModel{(*env)}, made_yQy{
false} {
175 QP_Param(arma::sp_mat
Q, arma::sp_mat
C, arma::sp_mat A, arma::sp_mat B,
176 arma::vec
c, arma::vec b, GRBEnv *env =
nullptr)
177 : env{env}, QuadModel{(*env)}, made_yQy{
false} {
178 this->
set(
Q,
C, A, B,
c, b);
180 if (!this->dataCheck())
182 "Error in QP_Param::QP_Param: Invalid data for constructor");
187 :
MP_Param(Qu), env{Qu.
env}, QuadModel{Qu.QuadModel}, made_yQy{
193 QP_Param &
set(
const arma::sp_mat &
Q,
const arma::sp_mat &
C,
194 const arma::sp_mat &A,
const arma::sp_mat &B,
196 const arma::vec &b)
final;
197 QP_Param &
set(arma::sp_mat &&
Q, arma::sp_mat &&
C, arma::sp_mat &&A,
198 arma::sp_mat &&B, arma::vec &&
c,
199 arma::vec &&b)
final;
207 unsigned int KKT(arma::sp_mat &M, arma::sp_mat &N, arma::vec &q)
const;
209 std::unique_ptr<GRBModel> solveFixed(arma::vec x);
213 double computeObjective(
const arma::vec &y,
const arma::vec &x,
214 bool checkFeas =
true,
double tol = 1e-6)
const;
220 b1 = (this->Nx + this->Ny) == (P.getNx() + P.getNy());
221 b2 = this->Nx >= P.getNy();
222 b3 = this->Ny <= P.getNx();
223 return b1 && b2 && b3;
225 QP_Param &addDummy(
unsigned int pars,
unsigned int vars = 0,
226 int position = -1)
override;
229 void write(std::string filename,
bool append)
const;
231 void save(std::string filename,
bool erase =
true)
const;
233 long int load(std::string filename,
long int pos = 0);
251 GRBEnv *env =
nullptr;
255 std::vector<std::shared_ptr<QP_Param>>
274 void set_positions();
281 explicit NashGame(GRBEnv *e, std::vector<std::shared_ptr<QP_Param>> Players,
282 arma::sp_mat MC, arma::vec MCRHS,
283 unsigned int n_LeadVar = 0, arma::sp_mat LeadA = {},
284 arma::vec LeadRHS = {});
290 friend std::ostream &
operator<<(std::ostream &os,
const NashGame &N) {
292 os <<
"--------------------------------------------------------------------" 295 os <<
"Nash Game with " << N.
Nplayers <<
" players" <<
'\n';
296 os <<
"--------------------------------------------------------------------" 299 os <<
"Number of primal variables:\t\t\t " << N.
getNprimals() <<
'\n';
300 os <<
"Number of dual variables:\t\t\t " << N.
getNduals() <<
'\n';
301 os <<
"Number of shadow price dual variables:\t\t " << N.
getNshadow()
303 os <<
"Number of leader variables:\t\t\t " << N.
getNleaderVars() <<
'\n';
304 os <<
"--------------------------------------------------------------------" 316 return this->primal_position.back();
323 inline unsigned int getNshadow()
const {
return this->MCRHS.n_rows; }
339 return this->dual_position.back() - this->dual_position.front() + 0;
345 return primal_position.at(i);
353 return dual_position.at(i);
357 const NashGame &FormulateLCP(arma::sp_mat &M, arma::vec &q,
perps &Compl,
358 bool writeToFile =
false,
359 std::string M_name =
"dat/LCP.txt",
360 std::string q_name =
"dat/q.txt")
const;
361 arma::sp_mat RewriteLeadCons()
const;
362 inline arma::vec
getLeadRHS()
const {
return this->LeaderConsRHS; }
364 return arma::join_cols(arma::join_cols(this->LeaderConsRHS, this->MCRHS),
369 std::unique_ptr<GRBModel> Respond(
unsigned int player,
const arma::vec &x,
370 bool fullvec =
true)
const;
371 double RespondSol(arma::vec &sol,
unsigned int player,
const arma::vec &x,
372 bool fullvec =
true)
const;
373 arma::vec ComputeQPObjvals(
const arma::vec &x,
bool checkFeas =
false)
const;
374 bool isSolved(
const arma::vec &sol,
unsigned int &violPlayer,
375 arma::vec &violSol,
double tol = 1e-4)
const;
377 NashGame &addDummy(
unsigned int par = 0,
int position = -1);
378 NashGame &addLeadCons(
const arma::vec &a,
double b);
380 void write(std::string filename,
bool append =
true,
bool KKT =
false)
const;
382 void save(std::string filename,
bool erase =
true)
const;
384 long int load(std::string filename,
long int pos = 0);
431 bool boundPrimals{
false};
432 double boundBigM{1e5};
434 long int addPolyMethodSeed{
436 bool indicators{
true};
441 unsigned int threads{
443 unsigned int aggressiveness{
454 int numIteration = {-1};
455 int numConstraints = {-1};
457 int numNonZero = {-1};
458 int lostIntermediateEq = {0};
460 bool numericalIssuesEncountered = {
463 std::vector<unsigned int> feasiblePolyhedra =
466 double wallClockTime = {0};
477 std::vector<unsigned int> SizesWithoutHull{};
480 std::unique_ptr<Game::LCP> lcp;
482 std::unique_ptr<GRBModel>
484 std::unique_ptr<GRBModel>
486 unsigned int nVarinEPEC{0};
490 unsigned int nCountr{0};
493 std::vector<std::shared_ptr<Game::NashGame>> countries_LL{};
494 std::vector<std::unique_ptr<Game::LCP>> countries_LCP{};
496 std::vector<std::shared_ptr<Game::QP_Param>>
498 std::vector<std::shared_ptr<Game::QP_objective>>
500 std::vector<std::shared_ptr<Game::QP_objective>>
501 LeadObjec_ConvexHull{};
506 std::vector<unsigned int> LeaderLocations{};
507 std::vector<const unsigned int *> LocEnds{};
512 std::vector<unsigned int> convexHullVariables{};
513 unsigned int n_MCVar{0};
516 bool finalized{
false};
518 std::chrono::high_resolution_clock::time_point
initTime;
522 bool warmstart(
const arma::vec x);
526 add_Dummy_Lead(
const unsigned int i);
527 void make_country_QP(
const unsigned int i);
528 void make_country_QP();
529 void make_country_LCP();
531 void iterativeNash();
532 void fullEnumerationNash();
533 void combinatorial_pure_NE(
534 const std::vector<long int> combination,
535 const std::vector<std::set<unsigned long int>> &excludeList);
537 const std::vector<long int> combination = {},
538 const std::vector<std::set<unsigned long int>> &excludeList = {});
539 void make_pure_LCP(
bool indicators =
false);
540 void computeLeaderLocations(
const unsigned int addSpaceForMC = 0);
542 bool getAllDevns(std::vector<arma::vec> &devns,
const arma::vec &guessSol,
543 const std::vector<arma::vec> &prevDev = {})
const;
544 unsigned int addDeviatedPolyhedron(
const std::vector<arma::vec> &devns,
545 bool &infeasCheck)
const;
546 void get_x_minus_i(
const arma::vec &x,
const unsigned int &i,
547 arma::vec &solOther)
const;
548 bool computeNashEq(
bool pureNE =
false,
double localTimeLimit = -1.0,
550 bool addRandomPoly2All(
unsigned int aggressiveLevel = 1,
551 bool stopOnSingleInfeasibility =
false);
558 virtual void make_obj_leader(
const unsigned int i,
562 virtual void prefinalize();
563 virtual void postfinalize();
586 std::unique_ptr<GRBModel> Respond(
const unsigned int i,
587 const arma::vec &x)
const;
588 double RespondSol(arma::vec &sol,
unsigned int player,
const arma::vec &x,
589 const arma::vec &prevDev)
const;
590 bool isSolved(
unsigned int *countryNumber, arma::vec *ProfDevn,
591 double tol = 51e-4)
const;
593 bool isSolved(
double tol = 51e-4)
const;
595 const arma::vec
getx()
const {
return this->sol_x; }
596 void reset() { this->sol_x.ones(); }
597 const arma::vec
getz()
const {
return this->sol_z; }
602 return this->Stats.AlgorithmParam.algorithm;
606 return this->Stats.AlgorithmParam.recoverStrategy;
609 this->Stats.AlgorithmParam.aggressiveness = a;
612 return this->Stats.AlgorithmParam.aggressiveness;
615 this->Stats.AlgorithmParam.threads = t;
616 this->env->set(GRB_IntParam_Threads, t);
619 return this->Stats.AlgorithmParam.threads;
622 this->Stats.AlgorithmParam.addPolyMethodSeed = t;
625 return this->Stats.AlgorithmParam.addPolyMethodSeed;
627 void setIndicators(
bool val) { this->Stats.AlgorithmParam.indicators = val; }
628 bool getIndicators()
const {
return this->Stats.AlgorithmParam.indicators; }
629 void setPureNE(
bool val) { this->Stats.AlgorithmParam.pureNE = val; }
630 bool getPureNE()
const {
return this->Stats.AlgorithmParam.pureNE; }
632 this->Stats.AlgorithmParam.boundPrimals = val;
635 return this->Stats.AlgorithmParam.boundPrimals;
637 void setBoundBigM(
double val) { this->Stats.AlgorithmParam.boundBigM = val; }
638 double getBoundBigM()
const {
return this->Stats.AlgorithmParam.boundBigM; }
639 void setTimeLimit(
double val) { this->Stats.AlgorithmParam.timeLimit = val; }
640 double getTimeLimit()
const {
return this->Stats.AlgorithmParam.timeLimit; }
642 this->Stats.AlgorithmParam.addPolyMethod = add;
645 return this->Stats.AlgorithmParam.addPolyMethod;
652 return this->countries_LL.size();
654 unsigned int getPosition_LeadFoll(
const unsigned int i,
655 const unsigned int j)
const;
656 unsigned int getPosition_LeadLead(
const unsigned int i,
657 const unsigned int j)
const;
658 unsigned int getPosition_LeadFollPoly(
const unsigned int i,
659 const unsigned int j,
660 const unsigned int k)
const;
661 unsigned int getPosition_LeadLeadPoly(
const unsigned int i,
662 const unsigned int j,
663 const unsigned int k)
const;
664 unsigned int getNPoly_Lead(
const unsigned int i)
const;
665 unsigned int getPosition_Probab(
const unsigned int i,
666 const unsigned int k)
const;
669 double getVal_LeadFoll(
const unsigned int i,
const unsigned int j)
const;
670 double getVal_LeadLead(
const unsigned int i,
const unsigned int j)
const;
671 double getVal_LeadFollPoly(
const unsigned int i,
const unsigned int j,
672 const unsigned int k,
673 const double tol = 1e-5)
const;
674 double getVal_LeadLeadPoly(
const unsigned int i,
const unsigned int j,
675 const unsigned int k,
676 const double tol = 1e-5)
const;
677 double getVal_Probab(
const unsigned int i,
const unsigned int k)
const;
681 bool isPureStrategy(
const unsigned int i,
const double tol = 1e-5)
const;
682 bool isPureStrategy(
const double tol = 1e-5)
const;
683 std::vector<unsigned int> mixedStratPoly(
const unsigned int i,
684 const double tol = 1e-5)
const;
693 const GRBModel &
getLcpModel()
const {
return *this->lcpmodel.get(); }
697 this->lcpmodel->write(filename);
unsigned int getnVarinEPEC() const noexcept
std::ostream & operator<<(std::ostream &os, const QP_Param &Q)
arma::sp_mat MarketClearing
Market clearing constraints.
arma::vec sol_z
Solution equation values.
const GRBModel & getLcpModel() const
const arma::vec getz() const
Class to handle parameterized quadratic programs(QP)
void setAggressiveness(unsigned int a)
std::chrono::high_resolution_clock::time_point initTime
MP_Param & setA(const arma::sp_mat &A)
Set the private variable A.
const LCP & getLcpDescr() const
struct Game::QP_constraints QP_constraints
struct to handle the constraint params of MP_Param/QP_Param
arma::sp_mat getQ() const
Read-only access to the private variable Q.
arma::vec getc() const
Read-only access to the private variable c.
std::vector< std::shared_ptr< QP_Param > > Players
The QP that each player solves.
void setBoundBigM(double val)
bool getBoundPrimals() const
unsigned int getPrimalLoc(unsigned int i=0) const
Gets the position of the primal variable of i th player.
class to handle parameterized mathematical programs(MP)
const arma::vec getx() const
string to_string(const Game::EPECAddPolyMethod add)
struct to handle the constraint params of MP_Param/QP_Param
NashGame(GRBEnv *e) noexcept
To be used only when NashGame is being loaded from a file.
void setIndicators(bool val)
struct Game::QP_objective QP_objective
struct to handle the objective params of MP_Param/QP_Param
bool getIndicators() const
unsigned int getNcountries() const noexcept
std::vector< unsigned int > dual_position
std::vector< std::pair< unsigned int, unsigned int > > perps
void setNumThreads(unsigned int t)
Stores statistics for a (solved) EPEC instance.
virtual void make_MC_cons(arma::sp_mat &MC, arma::vec &RHS) const
unsigned int MC_dual_position
void setTimeLimit(double val)
unsigned int getNduals() const
Gets the number of dual variables in the problem.
Game::EPECalgorithm getAlgorithm() const
arma::vec MCRHS
RHS to the Market Clearing constraints.
QP_Param(arma::sp_mat Q, arma::sp_mat C, arma::sp_mat A, arma::sp_mat B, arma::vec c, arma::vec b, GRBEnv *env=nullptr)
Set data at construct time.
bool isZero(arma::mat M, double tol=1e-6) noexcept
unsigned int getNx() const
Read-only access to the private variable Nx.
void setAddPolyMethod(Game::EPECAddPolyMethod add)
Class to handle a Nash game between leaders of Stackelberg games.
Class to handle and solve linear complementarity problems.
Solution found for the instance.
arma::sp_mat getA() const
Read-only access to the private variable A.
unsigned int getAggressiveness() const
double getBoundBigM() const
Game::EPECRecoverStrategy getRecoverStrategy() const
Adds polyhedra by selecting them in order.
MP_Param & setQ(const arma::sp_mat &Q)
Set the private variable Q.
unsigned int getNumThreads() const
arma::sp_mat getC() const
Read-only access to the private variable C.
void writeLcpModel(std::string filename) const
Instance proved to be infeasible.
Not started to solve the problem.
MP_Param & setb(const arma::vec &b)
Set the private variable b.
unsigned int getAddPolyMethodSeed() const
QP_Param(GRBEnv *env=nullptr)
Initialize only the size. Everything else is empty (can be updated later)
unsigned int getNleaderVars() const
Gets the number of leader variables.
arma::vec LeaderConsRHS
Upper level leader constraints RHS.
arma::sp_mat getB() const
Read-only access to the private variable B.
double getTimeLimit() const
Time limit reached, nash equilibrium not found.
struct to handle the objective params of MP_Param/QP_Param
arma::vec getMCLeadRHS() const
void setBoundPrimals(bool val)
Add random polyhedra in each iteration.
bool is_Playable(const QP_Param &P) const
void setAddPolyMethodSeed(unsigned int t)
unsigned int Leader_position
friend std::ostream & operator<<(std::ostream &os, const NashGame &N)
arma::vec getLeadRHS() const
arma::sp_mat LeaderConstraints
Upper level leader constraints LHS.
std::vector< unsigned int > primal_position
unsigned int getDualLoc(unsigned int i=0) const
Gets the location where the dual variables start.
virtual bool finalize()
Finalize the MP_Param object.
std::unique_ptr< GRBModel > lcpmodel
A Gurobi mode object of the LCP form of EPEC.
std::unique_ptr< Game::NashGame > nashgame
The EPEC nash game.
unsigned int getNy() const
Read-only access to the private variable Ny.
void print(const perps &C) noexcept
unsigned int Nplayers
Number of players in the Nash Game.
MP_Param & setC(const arma::sp_mat &C)
Set the private variable C.
QP_Param(const QP_Param &Qu)
Copy constructor.
Game::EPECAddPolyMethod getAddPolyMethod() const
Stores the configuration for EPEC algorithms.
Class to model Nash-cournot games with each player playing a QP.
std::unique_ptr< GRBModel > lcpmodel_base
arma::vec getb() const
Read-only access to the private variable b.
unsigned int getNshadow() const
Gets the number of Market clearing Shadow prices.
MP_Param & setB(const arma::sp_mat &B)
Set the private variable B.
unsigned int getLeaderLoc() const
Gets the positin where the Leader variables start.
unsigned int getNprimals() const
Return the number of primal variables.
unsigned int getMCdualLoc() const
Gets the positin where the Market-clearing dual variables start.
bool operator==(std::vector< short int > Fix1, std::vector< short int > Fix2)
const EPECStatistics getStatistics() const
Get the EPECStatistics object for the current instance.
MP_Param & setc(const arma::vec &c)
Set the private variable c.