SGSolve
sgapprox.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 _SGAPPROX_HPP
23 #define _SGAPPROX_HPP
24 
25 #include "sgcommon.hpp"
26 #include "sgutilities.hpp"
27 #include "sgenv.hpp"
28 #include "sggame.hpp"
29 #include "sgexception.hpp"
30 #include "sgsolution_pencilsharpening.hpp"
31 #include "sgnamespace.hpp"
32 
34 
48 class SGApprox
49 {
50 private:
51  const SGEnv & env;
53  const SGGame & game;
58  const double delta;
60  const int numPlayers;
61  const int numStates;
64  std::ofstream logfs;
68  double errorLevel;
70  vector<bool> facingEastNorth;
74  bool passNorth;
80  vector<bool> updatedThreatTuple;
85  vector< list<SGAction_PencilSharpening> > actions;
95  vector< const SGAction_PencilSharpening* > actionTuple;
99  vector<SG::Regime> regimeTuple;
103  list<SGAction_PencilSharpening>::const_iterator bestAction;
116  int westPoint;
119  int newWest;
121  int oldWest;
123 
127  void updateMinPayoffs();
128 
130 
135 
137 
142  void trimBindingContinuations() ;
143 
145 
148  void findBestDirection();
149 
151 
156  void calculateNewPivot();
157 
159 
164  double updatePivot(vector<double> & movements,
165  vector<double> & changes,
166  vector<SG::Regime> & regimeTuple,
167  const vector<double> & maxMovement,
168  const vector<SG::Regime> & maxMovementConstraints);
169 
171 
176  void updateFlags();
177 
179 
185  double distance(int newStart, int newEnd, int oldStart, int oldEnd) const;
186 
187  double distHelper(const SGPoint & p,
188  const SGPoint & qA,
189  const SGPoint & qB) const;
190 
192 
197  bool improves(const SGPoint & current,
198  const SGPoint & best,
199  const SGPoint & newDirection) const;
200 
202  void logAppend(ofstream & logfs,
203  int iter, int rev, const SGTuple & tuple,
204  int state, int action);
205 
206 public:
208  SGApprox(const SGEnv & _env,
209  const SGGame & _game,
211  env(_env), game(_game), soln(_soln),
212  delta(game.getDelta()), numPlayers(game.getNumPlayers()),
213  numStates(game.getNumStates()), errorLevel(1), sufficiencyFlag(true),
214  nullAction(env)
215  { }
216 
218 
224  void initialize();
225 
227  int getNumIterations() const {return numIterations; }
229  int getNumRevolutions() const {return numRevolutions; }
231  int getNumExtremeTuples() const {return extremeTuples.size(); }
237  list<SGAction_PencilSharpening>::const_iterator getBestAction() const { return bestAction; }
239  const SGPoint & getBestDirection() const { return bestDirection; }
241  const vector<const SGAction_PencilSharpening*> & getActionTuple() const { return actionTuple; }
243  const vector<SG::Regime> & getRegimeTuple() const { return regimeTuple; }
245  const SGPoint & getDirection() const { return currentDirection; }
247  const SGTuple & getPivot() const {return pivot; }
249  const SGTuple & getThreatTuple() const {return threatTuple; }
252  const vector< list<SGAction_PencilSharpening> > & getActions() const { return actions; }
254  const vector<SGTuple> & getExtremeTuples() const {return extremeTuples; }
255 
257  std::string progressString() const;
258 
260 
265  double generate(bool storeIteration = true);
266 
268 
270  bool passedNorth() const
271  {return passNorth; }
272 
274 
275  void end();
276 
277 };
278 
279 
280 #endif
SGApprox::numStates
const int numStates
Definition: sgapprox.hpp:61
SGApprox::facingEastNorth
vector< bool > facingEastNorth
Definition: sgapprox.hpp:70
SGApprox::nullAction
SGAction_PencilSharpening nullAction
Definition: sgapprox.hpp:114
SGApprox::newWest
int newWest
Definition: sgapprox.hpp:119
SGApprox::getExtremeTuples
const vector< SGTuple > & getExtremeTuples() const
Returns the array of extreme tuples.
Definition: sgapprox.hpp:254
SGApprox::oldWest
int oldWest
Definition: sgapprox.hpp:121
SGApprox::SGApprox
SGApprox(const SGEnv &_env, const SGGame &_game, SGSolution_PencilSharpening &_soln)
Constructor for SGApprox class.
Definition: sgapprox.hpp:208
SGApprox::bestRegime
SG::Regime bestRegime
Definition: sgapprox.hpp:110
SGApprox::actionTuple
vector< const SGAction_PencilSharpening * > actionTuple
Definition: sgapprox.hpp:95
SGApprox::getActionTuple
const vector< const SGAction_PencilSharpening * > & getActionTuple() const
Returns the current action tuple.
Definition: sgapprox.hpp:241
SGApprox
Approximation of the equilibrium payoff correspondence.
Definition: sgapprox.hpp:49
SGApprox::pivot
SGTuple pivot
Definition: sgapprox.hpp:93
SGApprox::sufficiencyFlag
bool sufficiencyFlag
Definition: sgapprox.hpp:77
SGGame
Describes a stochastic game.
Definition: sggame.hpp:40
SGApprox::end
void end()
Destructor.
Definition: sgapprox.cpp:24
SGApprox::getNumIterations
int getNumIterations() const
Returns the number of iterations thus far.
Definition: sgapprox.hpp:227
SGApprox::improves
bool improves(const SGPoint &current, const SGPoint &best, const SGPoint &newDirection) const
Checks whether or not newDirection is shallower than best, relative to current.
Definition: sgapprox.cpp:419
SGApprox::calculateNewPivot
void calculateNewPivot()
Calculates the new pivot.
Definition: sgapprox.cpp:437
SGApprox::distance
double distance(int newStart, int newEnd, int oldStart, int oldEnd) const
Calculates the distance between revolutions.
Definition: sgapprox.cpp:636
SGApprox::getNumExtremeTuples
int getNumExtremeTuples() const
Returns the number of tuples in the extremeTuples array.
Definition: sgapprox.hpp:231
SGApprox::logfs
std::ofstream logfs
Definition: sgapprox.hpp:64
SGApprox::threatTuple
SGTuple threatTuple
Definition: sgapprox.hpp:91
SGApprox::getNumRevolutions
int getNumRevolutions() const
Returns the number of revolutions of the pivot thus far.
Definition: sgapprox.hpp:229
SGSolution_PencilSharpening
Records the progress of SGSolver::solve().
Definition: sgsolution_pencilsharpening.hpp:41
SGApprox::updateFlags
void updateFlags()
Updates flags before the next iteration.
Definition: sgapprox.cpp:591
SGApprox::numRevolutions
int numRevolutions
Definition: sgapprox.hpp:67
SGApprox::updatePivot
double updatePivot(vector< double > &movements, vector< double > &changes, vector< SG::Regime > &regimeTuple, const vector< double > &maxMovement, const vector< SG::Regime > &maxMovementConstraints)
Updates the pivot.
Definition: sgapprox.cpp:541
SGApprox::game
const SGGame & game
Definition: sgapprox.hpp:53
SGApprox::passedNorth
bool passedNorth() const
Algorithm just passed north.
Definition: sgapprox.hpp:270
SGApprox::numPlayers
const int numPlayers
Definition: sgapprox.hpp:60
sgutilities.hpp
SGApprox::extremeTuples
vector< SGTuple > extremeTuples
Definition: sgapprox.hpp:89
SGApprox::trimBindingContinuations
void trimBindingContinuations()
Trims binding continuation values.
Definition: sgapprox.cpp:773
SGApprox::actions
vector< list< SGAction_PencilSharpening > > actions
Definition: sgapprox.hpp:85
SGApprox::bestAction
list< SGAction_PencilSharpening >::const_iterator bestAction
Definition: sgapprox.hpp:103
SGApprox::getPivot
const SGTuple & getPivot() const
Returns the current pivot.
Definition: sgapprox.hpp:247
SGApprox::passNorth
bool passNorth
Definition: sgapprox.hpp:74
SGApprox::westPoint
int westPoint
Definition: sgapprox.hpp:116
SGApprox::delta
const double delta
Definition: sgapprox.hpp:58
SGApprox::soln
SGSolution_PencilSharpening & soln
Definition: sgapprox.hpp:55
SGApprox::calculateBindingContinuations
void calculateBindingContinuations()
Calculates binding continuation values.
Definition: sgapprox.cpp:730
SGApprox::logAppend
void logAppend(ofstream &logfs, int iter, int rev, const SGTuple &tuple, int state, int action)
Outputs progress to the log file every iteration.
Definition: sgapprox.cpp:103
SGApprox::getBestDirection
const SGPoint & getBestDirection() const
Returns the best test direction.
Definition: sgapprox.hpp:239
SGApprox::updatedThreatTuple
vector< bool > updatedThreatTuple
Definition: sgapprox.hpp:80
SGEnv
Manages parameters for algorithm behavior.
Definition: sgenv.hpp:35
std::vector< SGTuple >
Definition: sgstl.cpp:27
SGApprox::env
const SGEnv & env
Definition: sgapprox.hpp:51
SGApprox::bestDirection
SGPoint bestDirection
Definition: sgapprox.hpp:108
SGApprox::generate
double generate(bool storeIteration=true)
Refines the approximation.
Definition: sgapprox.cpp:111
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGApprox::getDirection
const SGPoint & getDirection() const
Returns the current direction.
Definition: sgapprox.hpp:245
SGApprox::getThreatTuple
const SGTuple & getThreatTuple() const
Returns the current threat tuple.
Definition: sgapprox.hpp:249
SGApprox::getBestRegime
SG::Regime getBestRegime() const
Definition: sgapprox.hpp:234
SGAction_PencilSharpening
Enhanced version of SGBaseAction.
Definition: sgaction_pencilsharpening.hpp:43
SGApprox::getActions
const vector< list< SGAction_PencilSharpening > > & getActions() const
Definition: sgapprox.hpp:252
SGApprox::numIterations
int numIterations
Definition: sgapprox.hpp:66
SGApprox::regimeTuple
vector< SG::Regime > regimeTuple
Definition: sgapprox.hpp:99
SGApprox::findBestDirection
void findBestDirection()
Calculates the best direction.
Definition: sgapprox.cpp:181
SGApprox::progressString
std::string progressString() const
Returns a string indicating the algorithms progress.
Definition: sgapprox.cpp:159
SGApprox::currentDirection
SGPoint currentDirection
Definition: sgapprox.hpp:94
SGApprox::getBestAction
list< SGAction_PencilSharpening >::const_iterator getBestAction() const
Definition: sgapprox.hpp:237
SG::Regime
Regime
Indicates which incentive constraints are binding.
Definition: sgnamespace.hpp:160
SGApprox::getRegimeTuple
const vector< SG::Regime > & getRegimeTuple() const
Returns the current regime tuple.
Definition: sgapprox.hpp:243
SGApprox::initialize
void initialize()
Prepares the approximation for generation.
Definition: sgapprox.cpp:29
SGTuple
Definition: sgtuple.hpp:52
SGApprox::errorLevel
double errorLevel
Definition: sgapprox.hpp:68
SGApprox::updateMinPayoffs
void updateMinPayoffs()
Calculates the minimum IC continuation values.
Definition: sgapprox.cpp:708