MultiAgentDecisionProcess  Release 0.2.1
IndexTools.cpp
Go to the documentation of this file.
1 
28 #include "IndexTools.h"
29 
30 using namespace std;
31 
32 bool IndexTools::Increment(Index& index, size_t nrElems)
33 {
34  index = (index+1) % nrElems;
35  //carry_over:
36  return(index == 0);
37 }
38 
52 bool IndexTools::Increment(vector<Index>& indexVec,
53  const vector<size_t>& nrElems )
54 {
55  size_t vec_size = indexVec.size();
56  if(nrElems.size() != indexVec.size())
57  throw E("IndexTools::Increment - nrElems.size() != indexVec.size()");
58  if(vec_size == 0)
59  {
60  return true;
61  //return true is sufficient
62  //throw E("IndexTools::Increment - vec_size == 0");
63 
64  }
65 
66  bool carry_over = true;
67  Index i = vec_size - 1;
68  while(carry_over /*&& i >= 0* - i is unsigned!*/ )
69  {
70  //start towards templatization:
71  carry_over = Increment( indexVec[i] , nrElems[i] );
72  //old code
73  //indexVec[i] = (indexVec[i] + 1) % nrElems[i];
74  //carry_over = (indexVec[i] == 0);
75  if(i==0)
76  break;//we just incremented the first element
77  else
78  i--;//proceed with previous elem (if carry_over, of course)
79  }
80 
81  return(carry_over);
82 
83 }
84 
85 // ind -> joint
86 
96 Index IndexTools::IndividualToJointIndices(const vector<Index>& indices,
97  const vector<size_t>& nrElems)
98 {
99  size_t vec_size = nrElems.size();
100  if(vec_size == 0)
101  return 0;
102  size_t* step_size=CalculateStepSize(nrElems);
103 
104  Index jointI = 0;
105  for(Index i=0; i < vec_size ; i++)
106  jointI += indices[i] * step_size[i];
107 
108  delete [] step_size;
109 
110  return(jointI);
111 
112 }
122 Index IndexTools::IndividualToJointIndices(const vector<Index>& indices,
123  const vector<size_t>& nrElems, size_t n)
124 {
125  size_t vec_size = n; //let's assume compiler optimizes this away...(?)
126  if(vec_size == 0)
127  return 0;
128  size_t* step_size=CalculateStepSize(nrElems, n);
129 
130  Index jointI = 0;
131  for(Index i=0; i < vec_size ; i++)
132  jointI += indices[i] * step_size[i];
133 
134  delete [] step_size;
135 
136  return(jointI);
137 
138 }
140  const vector<size_t>& nrElems)
141 {
142  size_t vec_size = nrElems.size();
143  size_t* step_size=CalculateStepSize(nrElems);
144 
145  Index jointI = 0;
146  for(Index i=0; i < vec_size ; i++)
147  jointI += indices[i] * step_size[i];
148 
149  delete [] step_size;
150 
151  return(jointI);
152 }
154  const vector<size_t>& step_size)
155 {
156  size_t vec_size = indices.size();
157 
158  Index jointI = 0;
159  for(Index i=0; i < vec_size ; i++)
160  jointI += indices[i] * step_size[i];
161 
162  return(jointI);
163 }
164 
166  const size_t * step_size)
167 {
168  size_t vec_size = indices.size();
169 
170  Index jointI = 0;
171  for(Index i=0; i < vec_size ; i++)
172  jointI += indices[i] * step_size[i];
173 
174  return(jointI);
175 }
177  const size_t* step_size, size_t vec_size)
178 {
179  Index jointI = 0;
180  for(Index i=0; i < vec_size ; i++)
181  jointI += indices[i] * step_size[i];
182 
183  return(jointI);
184 }
185 
187  const vector<size_t> &step_size, size_t vec_size)
188 {
189  Index jointI = 0;
190  for(Index i=0; i < vec_size ; i++)
191  jointI += indices[i] * step_size[i];
192  return(jointI);
193 }
194 
195 //joint->ind
196 
198  const vector<size_t>& nrElems)
199 {
200  size_t* step_size=0;
201  size_t vec_size = nrElems.size();
202  vector<Index> result(vec_size);
203  if(vec_size > 0)
204  {
205  step_size=CalculateStepSize(nrElems);
206 
207  Index remainder = jointI;
208  // TODO vector (push_back) is too slow for this, change to:
209  // Index result[vec_size];
210 
211  // result.reserve(vec_size);
212  for(Index i=0; i < vec_size ; i++)
213  {
214  Index aI = remainder / step_size[i];
215  result[i]= aI;//store this indiv. index
216  remainder = remainder % step_size[i];
217  }
218  delete [] step_size;
219  }
220  return(result);
221  //jointI += indices[i] * step_size[i];
222  // return(jointI);
223 }
224 
226  const size_t * step_size, size_t vec_size )
227 {
228  Index remainder = jointI;
229 
230  Index resultArr[vec_size];
231  for(Index i=0; i < vec_size ; i++)
232  {
233  Index aI = remainder / step_size[i];
234  resultArr[i] = aI;//store this indiv. index
235  //remainder = remainder % step_size[i];
236  remainder -= step_size[i] * aI;
237  }
238  vector<Index> result(&resultArr[0], &resultArr[vec_size]);
239  return(result);
240 }
241 
243  Index jointI,
244  const vector<size_t> &step_size,
245  size_t vec_size
246  )
247 {
248  Index remainder = jointI;
249 
250  Index resultArr[vec_size];
251  for(Index i=0; i < vec_size ; i++)
252  {
253  Index aI = remainder / step_size[i];
254  resultArr[i] = aI;//store this indiv. index
255  //remainder = remainder % step_size[i];
256  remainder -= step_size[i] * aI;
257  }
258  vector<Index> result(&resultArr[0], &resultArr[vec_size]);
259  return(result);
260 }
262  Index jointI,
263  const std::vector<size_t> &stepSize
264  )
265 {
267  jointI, stepSize, stepSize.size()
268  );
269 }
270 
272  const size_t * step_size, size_t vec_size )
273 {
274  Index remainder = jointI;
275 
276  Index* resultArr = new Index[vec_size];
277  for(Index i=0; i < vec_size ; i++)
278  {
279  Index aI = remainder / step_size[i];
280  resultArr[i] = aI;//store this indiv. index
281  //remainder = remainder % step_size[i];
282  remainder -= step_size[i] * aI;
283  }
284  return(resultArr);
285 }
286 size_t * IndexTools::CalculateStepSize(const vector<size_t>& nrElems)
287 {
288  size_t vec_size = nrElems.size();
289  //increment indicates for each agent how many the joint index is
290  //incremented to get the next individual action...
291  size_t *step_size = new size_t[vec_size];
292  //the step_size for the last agent is 1
293  step_size[vec_size-1] = 1;
294  if(vec_size != 1)
295  {
296  Index i = vec_size-2;
297  while(1)
298  {
299  if(i>0)
300  {
301  step_size[i] = nrElems[i+1] * step_size[i+1];
302  i--;
303  }
304  else if(i==0)
305  {
306  step_size[i] = nrElems[i+1] * step_size[i+1];
307  break;
308  }
309  }
310  }
311  return(step_size);
312 }
313 
314 size_t * IndexTools::CalculateStepSize(const vector<size_t>& nrElems, size_t n)
315 {
316  size_t vec_size = n;
317  //increment indicates for each agent how many the joint index is
318  //incremented to get the next individual action...
319  size_t *step_size = new size_t[vec_size];
320  //the step_size for the last agent is 1
321  step_size[vec_size-1] = 1;
322  if(vec_size != 1)
323  {
324  Index i = vec_size-2;
325  while(1)
326  {
327  if(i>0)
328  {
329  step_size[i] = nrElems[i+1] * step_size[i+1];
330  i--;
331  }
332  else if(i==0)
333  {
334  step_size[i] = nrElems[i+1] * step_size[i+1];
335  break;
336  }
337  }
338  }
339  return(step_size);
340 }
341 
342 size_t IndexTools::CalculateNumberOfSequences(size_t o, size_t seqLength)
343 {
344  // sequences have length seqLength,
345  // if the number of options(the branching factor) each time-step is o
346  // then we get:
347  // sum_t=0...seqLength o^t == (o^(seqLength+1) - 1) / (o - 1)
348  // or
349  // sum_t=0...h-1 o^t == (o^h - 1) / (o - 1)
350  return( (size_t)
351  (
352  ( pow((double)o,(double)(seqLength + 1)) - 1 )
353  /
354  ( o - 1 )
355  )
356  );
357 }
358 
359 
360 // LIndex versions
361 
362 
363 
364 bool IndexTools::Increment(LIndex& index, LIndex& nrElems )
365 {
366  index = (index+1) % nrElems;
367  //carry_over:
368  return(index == 0);
369 }
370 
371 bool IndexTools::Increment(vector<LIndex>& indexVec, vector<LIndex>& nrElems )
372 {
373  LIndex vec_size = indexVec.size();
374  if(nrElems.size() != indexVec.size())
375  throw E("IndexTools::Increment - nrElems.size() != indexVec.size()");
376  if(vec_size == 0)
377  {
378  return true;
379  //return true is sufficient
380  //throw E("IndexTools::Increment - vec_size == 0");
381 
382  }
383 
384  bool carry_over = true;
385  LIndex i = vec_size - 1;
386  while(carry_over /*&& i >= 0* - i is unsigned!*/ )
387  {
388  //start towards templatization:
389  carry_over = Increment( indexVec[i] , nrElems[i] );
390  //old code
391  //indexVec[i] = (indexVec[i] + 1) % nrElems[i];
392  //carry_over = (indexVec[i] == 0);
393  if(i==0)
394  break;//we just incremented the first element
395  else
396  i--;//proceed with previous elem (if carry_over, of course)
397  }
398 
399  return(carry_over);
400 
401 }
402 LIndex IndexTools::IndividualToJointIndices(const vector<LIndex>& indices,
403  const vector<LIndex>& nrElems)
404 {
405  LIndex* step_size=CalculateStepSize(nrElems);
406  LIndex vec_size = nrElems.size();
407 
408  LIndex jointI = 0;
409  for(LIndex i=0; i < vec_size ; i++)
410  jointI += indices[i] * step_size[i];
411 
412  delete [] step_size;
413 
414  return(jointI);
415 
416 }
418  const vector<LIndex>& nrElems)
419 {
420  LIndex vec_size = nrElems.size();
421  LIndex* step_size=CalculateStepSize(nrElems);
422 
423  LIndex jointI = 0;
424  for(LIndex i=0; i < vec_size ; i++)
425  jointI += indices[i] * step_size[i];
426 
427  delete [] step_size;
428 
429  return(jointI);
430 }
432  const vector<LIndex>& step_size)
433 {
434  LIndex vec_size = indices.size();
435 
436  LIndex jointI = 0;
437  for(LIndex i=0; i < vec_size ; i++)
438  jointI += indices[i] * step_size[i];
439 
440  return(jointI);
441 }
442 
444  const LIndex * step_size)
445 {
446  LIndex vec_size = indices.size();
447 
448  LIndex jointI = 0;
449  for(LIndex i=0; i < vec_size ; i++)
450  jointI += indices[i] * step_size[i];
451 
452  return(jointI);
453 }
455  const LIndex* step_size, LIndex vec_size)
456 {
457  LIndex jointI = 0;
458  for(LIndex i=0; i < vec_size ; i++)
459  jointI += indices[i] * step_size[i];
460 
461  return(jointI);
462 }
464  const vector<LIndex> &step_size, LIndex vec_size)
465 {
466  LIndex jointI = 0;
467  for(LIndex i=0; i < vec_size ; i++)
468  jointI += indices[i] * step_size[i];
469  return(jointI);
470 }
472  const vector<LIndex>& nrElems)
473 {
474  LIndex* step_size=0;
475  LIndex vec_size = nrElems.size();
476  vector<LIndex> result(vec_size);
477  if(vec_size > 0)
478  {
479  step_size=CalculateStepSize(nrElems);
480  LIndex remainder = jointI;
481  for(LIndex i=0; i < vec_size ; i++)
482  {
483  LIndex aI = remainder / step_size[i];
484  result[i]= aI;//store this indiv. index
485  remainder = remainder % step_size[i];
486  }
487  delete [] step_size;
488  }
489  return(result);
490 }
492  const LIndex * step_size, LIndex vec_size )
493 {
494  LIndex remainder = jointI;
495 
496  LIndex resultArr[vec_size];
497  for(LIndex i=0; i < vec_size ; i++)
498  {
499  LIndex aI = remainder / step_size[i];
500  resultArr[i] = aI;//store this indiv. index
501  //remainder = remainder % step_size[i];
502  remainder -= step_size[i] * aI;
503  }
504  vector<LIndex> result(&resultArr[0], &resultArr[vec_size]);
505  return(result);
506 }
508  const vector<LIndex> &step_size, LIndex vec_size )
509 {
510  LIndex remainder = jointI;
511 
512  LIndex resultArr[vec_size];
513  for(LIndex i=0; i < vec_size ; i++)
514  {
515  LIndex aI = remainder / step_size[i];
516  resultArr[i] = aI;//store this indiv. index
517  //remainder = remainder % step_size[i];
518  remainder -= step_size[i] * aI;
519  }
520  vector<LIndex> result(&resultArr[0], &resultArr[vec_size]);
521  return(result);
522 }
523 
525  const std::vector<LIndex> &stepSize)
526 {
527  return JointToIndividualIndicesStepSize (jointI, stepSize,
528  stepSize.size());
529 }
530 LIndex * IndexTools::CalculateStepSize(const vector<LIndex>& nrElems)
531 {
532  LIndex vec_size = nrElems.size();
533  //increment indicates for each agent how many the joint index is
534  //incremented to get the next individual action...
535  LIndex * step_size = new LIndex[vec_size];
536  //the step_size for the last agent is 1
537  step_size[vec_size-1] = 1;
538  if(vec_size != 1)
539  {
540  LIndex i = vec_size-2;
541  while(1)
542  {
543  if(i>0)
544  {
545  step_size[i] = nrElems[i+1] * step_size[i+1];
546  i--;
547  }
548  else if(i==0)
549  {
550  step_size[i] = nrElems[i+1] * step_size[i+1];
551  break;
552  }
553  }
554  }
555  return(step_size);
556 }
557 
558 Index
560  Index oI, size_t nrA, size_t nrO)
561 {
562  return ( aI * nrO + oI );
563 }
564 Index
566 {
567  return ( (size_t) (aoI / nrO) );
568 }
569 Index
571 {
572  return ( (size_t) (aoI % nrO) );
573 }