Skip to content

Commit

Permalink
rename crossover_dtl.hpp -> crossover_impl.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Oct 11, 2023
1 parent 170a2b5 commit a8ad756
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/crossover/binary.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#include "binary.hpp"
#include "crossover_dtl.hpp"
#include "crossover_impl.hpp"
#include "../core/candidate.hpp"
#include "../utility/rng.hpp"
#include "../utility/utility.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#define GA_CROSSOVER_DTL_HPP

#include "../core/candidate.hpp"
#include "../utility/small_vector.hpp"
#include <vector>
#include <span>
#include <unordered_map>
#include <type_traits>
#include <concepts>
Expand All @@ -14,7 +16,7 @@ namespace gapp::crossover::dtl
{
/* General n-point crossover implementation for any gene type. */
template<typename T>
CandidatePair<T> nPointCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, std::vector<size_t> crossover_points);
CandidatePair<T> nPointCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, small_vector<size_t> crossover_points);

/* Simpler single-point crossover function for any gene type. */
template<typename T>
Expand Down Expand Up @@ -45,11 +47,11 @@ namespace gapp::crossover::dtl

/* Implementation of the position crossover for any gene type, only generates a single child. */
template<typename T>
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, const std::vector<size_t>& indices);
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, std::span<const size_t> indices);

/* Implementation of the position crossover for unsigned integer genes, only generates a single child. */
template<std::unsigned_integral T>
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, const std::vector<size_t>& indices);
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, std::span<const size_t> indices);


/* Find the indices of genes in the chromosomes chrom1 and chrom2 which belong to odd cycles. Used in the cycle crossover operator. */
Expand Down Expand Up @@ -95,20 +97,18 @@ namespace gapp::crossover::dtl

/* IMPLEMENTATION */

#include "../utility/rng.hpp"
#include "../utility/algorithm.hpp"
#include "../utility/functional.hpp"
#include "../utility/iterators.hpp"
#include "../utility/utility.hpp"
#include <array>
#include <unordered_set>
#include <algorithm>
#include <stdexcept>

namespace gapp::crossover::dtl
{
template<typename T>
CandidatePair<T> nPointCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, std::vector<size_t> crossover_points)
CandidatePair<T> nPointCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, small_vector<size_t> crossover_points)
{
const size_t chrom_len = parent1.chromosome.size();

Expand Down Expand Up @@ -294,7 +294,7 @@ namespace gapp::crossover::dtl


template<typename T>
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, const std::vector<size_t>& indices)
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, std::span<const size_t> indices)
{
GAPP_ASSERT(std::all_of(indices.begin(), indices.end(), detail::between(0_sz, parent1.chromosome.size() - 1)));
GAPP_ASSERT(parent1.chromosome.size() == parent2.chromosome.size());
Expand All @@ -317,7 +317,7 @@ namespace gapp::crossover::dtl
}

template<std::unsigned_integral T>
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, const std::vector<size_t>& indices)
Candidate<T> positionCrossoverImpl(const Candidate<T>& parent1, const Candidate<T>& parent2, std::span<const size_t> indices)
{
const size_t chrom_len = parent1.chromosome.size();

Expand Down
2 changes: 1 addition & 1 deletion src/crossover/integer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#include "integer.hpp"
#include "crossover_dtl.hpp"
#include "crossover_impl.hpp"
#include "../core/ga_base.hpp"
#include "../core/candidate.hpp"
#include "../utility/rng.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/crossover/permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "permutation.hpp"
#include "crossover_base.hpp"
#include "crossover_dtl.hpp"
#include "crossover_impl.hpp"
#include "../core/candidate.hpp"
#include "../utility/rng.hpp"
#include "../utility/utility.hpp"
Expand Down
10 changes: 4 additions & 6 deletions test/unit/crossover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "core/candidate.hpp"
#include "encoding/encoding.hpp"
#include "crossover/crossover.hpp"
#include "crossover/crossover_dtl.hpp"
#include "crossover/crossover_impl.hpp"
#include "test_utils.hpp"

using namespace gapp;
Expand Down Expand Up @@ -42,9 +42,7 @@ TEST_CASE("npoint_crossover", "[crossover]")
Candidate<char> parent2{ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
// // // //

std::vector<size_t> cx_points{ 1, 3, 7, 11 };

auto [child1, child2] = nPointCrossoverImpl(parent1, parent2, cx_points);
auto [child1, child2] = nPointCrossoverImpl(parent1, parent2, { 1, 3, 7, 11 });

REQUIRE(child1.chromosome == Chromosome<char>{ { 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 } });
REQUIRE(child2.chromosome == Chromosome<char>{ { 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0 } });
Expand Down Expand Up @@ -80,8 +78,8 @@ TEMPLATE_TEST_CASE("position_crossover", "[crossover]", int, unsigned)
Candidate<TestType> parent1{ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
Candidate<TestType> parent2{ { 4, 5, 0, 6, 1, 2, 8, 3, 9, 7 } };

auto child1 = positionCrossoverImpl(parent1, parent2, { 0, 3, 4, 7 });
auto child2 = positionCrossoverImpl(parent2, parent1, { 0, 3, 4, 7 });
auto child1 = positionCrossoverImpl(parent1, parent2, { { 0, 3, 4, 7 } });
auto child2 = positionCrossoverImpl(parent2, parent1, { { 0, 3, 4, 7 } });

REQUIRE(child1.chromosome == Chromosome<TestType>{ { 0, 5, 6, 3, 4, 1, 2, 7, 8, 9 } });
REQUIRE(child2.chromosome == Chromosome<TestType>{ { 4, 0, 2, 6, 1, 5, 7, 3, 8, 9 } });
Expand Down

0 comments on commit a8ad756

Please sign in to comment.