SGSolve
sgrisksharing_3player.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2019 Benjamin A. Brooks
3 //
4 // SGSolve free software: you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // SGSolve is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Benjamin A. Brooks
19 // ben@benjaminbrooks.net
20 // Chicago, IL
21 
22 #ifndef _SGRISKSHARING_3PLAYER_HPP
23 #define _SGRISKSHARING_3PLAYER_HPP
24 
25 #include "sg.hpp"
26 
29 {
30 private:
31  int numEndowments;
32  int c2e;
33  vector<vector<int> > E;
34  double eIncr;
35  double cIncr;
36  vector<int> numActions_total;
37  vector< vector<int> > transferList;;
38 
39 public:
41  int _numEndowments,
42  int _c2e):
43  numEndowments(_numEndowments),
44  c2e(_c2e),
45  E(numEndowments),
46  numActions_total(_numEndowments,1),
48  _numEndowments,
49  vector< vector<int> > (_numEndowments,vector<int>(3,1)))
50  {
51  // // A couple of checks on the number of endowments
52  // assert( (numEndowments%2) == 1 );
53  // assert( numEndowments>2 );
54 
55  // Set up the endowment grid and the numbers of actions
56  E.clear();
57  numStates=0;
58  for (int e0 = 0; e0 < numEndowments; e0++)
59  {
60  for (int e1 = 0; e1 < numEndowments-e0; e1++)
61  {
62  vector<int>newEndowments(3,0);
63  newEndowments[0] = e0; // player 0's endowment
64  newEndowments[1] = e1; // player 0's endowment
65  newEndowments[2] = numEndowments-1 -e0-e1;
66  E.push_back(newEndowments);
67 
68  numStates++;
69  } // for e1
70  } // for e0
71 
72 
73  eIncr = 1.0/(numEndowments-1.0);
74  cIncr = 1.0/( (numEndowments-1)*c2e );
75 
76  transferList.clear();
77 
78  vector<int> transferStartPositions(numEndowments+1,0);
79  // position in the transfer list where transfers for the given
80  // endowment level begin.
81 
82  for (int e = 0; e < numEndowments; e++)
83  {
84  transferStartPositions[e] = transferList.size();
85 
86  for (int t0 = 0; t0 < e*c2e+1; t0++)
87  {
88  for (int t1 = 0; t1 < e*c2e+1-t0; t1++)
89  {
90  vector<int> newTransfers (2,0);
91  newTransfers[0] = t0;
92  newTransfers[1] = t1;
93 
94  transferList.push_back(newTransfers);
95  }
96  }
97  }
98  transferStartPositions.back() = transferList.size();
99 
100  numActions = vector< vector<int> > (numStates,vector<int>(numPlayers,0));
101  numActions_total = vector< int > (numStates,1);
102 
103  for (int s = 0; s < numStates; s++)
104  {
105  for (int p = 0; p < numPlayers; p++)
106  {
107  // Action is a vector of how much to transfer to (i+1)%2 and (i+2)%2.
108  numActions[s][p] = transferStartPositions[E[s][p]+1];
109  numActions_total[s] *= numActions[s][p];
110  }
111  } // for e0
112 
113  } // constructor
114 
115  virtual SGPoint payoffs(int state, const vector<int> & actions) const
116  {
117  // Make the player's action a choice of how much to give away. We
118  // will compute how to distribute this to maximize welfare.
119  vector<int> netTransfers(numPlayers,0);
120  for (int p = 0; p < numPlayers; p++)
121  {
122  netTransfers[p] -= transferList[actions[p]][0];
123  netTransfers[p] -= transferList[actions[p]][1];
124  netTransfers[(p+1)%numPlayers] += transferList[actions[p]][0];
125  netTransfers[(p+2)%numPlayers] += transferList[actions[p]][1];
126  }
127 
129  for (int p = 0; p < numPlayers; p++)
130  payoffs[p] = sqrt(E[state][p]*eIncr + netTransfers[p]*cIncr);
131 
132  return payoffs;
133  } // payoffs
134 
135  virtual double probability(int e, const vector<int> & actions, int ep) const
136  {
137  return 1.0/numStates; // uniform distribution
138  } // probability
139 
140  virtual bool isEquilibriumAction(int state, const vector<int> & actions) const
141  {
142  // Return true if exactly one player receives transfers (from one
143  // or two players) or exactly two players receive transfers from
144  // one player.
145  int numReceivingXfers = 0;
146  int numSendingXfers = 0;
147 
148  vector<bool> receives(numPlayers,false);
149 
150  for (int p = 0; p < numPlayers; p++)
151  {
152  numSendingXfers += actions[p]>0;
153  if (transferList[actions[p]][0]>0)
154  receives[(p+1)%numPlayers] = true;
155  if (transferList[actions[p]][1]>0)
156  receives[(p+2)%numPlayers] = true;
157  }
158 
159  for (int p = 0; p < numPlayers; p++)
160  numReceivingXfers += receives[p];
161 
162  return (numReceivingXfers <= 1
163  || (numReceivingXfers == 2 && numSendingXfers == 1));
164  } // isEquilibriumAction
165 
166 };
167 
168 #endif
SGAbstractGame::numPlayers
int numPlayers
The number of players, always 2.
Definition: sgabstractgame.hpp:44
SGAbstractGame::numActions
vector< vector< int > > numActions
The numbers of actions in each state.
Definition: sgabstractgame.hpp:52
SGAbstractGame::numStates
int numStates
The number of states.
Definition: sgabstractgame.hpp:48
RiskSharingGame_3Player
Three player version of the Kocherlakota (1996) risk sharing game.
Definition: sgrisksharing_3player.hpp:29
SGAbstractGame
A virtual class for constructing games.
Definition: sgabstractgame.hpp:41
SGPoint
A vector in .
Definition: sgpoint.hpp:35
RiskSharingGame_3Player::probability
virtual double probability(int e, const vector< int > &actions, int ep) const
Transition probabilities.
Definition: sgrisksharing_3player.hpp:135
RiskSharingGame_3Player::payoffs
virtual SGPoint payoffs(int state, const vector< int > &actions) const
The payoff function.
Definition: sgrisksharing_3player.hpp:115
SGAbstractGame::SGAbstractGame
SGAbstractGame(int _numPlayers, double _delta, int _numStates, vector< vector< int > > _numActions)
Constructor for the pure virtual class.
Definition: sgabstractgame.hpp:70
SGAbstractGame::delta
double delta
The discount factor.
Definition: sgabstractgame.hpp:46
RiskSharingGame_3Player::isEquilibriumAction
virtual bool isEquilibriumAction(int state, const vector< int > &actions) const
Definition: sgrisksharing_3player.hpp:140