GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
BoundaryMutation.h
Go to the documentation of this file.
1#ifndef BOUNDARY_MUTATION_H
2#define BOUNDARY_MUTATION_H
3
4#pragma once
5
6#include "Mutation.h"
7#include <random>
8#include <vector>
9#include <cstddef>
10
11namespace galib {
12
21 template <typename GeneType>
22 class BoundaryMutation : public Mutation<GeneType> {
23 private:
24 GeneType lower_m;
25 GeneType upper_m;
26
27 public:
33 BoundaryMutation(GeneType lower, GeneType upper)
34 : lower_m(lower), upper_m(upper) {}
35
41 void mutate(Individual<GeneType>& individual, double mutation_rate) override {
42 if (mutation_rate <= 0.0) return;
43
44 thread_local static std::random_device rd;
45 thread_local static std::mt19937 gen(rd());
46 std::uniform_real_distribution<double> prob(0.0, 1.0);
47 std::uniform_int_distribution<int> coin(0, 1);
48
49 std::vector<GeneType>& genotype = individual.getGenotype();
50
51 for (std::size_t i = 0; i < genotype.size(); ++i) {
52 if (prob(gen) < mutation_rate) {
53 genotype[i] = (coin(gen) == 0) ? lower_m : upper_m;
54 }
55 }
56 }
57 };
58
59}
60
61#endif // BOUNDARY_MUTATION_H
Performs boundary mutation.
void mutate(Individual< GeneType > &individual, double mutation_rate) override
Randomly resets genes to lower or upper bounds.
BoundaryMutation(GeneType lower, GeneType upper)
Constructs a boundary mutation operator.
Represents a single candidate solution in the population.
Definition Individual.h:23
std::vector< GeneType > & getGenotype()
Returns a reference to the genotype vector.
Definition Individual.h:53
Interface for mutation operators.
Definition Mutation.h:19