print out bit-string of found solution

This commit is contained in:
2026-01-31 18:22:13 +01:00
parent b567855bc1
commit 0ab47a72f4

View File

@@ -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)
}