MultiAgentDecisionProcess  Release 0.2.1
TOICompactRewardDecPOMDPDiscrete.cpp
Go to the documentation of this file.
1 
29 
30 using namespace std;
31 
32 #define DEBUG_TOICompactRewardDecPOMDPDiscrete 0
33 
36  string name, string descr, string pf) :
37  TOIDecPOMDPDiscrete(name, descr, pf),
38  _m_nrTwoAgentStates(2,0),
39  _m_nrTwoAgentActions(2,0)
40 {
41  _m_initialized = false;
42 }
43 
46 {
47  throw(E("TOICompactRewardDecPOMDPDiscrete: copy ctor not yet implemented"));
48 }
49 
51 {
52  for(unsigned int i=0;i!=_m_p_rModels.size();++i)
53  delete(_m_p_rModels[i]);
54 }
55 
56 //Copy assignment operator
58 TOICompactRewardDecPOMDPDiscrete::operator=
60 {
61  if (this == &o) return *this; // Gracefully handle self assignment
62  // Put the normal assignment duties here...
63 
64  throw(E("TOICompactRewardDecPOMDPDiscrete: assignment not yet implemented"));
65 
66  return *this;
67 }
68 
70 {
71  if(b == false)
72  {
73  _m_initialized = false;
74  return(true);
75  }
76 
78  {
79  if( b == true )
80  {
81  if (_m_p_rModels.size()!=GetNrAgents())
82  throw E("TOICompactRewardDecPOMDPDiscrete::SetInitialized(true) : no reward models specified yet!");
83 
84  for(unsigned int i=0;i!=GetNrAgents();++i)
85  if (_m_p_rModels[i] == 0)
86  throw E("TOICompactRewardDecPOMDPDiscrete::SetInitialized(true) : no reward model specified yet! ( _m_p_rModels[i] == 0 )");
87 
88  size_t nrStates=GetIndividualDecPOMDPD(0)->GetNrStates();
89  for(unsigned int i=0;i!=GetNrAgents();++i)
90  if(GetIndividualDecPOMDPD(i)->GetNrStates()!=nrStates)
91  throw E("TOICompactRewardDecPOMDPDiscrete::SetInitialized(true) : all agents are required to have the same state space (same number of individual states)");
92  }
93 
94  for(Index i=0;i!=2;++i)
95  {
98  }
99 
100  _m_initialized = b;
101  return(true);
102  }
103  else
104  return(false);
105 }
106 
108 {
111 }
112 
114  const std::vector<Index> &indSIs,
115  const std::vector<Index> &indAIs) const
116 {
117  double reward=0;
118 
119  for(unsigned int i=0;i!=GetNrAgents();++i)
120  reward+=GetIndividualReward(indSIs[i],indAIs[i],i);
121 
122  switch(GetNrAgents())
123  {
124  case 2:
125  reward+=_m_p_rModel->Get(indSIs,indAIs);
126  break;
127  default:
128  {
129  vector<Index> indexVec(2,0);
130  vector<size_t> nrElems(2,GetNrAgents());
131 
132  do
133  {
134 #if 0
135  if(indexVec[0]!=indexVec[1] &&
136  GetTwoAgentReward(indexVec[0],indexVec[1],indSIs,indAIs)!=0)
137  cout << "adding i " << indexVec[0]
138  << " si " << indSIs[indexVec[0]]
139  << GetIndividualMADPD(indexVec[0])->GetState(indSIs[indexVec[0]])->SoftPrint()
140  << " j " << indexVec[1]
141  << " sj " << indSIs[indexVec[1]]
142  << GetIndividualMADPD(indexVec[1])->GetState(indSIs[indexVec[1]])->SoftPrint()
143  << " r "
144  << GetTwoAgentReward(indexVec[0],indexVec[1],
145  indSIs,indAIs) << endl;
146 #endif
147  if(indexVec[0]!=indexVec[1])
148  reward+=GetTwoAgentReward(indexVec[0],indexVec[1],
149  indSIs,indAIs);
150  }
151  while(!IndexTools::Increment(indexVec,nrElems));
152 
153 #if 0
154  reward2+=GetTwoAgentReward(0,1,indSIs,indAIs);
155  reward2+=GetTwoAgentReward(1,0,indSIs,indAIs);
156 
157  reward2+=GetTwoAgentReward(0,2,indSIs,indAIs);
158  reward2+=GetTwoAgentReward(2,0,indSIs,indAIs);
159 
160  reward2+=GetTwoAgentReward(1,2,indSIs,indAIs);
161  reward2+=GetTwoAgentReward(2,1,indSIs,indAIs);
162 #endif
163  break;
164  }
165  }
166 
167 #if DEBUG_TOICompactRewardDecPOMDPDiscrete
168  cout << "GetReward(" << sI << "," << jaI << ") = " << reward << endl;
169 #endif
170  return(reward);
171 }
172 
173 double
176  const vector<Index> &indSIs,
177  const vector<Index> &indAIs) const
178 {
179  vector<Index> sIs(2,0), aIs(2,0);
180 
181  sIs[0]=indSIs[i]; aIs[0]=indAIs[i];
182  sIs[1]=indSIs[j]; aIs[1]=indAIs[j];
183 #if 0
184  double reward=_m_p_rModel->
187 #endif
188 
189  double reward=_m_p_rModel->Get(sIs,aIs);
190 
191 #if 0
192  cout << reward << "indSIs " << SoftPrintVector(indSIs)
193  << SoftPrintVector(sIs) << " indAIs "
194  << SoftPrintVector(indAIs)
196  << SoftPrintVector(aIs)
198  << " i " << i << " j " << j << endl;
199 #endif
200  return(reward);
201 }
202 
205  Index agentID)
206 {
207  if(_m_p_rModels.size()<=agentID)
208  _m_p_rModels.resize(agentID+1);
209 
210  _m_p_rModels[agentID]=rewardModel;
211 }
212 
214 GetIndividualReward(Index indSI, Index indAI, Index agentID) const
215 {
216  double reward=_m_p_rModels[agentID]->Get(indSI,indAI);
217 #if DEBUG_TOICompactRewardDecPOMDPDiscrete
218  cout << "GetIndividualReward[" << agentID << "](" << indSI << "," << indAI
219  << ") = " << reward << endl;
220 #endif
221  return(reward);
222 }
223 
225 {
226  stringstream ss;
228 
229  if(_m_initialized)
230  {
231  ss << "Reward models: " << endl;
232  for(unsigned int i=0;i!=GetNrAgents();++i)
233  {
234  ss << "Individual rewards for agent " << i << endl;
235  ss << _m_p_rModels[i]->SoftPrint();
236  }
237  }
238  else
239  throw E("TOICompactRewardDecPOMDPDiscrete components (reward model) not initialized");
240 
241  return(ss.str());
242 }