💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.7 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_CHROMOSOME_HPP
  20. #define ANTKEEPER_CHROMOSOME_HPP
  21. #include "bit-math.hpp"
  22. #include <array>
  23. namespace dna
  24. {
  25. /**
  26. * Segregates the odd and even bits of a value.
  27. *
  28. * @param x Value to segregate.
  29. * @return Value with even bits of @p x in the lower half, and odd bits in the upper half.
  30. */
  31. template <class T>
  32. T segregate(T x)
  33. {
  34. T odd = deinterleave(x);
  35. T even = deinterleave(x >> 1);
  36. return odd | (even << (sizeof(T) << 2));
  37. }
  38. /**
  39. * Interleaves bits of the lower and upper halves of a value.
  40. *
  41. * @param x Value to desegregate.
  42. * @return Value with bits from the upper half of @p x interleaved with bits from the lower half.
  43. */
  44. template <class T>
  45. T desegregate(T x)
  46. {
  47. return interleave<T>(x, x >> (sizeof(T) << 2));
  48. }
  49. /**
  50. * Replicates each bit in the lower half of a value.
  51. *
  52. * @param x Value to replicate.
  53. * @return Value of @p x interleaved with itself.
  54. */
  55. template <class T>
  56. T replicate(T x)
  57. {
  58. x = bit_expand(x);
  59. return x | (x << 1);
  60. }
  61. /**
  62. * Performs
  63. */
  64. template <class T>
  65. std::array<T, 4> meiosis(T x, T mask)
  66. {
  67. x = segregate(x);
  68. T xl = x & (sizeof(T) << 2);
  69. T xh = x >> (sizeof(T) << 2);
  70. T a = xl;
  71. T b = crossover_n(xl, xh, mask);
  72. T c = crossover_n(xh, xl, mask);
  73. T d = xh;
  74. return {a, b, c, d};
  75. }
  76. /**
  77. *
  78. *
  79. * @param a 2c chromosome of first parent.
  80. * @param b 2c chromosome of second parent.
  81. * @param g Uniform random bit generator. `g()` will be called three times.
  82. * @return 2c chromosome of child.
  83. */
  84. template <class T, class URBG>
  85. T reproduce(T a, T b, URBG&& g)
  86. {
  87. auto gametes_a = meiosis(a, static_cast<T>(g()));
  88. auto gametes_b = meiosis(b, static_cast<T>(g()));
  89. T i = static_cast<T>(g());
  90. T ca = gametes_a[i & 3];
  91. T cb = gametes_b[(i >> 2) & 3];
  92. return interleave(ca, cb);
  93. }
  94. /**
  95. * Performs a single-point crossover between two values.
  96. *
  97. * @param a First value.
  98. * @param b Second value.
  99. * @param i Index of the crossover point.
  100. * @return Crossed over value.
  101. */
  102. template <class T>
  103. constexpr T crossover(T a, T b, int i) noexcept;
  104. /**
  105. * Performs an n-point crossover between two values.
  106. *
  107. * @param a First value.
  108. * @param b Second value.
  109. * @param mask Bit mask with set bits marking crossover points.
  110. * @return Crossed over value.
  111. */
  112. template <class T>
  113. constexpr T crossover_n(T a, T b, T mask) noexcept;
  114. /**
  115. * Mutates a value by flipping a single bit.
  116. *
  117. * @param x Value to mutate.
  118. * @param i Index of the bit to flip.
  119. * @return Mutated copy of @p x.
  120. */
  121. template <class T>
  122. T mutate(T x, int i);
  123. template <class T>
  124. inline constexpr T crossover(T a, T b, int i) noexcept
  125. {
  126. T mask = (T(1) << i) - 1;
  127. return bit_merge(b, a, mask);
  128. }
  129. template <class T>
  130. constexpr T crossover_n(T a, T b, T mask) noexcept
  131. {
  132. T merge = ~T(0) * parity(mask);
  133. while (mask)
  134. {
  135. merge ^= (mask ^ (mask - 1)) >> 1;
  136. mask &= mask - 1;
  137. }
  138. return bit_merge(a, b, merge);
  139. }
  140. template <class T>
  141. inline T mutate(T x, int i)
  142. {
  143. return x ^ (T(1) << i);
  144. }
  145. } // namespace dna
  146. #endif // ANTKEEPER_CHROMOSOME_HPP