Browse Source

Improve bitwise genetic functions

master
C. J. Howard 4 years ago
parent
commit
dbbcf493c8
6 changed files with 292 additions and 115 deletions
  1. +3
    -3
      src/game/genetics/allele.hpp
  2. +52
    -25
      src/game/genetics/bit-math.hpp
  3. +169
    -0
      src/game/genetics/chromosome.hpp
  4. +0
    -74
      src/game/genetics/crossover.hpp
  5. +56
    -0
      src/game/genetics/gene.hpp
  6. +12
    -13
      src/game/genetics/trait.hpp

src/game/genetics/zygosity.hpp → src/game/genetics/allele.hpp View File

@ -17,8 +17,8 @@
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_ZYGOSITY_HPP
#define ANTKEEPER_ZYGOSITY_HPP
#ifndef ANTKEEPER_ALLELE_HPP
#define ANTKEEPER_ALLELE_HPP
namespace dna
{
@ -55,4 +55,4 @@ inline constexpr bool heterozygous(T x) noexcept
} // namespace dna
#endif // ANTKEEPER_ZYGOSITY_HPP
#endif // ANTKEEPER_ALLELE_HPP

+ 52
- 25
src/game/genetics/bit-math.hpp View File

@ -49,7 +49,7 @@ constexpr T bit_extract(T x, T mask) noexcept;
* @param x Value to count.
* @return Number of set bits in @p x.
*/
template <typename T>
template <class T>
constexpr int popcount(T x) noexcept;
/**
@ -59,18 +59,18 @@ constexpr int popcount(T x) noexcept;
* @param y Second value.
* @return Hamming distance between @px and @p y.
*/
template <typename T>
template <class T>
constexpr int hamming_distance(T x, T y) noexcept;
template <typename T>
template <class 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
constexpr T bit_expand(T x) noexcept
{
x &= (1 << (sizeof(T) << 2)) - 1;
@ -81,25 +81,64 @@ constexpr T bit_pad(T x) noexcept
if constexpr(sizeof(T) >= 2)
x = (x ^ (x << 4)) & T(0x0f0f0f0f0f0f0f0f);
x = (x ^ (x << 2)) & T(0x3333333333333333);
x = (x ^ (x << 1)) & T(0x5555555555555555);
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
template <class T, class U = T>
constexpr U interleave(T a, T b) noexcept
{
return (bit_pad(b) << 1) | bit_pad(a);
return bit_expand<U>(a) | (bit_expand<U>(b) << 1);
}
template <typename T>
/**
* Returns the parity of a value.
*
* @param x Value to test.
* @return `1` if an odd number of bits are set, `0` otherwise.
*/
template <class T>
constexpr T parity(T x) noexcept
{
if constexpr(sizeof(T) >= 8)
x ^= x >> 32;
if constexpr(sizeof(T) >= 4)
x ^= x >> 16;
if constexpr(sizeof(T) >= 2)
x ^= x >> 8;
x ^= x >> 4;
return (0x6996 >> (x & 0xf)) & 1;
}
template <class T>
constexpr T deinterleave(T x) noexcept
{
x &= T(0x5555555555555555);
x = (x ^ (x >> 1)) & T(0x3333333333333333);
x = (x ^ (x >> 2)) & T(0x0f0f0f0f0f0f0f0f);
if constexpr(sizeof(T) >= 2)
x = (x ^ (x >> 4)) & T(0x00ff00ff00ff00ff);
if constexpr(sizeof(T) >= 4)
x = (x ^ (x >> 8)) & T(0x0000ffff0000ffff);
if constexpr(sizeof(T) >= 8)
x = (x ^ (x >> 16)) & T(0x00000000ffffffff);
return x;
}
template <class T>
constexpr T bit_swap_adjacent(T x) noexcept
{
return ((x & T(0xaaaaaaaaaaaaaaaa)) >> 1) | ((x & T(0x5555555555555555)) << 1);
}
template <typename T>
template <class T>
constexpr T bit_shuffle_adjacent(T x, T mask) noexcept
{
T y = bit_swap_adjacent(x);
@ -169,7 +208,7 @@ constexpr T bit_extract(T x, T mask) noexcept
return result;
}
template <typename T>
template <class T>
constexpr int popcount(T x) noexcept
{
int n = 0;
@ -178,24 +217,12 @@ constexpr int popcount(T x) noexcept
return n;
}
template <typename T>
template <class 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

+ 169
- 0
src/game/genetics/chromosome.hpp View File

@ -0,0 +1,169 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_CHROMOSOME_HPP
#define ANTKEEPER_CHROMOSOME_HPP
#include "bit-math.hpp"
#include <array>
namespace dna
{
/**
* Segregates the odd and even bits of a value.
*
* @param x Value to segregate.
* @return Value with even bits of @p x in the lower half, and odd bits in the upper half.
*/
template <class T>
T segregate(T x)
{
T odd = deinterleave(x);
T even = deinterleave(x >> 1);
return odd | (even << (sizeof(T) << 2));
}
/**
* Interleaves bits of the lower and upper halves of a value.
*
* @param x Value to desegregate.
* @return Value with bits from the upper half of @p x interleaved with bits from the lower half.
*/
template <class T>
T desegregate(T x)
{
return interleave<T>(x, x >> (sizeof(T) << 2));
}
/**
* Replicates each bit in the lower half of a value.
*
* @param x Value to replicate.
* @return Value of @p x interleaved with itself.
*/
template <class T>
T replicate(T x)
{
x = bit_expand(x);
return x | (x << 1);
}
/**
* Performs
*/
template <class T>
std::array<T, 4> meiosis(T x, T mask)
{
x = segregate(x);
T xl = x & (sizeof(T) << 2);
T xh = x >> (sizeof(T) << 2);
T a = xl;
T b = crossover_n(xl, xh, mask);
T c = crossover_n(xh, xl, mask);
T d = xh;
return {a, b, c, d};
}
/**
*
*
* @param a 2c chromosome of first parent.
* @param b 2c chromosome of second parent.
* @param g Uniform random bit generator. `g()` will be called three times.
* @return 2c chromosome of child.
*/
template <class T, class URBG>
T reproduce(T a, T b, URBG&& g)
{
auto gametes_a = meiosis(a, static_cast<T>(g()));
auto gametes_b = meiosis(b, static_cast<T>(g()));
T i = static_cast<T>(g());
T ca = gametes_a[i & 3];
T cb = gametes_b[(i >> 2) & 3];
return interleave(ca, cb);
}
/**
* Performs a single-point crossover between two values.
*
* @param a First value.
* @param b Second value.
* @param i Index of the crossover point.
* @return Crossed over value.
*/
template <class T>
constexpr T crossover(T a, T b, int i) noexcept;
/**
* Performs an n-point crossover between two values.
*
* @param a First value.
* @param b Second value.
* @param mask Bit mask with set bits marking crossover points.
* @return Crossed over value.
*/
template <class T>
constexpr T crossover_n(T a, T b, T mask) noexcept;
/**
* Mutates a value by flipping a single bit.
*
* @param x Value to mutate.
* @param i Index of the bit to flip.
* @return Mutated copy of @p x.
*/
template <class T>
T mutate(T x, int i);
template <class T>
inline constexpr T crossover(T a, T b, int i) noexcept
{
T mask = (T(1) << i) - 1;
return bit_merge(b, a, mask);
}
template <class T>
constexpr T crossover_n(T a, T b, T mask) noexcept
{
T merge = ~T(0) * parity(mask);
while (mask)
{
merge ^= (mask ^ (mask - 1)) >> 1;
mask &= mask - 1;
}
return bit_merge(a, b, merge);
}
template <class T>
inline T mutate(T x, int i)
{
return x ^ (T(1) << i);
}
} // namespace dna
#endif // ANTKEEPER_CHROMOSOME_HPP

+ 0
- 74
src/game/genetics/crossover.hpp View File

@ -1,74 +0,0 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_CROSSOVER_HPP
#define ANTKEEPER_CROSSOVER_HPP
#include "bit-math.hpp"
namespace dna
{
/**
* 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 First value.
* @param b Second value.
* @param mask Bit mask with set bits marking crossover points.
*/
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 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;
while (mask)
{
merge ^= (mask ^ (mask - 1)) >> 1;
mask &= mask - 1;
i = !i;
}
merge ^= ~T(0) * i;
return bit_merge<T>(a, b, merge);
}
} // namespace dna
#endif // ANTKEEPER_CROSSOVER_HPP

+ 56
- 0
src/game/genetics/gene.hpp View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_GENE_HPP
#define ANTKEEPER_GENE_HPP
namespace dna
{
/**
* Isolates a pair of bits and returns them in the two least significant bits.
*
* @param x Diploid chromosome with interleaved alleles.
* @param i Index of the pair to isolate.
* @return Isolated pair of bits in the two least significant bits.
*/
template <class T>
T isolate(T x, int i)
{
return (x >> (i << 1)) & 0b11;
}
/**
* Isolates a sequence of genes and returns their alleles in the least significant bits.
*
* @param x Diploid chromosome with interleaed alleles.
* @param i Index of the first gene in the sequence.
* @param n Length of the sequence, in genes.
* @return Alleles of the sequeuence in the least significant bits.
*/
template <class T>
T isolate_n(T x, int i, int n)
{
T mask = (T(1) << (n << 1)) - 1;
return (x >> (i << 1)) & mask;
}
} // namespace dna
#endif // ANTKEEPER_GENE_HPP

src/game/genetics/mutate.hpp → src/game/genetics/trait.hpp View File

@ -17,25 +17,24 @@
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_MUTATE_HPP
#define ANTKEEPER_MUTATE_HPP
#ifndef ANTKEEPER_TRAIT_HPP
#define ANTKEEPER_TRAIT_HPP
namespace dna
{
/**
* Mutates a value by flipping a single bit.
*
* @param x Value to mutate.
* @param i Index of the bit to flip.
* @return Mutated copy of @p x.
*/
template <class T>
T mutate(T x, int i)
template <typename T>
T mendelian_variant(T x)
{
return !!x;
}
template <typename T>
T nonmendelian_variant(T x)
{
return x ^ (T(1) << i);
return popcount(x);
}
} // namespace dna
#endif // ANTKEEPER_MUTATE_HPP
#endif // ANTKEEPER_TRAIT_HPP

Loading…
Cancel
Save