SGSolve
sgabstractgame.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 SGABSTRACTGAME_HPP
23 #define SGABSTRACTGAME_HPP
24 
26 
41 {
42 protected:
46  double delta;
48  int numStates;
50 
52  vector< vector<int> > numActions;
53 
55 
57  vector<int> indexToActions(int index,int state) const
58  {
59  vector<int> actions(numPlayers,0);
60  for (int p = 0; p < numPlayers; p++)
61  {
62  actions[p] = index%numActions[state][p];
63  index /= numActions[state][p];
64  }
65  return actions;
66  } // indexToActions
67 
68 public:
70  SGAbstractGame(int _numPlayers,
71  double _delta,
72  int _numStates,
73  vector< vector<int> > _numActions):
74  numPlayers(_numPlayers),
75  delta(_delta),
76  numStates(_numStates),
77  numActions(_numActions)
78  {}
79 
81 
82  SGAbstractGame(double _delta,
83  int _numStates,
84  vector< vector<int> > _numActions):
85  numPlayers(2),
86  delta(_delta),
87  numStates(_numStates),
88  numActions(_numActions)
89  {}
90 
92 
96  virtual SGPoint payoffs(int state,const vector<int> & actions) const = 0;
98 
103  virtual double probability(int state,const vector<int> &actions,int statep) const = 0;
106 
110  virtual bool isEquilibriumAction(int state, const vector<int> & actions) const
111  {return true;}
113 
120  virtual bool constrained(int player) const { return true;}
121 
123 
126  SGPoint payoffs(int state,int action) const
127  { return payoffs(state,indexToActions(action,state)); }
130 
133  double probability(int state,int action,int statep) const
134  { return probability(state,indexToActions(action,state),statep); }
137 
140  bool isEquilibriumAction(int state,int action) const
141  { return isEquilibriumAction(state,indexToActions(action,state)); }
142 
144  int getNumPlayers() const { return numPlayers; }
146  double getDelta() const { return delta; }
148  double getNumStates() const { return numStates; }
150  const vector< vector<int> > getNumActions() const { return numActions; }
151 };
152 
153 
154 #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
SGAbstractGame::getDelta
double getDelta() const
Returns the discount factor.
Definition: sgabstractgame.hpp:146
SGAbstractGame::SGAbstractGame
SGAbstractGame(double _delta, int _numStates, vector< vector< int > > _numActions)
Constructor for the pure virtual class.
Definition: sgabstractgame.hpp:82
SGAbstractGame::payoffs
SGPoint payoffs(int state, int action) const
An overloaded version of payoffs that uses a linear action index.
Definition: sgabstractgame.hpp:126
SGAbstractGame
A virtual class for constructing games.
Definition: sgabstractgame.hpp:41
SGAbstractGame::probability
virtual double probability(int state, const vector< int > &actions, int statep) const =0
Transition probabilities.
SGAbstractGame::constrained
virtual bool constrained(int player) const
Returns true if the given player is incentive constrained.
Definition: sgabstractgame.hpp:120
SGAbstractGame::getNumStates
double getNumStates() const
Returns the number of states.
Definition: sgabstractgame.hpp:148
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGAbstractGame::probability
double probability(int state, int action, int statep) const
Definition: sgabstractgame.hpp:133
SGAbstractGame::indexToActions
vector< int > indexToActions(int index, int state) const
Converts a linear index to multiindex.
Definition: sgabstractgame.hpp:57
SGAbstractGame::isEquilibriumAction
bool isEquilibriumAction(int state, int action) const
Definition: sgabstractgame.hpp:140
SGAbstractGame::isEquilibriumAction
virtual bool isEquilibriumAction(int state, const vector< int > &actions) const
Definition: sgabstractgame.hpp:110
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
SGAbstractGame::getNumActions
const vector< vector< int > > getNumActions() const
Returns the number of actions array.
Definition: sgabstractgame.hpp:150
SGAbstractGame::getNumPlayers
int getNumPlayers() const
Returns the number of players.
Definition: sgabstractgame.hpp:144
SGAbstractGame::payoffs
virtual SGPoint payoffs(int state, const vector< int > &actions) const =0
The payoff function.