Browse Source

Improve crossover function, add crossover_n, add more bit math utilities, add inherit function

master
C. J. Howard 3 years ago
parent
commit
72f4ab9bda
4 changed files with 158 additions and 32 deletions
  1. +105
    -0
      src/game/genetics/bit-math.hpp
  2. +36
    -15
      src/game/genetics/crossover.hpp
  3. +5
    -5
      src/game/genetics/mutate.hpp
  4. +12
    -12
      src/game/genetics/zygosity.hpp

+ 105
- 0
src/game/genetics/bit-math.hpp View File

@ -52,6 +52,93 @@ constexpr T bit_extract(T x, T mask) noexcept;
template <typename T>
constexpr int popcount(T x) noexcept;
/**
* Returns the number of differing bits between two values, known as *Hamming distance*.
*
* @param x First value.
* @param y Second value.
* @return Hamming distance between @px and @p y.
*/
template <typename T>
constexpr int hamming_distance(T x, T y) noexcept;
template <typename T>
constexpr T bit_merge(T a, T b, T mask) noexcept
{
return a ^ ((a ^ b) & mask);
}
template <class T>
constexpr T bit_pad(T x) noexcept
{
x &= (1 << (sizeof(T) << 2)) - 1;
if constexpr(sizeof(T) >= 8)
x = (x ^ (x << 16)) & T(0x0000ffff0000ffff);
if constexpr(sizeof(T) >= 4)
x = (x ^ (x << 8)) & T(0x00ff00ff00ff00ff);
if constexpr(sizeof(T) >= 2)
x = (x ^ (x << 4)) & T(0x0f0f0f0f0f0f0f0f);
x = (x ^ (x << 2)) & T(0x3333333333333333);
x = (x ^ (x << 1)) & T(0x5555555555555555);
return x;
}
template <typename T>
constexpr T bit_interleave(T a, T b) noexcept
{
return (bit_pad(b) << 1) | bit_pad(a);
}
template <typename T>
constexpr T bit_swap_adjacent(T x) noexcept
{
return ((x & T(0xaaaaaaaaaaaaaaaa)) >> 1) | ((x & T(0x5555555555555555)) << 1);
}
template <typename T>
constexpr T bit_shuffle_adjacent(T x, T mask) noexcept
{
T y = bit_swap_adjacent(x);
return bit_merge(x, y, bit_interleave<T>(mask, mask));
}
/**
* Shuffles the adjacent bits of a value.
*
* @param x Value to shuffle.
* @param g Uniform random bit generator.
* @return Value with adjacent bits shuffled.
*/
/*
template <class T, class URBG>
constexpr T bit_shuffle_adjacent(T x, URBG&& g) noexcept
{
return bit_swap_adjacent(x, static_cast<T>(g()));
}*/
/**
*
*/
template <class T, class U>
U bit_splice(T a, T b, U mask)
{
return bit_deposit(static_cast<U>(a), ~mask) | bit_deposit(static_cast<U>(b), mask);
}
/**
* For an n-bit number with r set bits, there are `n! / ((n - r)! * r!)` permutations.
*/
template <class T>
T next_bit_permutation(T x)
{
T y = (x | (x - 1)) + 1;
return y | ((((y & -y) / (x & -x)) >> 1) - 1);
}
template <class T>
constexpr T bit_deposit(T x, T mask) noexcept
{
@ -91,6 +178,24 @@ constexpr int popcount(T x) noexcept
return n;
}
template <typename T>
inline constexpr int hamming_distance(T x, T y) noexcept
{
return popcount(x ^ y);
}
/**
*
* @param mask Least significant bits indicate whether to inherit the first (0) or second (1) allele from @p a. Most significant bits indicate whether to inherit the first (0) or second (1) allele from @p b.
*/
template <class T>
constexpr T inherit(T a, T b, T mask) noexcept
{
a = bit_shuffle_adjacent<T>(a, mask);
b = bit_shuffle_adjacent<T>(b, mask >> (sizeof(T) << 2));
return bit_merge<T>(a, b, T(0x5555555555555555));
}
} // namespace dna
#endif // ANTKEEPER_BIT_MATH_HPP

+ 36
- 15
src/game/genetics/crossover.hpp View File

@ -20,32 +20,53 @@
#ifndef ANTKEEPER_CROSSOVER_HPP
#define ANTKEEPER_CROSSOVER_HPP
#include "bit-math.hpp"
namespace dna
{
/**
* Performs a genetic crossover between two chromosomes.
* Performs a single-point crossover between two values.
*
* @param a First value.
* @param b Second value.
* @parma pos Position of the crossover point.
*/
template <class T>
constexpr T crossover(T a, T b, int pos) noexcept;
/**
* Performs an n-point crossover between two values.
*
* @param a Chromosome of the first parent.
* @param b Chromosome of the second parent.
* @param g Uniform random bit generator.
* @return Chromosome of the new offspring.
* @param a First value.
* @param b Second value.
* @param mask Bit mask with set bits marking crossover points.
*/
template <class T, class URBG>
T crossover(T a, T b, URBG&& g)
template <class T>
constexpr T crossover_n(T a, T b, T mask) noexcept;
template <class T>
inline constexpr T crossover(T a, T b, int pos) noexcept
{
T c = 0, i = 1;
T mask = (T(1) << pos) - 1;
return bit_merge(b, a, mask);
}
template <class T>
constexpr T crossover_n(T a, T b, T mask) noexcept
{
T merge = 0, i = 0;
do
while (mask)
{
c |= (a >> (g() % 2)) & i;
i <<= 1;
c |= (b << (g() % 2)) & i;
i <<= 1;
merge ^= (mask ^ (mask - 1)) >> 1;
mask &= mask - 1;
i = !i;
}
while (i);
return c;
merge ^= ~T(0) * i;
return bit_merge<T>(a, b, merge);
}
} // namespace dna

+ 5
- 5
src/game/genetics/mutate.hpp View File

@ -24,16 +24,16 @@ namespace dna
{
/**
* Mutates a value by flipping a single random bit.
* Mutates a value by flipping a single bit.
*
* @param x Value to mutate.
* @param g Uniform random bit generator.
* @param i Index of the bit to flip.
* @return Mutated copy of @p x.
*/
template <class T, class URBG>
T mutate(T x, URBG&& g)
template <class T>
T mutate(T x, int i)
{
return x ^ (T(1) << (g() % (sizeof(T) << 3)));
return x ^ (T(1) << i);
}
} // namespace dna

+ 12
- 12
src/game/genetics/zygosity.hpp View File

@ -24,33 +24,33 @@ namespace dna
{
/**
* Tests a gene for heterozygosity.
* Tests the two least significant bits of a value for equality
*
* @param x Gene with alleles encoded in the two least significant bits.
* @return `true` if the gene is heterozygous, `false` otherwise.
* @param x Value to test.
* @return `true` if the two least significant bits are equal, `false` otherwise.
*/
template <class T>
bool is_heterozygous(T x);
constexpr bool homozygous(T x) noexcept;
/**
* Tests a gene for homozygosity.
* Tests the two least significant bits of a value for inequality.
*
* @param x Gene with alleles encoded in the two least significant bits.
* @return `true` if the gene is homozygous, `false` otherwise.
* @param x Value to test.
* @return `true` if the two least significant bits are inequal, `false` otherwise.
*/
template <class T>
bool is_homozygous(T x);
constexpr bool heterozygous(T x) noexcept;
template <class T>
inline bool is_heterozygous<T>(T x)
inline constexpr bool homozygous<T>(T x) noexcept
{
return (x & 1) != (x >> 1);
return (x & 1) == ((x >> 1) & 1);
}
template <class T>
inline bool is_homozygous<T>(T x)
inline constexpr bool heterozygous<T>(T x) noexcept
{
return (x & 1) == (x >> 1);
return (x & 1) != ((x >> 1) & 1);
}
} // namespace dna

Loading…
Cancel
Save