diff --git a/src/main.odin b/src/main.odin index 83d841f..7a3eef5 100644 --- a/src/main.odin +++ b/src/main.odin @@ -5,6 +5,7 @@ import "core:fmt" import "core:math" import "core:math/rand" import "core:os" +import "core:slice" import "core:strconv" import "core:strings" @@ -212,6 +213,33 @@ inversion_mutation :: proc(chrom: ^Chromosome) { } } +elitism_survivor_selection :: proc( + parents: ^Population, + offspring: ^Population, + parent_fitnesses: []int, + offspring_fitnesses: []int, +) -> ( + res: Population, +) { + combined_size := POPULATION_SIZE * 2 + combined_fitnesses := make([]int, combined_size) + defer delete(combined_fitnesses) + + copy(combined_fitnesses[:POPULATION_SIZE], parent_fitnesses) + copy(combined_fitnesses[POPULATION_SIZE:], offspring_fitnesses) + + slice.sort(combined_fitnesses) + + for &s, i in res { + if i < POPULATION_SIZE { + s = parents[i] + } else { + s = offspring[i - POPULATION_SIZE] + } + } + return +} + main :: proc() { items, ok := read_data(DATA_FILE) if !ok {