permutation.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. void permutation::random() {
  18. // Knuth Fisher Yates shuffle
  19. // there is a bias due to the use of rand()%_
  20. for(int i = size-1; i > 1; i--)
  21. transpose(i, 1+rand()%i);
  22. }
  23. int permutation::operator[](int i) {
  24. assert(0 <= i && i < size);
  25. return sigma[i];
  26. }
  27. bool permutation::check() {
  28. if(size != (int)sigma.size() || sigma[0] != 0)
  29. return false;
  30. vector<bool> seen(size);
  31. for(int i = 0; i < size; i++)
  32. seen[sigma[i]] = true;
  33. for(int i = 0; i < size; i++)
  34. if(!seen[i])
  35. return false;
  36. return true;
  37. }
  38. int permutation::get_size() {
  39. return size;
  40. }
  41. // Algorithm:
  42. // for each i (randomly : we use a permutation :P (it always starts with 0 but
  43. // it's OK since a[0]=b[0]=0 and also we want ret[0]=0))
  44. // we choose either a[i] or b[i]
  45. // if one value was already choosen, we choose the other
  46. // if both were, we choose a value we have seen but not choose (in the set disp)
  47. permutation permutation_crossover(permutation &a, permutation &b) {
  48. assert(a.size == b.size);
  49. permutation ret(a.size);
  50. permutation ord(a.size); // order in which we consider elements of permutations a and b
  51. vector<bool> seen(a.size);
  52. set<int> disp;
  53. ord.random();
  54. for(int i = 0; i < a.size; i++) {
  55. int j = ord[i];
  56. bool seen_a = seen[a[j]], seen_b = seen[b[j]];
  57. if(seen_a && seen_b) {
  58. // take a random value in disp
  59. assert(disp.size()>0);
  60. auto it = disp.begin();
  61. advance(it, rand()%disp.size());
  62. ret.sigma[i] = *it;
  63. }
  64. else if(seen_a) {
  65. // take value in b
  66. ret.sigma[i] = b[j];
  67. }
  68. else if(seen_b) {
  69. // take value in a
  70. ret.sigma[i] = a[j];
  71. }
  72. else if(rand()%2) {
  73. // take value in b
  74. ret.sigma[i] = b[j];
  75. disp.insert(a[j]);
  76. }
  77. else {
  78. // take value in a
  79. ret.sigma[i] = a[j];
  80. disp.insert(b[j]);
  81. }
  82. seen[ret[i]] = true;
  83. disp.erase(ret[i]);
  84. }
  85. assert(ret.check());
  86. return ret;
  87. }