22 template <
typename GeneType =
double>
25 std::vector<GeneType> genotype;
28 static constexpr std::size_t PRINT_LIMIT = 5;
29 static constexpr int PRINT_PRECISION = 4;
34 Individual() : fitness(std::numeric_limits<double>::max()) {}
41 : genotype(num_genes), fitness(std::numeric_limits<double>::max()) {}
47 [[nodiscard]] std::size_t
size()
const {
return genotype.size(); }
59 const std::vector<GeneType>&
getGenotype()
const {
return genotype; }
65 void setGenotype(
const std::vector<GeneType>& new_genotype) { genotype = new_genotype; }
71 void setGenotype(std::vector<GeneType>&& new_genotype)
noexcept { genotype = std::move(new_genotype); }
77 [[nodiscard]]
double getFitness()
const {
return fitness; }
83 void setFitness(
double new_fitness) { fitness = new_fitness; }
90 return fitness <=> other.fitness;
98 return fitness == other.fitness;
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] <<
" ";
111 if (ind.genotype.size() > PRINT_LIMIT) os <<
"... ";
Represents a single candidate solution in the population.
double getFitness() const
Gets the current fitness score.
Individual(std::size_t num_genes)
Constructs an Individual with a specific number of genes.
bool operator==(const Individual &other) const
Equality operator based on fitness.
void setGenotype(const std::vector< GeneType > &new_genotype)
Sets the genotype by copying.
friend std::ostream & operator<<(std::ostream &os, const Individual &ind)
Streams a summary of the individual (fitness and first few genes).
Individual()
Default constructor. Initializes fitness to the maximum possible double value.
const std::vector< GeneType > & getGenotype() const
Returns a constant reference to the genotype vector.
auto operator<=>(const Individual &other) const
Three-way comparison operator based on fitness (for minimization).
std::size_t size() const
Returns the number of genes in the genotype.
std::vector< GeneType > & getGenotype()
Returns a reference to the genotype vector.
void setFitness(double new_fitness)
Sets a new fitness score.
void setGenotype(std::vector< GeneType > &&new_genotype) noexcept
Sets the genotype by moving.