permutation.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "permutation.h"
  2. #include <algorithm> // swap, advance
  3. #include <cstdlib> // rand
  4. #include <cassert>
  5. #include <set>
  6. using namespace std;
  7. permutation::permutation(int size) : size(size), sigma(size) {
  8. assert(size > 0);
  9. for(int i = 0; i < size; i++)
  10. sigma[i] = i;
  11. }
  12. void permutation::transpose(int i, int j) {
  13. assert(0 < i && i < size);
  14. assert(0 < j && j < size);
  15. std::swap(sigma[i], sigma[j]);
  16. }
  17. // TODO rapport : dire qu'on garde l'invariant pi[0] = 0
  18. void permutation::random() {
  19. // Knuth shuffle
  20. // there is a bias due to the use of rand()%_
  21. for(int i = 1; i < size; i++)
  22. transpose(i, 1+rand()%i);
  23. }
  24. int permutation::operator[](int i) {
  25. assert(0 <= i && i < size);
  26. return sigma[i];
  27. }
  28. bool permutation::check() {
  29. if(size != (int)sigma.size() || sigma[0] != 0)
  30. return false;
  31. vector<bool> seen(size);
  32. for(int i = 0; i < size; i++)
  33. seen[sigma[i]] = true;
  34. for(int i = 0; i < size; i++)
  35. if(!seen[i])
  36. return false;
  37. return true;
  38. }
  39. // Algorithm:
  40. // for each i (randomly : we use a permutation :P (it always starts with 0 but
  41. // it's OK since a[0]=b[0]=0 and also we want ret[0]=0))
  42. // we choose either a[i] or b[i]
  43. // if one value was already choosen, we choose the other
  44. // if both were, we choose a value we have seen but not choose (in the set disp)
  45. permutation permutation_crossover(permutation &a, permutation &b) {
  46. assert(a.size == b.size);
  47. permutation ret(a.size);
  48. permutation ord(a.size); // order in which we consider elements of permutations a and b
  49. vector<bool> seen(a.size);
  50. set<int> disp;
  51. ord.random();
  52. for(int i = 0; i < a.size; i++) {
  53. int j = ord[i];
  54. bool seen_a = seen[a[j]], seen_b = seen[b[j]];
  55. if(seen_a && seen_b) {
  56. // take a random value in disp
  57. assert(disp.size()>0);
  58. auto it = disp.begin();
  59. advance(it, rand()%disp.size());
  60. ret.sigma[i] = *it;
  61. }
  62. else if(seen_a) {
  63. // take value in b
  64. ret.sigma[i] = b[j];
  65. }
  66. else if(seen_b) {
  67. // take value in a
  68. ret.sigma[i] = a[j];
  69. }
  70. else if(rand()%2) {
  71. // take value in b
  72. ret.sigma[i] = b[j];
  73. disp.insert(a[j]);
  74. }
  75. else {
  76. // take value in a
  77. ret.sigma[i] = a[j];
  78. disp.insert(b[j]);
  79. }
  80. seen[ret[i]] = true;
  81. disp.erase(ret[i]);
  82. }
  83. assert(ret.check());
  84. return ret;
  85. }