-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.c
More file actions
53 lines (39 loc) · 1.55 KB
/
selection.c
File metadata and controls
53 lines (39 loc) · 1.55 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "selection.h"
#include <stdlib.h>
void stochastic_universal_sampling(individual population[], int population_size, int selection_size) {
// Calculate total fitness of the population
int total_fitness = 0;
for (int i = 0; i < population_size; i++) {
total_fitness += population[i].fitness;
}
// Compute the distance between the pointers
int distance = total_fitness / selection_size;
// Randomly select the start point in the range [0, distance)
int start = (rand() % distance);
individual selected[selection_size];
// Start the "sweep" across the population
for (int i = 0; i < selection_size; i++) {
int pointer = start + i * distance;
// Linearly traverse the population to find the individual
int cumulative_fitness = 0;
int idx = 0;
while (cumulative_fitness < pointer) {
cumulative_fitness += population[idx].fitness;
idx++;
}
selected[i] = population[idx];
}
*population = *selected;
}
void truncate_selection(individual population[], int population_size, int selection_size) {
individual selected[selection_size];
qsort(population, population_size, sizeof(individual), compare_individuals_by_fitness);
// Select the top selection_size individuals
for (int i = 0; i < selection_size; i++) {
selected[i] = population[i];
}
// Replace the original population with the selected individuals
for (int i = 0; i < selection_size; i++) {
population[i] = selected[i];
}
}