SGSolve
sgtuple.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 _SGTUPLE_HPP
23 #define _SGTUPLE_HPP
24 
25 #include "sgcommon.hpp"
26 #include "sgpoint.hpp"
27 #include "sgexception.hpp"
28 
30 
37 {
38 private:
39  double tol;
40 public:
41  SGPointComparator(double _tol):
42  tol(_tol)
43  {}
44 
45  bool operator()(const SGPoint & pnt0, const SGPoint & pnt1) const
46  {
47  return SGPoint::distance(pnt0,pnt1) < tol;
48  }
49 };
50 
51 class SGTuple
52 {
53 private:
56 public:
58  SGTuple(int numStates)
59  { points = vector<SGPoint> (numStates); }
60 
62  SGTuple(int _numStates,
63  const SGPoint & point)
64  {
65  points = vector<SGPoint> (_numStates,point);
66  }
67 
69  SGTuple() {}
70  ~SGTuple() {}
71 
73 
75  SGPoint expectation(const vector<double> & prob) const;
76 
78 
80  double expectation(const vector<double> & prob, int player) const;
81 
83  SGPoint average() const;
85  double average(int player) const;
86 
88  static double distance(const SGTuple& t0,
89  const SGTuple& t1);
91 
93  bool strictlyLessThan(const SGTuple & tuple, int coordinate) const;
94 
96  bool anyNaN() const;
97 
99  void roundTuple(double tol);
100 
102  double max(int coordinate) const;
104  double min(int coordinate) const;
105 
107  double max(int coordinate, int & index) const;
109  double min(int coordinate, int & index) const;
110 
112  SGPoint max() const;
114  SGPoint min() const;
115 
117 
118  void maxmin(int coordinate,
119  double & max, int & maxIndex,
120  double & min,int & minIndex) const;
121 
123  void push_back(const SGPoint & point);
125  void clear();
127  void erase(int start, int end);
129  void emplace(int location,const SGPoint & point);
131  void unique(double tol);
132 
134  int size() const { return points.size(); }
135 
136  // Operators
138  inline SGPoint& operator[](int state)
139  {
140  // state = state % points.size(); // Wrap around
141  // if(state < 0 ||
142  // state >= points.size())
143  // throw(SGException(SG::OUT_OF_BOUNDS));
144  // if (points.size() == 0)
145  // throw(SGException(SG::EMPTY_TUPLE));
146 
147  return points[state];
148  }
149 
151  inline const SGPoint& operator[](int state) const
152  {
153  // if (points.size() == 0)
154  // throw(SGException(SG::EMPTY_TUPLE));
155  // if(state < 0 ||
156  // state >= points.size())
157  // throw(SGException(SG::OUT_OF_BOUNDS));
158  // state = state % points.size(); // Wrap around
159 
160  return points[state];
161  }
162 
164  SGTuple& operator=(const SGTuple & rhs);
166  SGTuple& operator+=(const SGTuple & rhs);
168  SGTuple& operator-=(const SGTuple & rhs);
170 
171  SGTuple& operator+=(const SGPoint & rhs);
173 
174  SGTuple& operator-=(const SGPoint & rhs);
176  SGTuple& operator*=(double d);
178  SGTuple& operator/=(double d);
180  const SGTuple operator+(const SGTuple & rhs) const;
182  const SGTuple operator+(const SGPoint & rhs) const;
184  const SGTuple operator-(const SGTuple & rhs) const;
186  const SGTuple operator-(const SGPoint & rhs) const;
188  friend SGTuple operator*(double d,const SGTuple & point);
190  friend SGTuple operator*(const SGTuple & point,double d);
192  friend SGTuple operator/(const SGTuple & point,double d);
194  friend ostream& operator<<(ostream& out, const SGTuple& rhs);
195 
197  template<class Archive>
198  void serialize(Archive &ar, const unsigned int version)
199  {
200  ar & points;
201  }
202 
203  friend class boost::serialization::access;
204 }; // SGTuple
205 
206 #endif
SGTuple::points
vector< SGPoint > points
Definition: sgtuple.hpp:54
SGPoint::distance
static double distance(const SGPoint &p0, const SGPoint &p1)
Calculates distance between p0 and p1 in the sup-norm.
Definition: sgpoint.cpp:96
SGTuple::maxmin
void maxmin(int coordinate, double &max, int &maxIndex, double &min, int &minIndex) const
Max and min.
Definition: sgtuple.cpp:320
SGTuple::operator+
const SGTuple operator+(const SGTuple &rhs) const
Adds corresponding elements of lhs and rhs.
Definition: sgtuple.cpp:159
SGTuple::size
int size() const
Returns the number of points in the tuple.
Definition: sgtuple.hpp:134
SGTuple::average
SGPoint average() const
Returns the average of the points in the tuple.
Definition: sgtuple.cpp:50
SGTuple::serialize
void serialize(Archive &ar, const unsigned int version)
Serialize using boost.
Definition: sgtuple.hpp:198
SGTuple::strictlyLessThan
bool strictlyLessThan(const SGTuple &tuple, int coordinate) const
Strictly less than.
Definition: sgtuple.cpp:206
SGPointComparator
Tuple of SGPoint objects.
Definition: sgtuple.hpp:37
SGPointComparator::tol
double tol
Definition: sgtuple.hpp:39
SGTuple::operator-
const SGTuple operator-(const SGTuple &rhs) const
Subtracts corresponding elements of rhs from lhs.
Definition: sgtuple.cpp:165
SGTuple::operator*
friend SGTuple operator*(double d, const SGTuple &point)
Left scalar multiplication of a tuple.
Definition: sgtuple.cpp:171
SGTuple::operator[]
SGPoint & operator[](int state)
Random access operator.
Definition: sgtuple.hpp:138
SGTuple::operator*=
SGTuple & operator*=(double d)
Scalar multiplication of the tuple.
Definition: sgtuple.cpp:142
SGTuple::min
SGPoint min() const
Returns a point equal to the pointwise minimum.
Definition: sgtuple.cpp:285
SGTuple::roundTuple
void roundTuple(double tol)
Round off significant digits smaller than tol.
Definition: sgtuple.cpp:341
SGTuple::erase
void erase(int start, int end)
Erases elements.
Definition: sgtuple.cpp:246
SGTuple::SGTuple
SGTuple(int _numStates, const SGPoint &point)
Initializes tuple to _numPoints copies of point.
Definition: sgtuple.hpp:62
SGTuple::operator/=
SGTuple & operator/=(double d)
Scalar division of the tuple.
Definition: sgtuple.cpp:149
std::vector< SGPoint >
Definition: sgstl.cpp:25
SGTuple::emplace
void emplace(int location, const SGPoint &point)
Adds an element at the location.
Definition: sgtuple.cpp:251
SGTuple::anyNaN
bool anyNaN() const
Check if any of the entries are NaN.
Definition: sgtuple.cpp:226
SGTuple::distance
static double distance(const SGTuple &t0, const SGTuple &t1)
Calculates the distance between t0 and t1 in the sup norm.
Definition: sgtuple.cpp:193
SGTuple::max
SGPoint max() const
Returns a point equal to the pointwise maximum.
Definition: sgtuple.cpp:280
SGTuple::operator/
friend SGTuple operator/(const SGTuple &point, double d)
Right scalar division of a tuple.
Definition: sgtuple.cpp:177
SGTuple::operator=
SGTuple & operator=(const SGTuple &rhs)
Assignment operator.
Definition: sgtuple.cpp:96
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGTuple::unique
void unique(double tol)
Removes non-unique elements.
Definition: sgtuple.cpp:256
SGTuple::operator+=
SGTuple & operator+=(const SGTuple &rhs)
Augmented addition of tuples.
Definition: sgtuple.cpp:105
SGTuple::clear
void clear()
Sets the tuple equal to an empty tuple.
Definition: sgtuple.cpp:241
SGTuple::operator<<
friend ostream & operator<<(ostream &out, const SGTuple &rhs)
Output formatted tuple to file stream.
Definition: sgtuple.cpp:180
SGTuple::SGTuple
SGTuple(int numStates)
Initializes tuple to _numStates zero-vectors.
Definition: sgtuple.hpp:58
SGTuple::operator-=
SGTuple & operator-=(const SGTuple &rhs)
Augmented subtraction of tuples.
Definition: sgtuple.cpp:125
SGTuple::operator[]
const SGPoint & operator[](int state) const
Const random access operator.
Definition: sgtuple.hpp:151
SGTuple::expectation
SGPoint expectation(const vector< double > &prob) const
Mathematical expectation.
Definition: sgtuple.cpp:27
SGTuple::push_back
void push_back(const SGPoint &point)
Adds a new point to the back of the tuple.
Definition: sgtuple.cpp:236
SGTuple
Definition: sgtuple.hpp:52
SGTuple::SGTuple
SGTuple()
Default constructor for empty tuple.
Definition: sgtuple.hpp:69