implement tournament_selection

This commit is contained in:
2026-01-31 11:40:57 +01:00
parent 763b2fdb49
commit eb4a13524c

View File

@@ -70,6 +70,21 @@ destroy_population :: proc(pop: ^Population) {
}
}
tournament_selection :: proc(pop: ^Population, fitnesses: []int, k: int = 3) -> ^Chromosome {
best_idx := rand.int_max(POPULATION_SIZE)
best_fitness := fitnesses[best_idx]
for _ in 1 ..< k {
idx := rand.int_max(POPULATION_SIZE)
if fitnesses[idx] < best_fitness {
best_idx = idx
best_fitness = fitnesses[idx]
}
}
return &pop[best_idx]
}
main :: proc() {
items, ok := read_data(DATA_FILE)
if !ok {