BCESolve
bcegame.hpp
1// This file is part of the BCESolve library for games of incomplete
2// information
3// Copyright (C) 2022 Benjamin A. Brooks
4//
5// BCESolve free software: you can redistribute it and/or modify it
6// under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// BCESolve is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see
17// <http://www.gnu.org/licenses/>.
18//
19// Benjamin A. Brooks
20// ben@benjaminbrooks.net
21// Chicago, IL
22
23// Game class for BCE
24// BAB 12/29/2011
25#ifndef BCEGAME_HPP
26#define BCEGAME_HPP
27
28#include "bceabstractgame.hpp"
29#include <boost/archive/text_iarchive.hpp>
30#include <boost/archive/text_oarchive.hpp>
31
32
34
40{
46private:
48
49 vector< vector< vector<double> > > objectiveData;
51
52 vector<double> priorData;
54
55 vector< vector<double> > conditionalData;
57
58 vector< vector< vector<bool> > > dominatedData;
60
61 vector< vector< vector<bool> > > feasibleDeviationData;
62
63public:
65 BCEGame();
66
68
69 BCEGame(const BCEAbstractGame & game);
70
73 {}
74
76
79 double prior(int state, const vector<int> &types) const
80 {
81 return priorData[state]
82 *conditionalData[state][types[0] + types[1] * numTypes[0]];
83 } // prior
84
86
87 double prior(int state) const
88 {
89 return priorData[state];
90 } // prior
91
93 double prior(int player, int type) const
94 {
95 double probSum = 0;
96 vector<int> types (2,0);
97 types[player] = type;
98 for (int state = 0; state < numStates; state++)
99 {
100 for (types[1-player]=0;
101 types[1-player]<numTypes[1-player];
102 types[1-player]++)
103 probSum += prior(state,types);
104 }
105 return probSum;
106 } // prior
107
109
111 double conditional(int state, const vector<int> &types) const
112 {
113 return conditionalData[state][types[0] + types[1] * numTypes[0]];
114 } // prior
115
117 bool setConditional(int state, const vector<int> &types, double value)
118 {
119 conditionalData[state][types[0] + types[1] * numTypes[0]] = value;
120
121 return true;
122 } // setPrior
123
124 bool setPrior(int state, double value)
125 {
126 priorData[state] = value;
127
128 return true;
129 } // setPrior
130
132
137 double objective(int state, const vector<int> &actions,
138 int obj) const
139 {
140 return objectiveData[obj][state]
141 [actions[0] + actions[1]*numActions[0]];
142 } // objective
143
145 bool setObjective(int state, const vector<int> &actions,
146 int obj, double value)
147 {
148 objectiveData[obj][state]
149 [actions[0] + actions[1]*numActions[0]] = value;
150 return true;
151 } // setObjective
152
154
158 bool dominated(int action, int type, int player) const
159 {
160 return dominatedData[player][type][action];
161 } // dominated
162
164 void setDominated(int action, int type, int player, bool value)
165 {
166 dominatedData[player][type][action] = value;
167 } // setDominated
168
170
174 bool feasibleDeviation(int action, int dev,
175 int type, int player) const
176 {
177 return feasibleDeviationData[player][type][action+dev*numActions[player]];
178 } // feasibleDeviation
179
181 bool setFeasibleDeviation(int action, int dev,
182 int type, int player, bool value)
183 {
184 feasibleDeviationData[player][type][action+dev*numActions[player]] = value;
185
186 return true;
187 } // setFeasibleDeviation
188
190 bool addObjective(int position,string label);
192 bool removeObjective(int obj);
194 bool addState(int position);
196 bool removeState(int state);
198 bool addType(int player, int position);
200 bool removeType(int player, int type);
202 bool addAction(int player, int position);
204 bool removeAction(int player, int action);
205
207 template <class Archive>
208 void serialize(Archive & ar, const unsigned int version)
209 {
210 ar & numPlayers;
211 ar & numActions;
212 ar & numTypes;
213 ar & numStates;
214 ar & numObjectives;
215 ar & objectiveLabels;
216 ar & objectiveData;
217 ar & priorData;
218 ar & conditionalData;
219 ar & dominatedData;
222 ar & numPrivateStates;
223 } // serialize
224
226 static void save(const BCEGame & game, const char* filename)
227 {
228 ofstream ofs(filename);
229
230 if (ofs.good())
231 {
232 boost::archive::text_oarchive oa(ofs);
233 oa << game;
234 ofs.close();
235 }
236 else
238 }
239
241 static void load(BCEGame & game, const char* filename)
242 {
243 ifstream ifs(filename);
244
245 if (ifs.good())
246 {
247 boost::archive::text_iarchive ia(ifs);
248 ia >> game;
249 ifs.close();
250 }
251 else
253 }
254
255 friend class boost::serialization::access;
256}; // BCEGame
257
258#endif
The base class for games of incomplete information.
Definition: bceabstractgame.hpp:52
int numObjectives
The number of objective functions. Must be >= 2.
Definition: bceabstractgame.hpp:69
vector< int > numTypes
The number of private types for each player.
Definition: bceabstractgame.hpp:67
int numStates
The number of payoff relevant states.
Definition: bceabstractgame.hpp:65
vector< int > numActions
The number of action profiles for each player.
Definition: bceabstractgame.hpp:63
int numPlayers
The number of players, always 2.
Definition: bceabstractgame.hpp:61
bool hasProductStructureData
Indicates if the state has a product structure.
Definition: bceabstractgame.hpp:74
vector< int > numPrivateStates
Number of states for each player.
Definition: bceabstractgame.hpp:80
vector< string > objectiveLabels
Labels for the objectives.
Definition: bceabstractgame.hpp:82
Exception class for BCESolve.
Definition: bceexception.hpp:33
@ FailedOpen
Definition: bceexception.hpp:46
The base class for games of incomplete information.
Definition: bcegame.hpp:40
bool removeState(int state)
Removes state from the game.
Definition: bcegame.cpp:176
static void save(const BCEGame &game, const char *filename)
Serialize a BCEGame object using Boost.
Definition: bcegame.hpp:226
bool removeType(int player, int type)
Remove type for player.
Definition: bcegame.cpp:224
bool addAction(int player, int position)
Add action for player at position.
Definition: bcegame.cpp:255
void serialize(Archive &ar, const unsigned int version)
Serialization routine.
Definition: bcegame.hpp:208
double conditional(int state, const vector< int > &types) const
Conditional distribution of types given states.
Definition: bcegame.hpp:111
bool removeAction(int player, int action)
Remove action for player.
Definition: bcegame.cpp:317
bool addType(int player, int position)
Add type for player at position.
Definition: bcegame.cpp:192
vector< vector< double > > conditionalData
Data for conditional distributions of types.
Definition: bcegame.hpp:55
bool removeObjective(int obj)
Removes the objective obj.
Definition: bcegame.cpp:147
double prior(int state, const vector< int > &types) const
Prior over state and types.
Definition: bcegame.hpp:79
vector< vector< vector< bool > > > dominatedData
Data for the dominated array.
Definition: bcegame.hpp:58
void setDominated(int action, int type, int player, bool value)
Set dominated.
Definition: bcegame.hpp:164
vector< double > priorData
Data for the prior.
Definition: bcegame.hpp:52
bool addState(int position)
Adds a new state after position.
Definition: bcegame.cpp:159
BCEGame()
Default constructor.
Definition: bcegame.cpp:25
static void load(BCEGame &game, const char *filename)
Deserialize a BCEGame object using Boost.
Definition: bcegame.hpp:241
bool feasibleDeviation(int action, int dev, int type, int player) const
Check if deviation is feasible.
Definition: bcegame.hpp:174
bool dominated(int action, int type, int player) const
Indicates if a combination of actions and types is dominated.
Definition: bcegame.hpp:158
bool setConditional(int state, const vector< int > &types, double value)
Sets the prior.
Definition: bcegame.hpp:117
double prior(int player, int type) const
Prior over types.
Definition: bcegame.hpp:93
bool setObjective(int state, const vector< int > &actions, int obj, double value)
Sets the objective.
Definition: bcegame.hpp:145
~BCEGame()
Destructor.
Definition: bcegame.hpp:72
bool setFeasibleDeviation(int action, int dev, int type, int player, bool value)
Set feasible.
Definition: bcegame.hpp:181
double prior(int state) const
Prior over state.
Definition: bcegame.hpp:87
bool addObjective(int position, string label)
Adds a new objective after position.
Definition: bcegame.cpp:132
vector< vector< vector< double > > > objectiveData
Data for the objectives.
Definition: bcegame.hpp:49
double objective(int state, const vector< int > &actions, int obj) const
Objective function.
Definition: bcegame.hpp:137
vector< vector< vector< bool > > > feasibleDeviationData
Data for the feasibleDeviation array.
Definition: bcegame.hpp:61