SGSolve
sggame.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 _SGGAME_HPP
23 #define _SGGAME_HPP
24 
25 #include "sgcommon.hpp"
26 #include "sgutilities.hpp"
27 #include "sgexception.hpp"
28 #include "sgtuple.hpp"
29 #include "sgabstractgame.hpp"
30 #include <boost/archive/text_iarchive.hpp>
31 #include <boost/archive/text_oarchive.hpp>
32 #include <boost/serialization/utility.hpp>
33 
35 
39 class SGGame
40 {
41 protected:
42  double delta;
43  int numPlayers;
44  int numStates;
45  vector< vector<int> > numActions;
55  vector<int> numActions_total;
58  vector< vector<SGPoint> > payoffs;
65  vector< vector< vector<double> > > probabilities;
77  vector< vector<bool> > eqActions;
89  vector<bool> unconstrained;
94  template <class Archive>
96  void serialize(Archive& ar, const unsigned int version)
97  {
98  ar & delta;
99  ar & numPlayers;
100  ar & numStates;
101  ar & numActions;
102  ar & numActions_total;
103  ar & payoffs;
104  ar & probabilities;
105  ar & eqActions;
106  ar & unconstrained;
107  }
108 
109 public:
112  numStates(1),
113  numActions(1,vector<int>(2,1)),
114  numActions_total(1,1),
115  payoffs(1,vector<SGPoint>(1,SGPoint(0,0))),
116  probabilities(1,vector< vector<double> > (1,vector<double> (1,1))),
117  delta(0.9),
118  numPlayers(2),
119  unconstrained(2,false)
120  {}
121 
123 
130  SGGame(const SGAbstractGame & game);
131 
132  ~SGGame() {}
134 
136  SGGame(double _delta,
137  int _numStates,
138  const vector< vector<int> > & _numActions,
139  const vector< vector< vector<double> > > & _payoffs,
140  const vector< vector< vector<double> > > & _probabilities);
141 
143  SGGame(double _delta,
144  int _numStates,
145  const vector< vector<int> > & _numActions,
146  const vector< vector< vector<double> > > & _payoffs,
147  const vector< vector< vector<double> > > & _probabilities,
148  const vector<bool> & _unconstrained);
149 
151 
155  SGGame(double _delta,
156  int _numStates,
157  const vector< vector<int> > & _numActions,
158  const vector< vector< vector<double> > > & _payoffs,
159  const vector< vector< vector<double> > > & _probabilities,
160  const vector< vector<bool> > & _eqActions,
161  const vector<bool> & _unconstrained);
162 
164 
165  SGGame(int _numPlayers,
166  double _delta,
167  int _numStates,
168  const vector< vector<int> > & _numActions,
169  const vector< vector< vector<double> > > & _payoffs,
170  const vector< vector< vector<double> > > & _probabilities,
171  const vector< vector<bool> > & _eqActions,
172  const vector<bool> & _unconstrained);
173 
175  double getDelta() const { return delta; }
177  int getNumPlayers() const { return numPlayers; }
179  int getNumStates() const { return numStates; }
181  const vector< vector<int> > & getNumActions() const
182  { return numActions; }
185  const vector<int> & getNumActions_total () const
186  { return numActions_total; }
188  const vector< vector< vector<double> > > & getProbabilities() const
189  {return probabilities;}
191  const vector< vector<SGPoint> > & getPayoffs() const
192  { return payoffs; }
194  const vector< vector<bool> > & getEquilibriumActions() const
195  { return eqActions; }
198  void getPayoffBounds(SGPoint & UB, SGPoint & LB) const;
199 
201  const vector<bool> & getConstrained() const
202  { return unconstrained; }
203 
205 
206  bool setDiscountFactor(double newDelta);
208 
210  bool setPayoff(int state, int action, int player, double payoff);
212 
214  bool setProbability(int state, int action,
215  int newState, double prob);
217  bool setConstrained(const vector<bool> & _unconstrained);
219 
222  bool addAction(int state, int player, int position);
224  bool removeAction(int state, int player, int action);
226 
229  bool addState(int position);
231  bool removeState(int state);
232 
234  bool transitionProbsSumToOne(double tolerance=1e-3) const;
235 
237  bool profitableDeviation(vector<int> & input, SGGame game);
238 
240  static void save(const SGGame & game, const char* filename)
241  {
242  std::ofstream ofs(filename);
243 
244  if (ofs.good())
245  {
246  boost::archive::text_oarchive oa(ofs);
247  oa << game;
248  }
249  else
251  }
252 
254  static void load(SGGame & game, const char* filename)
255  {
256  std::ifstream ifs(filename);
257  if (ifs.good())
258  {
259  boost::archive::text_iarchive ia(ifs);
260  ia >> game;
261  }
262  else
264  }
265 
266  friend class boost::serialization::access;
267  friend class SGSolver;
268 };
269 
270 #endif
SGGame::getPayoffBounds
void getPayoffBounds(SGPoint &UB, SGPoint &LB) const
Definition: sggame.cpp:167
SGGame::unconstrained
vector< bool > unconstrained
Definition: sggame.hpp:89
SGGame::getNumPlayers
int getNumPlayers() const
Returns SGGame::numPlayers, the number of players.
Definition: sggame.hpp:177
SGGame::getPayoffs
const vector< vector< SGPoint > > & getPayoffs() const
Returns a const reference to the payoffs.
Definition: sggame.hpp:191
SGGame::setConstrained
bool setConstrained(const vector< bool > &_unconstrained)
Sets whether or not players are incentive constrained.
Definition: sggame.cpp:380
SGGame::serialize
void serialize(Archive &ar, const unsigned int version)
Serializes the game using boost.
Definition: sggame.hpp:96
SGGame::getDelta
double getDelta() const
Returns SGGame::delta, the discount factor.
Definition: sggame.hpp:175
SGGame
Describes a stochastic game.
Definition: sggame.hpp:40
SGGame::SGGame
SGGame()
Default constructor.
Definition: sggame.hpp:111
SG::FAILED_OPEN
@ FAILED_OPEN
Definition: sgnamespace.hpp:32
SGGame::addState
bool addState(int position)
Adds a new state.
Definition: sggame.cpp:327
SGGame::save
static void save(const SGGame &game, const char *filename)
Static method for saving an SGGame object to the file filename.
Definition: sggame.hpp:240
SGGame::getNumActions_total
const vector< int > & getNumActions_total() const
Definition: sggame.hpp:185
SGException
SGSolve specific exceptions.
Definition: sgexception.hpp:36
SGGame::getNumActions
const vector< vector< int > > & getNumActions() const
Sets the argument _numActions equal to SGGame::numActions.
Definition: sggame.hpp:181
SGGame::eqActions
vector< vector< bool > > eqActions
Definition: sggame.hpp:77
SGGame::setDiscountFactor
bool setDiscountFactor(double newDelta)
Set discount factor.
Definition: sggame.cpp:192
SGGame::setProbability
bool setProbability(int state, int action, int newState, double prob)
Set probability.
Definition: sggame.cpp:215
SGGame::profitableDeviation
bool profitableDeviation(vector< int > &input, SGGame game)
Check if there exists a profitable deviation.
Definition: sggame.cpp:408
sgutilities.hpp
SGAbstractGame
A virtual class for constructing games.
Definition: sgabstractgame.hpp:41
SGGame::numActions
vector< vector< int > > numActions
Definition: sggame.hpp:45
SGGame::payoffs
vector< vector< SGPoint > > payoffs
Definition: sggame.hpp:58
SGGame::getEquilibriumActions
const vector< vector< bool > > & getEquilibriumActions() const
Returns a const reference to the equilibrium actions.
Definition: sggame.hpp:194
SGGame::probabilities
vector< vector< vector< double > > > probabilities
Definition: sggame.hpp:65
SGGame::getProbabilities
const vector< vector< vector< double > > > & getProbabilities() const
Returns a const reference to probabilities.
Definition: sggame.hpp:188
SGGame::addAction
bool addAction(int state, int player, int position)
Adds a new action.
Definition: sggame.cpp:229
SGGame::delta
double delta
Definition: sggame.hpp:42
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGGame::removeState
bool removeState(int state)
Removes the given state.
Definition: sggame.cpp:356
SGGame::transitionProbsSumToOne
bool transitionProbsSumToOne(double tolerance=1e-3) const
Check if transition probabilities sum to one.
Definition: sggame.cpp:390
SGGame::getNumStates
int getNumStates() const
Returns the number of states.
Definition: sggame.hpp:179
SGGame::getConstrained
const vector< bool > & getConstrained() const
Returns the unconstrained vector.
Definition: sggame.hpp:201
SGGame::numStates
int numStates
Definition: sggame.hpp:44
SGGame::load
static void load(SGGame &game, const char *filename)
Static method for loading an SGGame object from the file filename.
Definition: sggame.hpp:254
SGGame::setPayoff
bool setPayoff(int state, int action, int player, double payoff)
Set payoffs.
Definition: sggame.cpp:202
SGGame::numActions_total
vector< int > numActions_total
Definition: sggame.hpp:55
SGGame::removeAction
bool removeAction(int state, int player, int action)
Removes the given action.
Definition: sggame.cpp:279
SGGame::numPlayers
int numPlayers
Definition: sggame.hpp:43