💿🐜 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.

88 lines
3.0 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_DNA_CROSSOVER_HPP
  20. #define ANTKEEPER_DNA_CROSSOVER_HPP
  21. #include <algorithm>
  22. #include <iterator>
  23. #include <random>
  24. namespace dna
  25. {
  26. /**
  27. * Exchanges elements between two ranges, starting at a random offset.
  28. *
  29. * @param first1,last1 First range of elements to crossover.
  30. * @param first2 Beginning of the second range of elements to crossover.
  31. * @param g Uniform random bit generator.
  32. * @return Iterator to the start of the crossover in the second range.
  33. */
  34. template <class ForwardIt1, class ForwardIt2, class URBG>
  35. ForwardIt2 crossover(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, URBG&& g);
  36. /**
  37. * Exchanges elements between two ranges multiple times, starting at a random offset each time.
  38. *
  39. * @param first1,last1 First range of elements to crossover.
  40. * @param first2 Beginning of the second range of elements to crossover.
  41. * @param count Number of times to crossover.
  42. * @param g Uniform random bit generator.
  43. */
  44. template <class ForwardIt1, class ForwardIt2, class Size, class URBG>
  45. void crossover_n(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, Size count, URBG&& g);
  46. template <class ForwardIt1, class ForwardIt2, class URBG>
  47. ForwardIt2 crossover(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, URBG&& g)
  48. {
  49. typedef typename std::iterator_traits<ForwardIt1>::difference_type difference_t;
  50. std::uniform_int_distribution<difference_t> distribution(0, std::distance(first1, last1) - 1);
  51. difference_t pos = distribution(g);
  52. std::advance(first1, pos);
  53. std::advance(first2, pos);
  54. std::swap_ranges(first1, last1, first2);
  55. return first2;
  56. }
  57. template <class ForwardIt1, class ForwardIt2, class Size, class URBG>
  58. void crossover_n(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, Size count, URBG&& g)
  59. {
  60. typedef typename std::iterator_traits<ForwardIt1>::difference_type difference_t;
  61. std::uniform_int_distribution<difference_t> distribution(0, std::distance(first1, last1) - 1);
  62. ForwardIt1 crossover1, crossover2;
  63. while (count)
  64. {
  65. crossover1 = first1;
  66. crossover2 = first2;
  67. difference_t pos = distribution(g);
  68. std::advance(crossover1, pos);
  69. std::advance(crossover2, pos);
  70. std::swap_ranges(crossover1, last1, crossover2);
  71. --count;
  72. }
  73. }
  74. } // namespace dna
  75. #endif // ANTKEEPER_DNA_CROSSOVER_HPP