From 0ab47a72f42668d067b13137205e44df0803b880 Mon Sep 17 00:00:00 2001 From: Fredrik Robertsen Date: Sat, 31 Jan 2026 18:22:13 +0100 Subject: [PATCH] print out bit-string of found solution --- src/main.odin | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.odin b/src/main.odin index 7289cfa..159aada 100644 --- a/src/main.odin +++ b/src/main.odin @@ -342,6 +342,7 @@ run_ga :: proc() { best_fitness := math.min(int) best_generation := 0 + best_chromosome: Chromosome for gen in 0 ..< GENERATIONS { fitnesses := evaluate_population(&population) @@ -351,11 +352,11 @@ run_ga :: proc() { if f <= best_fitness {continue} best_fitness = f best_generation = gen - - chrom := population[i] + bit_array.destroy(best_chromosome) + best_chromosome = copy_chromosome(population[i]) tot_profit, tot_weight := 0, 0 - for idx in 0 ..< bit_array.len(chrom) { - if !bit_array.get(chrom, idx) {continue} + for idx in 0 ..< bit_array.len(best_chromosome) { + if !bit_array.get(best_chromosome, idx) {continue} tot_profit += items[idx].profit tot_weight += items[idx].weight } @@ -386,6 +387,12 @@ run_ga :: proc() { } fmt.printfln("\nFinal Best: Fitness=%d (Generation %d)", best_fitness, best_generation) + fmt.println("this solution is the following bit-string:") + for i in 0 ..< best_chromosome.length { + b := bit_array.get(best_chromosome, i) + fmt.print(i32(b)) + } + fmt.println() write_results(OUTPUT_FILE, stats[:]) fmt.println("successfully wrote data to", OUTPUT_FILE) }