GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
Individual.h
Go to the documentation of this file.
1#ifndef INDIVIDUAL_H
2#define INDIVIDUAL_H
3
4#pragma once
5
6#include <vector>
7#include <limits>
8#include <iostream>
9#include <iomanip>
10#include <algorithm>
11
12namespace galib {
13
22 template <typename GeneType = double>
23 class Individual {
24 private:
25 std::vector<GeneType> genotype;
26 double fitness;
27
28 static constexpr std::size_t PRINT_LIMIT = 5;
29 static constexpr int PRINT_PRECISION = 4;
30 public:
34 Individual() : fitness(std::numeric_limits<double>::max()) {}
35
40 explicit Individual(std::size_t num_genes)
41 : genotype(num_genes), fitness(std::numeric_limits<double>::max()) {}
42
47 [[nodiscard]] std::size_t size() const { return genotype.size(); }
48
53 std::vector<GeneType>& getGenotype() { return genotype; }
54
59 const std::vector<GeneType>& getGenotype() const { return genotype; }
60
65 void setGenotype(const std::vector<GeneType>& new_genotype) { genotype = new_genotype; }
66
71 void setGenotype(std::vector<GeneType>&& new_genotype) noexcept { genotype = std::move(new_genotype); }
72
77 [[nodiscard]] double getFitness() const { return fitness; }
78
83 void setFitness(double new_fitness) { fitness = new_fitness; }
84
89 auto operator<=>(const Individual& other) const {
90 return fitness <=> other.fitness;
91 }
92
97 bool operator==(const Individual& other) const {
98 return fitness == other.fitness;
99 }
100
104 friend std::ostream& operator<<(std::ostream& os, const Individual& ind) {
105 os << "[ ";
106
107 std::size_t limit = std::min(ind.genotype.size(), PRINT_LIMIT);
108 for (std::size_t i = 0; i < limit; ++i) {
109 os << std::fixed << std::setprecision(PRINT_PRECISION) << ind.genotype[i] << " ";
110 }
111 if (ind.genotype.size() > PRINT_LIMIT) os << "... ";
112 os << "]";
113
114 return os;
115 }
116 };
117
118}
119
120#endif // INDIVIDUAL_H
Represents a single candidate solution in the population.
Definition Individual.h:23
double getFitness() const
Gets the current fitness score.
Definition Individual.h:77
Individual(std::size_t num_genes)
Constructs an Individual with a specific number of genes.
Definition Individual.h:40
bool operator==(const Individual &other) const
Equality operator based on fitness.
Definition Individual.h:97
void setGenotype(const std::vector< GeneType > &new_genotype)
Sets the genotype by copying.
Definition Individual.h:65
friend std::ostream & operator<<(std::ostream &os, const Individual &ind)
Streams a summary of the individual (fitness and first few genes).
Definition Individual.h:104
Individual()
Default constructor. Initializes fitness to the maximum possible double value.
Definition Individual.h:34
const std::vector< GeneType > & getGenotype() const
Returns a constant reference to the genotype vector.
Definition Individual.h:59
auto operator<=>(const Individual &other) const
Three-way comparison operator based on fitness (for minimization).
Definition Individual.h:89
std::size_t size() const
Returns the number of genes in the genotype.
Definition Individual.h:47
std::vector< GeneType > & getGenotype()
Returns a reference to the genotype vector.
Definition Individual.h:53
void setFitness(double new_fitness)
Sets a new fitness score.
Definition Individual.h:83
void setGenotype(std::vector< GeneType > &&new_genotype) noexcept
Sets the genotype by moving.
Definition Individual.h:71