implement tournament_selection
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user