SGSolve
sgsimulator.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2016 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 _SGSIMULATOR_HPP
23 #define _SGSIMULATOR_HPP
24 
25 #include "sgsolution_pencilsharpening.hpp"
26 #include <chrono>
27 #include <random>
28 #include <utility>
29 
30 
32 
74 {
75  typedef pair< list<SGIteration_PencilSharpening>::const_iterator, double > transitionPair;
76 
77 private:
80 
82  list<SGIteration_PencilSharpening>::const_iterator startOfLastRev;
83 
85 
91  vector< vector< list<transitionPair> > > transitionTable;
92 
94 
96  vector< vector<int> > actionDistr;
99  vector<int> tupleDistr;
102  vector<int> stateDistr;
103 
105  int numSim;
107  int numIter;
108 
110  bool logFlag;
111 
113  double weightTol;
114 
117  std::stringstream ss;
118 
120  std::stringstream transitionTableSS;
121 
122 public:
125  soln(_soln), logFlag(false), weightTol(1e-4)
126  {}
127 
129  const vector< vector<int> > getActionDistr() const { return actionDistr; }
131  const vector<int> getStateDistr() const { return stateDistr; }
133  const vector<int> getTupleDistr() const { return tupleDistr; }
135  int getStartOfLastRev() const { return startOfLastRev->getIteration(); }
137  int getNumIter() const { return numIter; }
138 
140  void setLogFlag(bool newFlag) { logFlag = newFlag; };
141 
143  const std::stringstream & getStringStream() const {return ss;}
145  const std::stringstream & getTransitionTableStringStream() const
146  {return transitionTableSS;}
147 
149  void initialize ();
150 
152  void simulate(int _numSim, int _numIter, int initialState, int initialTuple);
153 
156  {
157  SGPoint payoffs(0.0,0.0);
158  for (int state = 0;
159  state < soln.getGame().getNumStates();
160  state++)
161  {
162  for (int action = 0;
163  action < soln.getGame().getNumActions_total()[state];
164  action++)
165  {
166  payoffs += (1.0*actionDistr[state][action])/(1.0*numIter*numSim)
167  * soln.getGame().getPayoffs()[state][action];
168  }
169  } // state
170 
171  return payoffs;
172  }
173 
174 }; // SGSimulator
175 
176 #endif
SGSimulator::transitionTable
vector< vector< list< transitionPair > > > transitionTable
The transition table.
Definition: sgsimulator.hpp:91
SGSimulator::getLongRunPayoffs
SGPoint getLongRunPayoffs()
Returns the long run action distribution.
Definition: sgsimulator.hpp:155
SGSimulator::simulate
void simulate(int _numSim, int _numIter, int initialState, int initialTuple)
Forward simulates the equilibrium.
Definition: sgsimulator.cpp:244
SGSimulator::getStringStream
const std::stringstream & getStringStream() const
Returns the stringstream describing the first 200 periods.
Definition: sgsimulator.hpp:143
SGGame::getPayoffs
const vector< vector< SGPoint > > & getPayoffs() const
Returns a const reference to the payoffs.
Definition: sggame.hpp:191
SGSimulator::getStateDistr
const vector< int > getStateDistr() const
Returns the state frequency distribution.
Definition: sgsimulator.hpp:131
SGSolution_PencilSharpening::getGame
const SGGame & getGame() const
Get method for the game.
Definition: sgsolution_pencilsharpening.hpp:59
SGSimulator::logFlag
bool logFlag
True if saving log information to the stringstreams.
Definition: sgsimulator.hpp:110
SGSimulator::stateDistr
vector< int > stateDistr
Definition: sgsimulator.hpp:102
SGSimulator::getNumIter
int getNumIter() const
Returns the number of iterations for the current simulation.
Definition: sgsimulator.hpp:137
SGSimulator::weightTol
double weightTol
Tolerance for computing the transition table.
Definition: sgsimulator.hpp:113
SGSimulator::tupleDistr
vector< int > tupleDistr
Definition: sgsimulator.hpp:99
SGSimulator::numSim
int numSim
The number of simulations to run.
Definition: sgsimulator.hpp:105
SGSolution_PencilSharpening
Records the progress of SGSolver::solve().
Definition: sgsolution_pencilsharpening.hpp:41
SGSimulator::getTupleDistr
const vector< int > getTupleDistr() const
Returns the tuple frequency distribution.
Definition: sgsimulator.hpp:133
SGSimulator::initialize
void initialize()
Initializes the transition table.
Definition: sgsimulator.cpp:24
SGSimulator::startOfLastRev
list< SGIteration_PencilSharpening >::const_iterator startOfLastRev
Points to the beginning of the last revolution.
Definition: sgsimulator.hpp:82
SGSimulator::actionDistr
vector< vector< int > > actionDistr
The distribution of actions in each state.
Definition: sgsimulator.hpp:96
SGSimulator::getActionDistr
const vector< vector< int > > getActionDistr() const
Returns the action frequency distributions.
Definition: sgsimulator.hpp:129
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGSimulator::soln
const SGSolution_PencilSharpening & soln
The associated SGSolution object.
Definition: sgsimulator.hpp:79
SGSimulator::SGSimulator
SGSimulator(const SGSolution_PencilSharpening &_soln)
Constructor.
Definition: sgsimulator.hpp:124
SGSimulator::ss
std::stringstream ss
Definition: sgsimulator.hpp:117
SGSimulator
Class for forward simulating equilibria.
Definition: sgsimulator.hpp:74
SGSimulator::getTransitionTableStringStream
const std::stringstream & getTransitionTableStringStream() const
Returns the stringstream describing the transition table.
Definition: sgsimulator.hpp:145
SGSimulator::numIter
int numIter
The number of periods for each simulation.
Definition: sgsimulator.hpp:107
SGSimulator::transitionTableSS
std::stringstream transitionTableSS
Contains a text description of the transition table.
Definition: sgsimulator.hpp:120
SGSimulator::setLogFlag
void setLogFlag(bool newFlag)
Mutator method for the log flag.
Definition: sgsimulator.hpp:140
SGSimulator::getStartOfLastRev
int getStartOfLastRev() const
Returns the start of the last revolution.
Definition: sgsimulator.hpp:135