generate and destroy population

This commit is contained in:
2026-01-31 11:02:36 +01:00
parent 213e796068
commit 763b2fdb49

View File

@@ -3,6 +3,7 @@ package main
import "core:container/bit_array"
import "core:fmt"
import "core:math"
import "core:math/rand"
import "core:os"
import "core:strconv"
import "core:strings"
@@ -10,6 +11,7 @@ import "core:strings"
DATA_FILE :: "res/knapPI_12_500_1000_82.csv"
NUMBER_OF_ITEMS :: 500
CAPACITY :: 280785
POPULATION_SIZE :: 100
Item :: struct {
profit, weight: int,
@@ -50,6 +52,23 @@ fitness :: proc(chrom: ^Chromosome) -> int {
return tot_profit + penalize(distance(tot_weight, CAPACITY))
}
Population :: [POPULATION_SIZE]Chromosome
generate_population :: proc() -> (res: Population) {
for &chrom in res {
chrom = bit_array.create(NUMBER_OF_ITEMS)^
for i in 0 ..< NUMBER_OF_ITEMS {
bit_array.set(&chrom, i, rand.int_max(2) == 1)
}
}
return
}
destroy_population :: proc(pop: ^Population) {
for &chrom in pop {
bit_array.destroy(&chrom)
}
}
main :: proc() {
items, ok := read_data(DATA_FILE)