Browse Source

Add crossover and mutate genetic operators

master
C. J. Howard 3 years ago
parent
commit
cd450aa3d5
4 changed files with 107 additions and 18 deletions
  1. +5
    -3
      src/game/genetics/allele.hpp
  2. +3
    -3
      src/game/genetics/gene.hpp
  3. +87
    -0
      src/game/genetics/genetic-operators.hpp
  4. +12
    -12
      src/game/genetics/zygosity.hpp

+ 5
- 3
src/game/genetics/allele.hpp View File

@ -20,17 +20,19 @@
#ifndef ANTKEEPER_ALLELE_HPP
#define ANTKEEPER_ALLELE_HPP
#include <cstdint>
namespace dna
{
/// Enumerates allele types.
enum class allele: bool
enum allele: std::uint8_t
{
/// Indicates an allele is recessive.
recessive = false,
recessive = 0,
/// Indicates an allele is dominant.
dominant = true
dominant = 1
};
} // namespace dna

+ 3
- 3
src/game/genetics/gene.hpp View File

@ -20,13 +20,13 @@
#ifndef ANTKEEPER_GENE_HPP
#define ANTKEEPER_GENE_HPP
#include "allele.hpp"
#include <cstdint>
namespace dna
{
/// A gene with two alleles.
typedef std::array<allele, 2> gene;
/// A 2-bit gene with two alleles.
typedef std::uint8_t gene;
} // namespace dna

+ 87
- 0
src/game/genetics/genetic-operators.hpp View File

@ -0,0 +1,87 @@
/*
* 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_GENETIC_OPERATORS_HPP
#define ANTKEEPER_GENETIC_OPERATORS_HPP
#include <cstdlib>
#include <iterator>
namespace dna
{
/**
* Performs a genetic crossover on a range of interleaved allele blocks defined by `[first1, last1)`.
*
* @param[in] first1,last1 First range of interleaved allele blocks to crossover.
* @param[in] first2 Beginning of the the second range of interleaved allele blocks to crossover.
* @param[out] d_first Beginning of the destination range.
* @param[in,out] g A uniform random bit generator.
* @return Output iterator to the block past the last block crossed over.
*/
template <class InputIt1, class InputIt2, class OutputIt, class URBG>
OutputIt crossover(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, URBG&& g)
{
typedef typename std::iterator_traits<OutputIt>::value_type block_t;
static constexpr std::size_t bits_per_block = sizeof(block_t) << 3;
while (first1 != last1)
{
block_t block = 0;
for (std::size_t i = 0; i < bits_per_block;)
{
block_t allele1 = ((*first1) >> (i + (g() % 2))) & 1;
block_t allele2 = ((*first2) >> (i + (g() % 2))) & 1;
block |= allele1 << i++;
block |= allele2 << i++;
}
*d_first++ = block;
++first1;
++first2;
}
return d_first;
}
/**
* Performs a genetic mutation by flipping the bit of a single allele in a range of interleaved allele blocks defined by `[first1, last1)`.
*
* @param[in,out] first,last Range of interleaved allele blocks to mutate.
* @param[in,out] g A uniform random bit generator.
* @return Locus of the mutated allele.
*/
template <class RandomIt, class URBG>
typename std::iterator_traits<RandomIt>::difference_type mutate(RandomIt first, RandomIt last, URBG&& g)
{
typedef typename std::iterator_traits<RandomIt>::value_type block_t;
typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
static constexpr diff_t bits_per_block = sizeof(block_t) << 3;
diff_t i = g() % ((last - first) * bits_per_block);
first[i / bits_per_block] ^= block_t(1) << (i % bits_per_block);
return i;
}
} // namespace dna
#endif // ANTKEEPER_GENETIC_OPERATORS_HPP

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

@ -26,39 +26,39 @@ namespace dna
{
/// A homozygous recessive gene.
static constexpr gene homozygous_recessive = {allele::recessive, allele::recessive};
static constexpr gene homozygous_recessive = 0b00;
/// A homozygous dominant gene.
static constexpr gene homozygous_dominant = {allele::recessive, allele::recessive};
static constexpr gene homozygous_dominant = 0b11;
/// Returns `true` if gene @p x is heterozygous, `false` otherwise.
bool is_heterozygous(const gene& x);
bool is_heterozygous(gene x);
/// Returns `true` if gene @p x is homozygous, `false` otherwise.
bool is_homozygous(const gene& x);
bool is_homozygous(gene x);
/// Returns `true` if gene @p x is homozygous recessive, `false` otherwise.
bool is_homozygous_recessive(const gene& x);
bool is_homozygous_recessive(gene x);
/// Returns `true` if gene @p x is homozygous dominant, `false` otherwise.
bool is_homozygous_dominant(const gene& x);
bool is_homozygous_dominant(gene x);
inline bool is_heterozygous(const gene& x)
inline bool is_heterozygous(gene x)
{
return x[0] != x[1];
return x - 1 < 2;
}
inline bool is_homozygous(const gene& x)
inline bool is_homozygous(gene x)
{
return x[0] == x[1];
return (x & 1) == (x >> 1);
}
inline bool is_homozygous_recessive(const gene& x)
inline bool is_homozygous_recessive(gene x)
{
return x == homozygous_recessive;
}
inline bool is_homozygous_dominant(const gene& x)
inline bool is_homozygous_dominant(gene x)
{
return x == homozygous_dominant;
}

Loading…
Cancel
Save