diff --git a/src/common.odin b/src/common.odin index c63b56a..40326d0 100644 --- a/src/common.odin +++ b/src/common.odin @@ -5,7 +5,6 @@ import "core:container/bit_array" Chromosome :: ^bit_array.Bit_Array Population :: [POPULATION_SIZE]Chromosome -POPULATION_SIZE :: 1000 GENERATIONS :: 100 POPULATION_SIZE :: 1000 ELITISM_COUNT :: 10 @@ -15,7 +14,6 @@ CROSSOVER_RATE :: 0.7 MUTATION_RATE :: 0.015 RANDOM_SEED :: u64(42) OUTPUT_FILE :: "output/data.csv" -ELITISM_COUNT :: 50 Problem :: struct { name: string, diff --git a/src/ga.odin b/src/ga.odin index 5f03ced..5551a73 100644 --- a/src/ga.odin +++ b/src/ga.odin @@ -211,21 +211,27 @@ create_offspring :: proc(pop: ^Population, fitnesses: []f64, maximize: bool) -> offspring[i] = clone_chromosome(pop[elite_idx[i]]) } - for i := ELITISM_COUNT; i < POPULATION_SIZE; i += 2 { + offspring_count := POPULATION_SIZE - ELITISM_COUNT - SKEW + for i := ELITISM_COUNT; i < ELITISM_COUNT + offspring_count; i += 2 { parent1 := roulette_selection(pop, fitnesses, maximize) parent2 := roulette_selection(pop, fitnesses, maximize) child1, child2 := uniform_crossover(parent1, parent2) bit_flip_mutation(child1) - if i + 1 < POPULATION_SIZE { + offspring[i] = child1 + + if i + 1 < ELITISM_COUNT + offspring_count { bit_flip_mutation(child2) offspring[i + 1] = child2 } else { bit_array.destroy(child2) } + } - offspring[i] = child1 + for i := ELITISM_COUNT + offspring_count; i < POPULATION_SIZE; i += 1 { + parent := roulette_selection(pop, fitnesses, maximize) + offspring[i] = clone_chromosome(parent) } return offspring