fix penalty bug
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user