GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
GaussianMutation.h
Go to the documentation of this file.
1#ifndef GAUSSIAN_MUTATION_H
2#define GAUSSIAN_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 GaussianMutation : public Mutation<GeneType> {
23 private:
24 double sigma_m;
25 public:
30 explicit GaussianMutation(double sigma)
31 : sigma_m(sigma) {}
32
33 double getSigma() const { return sigma_m; }
34
40 void mutate(Individual<GeneType>& individual, double mutation_rate) override {
41 if (mutation_rate <= 0.0) { return; }
42
43 thread_local static std::random_device rd;
44 thread_local static std::mt19937 gen(rd());
45 thread_local static std::uniform_real_distribution<double> distribution(0.0, 1.0);
46 thread_local static std::normal_distribution<double> gaussian_distribution;
47
48 std::vector<GeneType>& genotype = individual.getGenotype();
49
50 for (std::size_t i = 0; i < genotype.size(); ++i) {
51 if (distribution(gen) < mutation_rate) {
52 genotype[i] += gaussian_distribution(gen, std::normal_distribution<double>::param_type(0.0, sigma_m));
53 }
54 }
55 }
56 };
57
58}
59
60#endif // GAUSSIAN_MUTATION_H
Performs Gaussian mutation.
void mutate(Individual< GeneType > &individual, double mutation_rate) override
Adds Gaussian noise to the individual's genes.
GaussianMutation(double sigma)
Constructs the 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