EPEC solve
Solving Equilibrium Problems with Equilibrium Constraints (EPECs)
example.cpp
Go to the documentation of this file.
1 #include "epecsolve.h"
2 #include <boost/log/core.hpp>
3 #include <boost/log/expressions.hpp>
4 #include <boost/log/trivial.hpp>
5 #include <gurobi_c++.h>
6 
7 class My_EPEC_Prob : public EPEC {
8 public:
9  My_EPEC_Prob(GRBEnv *e) : EPEC(e) {}
10  void addLeader(std::shared_ptr<Game::NashGame> N, const unsigned int i) {
11  this->countries_LL.push_back(N);
12  ends[i] = N->getNprimals() + N->getNleaderVars();
13  this->LocEnds.push_back(&ends[i]);
14  }
15 
16 private:
17  unsigned int ends[2];
18  void updateLocs() override {
19  ends[0] = this->convexHullVariables.at(0) + 3;
20  ends[1] = this->convexHullVariables.at(1) + 3;
21  }
22  void make_obj_leader(const unsigned int i,
23  Game::QP_objective &QP_obj) override {
24  QP_obj.Q.zeros(3, 3);
25  QP_obj.C.zeros(3, 3);
26  QP_obj.c.zeros(3);
27  switch (i) {
28  case 0: // uv_leader's objective
29  QP_obj.C(1, 0) = 1;
30  QP_obj.c(0) = 1;
31  QP_obj.c(2) = -1;
32  break;
33  case 1: // xy_leader's objective
34  QP_obj.C(1, 2) = 1;
35  QP_obj.c(0) = 1;
36  QP_obj.c(2) = 1;
37  break;
38  default:
39  throw std::string("Invalid make_obj_leader");
40  }
41  }
42 };
43 
44 std::shared_ptr<Game::NashGame> uv_leader(GRBEnv *env) {
45  // 2 variable and 2 constraints
46  arma::sp_mat Q(2, 2), C(2, 1), A(2, 1), B(2, 2);
47  arma::vec c(2, arma::fill::zeros);
48  arma::vec b(2, arma::fill::zeros);
49  // Q remains as 0
50  // C remains as 0
51  // c
52  c(0) = -1;
53  c(1) = 1;
54  // A
55  A(0, 0) = -1;
56  A(1, 0) = 1;
57  // B
58  B(0, 0) = 2;
59  B(0, 1) = 1;
60  B(1, 0) = 1;
61  B(1, 1) = -2;
62  auto foll = std::make_shared<Game::QP_Param>(Q, C, A, B, c, b, env);
63 
64  // Lower level Market clearing constraints - empty
65  arma::sp_mat MC(0, 3);
66  arma::vec MCRHS(0, arma::fill::zeros);
67 
68  arma::sp_mat LeadCons(1, 3);
69  arma::vec LeadRHS(1);
70  LeadCons(0, 0) = 1;
71  LeadCons(0, 1) = 1;
72  LeadCons(0, 2) = 1;
73  LeadRHS(0) = 5;
74 
75  auto N = std::make_shared<Game::NashGame>(
76  env, std::vector<std::shared_ptr<Game::QP_Param>>{foll}, MC, MCRHS, 1,
77  LeadCons, LeadRHS);
78  return N;
79 }
80 
81 std::shared_ptr<Game::NashGame> xy_leader(GRBEnv *env) {
82  // 2 variable and 2 constraints
83  arma::sp_mat Q(2, 2), C(2, 1), A(2, 1), B(2, 2);
84  arma::vec c(2, arma::fill::zeros);
85  arma::vec b(2, arma::fill::zeros);
86  // Q remains as 0
87  // C remains as 0
88  // c
89  c(0) = 1;
90  c(1) = -1;
91  // A
92  A(0, 0) = 1;
93  A(1, 0) = -1;
94  // B
95  B(0, 0) = -1;
96  B(0, 1) = 1;
97  B(1, 0) = -1;
98  B(1, 1) = 1;
99  // b
100  b(0) = 5;
101  b(1) = -3;
102  auto foll = std::make_shared<Game::QP_Param>(Q, C, A, B, c, b, env);
103 
104  // Lower level Market clearing constraints - empty
105  arma::sp_mat MC(0, 3);
106  arma::vec MCRHS(0, arma::fill::zeros);
107 
108  arma::sp_mat LeadCons(2, 3);
109  arma::vec LeadRHS(2);
110  LeadCons(0, 0) = 1;
111  LeadCons(0, 1) = 1;
112  LeadCons(0, 2) = 1;
113  LeadRHS(0) = 7;
114  // Comment the following four lines for another example ;)
115  LeadCons(1, 0) = -1;
116  LeadCons(1, 1) = 1;
117  LeadCons(1, 2) = 0;
118  LeadRHS(1) = 0;
119 
120  auto N = std::make_shared<Game::NashGame>(
121  env, std::vector<std::shared_ptr<Game::QP_Param>>{foll}, MC, MCRHS, 1,
122  LeadCons, LeadRHS);
123  return N;
124 }
125 
126 int main() {
127  GRBEnv env;
128  boost::log::core::get()->set_filter(boost::log::trivial::severity >=
129  boost::log::trivial::warning);
130  My_EPEC_Prob epec(&env);
131  // Adding uv_leader
132  auto uv_lead = uv_leader(&env);
133  epec.addLeader(uv_lead, 0);
134  // Adding xy_leader
135  auto xy_lead = xy_leader(&env);
136  epec.addLeader(xy_lead, 1);
137  // Finalize
138  epec.finalize();
140  // Solve
141  try {
142  epec.findNashEq();
143  } catch (std::string &s) {
144  std::cerr << "Error caught: " << s << '\n';
145  throw;
146  }
147  // auto M = epec.getLcpModel();
148  // M.write("dat/ex_model.lp");
149  // M.optimize();
150  // M.write("dat/ex_sol.sol");
151 
152  std::cout << "\nUV LEADER\n";
153  std::cout << "u: " << epec.getVal_LeadLead(0, 0) << '\n';
154  std::cout << "v_1: " << epec.getVal_LeadFoll(0, 0) << '\n';
155  std::cout << "v_2: " << epec.getVal_LeadFoll(0, 1) << '\n';
156  auto uv_strats = epec.mixedStratPoly(0);
157  std::for_each(std::begin(uv_strats), std::end(uv_strats),
158  [&epec](const unsigned int i) {
159  std::cout << "With probability " << epec.getVal_Probab(0, i)
160  << '\n';
161  std::cout << "(" << epec.getVal_LeadLeadPoly(0, 0, i) << ", "
162  << epec.getVal_LeadFollPoly(0, 0, i) << ", "
163  << epec.getVal_LeadFollPoly(0, 1, i) << ")\n";
164  });
165  std::cout << '\n';
166  std::cout << "\nXY LEADER\n";
167  std::cout << "x: " << epec.getVal_LeadLead(1, 0) << '\n';
168  std::cout << "y_1: " << epec.getVal_LeadFoll(1, 0) << '\n';
169  std::cout << "y_2: " << epec.getVal_LeadFoll(1, 1) << '\n';
170  auto xy_strats = epec.mixedStratPoly(1);
171  std::for_each(std::begin(xy_strats), std::end(xy_strats),
172  [&epec](const unsigned int i) {
173  std::cout << "With probability " << epec.getVal_Probab(1, i)
174  << '\n';
175  std::cout << "(" << epec.getVal_LeadLeadPoly(1, 0, i) << ", "
176  << epec.getVal_LeadFollPoly(1, 0, i) << ", "
177  << epec.getVal_LeadFollPoly(1, 1, i) << ")\n";
178  });
179  std::cout << '\n';
180  return 0;
181 }
arma::vec c
Definition: games.h:42
void updateLocs() override
Definition: example.cpp:18
std::shared_ptr< Game::NashGame > uv_leader(GRBEnv *env)
Definition: example.cpp:44
unsigned int ends[2]
Definition: example.cpp:17
double getVal_Probab(const unsigned int i, const unsigned int k) const
Definition: Games.cpp:2409
void findNashEq()
Definition: Games.cpp:2182
void finalize()
Finalizes the creation of a Game::EPEC object.
Definition: Games.cpp:1226
arma::sp_mat C
Definition: games.h:41
GRBEnv * env
Definition: games.h:515
My_EPEC_Prob(GRBEnv *e)
Definition: example.cpp:9
Class to handle a Nash game between leaders of Stackelberg games.
Definition: games.h:475
arma::sp_mat Q
Definition: games.h:40
double getVal_LeadLeadPoly(const unsigned int i, const unsigned int j, const unsigned int k, const double tol=1e-5) const
Definition: Games.cpp:2456
std::vector< unsigned int > convexHullVariables
Definition: games.h:512
void addLeader(std::shared_ptr< Game::NashGame > N, const unsigned int i)
Definition: example.cpp:10
struct to handle the objective params of MP_Param/QP_Param
Definition: games.h:39
void make_obj_leader(const unsigned int i, Game::QP_objective &QP_obj) override
Can be instantiated by a derived class only!
Definition: example.cpp:22
std::vector< const unsigned int * > LocEnds
Definition: games.h:511
std::vector< std::shared_ptr< Game::NashGame > > countries_LL
Definition: games.h:493
double getVal_LeadFoll(const unsigned int i, const unsigned int j) const
Definition: Games.cpp:2418
std::vector< unsigned int > mixedStratPoly(const unsigned int i, const double tol=1e-5) const
Definition: Games.cpp:2391
std::shared_ptr< Game::NashGame > xy_leader(GRBEnv *env)
Definition: example.cpp:81
double getVal_LeadFollPoly(const unsigned int i, const unsigned int j, const unsigned int k, const double tol=1e-5) const
Definition: Games.cpp:2438
int main()
Definition: example.cpp:126
double getVal_LeadLead(const unsigned int i, const unsigned int j) const
Definition: Games.cpp:2428
void setAlgorithm(Game::EPECalgorithm algorithm)
Definition: Games.cpp:2274