EPEC solve
Solving Equilibrium Problems with Equilibrium Constraints (EPECs)
lcptolp.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include "epecsolve.h"
8 #include <armadillo>
9 #include <gurobi_c++.h>
10 #include <iostream>
11 #include <memory>
12 #include <set>
13 
14 // using namespace Game;
15 
16 namespace Game {
17 
18 arma::vec LPSolve(const arma::sp_mat &A, const arma::vec &b, const arma::vec &c,
19  int &status, bool Positivity = false);
20 
21 unsigned int ConvexHull(const std::vector<arma::sp_mat *> *Ai,
22  const std::vector<arma::vec *> *bi, arma::sp_mat &A,
23  arma::vec &b, const arma::sp_mat Acom = {},
24  const arma::vec bcom = {});
25 
26 void compConvSize(arma::sp_mat &A, const unsigned int nFinCons,
27  const unsigned int nFinVar,
28  const std::vector<arma::sp_mat *> *Ai,
29  const std::vector<arma::vec *> *bi, const arma::sp_mat &Acom,
30  const arma::vec &bcom);
41 class LCP {
42  using spmat_Vec = std::vector<std::unique_ptr<arma::sp_mat>>;
43  using vec_Vec = std::vector<std::unique_ptr<arma::vec>>;
44 
45 private:
46  // Essential data ironment for MIP/LP solves
47  GRBEnv *env;
48  arma::sp_mat M;
49  arma::vec q;
51  unsigned int LeadStart{1}, LeadEnd{0}, nLeader{0};
52  arma::sp_mat _A = {};
53  arma::vec _b = {};
54  // Temporary data
56  bool madeRlxdModel{false};
57  unsigned int nR, nC;
58 
59  int polyCounter{0};
60  unsigned int feasiblePolyhedra{0};
61  unsigned int sequentialPolyCounter{0};
65  std::set<unsigned long int> AllPolyhedra =
66  {};
67  std::set<unsigned long int> feasiblePoly =
68  {};
69  std::set<unsigned long int> knownInfeas =
70  {};
71  unsigned long int maxTheoreticalPoly{0};
72  std::unique_ptr<spmat_Vec>
73  Ai;
74  std::unique_ptr<vec_Vec>
75  bi;
76  GRBModel RlxdModel;
77 
79  bool errorCheck(bool throwErr = true) const;
80  void defConst(GRBEnv *env);
81  void makeRelaxed();
83  const unsigned int nCompl = this->Compl.size();
84  // 2^n - the number of polyhedra theoretically
85  this->maxTheoreticalPoly = static_cast<unsigned long int>(pow(2, nCompl));
88  }
89  /* Solving relaxations and restrictions */
90  std::unique_ptr<GRBModel> LCPasMIP(std::vector<unsigned int> FixEq = {},
91  std::vector<unsigned int> FixVar = {},
92  bool solve = false);
93  std::unique_ptr<GRBModel> LCPasMIP(std::vector<short int> Fixes, bool solve);
94  std::unique_ptr<GRBModel>
95  LCP_Polyhed_fixed(std::vector<unsigned int> FixEq = {},
96  std::vector<unsigned int> FixVar = {});
97  std::unique_ptr<GRBModel> LCP_Polyhed_fixed(arma::Col<int> FixEq,
98  arma::Col<int> FixVar);
99  template <class T> inline bool isZero(const T val) const {
100  return (val >= -eps && val <= eps);
101  }
102  inline std::vector<short int> solEncode(GRBModel *model) const;
103  std::vector<short int> solEncode(const arma::vec &x) const;
104  std::vector<short int> solEncode(const arma::vec &z,
105  const arma::vec &x) const;
106  bool FixToPoly(const std::vector<short int> Fix, bool checkFeas = false,
107  bool custom = false, spmat_Vec *custAi = {},
108  vec_Vec *custbi = {});
109  LCP &FixToPolies(const std::vector<short int> Fix, bool checkFeas = false,
110  bool custom = false, spmat_Vec *custAi = {},
111  vec_Vec *custbi = {});
112  unsigned long int getNextPoly(Game::EPECAddPolyMethod method);
113 
114 public:
115  // Fudgible data
116  long double bigM{1e7};
117  long double eps{
118  1e-6};
119  long double eps_int{1e-8};
120  bool useIndicators{
122  true};
123  long int addPolyMethodSeed = {
125  -1};
126 
129  LCP() = delete;
131 
132  LCP(GRBEnv *e)
133  : env{e},
134  RlxdModel(*e){};
135 
136  LCP(GRBEnv *env, arma::sp_mat M, arma::vec q, unsigned int LeadStart,
137  unsigned LeadEnd, arma::sp_mat A = {},
138  arma::vec b = {}); // Constructor with M,q,leader posn
139  LCP(GRBEnv *env, arma::sp_mat M, arma::vec q, perps Compl,
140  arma::sp_mat A = {},
141  arma::vec b = {}); // Constructor with M, q, compl pairs
142  LCP(GRBEnv *env, const Game::NashGame &N);
143  // LCP(const LCP &) = default;
144  // LCP &operator=(const LCP &) = default;
145 
147  ~LCP();
148 
150  inline arma::sp_mat getM() { return this->M; }
151  inline arma::sp_mat *getMstar() {
152  return &(this->M);
153  }
154  inline arma::vec getq() { return this->q; }
155  inline arma::vec *getqstar() {
156  return &(this->q);
157  }
158  inline unsigned int getLStart() {
159  return LeadStart;
160  }
161  inline unsigned int getLEnd() {
162  return LeadEnd;
163  }
164  inline perps getCompl() {
165  return this->Compl;
166  }
167  void print(std::string end = "\n");
168  inline unsigned int getNcol() { return this->M.n_cols; };
169 
170  inline unsigned int getNrow() { return this->M.n_rows; };
171 
172  bool extractSols(GRBModel *model, arma::vec &z, arma::vec &x,
173  bool extractZ = false) const;
174 
175  /* Getting single point solutions */
176  std::unique_ptr<GRBModel> LCPasQP(bool solve = false);
177  std::unique_ptr<GRBModel> LCPasMIP(bool solve = false);
178  std::unique_ptr<GRBModel> MPECasMILP(const arma::sp_mat &C,
179  const arma::vec &c,
180  const arma::vec &x_minus_i,
181  bool solve = false);
182  std::unique_ptr<GRBModel>
183  MPECasMIQP(const arma::sp_mat &Q, const arma::sp_mat &C, const arma::vec &c,
184  const arma::vec &x_minus_i, bool solve = false);
185  /* Convex hull computation */
186  unsigned int ConvexHull(arma::sp_mat &A, arma::vec &b);
187  unsigned int conv_Npoly() const;
188  unsigned int conv_PolyPosition(const unsigned long int i) const;
189  unsigned int conv_PolyWt(const unsigned long int i) const;
190 
191  std::set<unsigned long int> getAllPolyhedra() const {
192  return this->AllPolyhedra;
193  };
194  unsigned long int getNumTheoreticalPoly() const noexcept {
195  return this->maxTheoreticalPoly;
196  }
197 
198  LCP &makeQP(Game::QP_objective &QP_obj, Game::QP_Param &QP);
199 
200  std::set<std::vector<short int>>
201  addAPoly(unsigned long int nPoly = 1,
203  std::set<std::vector<short int>> Polys = {});
204  bool addThePoly(const unsigned long int &decimalEncoding);
205  bool checkPolyFeas(const unsigned long int &decimalEncoding);
206  bool checkPolyFeas(const std::vector<short int> &Fix);
207  void clearPolyhedra() {
208  this->Ai->clear();
209  this->bi->clear();
210  this->AllPolyhedra.clear();
211  }
212  LCP &addPolyFromX(const arma::vec &x, bool &ret);
213  LCP &EnumerateAll(bool solveLP = true);
214  std::string feas_detail_str() const;
215  unsigned int getFeasiblePolyhedra() const { return this->feasiblePolyhedra; }
216  void write(std::string filename, bool append = true) const;
217  void save(std::string filename, bool erase = true) const;
218  long int load(std::string filename, long int pos = 0);
219 };
220 } // namespace Game
221 
222 /* Example for LCP */
std::set< unsigned long int > AllPolyhedra
Decimal encoding of polyhedra that have been enumerated.
Definition: lcptolp.h:65
std::set< unsigned long int > knownInfeas
Decimal encoding of polyhedra known to be infeasible.
Definition: lcptolp.h:69
unsigned int getFeasiblePolyhedra() const
Definition: lcptolp.h:215
void clearPolyhedra()
Definition: lcptolp.h:207
unsigned int conv_PolyWt(const unsigned long int i) const
Definition: LCPtoLP.cpp:1543
Class to handle parameterized quadratic programs(QP)
Definition: games.h:146
unsigned int getLStart()
Read-only access to LCP::LeadStart.
Definition: lcptolp.h:158
std::set< unsigned long int > feasiblePoly
Decimal encoding of polyhedra that have been enumerated.
Definition: lcptolp.h:67
LCP & EnumerateAll(bool solveLP=true)
Brute force computation of LCP feasible region.
Definition: LCPtoLP.cpp:1237
bool checkPolyFeas(const unsigned long int &decimalEncoding)
Definition: LCPtoLP.cpp:990
long double eps_int
Definition: lcptolp.h:119
GRBModel RlxdModel
Definition: lcptolp.h:76
bool FixToPoly(const std::vector< short int > Fix, bool checkFeas=false, bool custom=false, spmat_Vec *custAi={}, vec_Vec *custbi={})
Computes the equation of the feasibility polyhedron corresponding to the given Fix.
Definition: LCPtoLP.cpp:900
void defConst(GRBEnv *env)
Assign default values to LCP attributes.
Definition: LCPtoLP.cpp:58
unsigned int LeadStart
Definition: lcptolp.h:51
arma::vec LPSolve(const arma::sp_mat &A, const arma::vec &b, const arma::vec &c, int &status, bool Positivity=false)
Definition: LCPtoLP.cpp:721
unsigned long int maxTheoreticalPoly
Definition: lcptolp.h:71
LCP & makeQP(Game::QP_objective &QP_obj, Game::QP_Param &QP)
Definition: LCPtoLP.cpp:1265
void write(std::string filename, bool append=true) const
Definition: LCPtoLP.cpp:1431
arma::sp_mat M
M in that defines the LCP.
Definition: lcptolp.h:48
void compConvSize(arma::sp_mat &A, const unsigned int nFinCons, const unsigned int nFinVar, const std::vector< arma::sp_mat *> *Ai, const std::vector< arma::vec *> *bi, const arma::sp_mat &Acom, const arma::vec &bcom)
bool errorCheck(bool throwErr=true) const
Definition: LCPtoLP.cpp:455
unsigned int getNrow()
Definition: lcptolp.h:170
unsigned long int getNumTheoreticalPoly() const noexcept
Definition: lcptolp.h:194
long double eps
The threshold for optimality and feasability tollerances.
Definition: lcptolp.h:117
std::unique_ptr< GRBModel > MPECasMILP(const arma::sp_mat &C, const arma::vec &c, const arma::vec &x_minus_i, bool solve=false)
Helps solving an LCP as an MIP.
Definition: LCPtoLP.cpp:1357
std::vector< std::pair< unsigned int, unsigned int > > perps
Definition: epecsolve.h:15
std::vector< std::unique_ptr< arma::sp_mat > > spmat_Vec
Definition: lcptolp.h:42
std::set< unsigned long int > getAllPolyhedra() const
Definition: lcptolp.h:191
arma::sp_mat * getMstar()
Reference access to LCP::M.
Definition: lcptolp.h:151
unsigned int LeadEnd
Definition: lcptolp.h:51
arma::vec q
q in that defines the LCP
Definition: lcptolp.h:49
arma::vec _b
Definition: lcptolp.h:53
unsigned int getNcol()
Definition: lcptolp.h:168
unsigned int ConvexHull(const std::vector< arma::sp_mat *> *Ai, const std::vector< arma::vec *> *bi, arma::sp_mat &A, arma::vec &b, const arma::sp_mat Acom={}, const arma::vec bcom={})
int reverseSequentialPolyCounter
Definition: lcptolp.h:62
arma::vec getq()
Read-only access to LCP::q.
Definition: lcptolp.h:154
GRBEnv * env
Gurobi env.
Definition: lcptolp.h:47
std::unique_ptr< GRBModel > LCPasQP(bool solve=false)
Solves the LCP as a QP using Gurobi.
Definition: LCPtoLP.cpp:1295
long int addPolyMethodSeed
Definition: lcptolp.h:124
std::unique_ptr< vec_Vec > bi
Vector to contain the RHS of inner approx polyhedra.
Definition: lcptolp.h:75
int polyCounter
Definition: lcptolp.h:59
bool madeRlxdModel
Keep track if LCP::RlxdModel is made.
Definition: lcptolp.h:56
perps Compl
Compl stores data in <Eqn, Var> form.
Definition: lcptolp.h:50
bool isZero(const T val) const
Definition: lcptolp.h:99
Class to handle and solve linear complementarity problems.
Definition: lcptolp.h:41
unsigned int conv_PolyPosition(const unsigned long int i) const
Definition: LCPtoLP.cpp:1527
Definition: epecsolve.h:26
unsigned int nR
Definition: lcptolp.h:57
std::unique_ptr< GRBModel > LCPasMIP(std::vector< unsigned int > FixEq={}, std::vector< unsigned int > FixVar={}, bool solve=false)
~LCP()
Destructor of LCP.
Definition: LCPtoLP.cpp:174
bool useIndicators
constraints. BigM formulation otherwise
Definition: lcptolp.h:121
std::string feas_detail_str() const
Definition: LCPtoLP.cpp:1506
bool extractSols(GRBModel *model, arma::vec &z, arma::vec &x, bool extractZ=false) const
Extracts variable and equation values from a solved Gurobi model for LCP.
Definition: LCPtoLP.cpp:771
std::vector< short int > solEncode(GRBModel *model) const
Given a Gurobi model, extracts variable values and equation values, encodes it in 0/+1/-1 format and ...
Definition: LCPtoLP.cpp:845
Adds polyhedra by selecting them in order.
perps getCompl()
Read-only access to LCP::Compl.
Definition: lcptolp.h:164
std::unique_ptr< spmat_Vec > Ai
Vector to contain the LHS of inner approx polyhedra.
Definition: lcptolp.h:73
LCP()=delete
Class has no default constructors.
LCP & FixToPolies(const std::vector< short int > Fix, bool checkFeas=false, bool custom=false, spmat_Vec *custAi={}, vec_Vec *custbi={})
Computes the equation of the feasibility polyhedron corresponding to the given Fix.
Definition: LCPtoLP.cpp:1061
long int load(std::string filename, long int pos=0)
Definition: LCPtoLP.cpp:1463
arma::sp_mat getM()
Read-only access to LCP::M.
Definition: lcptolp.h:150
unsigned int conv_Npoly() const
Definition: LCPtoLP.cpp:1518
unsigned int sequentialPolyCounter
Definition: lcptolp.h:61
unsigned int getLEnd()
Read-only access to LCP::LeadEnd.
Definition: lcptolp.h:161
void print(std::string end="\)
Print a summary of the LCP.
Definition: LCPtoLP.cpp:478
EPECAddPolyMethod
Definition: epecsolve.h:34
unsigned int ConvexHull(arma::sp_mat &A, arma::vec &b)
Definition: LCPtoLP.cpp:484
struct to handle the objective params of MP_Param/QP_Param
Definition: games.h:39
void makeRelaxed()
Makes a Gurobi object that relaxes complementarity constraints in an LCP.
Definition: LCPtoLP.cpp:180
arma::vec * getqstar()
Reference access to LCP::q.
Definition: lcptolp.h:155
bool addThePoly(const unsigned long int &decimalEncoding)
Definition: LCPtoLP.cpp:1224
std::unique_ptr< GRBModel > MPECasMIQP(const arma::sp_mat &Q, const arma::sp_mat &C, const arma::vec &c, const arma::vec &x_minus_i, bool solve=false)
Helps solving an LCP as an MIQPs.
Definition: LCPtoLP.cpp:1399
std::vector< std::unique_ptr< arma::vec > > vec_Vec
Definition: lcptolp.h:43
long double bigM
bigM used to rewrite the LCP as MIP
Definition: lcptolp.h:116
unsigned int nLeader
Definition: lcptolp.h:51
unsigned long int getNextPoly(Game::EPECAddPolyMethod method)
Definition: LCPtoLP.cpp:1109
std::unique_ptr< GRBModel > LCP_Polyhed_fixed(std::vector< unsigned int > FixEq={}, std::vector< unsigned int > FixVar={})
void save(std::string filename, bool erase=true) const
Definition: LCPtoLP.cpp:1449
std::set< std::vector< short int > > addAPoly(unsigned long int nPoly=1, Game::EPECAddPolyMethod method=Game::EPECAddPolyMethod::sequential, std::set< std::vector< short int >> Polys={})
Definition: LCPtoLP.cpp:1169
Class to model Nash-cournot games with each player playing a QP.
Definition: games.h:249
unsigned int feasiblePolyhedra
Definition: lcptolp.h:60
arma::sp_mat _A
Definition: lcptolp.h:52
void initializeNotProcessed()
Definition: lcptolp.h:82
LCP & addPolyFromX(const arma::vec &x, bool &ret)
Definition: LCPtoLP.cpp:859
unsigned int nC
Definition: lcptolp.h:57
LCP(GRBEnv *e)
Definition: lcptolp.h:132