SGSolve
sgproductpolicy.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 _SGPRODUCTPOLICY_HPP
23 #define _SGPRODUCTPOLICY_HPP
24 
25 #include "sgpolicy.hpp"
26 
28 struct policyComp {
29  bool operator() (const SGPolicy & lhs, const SGPolicy & rhs)
30  { return lhs < rhs;}
31 };
32 
33 typedef set<SGPolicy,policyComp> SGPolicySet;
34 
36 
39 {
40 private:
41  vector<SGPolicySet> policies;
44  vector<double> levels;
46 public:
48  SGProductPolicy(int _numStates, const SGPoint & _dir):
49  policies(_numStates), dir(_dir), levels(_numStates)
50  {}
51 
53  const SGPolicySet & getPolicies(int state) const
54  {
55  if (state >= policies.size() || state<0)
57 
58  return policies[state];
59  }
60 
62  const SGPoint & getDir() const
63  { return dir; }
64 
66  const vector<double> & getLevels() const
67  { return levels; }
68 
70  void insertPolicy(int state, const SGPolicy & policy)
71  {
72  if (state >= policies.size() || state<0)
74 
75  policies[state].insert(policy);
76  }
77 
79  void erasePolicy(int state, const SGPolicy & policy)
80  {
81  if (state >= policies.size() || state<0)
83 
84  policies[state].erase(policy);
85  }
86 
88  void setLevel(int state, double lvl)
89  {
90  if (state >= policies.size() || state<0)
92 
93  levels[state] = lvl;
94  }
95 
97  void clear()
98  {
99  for (int state = 0; state < policies.size(); state++)
100  policies[state].clear();
101  }
102 
104  bool isempty() const
105  {
106  bool tf = false;
107  for (int state = 0; state < policies.size(); state++)
108  {
109  if (policies[state].size()==0)
110  tf = true;
111  }
112  return tf;
113  }
114 
116  int dimension() const
117  {
118  int dim = 0;
119  for (int state = 0; state < policies.size(); state++)
120  dim += policies[state].size();
121 
122  return dim-policies.size()+1;
123  }
124 
126  std::string hash() const;
127 
129  int numStates() const {return policies.size();}
130 }; // SGProductPolicy
131 
132 #endif
SGProductPolicy::SGProductPolicy
SGProductPolicy(int _numStates, const SGPoint &_dir)
Constructor.
Definition: sgproductpolicy.hpp:48
SGProductPolicy
Class for storing product policies.
Definition: sgproductpolicy.hpp:39
SGPolicy
A policy for the max-min-max algorithm.
Definition: sgpolicy.hpp:34
SGProductPolicy::hash
std::string hash() const
Returns a unique identifier for the product policy.
Definition: sgproductpolicy.cpp:25
SGProductPolicy::erasePolicy
void erasePolicy(int state, const SGPolicy &policy)
Removes a policy from the given state.
Definition: sgproductpolicy.hpp:79
SGProductPolicy::levels
vector< double > levels
Definition: sgproductpolicy.hpp:44
SGProductPolicy::getDir
const SGPoint & getDir() const
Get method for the direction.
Definition: sgproductpolicy.hpp:62
SGException
SGSolve specific exceptions.
Definition: sgexception.hpp:36
policyComp
Function object for comparing two policies.
Definition: sgproductpolicy.hpp:28
SGProductPolicy::clear
void clear()
Clears the policies in all states.
Definition: sgproductpolicy.hpp:97
SG::OUT_OF_BOUNDS
@ OUT_OF_BOUNDS
Definition: sgnamespace.hpp:47
SGProductPolicy::numStates
int numStates() const
Returns the number of states.
Definition: sgproductpolicy.hpp:129
SGProductPolicy::dir
SGPoint dir
Definition: sgproductpolicy.hpp:43
SGProductPolicy::getLevels
const vector< double > & getLevels() const
Get method for levels.
Definition: sgproductpolicy.hpp:66
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGProductPolicy::dimension
int dimension() const
Returns the total number of policies, minus the number of states.
Definition: sgproductpolicy.hpp:116
SGProductPolicy::policies
vector< SGPolicySet > policies
Definition: sgproductpolicy.hpp:41
SGProductPolicy::isempty
bool isempty() const
Returns whether the policy set is empty in any state.
Definition: sgproductpolicy.hpp:104
SGProductPolicy::setLevel
void setLevel(int state, double lvl)
Sets the level in a given state.
Definition: sgproductpolicy.hpp:88
SGProductPolicy::getPolicies
const SGPolicySet & getPolicies(int state) const
Get method for policies in a given state.
Definition: sgproductpolicy.hpp:53
SGProductPolicy::insertPolicy
void insertPolicy(int state, const SGPolicy &policy)
Inserts a new policy in the given state.
Definition: sgproductpolicy.hpp:70