GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
UniformMutation.h
Go to the documentation of this file.
1#ifndef UNIFORM_MUTATION_H
2#define UNIFORM_MUTATION_H
3
4#pragma once
5
6#include "Mutation.h"
7#include <random>
8#include <vector>
9#include <cstddef>
10
11namespace galib {
12
20 template <typename GeneType>
21 class UniformMutation : public Mutation<GeneType> {
22 private:
23 GeneType lower_m;
24 GeneType upper_m;
25
26 public:
32 UniformMutation(GeneType lower, GeneType upper)
33 : lower_m(lower), upper_m(upper) {}
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 std::uniform_real_distribution<GeneType> value_dist(lower_m, upper_m);
46 std::uniform_real_distribution<double> prob(0.0, 1.0);
47
48 std::vector<GeneType>& genotype = individual.getGenotype();
49
50 for (std::size_t i = 0; i < genotype.size(); ++i) {
51 if (prob(gen) < mutation_rate) {
52 genotype[i] = value_dist(gen);
53 }
54 }
55 }
56 };
57
58}
59
60#endif // UNIFORM_MUTATION_H
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
Performs uniform mutation.
UniformMutation(GeneType lower, GeneType upper)
Constructs the mutation operator.
void mutate(Individual< GeneType > &individual, double mutation_rate) override
Replaces genes with random values from [lower, upper].