diff --git a/src/main.odin b/src/main.odin index 59047b7..6a8c441 100644 --- a/src/main.odin +++ b/src/main.odin @@ -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 {