-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndividual.cpp
More file actions
30 lines (26 loc) · 998 Bytes
/
Copy pathIndividual.cpp
File metadata and controls
30 lines (26 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "Individual.h"
#include <cmath>
#include <iostream>
Individual::Individual(int chromosomeLength, Problem* problem)
: m_problem(problem),
gen(std::random_device{}()),
dis(problem->getLowerBound(), problem->getUpperBound()) {
for (int i = 0; i < chromosomeLength; i++) {
m_chromosome.push_back(dis(gen)); // Initialize chromosome with random values
}
m_fitness = calculateFitness(); // Calculate initial fitness
}
double Individual::calculateFitness() {
// Fitness is calculated as the sum of objective function value and penalty function value
m_fitness =
m_problem->objectiveFunction(m_chromosome) + m_problem->penaltyFunction(m_chromosome);
return m_fitness;
}
void Individual::print() {
std::cout << "Fitness: " << m_fitness << "\n";
std::cout << "Chromosome: ";
for (double gene : m_chromosome) {
std::cout << gene << " "; // Print each gene in the chromosome
}
std::cout << "\n";
}