EPEC solve
Solving Equilibrium Problems with Equilibrium Constraints (EPECs)
models.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include "epecsolve.h"
10 #include <armadillo>
11 #include <gurobi_c++.h>
12 #include <iostream>
13 #include <memory>
14 
15 namespace Models {
16 typedef struct FollPar FollPar;
17 typedef struct DemPar DemPar;
18 typedef struct LeadPar LeadPar;
19 typedef struct LeadAllPar LeadAllPar;
20 
22 
24 struct FollPar {
25  std::vector<double> costs_quad =
26  {};
27  std::vector<double> costs_lin =
29  {};
30  std::vector<double> capacities =
32  {};
33  std::vector<double> emission_costs =
35  {};
36  std::vector<double> tax_caps = {};
38  std::vector<std::string> names = {};
39  FollPar(std::vector<double> costs_quad_ = {},
40  std::vector<double> costs_lin_ = {},
41  std::vector<double> capacities_ = {},
42  std::vector<double> emission_costs_ = {},
43  std::vector<double> tax_caps_ = {},
44  std::vector<std::string> names_ = {})
45  : costs_quad{costs_quad_}, costs_lin{costs_lin_}, capacities{capacities_},
46  emission_costs{emission_costs_}, tax_caps(tax_caps_), names{names_} {}
47 };
48 
50 struct DemPar {
51  double alpha = 100;
52  double beta = 2;
54  DemPar(double alpha = 100, double beta = 2) : alpha{alpha}, beta{beta} {};
56 };
57 
59 struct LeadPar {
60  double import_limit = -1;
61  double export_limit = -1;
63  double price_limit =
65  -1;
66 
67  Models::TaxType tax_type =
69  bool tax_revenue = false;
71  // LeadPar(double imp_lim = -1, double exp_lim = -1, double price_limit = -1,
73  // bool tax_revenue = false, Models::TaxType tax_type_ =
74  // Models::TaxType::StandardTax)
75  // : import_limit{imp_lim}, export_limit{exp_lim},
76  // price_limit{price_limit},tax_type{tax_type_}, tax_revenue{tax_revenue} {}
77  LeadPar(double imp_lim = -1, double exp_lim = -1, double price_limit = -1,
78  bool tax_revenue = false, unsigned int tax_type_ = 0)
79  : import_limit{imp_lim}, export_limit{exp_lim}, price_limit{price_limit},
80  tax_revenue{tax_revenue} {
81  switch (tax_type_) {
82  case 0:
84  break;
85  case 1:
86  tax_type = Models::TaxType::SingleTax;
87  break;
88  case 2:
89  tax_type = Models::TaxType::CarbonTax;
90  break;
91  default:
93  }
94  }
95 };
96 
98 struct LeadAllPar {
99  unsigned int n_followers;
100  std::string name;
101  Models::FollPar FollowerParam = {};
102  Models::DemPar DemandParam = {};
103  Models::LeadPar LeaderParam = {};
104  LeadAllPar(unsigned int n_foll, std::string name, Models::FollPar FP = {},
105  Models::DemPar DP = {}, Models::LeadPar LP = {})
106  : n_followers{n_foll}, name{name}, FollowerParam{FP}, DemandParam{DP},
107  LeaderParam{LP} {
108  // Nothing here
109  }
110 };
111 
113 struct EPECInstance {
114  std::vector<Models::LeadAllPar> Countries = {};
115  arma::sp_mat TransportationCosts = {};
116 
117  EPECInstance(std::string filename) {
118  this->load(filename);
119  }
120  EPECInstance(std::vector<Models::LeadAllPar> Countries_, arma::sp_mat Transp_)
121  : Countries{Countries_}, TransportationCosts{Transp_} {}
123 
124  void load(std::string filename);
126 
127  void save(std::string filename);
129 };
130 
131 enum class LeaderVars {
133  NetImport,
134  NetExport,
136  Caps,
137  Tax,
138  TaxQuad,
139  DualVar,
141  End
142 };
143 
144 std::ostream &operator<<(std::ostream &ost, const FollPar P);
145 
146 std::ostream &operator<<(std::ostream &ost, const DemPar P);
147 
148 std::ostream &operator<<(std::ostream &ost, const LeadPar P);
149 
150 std::ostream &operator<<(std::ostream &ost, const LeadAllPar P);
151 
152 std::ostream &operator<<(std::ostream &ost, const LeaderVars l);
153 
154 std::ostream &operator<<(std::ostream &ost, EPECInstance I);
155 
156 using LeadLocs = std::map<LeaderVars, unsigned int>;
157 
158 void increaseVal(LeadLocs &L, const LeaderVars start, const unsigned int val,
159  const bool startnext = true);
160 void decreaseVal(LeadLocs &L, const LeaderVars start, const unsigned int val,
161  const bool startnext = true);
162 
163 void init(LeadLocs &L);
164 
166 
167 class EPEC : public Game::EPEC {
168  // Mandatory virtuals
169 private:
170  void make_obj_leader(const unsigned int i, Game::QP_objective &QP_obj) final;
171  virtual void updateLocs() override;
172  virtual void prefinalize() override;
173  virtual void postfinalize() override{};
174  // override;
175 
176 public:
177  // Rest
178 private:
179  std::vector<LeadAllPar> AllLeadPars =
180  {};
181  std::vector<std::shared_ptr<Game::QP_Param>> MC_QP =
182  {};
183  arma::sp_mat TranspCosts =
185  {};
186  std::vector<unsigned int> nImportMarkets =
187  {};
188  std::vector<LeadLocs> Locations =
189  {};
190 
191  std::map<std::string, unsigned int> name2nos = {};
192  unsigned int taxVars = {0};
193  std::vector<arma::sp_mat>
194  LeadConses{};
195  std::vector<arma::vec> LeadRHSes{};
196 
197  bool dataCheck(const bool chkAllLeadPars = true,
198  const bool chkcountriesLL = true, const bool chkMC_QP = true,
199  const bool chkLeadConses = true,
200  const bool chkLeadRHSes = true,
201  const bool chknImportMarkets = true,
202  const bool chkLocations = true,
203  const bool chkLeaderLocations = true,
204  const bool chkLeadObjec = true) const;
205 
206  // Super low level
209  bool ParamValid(const LeadAllPar &Param) const;
210 
212  void make_LL_QP(const LeadAllPar &Params, const unsigned int follower,
213  Game::QP_Param *Foll, const LeadLocs &Loc) const noexcept;
214 
216  void make_LL_LeadCons(arma::sp_mat &LeadCons, arma::vec &LeadRHS,
217  const LeadAllPar &Param,
218  const Models::LeadLocs &Loc = {},
219  const unsigned int import_lim_cons = 1,
220  const unsigned int export_lim_cons = 1,
221  const unsigned int price_lim_cons = 1,
222  const unsigned int activeTaxCaps = 0) const noexcept;
223 
224  void add_Leaders_tradebalance_constraints(const unsigned int i);
225 
226  void make_MC_leader(const unsigned int i);
227 
228  void make_MC_cons(arma::sp_mat &MCLHS, arma::vec &MCRHS) const override;
229 
230  void WriteCountry(const unsigned int i, const std::string filename,
231  const arma::vec x, const bool append = true) const;
232 
233  void WriteFollower(const unsigned int i, const unsigned int j,
234  const std::string filename, const arma::vec x) const;
235 
236 public: // Attributes
237  bool quadraticTax = {false};
238 
240  // double timeLimit = {-1}; ///< Controls the timeLimit (s) for findNashEq
241 
242  EPEC() = delete;
243 
244  EPEC(GRBEnv *env, arma::sp_mat TranspCosts = {})
245  : Game::EPEC(env), TranspCosts{TranspCosts} {}
246 
247  // Unit tests
248  void testLCP(const unsigned int i);
249 
251  EPEC &addCountry(
253  LeadAllPar Params,
256  const unsigned int addnlLeadVars = 0);
257 
258  EPEC &addTranspCosts(const arma::sp_mat &costs);
259 
260  unsigned int
261  getPosition(const unsigned int countryCount,
262  const LeaderVars var = LeaderVars::FollowerStart) const;
263 
264  unsigned int
265  getPosition(const std::string countryCount,
266  const LeaderVars var = LeaderVars::FollowerStart) const;
267 
268  EPEC &unlock();
269 
270  std::unique_ptr<GRBModel> Respond(const std::string name,
271  const arma::vec &x) const;
272  // Data access methods
273  Game::NashGame *get_LowerLevelNash(const unsigned int i) const;
274 
275  Game::LCP *playCountry(std::vector<Game::LCP *> countries);
276 
277  // Writing model files
278  void write(const std::string filename, const unsigned int i,
279  bool append = true) const;
280 
281  void write(const std::string filename, bool append = true) const;
282 
283  void readSolutionJSON(const std::string filename);
284 
285  void writeSolutionJSON(std::string filename, const arma::vec x,
286  const arma::vec z) const;
287 
288  void writeSolution(const int writeLevel, std::string filename) const;
289 
291  const EPECInstance getInstance() const {
292  return EPECInstance(this->AllLeadPars, this->TranspCosts);
293  }
294 };
295 
296 } // namespace Models
297 
298 // Gurobi functions
299 std::string to_string(const GRBVar &var);
300 
301 std::string to_string(const GRBConstr &cons, const GRBModel &model);
302 
303 // ostream functions
304 namespace Models {
305 enum class prn { label, val };
306 
307 std::ostream &operator<<(std::ostream &ost, Models::prn l);
308 } // namespace Models
309 
311 /* Examples */
Stores the parameters of the follower in a country model.
Definition: models.h:24
Class to handle parameterized quadratic programs(QP)
Definition: games.h:146
unsigned int n_followers
Number of followers in the country.
Definition: models.h:99
Stores the parameters of the leader in a country model.
Definition: models.h:59
std::map< LeaderVars, unsigned int > LeadLocs
Definition: models.h:156
Stores the parameters of a country model.
Definition: models.h:98
void increaseVal(LeadLocs &L, const LeaderVars start, const unsigned int val, const bool startnext=true)
Definition: Models.cpp:1033
LeadAllPar(unsigned int n_foll, std::string name, Models::FollPar FP={}, Models::DemPar DP={}, Models::LeadPar LP={})
Definition: models.h:104
TaxType
Definition: models.h:21
FollPar(std::vector< double > costs_quad_={}, std::vector< double > costs_lin_={}, std::vector< double > capacities_={}, std::vector< double > emission_costs_={}, std::vector< double > tax_caps_={}, std::vector< std::string > names_={})
Definition: models.h:39
LeaderVars
Definition: models.h:131
struct DemPar DemPar
Definition: models.h:17
std::string name
Country Name.
Definition: models.h:100
void decreaseVal(LeadLocs &L, const LeaderVars start, const unsigned int val, const bool startnext=true)
Definition: Models.cpp:1047
Class to handle a Nash game between leaders of Stackelberg games.
Definition: games.h:475
Stores a single Instance.
Definition: models.h:113
Class to handle and solve linear complementarity problems.
Definition: lcptolp.h:41
EPEC(GRBEnv *env, arma::sp_mat TranspCosts={})
Definition: models.h:244
EPECInstance(std::vector< Models::LeadAllPar > Countries_, arma::sp_mat Transp_)
Constructor from instance objects.
Definition: models.h:120
std::string to_string(const GRBVar &var)
Definition: Models.cpp:1091
void init(LeadLocs &L)
Definition: Models.cpp:1061
LeaderVars operator+(Models::LeaderVars a, int b)
Definition: Models.cpp:1068
struct to handle the objective params of MP_Param/QP_Param
Definition: games.h:39
EPECInstance(std::string filename)
Constructor from instance file.
Definition: models.h:117
const EPECInstance getInstance() const
Get the current EPECInstance loaded.
Definition: models.h:291
Definition: models.h:15
LeadPar(double imp_lim=-1, double exp_lim=-1, double price_limit=-1, bool tax_revenue=false, unsigned int tax_type_=0)
Definition: models.h:77
Stores the parameters of the demand curve in a country model.
Definition: models.h:50
Class to model Nash-cournot games with each player playing a QP.
Definition: games.h:249
virtual void postfinalize() override
Empty function - optionally reimplementable in derived class.
Definition: models.h:173
std::ostream & operator<<(std::ostream &ost, const FollPar P)