MultiAgentDecisionProcess  Release 0.2.1
BGIP_SolverAlternatingMaximization.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _BGIP_SOLVERALTERNATINGMAXIMIZATION_H_
30 #define _BGIP_SOLVERALTERNATINGMAXIMIZATION_H_ 1
31 
32 /* the include directives */
33 #include <iostream>
34 #include "Globals.h"
35 #include "TimeTools.h"
36 #include <float.h>
37 #include "Referrer.h"
38 
41 
42 
43 #define DEBUG_BGIP_SOLVER_AM_SOLVE 0
44 #define DEBUG_BGIP_SOLVER_AM_SOLVE_EXTRAVERBOSE 0
45 #define CHECK_NEW_BR 0
46 
54 template<class JP>
57 {
58 private:
59 
60  unsigned int _m_nrRestarts;
62 #define DEBUG_CBR 0
63  double ComputeBestResponse(JP & jpolBG,
64  Index optimizingAgentI)
65  {
66  if(DEBUG_CBR)
67  {
68  std::cout << "ComputeBestResponse called - current jpolBG:"<<std::endl;
69  jpolBG.Print();
70  std::cout << "value="<< this->Evaluate(jpolBG) <<std::endl;
71  }
73  Index agI = optimizingAgentI;
74  std::vector< std::vector<double> > v (bgip->GetNrTypes(agI), std::vector<double>(
75  bgip->GetNrActions(agI), 0.0) );
76 
77  //compute all type-action values for agent agI
78  for(Index jt = 0; jt < bgip->GetNrJointTypes(); jt++)
79  {
80  std::vector<Index> types = bgip->JointToIndividualTypeIndices(jt);
81  Index type = types.at(agI);
82  double jtProb = bgip->GetProbability(jt);
83  Index jaI = jpolBG.GetJointActionIndex(jt);
84  std::vector<Index> avec = bgip->JointToIndividualActionIndices(jaI);
85  for(Index acI =0 ; acI < bgip->GetNrActions(agI); acI++)
86  {
87  avec.at(agI) = acI;
88  Index jaInew = bgip->IndividualToJointActionIndices(avec);
89  v[type][acI] += jtProb * bgip->GetUtility(jt, jaInew);
90  }
91  }
92  //compute the best response and value
93  double v_best = 0.0;
94  for(Index type = 0; type < bgip->GetNrTypes(agI); type++)
95  {
96  double v_t = -DBL_MAX;
97  Index best_a = 0;
98  for(Index acI = 0; acI < bgip->GetNrActions(agI); acI++)
99  {
100  double v_t_a = v[type][acI];
101  if(v_t_a > v_t)
102  {
103  best_a = acI;
104  v_t = v_t_a;
105  }
106  }
107  v_best += v_t;
108  jpolBG.SetAction(agI, type, best_a);
109  }
110  return v_best;
111  }
112 
113 protected:
114 
115 public:
116  // Constructor, destructor and copy assignment.
120  unsigned int nrRestarts=10,
121  int verbose=0,
122  size_t nrSolutions=1) :
123  BayesianGameIdenticalPayoffSolver<JP>(bg, nrSolutions),
124  _m_nrRestarts(nrRestarts),
125  _m_verbose(verbose)
126  {
127  }
128 
131  double Solve()
132  {
133  if(_m_verbose >= 1)
134  std::cout << "BGIP_SolverAlternatingMaximization::Solve() started"<<std::endl;
136 
137  struct timeval start_time, cur_time;
138  if(gettimeofday(&start_time, NULL) != 0)
139  throw "Error with gettimeofday";
140 
142  JP jpolBG(*temp);
143  JP best=jpolBG;
144  delete temp;
145  double vmax = -DBL_MAX;
146 
147  for(Index r=0; r < _m_nrRestarts; ++r)
148  {
149  if(_m_verbose >= 1)
150  std::cout << "AM: new restart"<<std::endl;
151  double v_thisAMrun = -DBL_MAX;
152  jpolBG.RandomInitialization();
153 
154  //eval the expected payoff of this jpolBG
155  double v = this->Evaluate(jpolBG);
156 
157  v_thisAMrun = v;
158  Index optimizingAgentI = 0;
159 #if DEBUG_BGIP_SOLVER_AM_SOLVE
160  {
161  std::std::cout<<"\n>new AM restart - initialized with v="<<
162  v_thisAMrun;
163 #if DEBUG_BGIP_SOLVER_AM_SOLVE_EXTRAVERBOSE
164  std::std::cout << " - pol:"<<std::std::endl;
165  jpolBG.Print();
166 #endif
167  std::std::cout << std::std::endl;
168  }
169 #endif
170 
171  bool improving = true;
172  size_t nr_non_improving_agents = 0;
173 
174 //TODO: this fails if (in the 2 agent case) the agent that cannot make
175 // improvement is the first to try
176 // while(nr_non_improving_agents < (bgip->GetNrAgents() - 1) )
177  while(nr_non_improving_agents < bgip->GetNrAgents() )
178  {
179  if(_m_verbose >= 2 )
180  std::cout << "AM: computing new best response...";
181  improving = false;//unless improvement is found
182  //find best response
183  v = ComputeBestResponse(jpolBG, optimizingAgentI);
184  if(_m_verbose >= 2)
185  std::cout << "v="<<v<<std::endl;
186 #if CHECK_NEW_BR
187  JP checkjpolBG = jpolBG;
188  double vcheck =
189  ComputeBestResponseOld(checkjpolBG, optimizingAgentI);
190  if(jpolBG.GetIndex() != checkjpolBG.GetIndex())
191  {
192  std::cout << "Error best response computation failed!!!"<<std::endl;
193  std::cout << "v="<<v<<", vcheck="<<vcheck<<std::endl;
194  std::cout << "jpolBG="<< jpolBG.SoftPrint();
195  std::cout << "checkjpolBG="<< jpolBG.SoftPrint();
196  }
197 #endif
198 #if DEBUG_BGIP_SOLVER_AM_SOLVE
199  {
200  std::std::cout<<"best response achieves v="<< v ;
201 #if DEBUG_BGIP_SOLVER_AM_SOLVE_EXTRAVERBOSE
202  std::std::cout << " - pol:"<<std::std::endl;
203  jpolBG.Print();
204 #endif
205  std::std::cout << std::std::endl;
206  }
207 #endif
208 
209  if(v > v_thisAMrun + 1e-9)
210  {
211 #if DEBUG_BGIP_SOLVER_AM_SOLVE
212  std::std::cout << "(improving)" <<std::std::endl;
213 #endif
214  v_thisAMrun = v;
215  improving = true;
216  }
217 
218  //next agent
219  if(improving)
220  nr_non_improving_agents = 0;
221  else
222  nr_non_improving_agents++;
223 
224  optimizingAgentI = (optimizingAgentI+1) % bgip->GetNrAgents();
225  } // end of this alternating maximization run
227  std::cout << "AM run ended" <<std::endl;
228 
229  size_t oldNrSolutionsFound=
231 
232  //store the solution
233  BayesianGameIdenticalPayoffSolver<JP>::_m_solution.AddSolution( *(new JP(jpolBG)), // make a copy! (jpolBG is local)
234  v_thisAMrun );
235  if(_m_verbose >= 1)
237  != (oldNrSolutionsFound+1))
238  std::cout << "AM: computed policy not added to BGIPSolution, probably a duplicate" << std::endl;
239 
240  if(v_thisAMrun > vmax)
241  {
242  vmax = v_thisAMrun;
243  best = jpolBG;
245  std::cout << "(best solution)" <<std::endl;
246 
248  double delta = TimeTools::GetDeltaTimeDouble(start_time, cur_time);
251  }
252  }
253  }
254  //end the line in the results file
258  }
259 
260  BayesianGameIdenticalPayoffSolver<JP>::_m_solution.SetPolicy(best);//best.Print();
262 
263 #if 0
264  //check the solution
265  std::cout << "BGIP_SolverAlternatingMaximization finished, first solution: " <<
266  _m_solution.GetNextSolution()->SoftPrint() << std::endl;
267 #endif
268  return(vmax);
269  }
270 
271 };
272 
273 
274 #endif /* !_BGIP_SOLVERALTERNATINGMAXIMIZATION_H_ */
275 
276 // Local Variables: ***
277 // mode:c++ ***
278 // End: ***