diff --git a/src/common.odin b/src/common.odin index 71a2c37..f59dccd 100644 --- a/src/common.odin +++ b/src/common.odin @@ -6,12 +6,12 @@ Chromosome :: ^bit_array.Bit_Array Population :: [POPULATION_SIZE]Chromosome GENERATIONS :: 100 -POPULATION_SIZE :: 1000 -ELITISM_COUNT :: 10 +POPULATION_SIZE :: 100 +ELITISM_COUNT :: 0 SKEW :: 0 -TOURNAMENT_SIZE :: 100 +TOURNAMENT_SIZE :: 5 CROSSOVER_RATE :: 0.7 -MUTATION_RATE :: 0.015 +MUTATION_RATE :: 0.01 RANDOM_SEED :: u64(42) OUTPUT_FILE :: "output/data.csv" SURVIVOR_SELECTION_POLICY :: probabilistic_crowding diff --git a/src/ga.odin b/src/ga.odin index 3aa9f5a..bd132bb 100644 --- a/src/ga.odin +++ b/src/ga.odin @@ -281,8 +281,19 @@ deterministic_crowding :: proc( maximize: bool, ) -> Population { next_gen: Population + start_idx := 0 - for i := 0; i < POPULATION_SIZE; i += 2 { + when ELITISM_COUNT > 0 { + elite_idx := get_best_indices(pop_fitnesses[:], ELITISM_COUNT, maximize) + defer delete(elite_idx) + + for i in 0 ..< ELITISM_COUNT { + next_gen[i] = clone_chromosome(pop[elite_idx[i]]) + } + start_idx = ELITISM_COUNT + } + + for i := start_idx; i < POPULATION_SIZE; i += 2 { p1 := pop[i] p2 := pop[i + 1] if i + 1 < POPULATION_SIZE else pop[i] c1 := offspring[i] @@ -327,8 +338,19 @@ probabilistic_crowding :: proc( maximize: bool, ) -> Population { next_gen: Population + start_idx := 0 - for i := 0; i < POPULATION_SIZE; i += 2 { + when ELITISM_COUNT > 0 { + elite_idx := get_best_indices(pop_fitnesses[:], ELITISM_COUNT, maximize) + defer delete(elite_idx) + + for i in 0 ..< ELITISM_COUNT { + next_gen[i] = clone_chromosome(pop[elite_idx[i]]) + } + start_idx = ELITISM_COUNT + } + + for i := start_idx; i < POPULATION_SIZE; i += 2 { p1 := pop[i] p2 := pop[i + 1] if i + 1 < POPULATION_SIZE else pop[i] c1 := offspring[i] @@ -408,7 +430,6 @@ write_results :: proc(filename: string, stats: []Stats) -> bool { return true } -// Standard generational replacement (no crowding) generational_replacement :: proc( pop: ^Population, offspring: ^Population, @@ -418,15 +439,15 @@ generational_replacement :: proc( ) -> Population { next_gen: Population - // Apply elitism - elite_idx := get_best_indices(pop_fitnesses[:], ELITISM_COUNT, maximize) - defer delete(elite_idx) + when ELITISM_COUNT > 0 { + elite_idx := get_best_indices(pop_fitnesses[:], ELITISM_COUNT, maximize) + defer delete(elite_idx) - for i in 0 ..< ELITISM_COUNT { - next_gen[i] = clone_chromosome(pop[elite_idx[i]]) + for i in 0 ..< ELITISM_COUNT { + next_gen[i] = clone_chromosome(pop[elite_idx[i]]) + } } - // Rest are offspring (standard replacement) for i := ELITISM_COUNT; i < POPULATION_SIZE; i += 1 { next_gen[i] = clone_chromosome(offspring[i]) }