fix penalty bug

This commit is contained in:
2026-01-31 15:55:51 +01:00
parent dcdf9ebd2a
commit 8ca904fc0a

View File

@@ -38,14 +38,6 @@ read_data :: proc(file: string) -> (res: [NUMBER_OF_ITEMS]Item, ok := true) {
Chromosome :: bit_array.Bit_Array
distance :: proc(x, y: int) -> int {
return math.abs(x - y)
}
penalize :: proc(d: int) -> int {
return math.min(d, 0)
}
// side-effect: reads global `items`
fitness :: proc(chrom: ^Chromosome) -> int {
tot_profit, tot_weight := 0, 0
@@ -54,7 +46,7 @@ fitness :: proc(chrom: ^Chromosome) -> int {
tot_profit += items[idx].profit
tot_weight += items[idx].weight
}
return tot_profit + penalize(distance(tot_weight, CAPACITY))
return tot_profit - 3 * math.max(tot_weight - CAPACITY, 0)
}
Population :: [POPULATION_SIZE]Chromosome