readd generational replacement, add constant

This commit is contained in:
2026-02-08 22:12:03 +01:00
parent adbaafa5ae
commit d8f98e82e7
3 changed files with 52 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ CROSSOVER_RATE :: 0.7
MUTATION_RATE :: 0.015
RANDOM_SEED :: u64(42)
OUTPUT_FILE :: "output/data.csv"
SURVIVOR_SELECTION_POLICY :: probabilistic_crowding
Problem :: struct {
name: string,

View File

@@ -407,3 +407,53 @@ write_results :: proc(filename: string, stats: []Stats) -> bool {
csv.writer_flush(&w)
return true
}
// Standard generational replacement (no crowding)
generational_replacement :: proc(
pop: ^Population,
offspring: ^Population,
pop_fitnesses: []f64,
offspring_fitnesses: []f64,
maximize: bool,
) -> Population {
next_gen: Population
// Apply elitism
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]])
}
// Rest are offspring (standard replacement)
for i := ELITISM_COUNT; i < POPULATION_SIZE; i += 1 {
next_gen[i] = clone_chromosome(offspring[i])
}
return next_gen
}
get_best_indices :: proc(fitnesses: []f64, count: int, maximize: bool) -> []int {
indices := make([]int, len(fitnesses))
for i in 0 ..< len(indices) {
indices[i] = i
}
// Partial sort for top N
for i in 0 ..< count {
best := i
for j in i + 1 ..< len(indices) {
is_better := maximize ? fitnesses[indices[j]] > fitnesses[indices[best]] : fitnesses[indices[j]] < fitnesses[indices[best]]
if is_better {
best = j
}
}
indices[i], indices[best] = indices[best], indices[i]
}
result := make([]int, count)
copy(result, indices[:count])
delete(indices)
return result
}

View File

@@ -33,5 +33,5 @@ main :: proc() {
fmt.printfln("RMSE: %.4f\n", baseline)
}
run_ga(problem, deterministic_crowding)
run_ga(problem, SURVIVOR_SELECTION_POLICY)
}