holy what is going on
This commit is contained in:
40
src/common.odin
Normal file
40
src/common.odin
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import "core:container/bit_array"
|
||||
|
||||
// Knapsack
|
||||
OUTPUT_FILE :: "output/data.csv"
|
||||
DATA_FILE :: "res/knapPI_12_500_1000_82.csv"
|
||||
NUMBER_OF_ITEMS :: 500
|
||||
CAPACITY :: 280785
|
||||
|
||||
Item :: struct {
|
||||
profit, weight: int,
|
||||
}
|
||||
|
||||
// Feature selection
|
||||
DATASET_FILE :: "res/dataset.csv"
|
||||
NUMBER_OF_FEATURES :: 100
|
||||
DATASET_ROWS :: 1994
|
||||
|
||||
Dataset_Record :: struct {
|
||||
features: [NUMBER_OF_FEATURES]f64,
|
||||
target: f64,
|
||||
}
|
||||
Dataset :: #soa[DATASET_ROWS]Dataset_Record
|
||||
|
||||
// GA
|
||||
Chromosome :: ^bit_array.Bit_Array
|
||||
Population :: [POPULATION_SIZE]Chromosome
|
||||
POPULATION_SIZE :: 100
|
||||
GENERATIONS :: 100
|
||||
TOURNAMENT_SIZE :: 3
|
||||
CROSSOVER_RATE :: 0.8
|
||||
MUTATION_RATE :: 0.01
|
||||
RANDOM_SEED :: u64(42)
|
||||
|
||||
// stats
|
||||
Data :: struct {
|
||||
best, worst: int,
|
||||
mean: f32,
|
||||
}
|
||||
323
src/linreg.odin
Normal file
323
src/linreg.odin
Normal file
@@ -0,0 +1,323 @@
|
||||
package main
|
||||
|
||||
import "core:container/bit_array"
|
||||
import "core:math"
|
||||
import "core:math/rand"
|
||||
|
||||
// Solves Ax = b using Gaussian elimination with partial pivoting
|
||||
solve_linear_system :: proc(A: [][]f64, b: []f64) -> []f64 {
|
||||
n := len(A)
|
||||
|
||||
// Create augmented matrix [A|b]
|
||||
aug := make([][]f64, n)
|
||||
for i in 0 ..< n {
|
||||
aug[i] = make([]f64, n + 1)
|
||||
copy(aug[i][:n], A[i])
|
||||
aug[i][n] = b[i]
|
||||
}
|
||||
defer {
|
||||
for row in aug {delete(row)}
|
||||
delete(aug)
|
||||
}
|
||||
|
||||
// Forward elimination with partial pivoting
|
||||
for col in 0 ..< n {
|
||||
// Find pivot (largest absolute value in column)
|
||||
max_row := col
|
||||
for row in col + 1 ..< n {
|
||||
if math.abs(aug[row][col]) > math.abs(aug[max_row][col]) {
|
||||
max_row = row
|
||||
}
|
||||
}
|
||||
|
||||
// Swap rows
|
||||
if max_row != col {
|
||||
aug[col], aug[max_row] = aug[max_row], aug[col]
|
||||
}
|
||||
|
||||
// Check for singular matrix
|
||||
if math.abs(aug[col][col]) < 1e-10 {
|
||||
// Matrix is singular, return zero vector
|
||||
x := make([]f64, n)
|
||||
return x
|
||||
}
|
||||
|
||||
// Eliminate column entries below pivot
|
||||
for row in col + 1 ..< n {
|
||||
factor := aug[row][col] / aug[col][col]
|
||||
for j in col ..< n + 1 {
|
||||
aug[row][j] -= factor * aug[col][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Back substitution
|
||||
x := make([]f64, n)
|
||||
for i in 0 ..< n {
|
||||
row := n - 1 - i // Process from bottom to top
|
||||
x[row] = aug[row][n]
|
||||
for j in row + 1 ..< n {
|
||||
x[row] -= aug[row][j] * x[j]
|
||||
}
|
||||
x[row] /= aug[row][row]
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// Linear regression using normal equation: β = (X^T X)^-1 X^T y
|
||||
train_linear_regression :: proc(X: [][]f64, y: []f64) -> []f64 {
|
||||
n := len(X)
|
||||
m := len(X[0])
|
||||
|
||||
// Compute X^T X
|
||||
XtX := make([][]f64, m)
|
||||
for i in 0 ..< m {
|
||||
XtX[i] = make([]f64, m)
|
||||
for j in 0 ..< m {
|
||||
sum := 0.0
|
||||
for k in 0 ..< n {
|
||||
sum += X[k][i] * X[k][j]
|
||||
}
|
||||
XtX[i][j] = sum
|
||||
}
|
||||
}
|
||||
defer {
|
||||
for row in XtX {delete(row)}
|
||||
delete(XtX)
|
||||
}
|
||||
|
||||
// Compute X^T y
|
||||
Xty := make([]f64, m)
|
||||
defer delete(Xty)
|
||||
for i in 0 ..< m {
|
||||
sum := 0.0
|
||||
for k in 0 ..< n {
|
||||
sum += X[k][i] * y[k]
|
||||
}
|
||||
Xty[i] = sum
|
||||
}
|
||||
|
||||
// Solve (X^T X) β = X^T y using Gaussian elimination
|
||||
beta := solve_linear_system(XtX, Xty)
|
||||
return beta
|
||||
}
|
||||
|
||||
predict :: proc(X: [][]f64, beta: []f64) -> []f64 {
|
||||
predictions := make([]f64, len(X))
|
||||
for i in 0 ..< len(X) {
|
||||
sum := 0.0
|
||||
for j in 0 ..< len(beta) {
|
||||
sum += X[i][j] * beta[j]
|
||||
}
|
||||
predictions[i] = sum
|
||||
}
|
||||
return predictions
|
||||
}
|
||||
|
||||
rmse :: proc(predictions: []f64, actual: []f64) -> f64 {
|
||||
sum := 0.0
|
||||
for i in 0 ..< len(predictions) {
|
||||
diff := predictions[i] - actual[i]
|
||||
sum += diff * diff
|
||||
}
|
||||
return math.sqrt(sum / f64(len(predictions)))
|
||||
}
|
||||
|
||||
train_test_split :: proc(
|
||||
X: [][]f64,
|
||||
y: []f64,
|
||||
test_size: f64 = 0.2,
|
||||
random_seed: u64 = 0,
|
||||
) -> (
|
||||
X_train, X_test: [][]f64,
|
||||
y_train, y_test: []f64,
|
||||
) {
|
||||
n := len(X)
|
||||
test_count := int(f64(n) * test_size)
|
||||
train_count := n - test_count
|
||||
|
||||
if n == 0 || len(X[0]) == 0 {
|
||||
return nil, nil, nil, nil
|
||||
}
|
||||
|
||||
n_features := len(X[0])
|
||||
|
||||
// Create shuffled indices
|
||||
indices := make([]int, n)
|
||||
defer delete(indices)
|
||||
for i in 0 ..< n {
|
||||
indices[i] = i
|
||||
}
|
||||
|
||||
// Shuffle
|
||||
rng := rand.create(random_seed)
|
||||
context.random_generator = rand.default_random_generator(&rng)
|
||||
rand.shuffle(indices[:])
|
||||
|
||||
// Allocate splits
|
||||
X_train = make([][]f64, train_count)
|
||||
X_test = make([][]f64, test_count)
|
||||
y_train = make([]f64, train_count)
|
||||
y_test = make([]f64, test_count)
|
||||
|
||||
// Copy training data (DEEP COPY)
|
||||
for i in 0 ..< train_count {
|
||||
idx := indices[i]
|
||||
X_train[i] = make([]f64, n_features)
|
||||
copy(X_train[i], X[idx])
|
||||
y_train[i] = y[idx]
|
||||
}
|
||||
|
||||
// Copy test data (DEEP COPY)
|
||||
for i in 0 ..< test_count {
|
||||
idx := indices[train_count + i]
|
||||
X_test[i] = make([]f64, n_features)
|
||||
copy(X_test[i], X[idx])
|
||||
y_test[i] = y[idx]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// Extract columns based on bit_array chromosome
|
||||
get_columns :: proc(X: [][]f64, chrom: ^bit_array.Bit_Array) -> [][]f64 {
|
||||
n_rows := len(X)
|
||||
n_cols := bit_array.len(chrom)
|
||||
|
||||
// Count selected features
|
||||
selected_count := 0
|
||||
for i in 0 ..< n_cols {
|
||||
if bit_array.get(chrom, i) {
|
||||
selected_count += 1
|
||||
}
|
||||
}
|
||||
|
||||
if selected_count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create subset with only selected columns
|
||||
subset := make([][]f64, n_rows)
|
||||
for i in 0 ..< n_rows {
|
||||
subset[i] = make([]f64, selected_count)
|
||||
col_idx := 0
|
||||
for j in 0 ..< n_cols {
|
||||
if bit_array.get(chrom, j) {
|
||||
subset[i][col_idx] = X[i][j]
|
||||
col_idx += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return subset
|
||||
}
|
||||
|
||||
// Fitness function for feature selection
|
||||
get_fitness :: proc(
|
||||
X: [][]f64,
|
||||
y: []f64,
|
||||
chrom: ^bit_array.Bit_Array,
|
||||
random_seed: u64 = 0,
|
||||
) -> f64 {
|
||||
X_selected := get_columns(X, chrom)
|
||||
if X_selected == nil {
|
||||
return math.F64_MAX
|
||||
}
|
||||
defer {
|
||||
for row in X_selected {delete(row)}
|
||||
delete(X_selected)
|
||||
}
|
||||
|
||||
// Split data
|
||||
X_train, X_test, y_train, y_test := train_test_split(X_selected, y, 0.2, random_seed)
|
||||
defer {
|
||||
delete(X_train)
|
||||
delete(X_test)
|
||||
delete(y_train)
|
||||
delete(y_test)
|
||||
}
|
||||
|
||||
// Train model
|
||||
beta := train_linear_regression(X_train, y_train)
|
||||
defer delete(beta)
|
||||
|
||||
// Predict on test set
|
||||
predictions := predict(X_test, beta)
|
||||
defer delete(predictions)
|
||||
|
||||
// Return RMSE
|
||||
return rmse(predictions, y_test)
|
||||
}
|
||||
|
||||
// Extract selected features from dataset based on chromosome
|
||||
get_selected_features :: proc(dataset: Dataset, chrom: Chromosome) -> (X: [][]f64, y: []f64) {
|
||||
n_rows := len(dataset)
|
||||
n_features := bit_array.len(chrom)
|
||||
|
||||
// Count selected features
|
||||
selected_count := 0
|
||||
for i in 0 ..< n_features {
|
||||
if bit_array.get(chrom, i) {
|
||||
selected_count += 1
|
||||
}
|
||||
}
|
||||
|
||||
if selected_count == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Allocate
|
||||
X = make([][]f64, n_rows)
|
||||
y = make([]f64, n_rows)
|
||||
|
||||
// Extract
|
||||
for i in 0 ..< n_rows {
|
||||
X[i] = make([]f64, selected_count)
|
||||
col_idx := 0
|
||||
for j in 0 ..< n_features {
|
||||
if bit_array.get(chrom, j) {
|
||||
X[i][col_idx] = dataset[i].features[j]
|
||||
col_idx += 1
|
||||
}
|
||||
}
|
||||
y[i] = dataset[i].target
|
||||
}
|
||||
|
||||
return X, y
|
||||
}
|
||||
|
||||
|
||||
// Fitness for feature selection (returns RMSE)
|
||||
fitness_feature_selection :: proc(
|
||||
dataset: Dataset,
|
||||
chrom: Chromosome,
|
||||
random_seed: u64 = 0,
|
||||
) -> f64 {
|
||||
X, y := get_selected_features(dataset, chrom)
|
||||
if X == nil {
|
||||
return math.F64_MAX
|
||||
}
|
||||
defer {
|
||||
for row in X {delete(row)}
|
||||
delete(X)
|
||||
delete(y)
|
||||
}
|
||||
|
||||
X_train, X_test, y_train, y_test := train_test_split(X, y, 0.2, random_seed)
|
||||
defer {
|
||||
delete(X_train)
|
||||
delete(X_test)
|
||||
delete(y_train)
|
||||
delete(y_test)
|
||||
}
|
||||
|
||||
beta := train_linear_regression(X_train, y_train)
|
||||
defer delete(beta)
|
||||
|
||||
predictions := predict(X_test, beta)
|
||||
defer delete(predictions)
|
||||
|
||||
return rmse(predictions, y_test)
|
||||
}
|
||||
189
src/linreg_test.odin
Normal file
189
src/linreg_test.odin
Normal file
@@ -0,0 +1,189 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:math"
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
test_solve_simple_2x2 :: proc(t: ^testing.T) {
|
||||
// Properly allocate 2D array
|
||||
A := [][]f64{{2.0, 1.0}, {1.0, 3.0}}
|
||||
b := []f64{5.0, 6.0}
|
||||
|
||||
x := solve_linear_system(A, b)
|
||||
defer delete(x)
|
||||
|
||||
testing.expect(t, math.abs(x[0] - 1.8) < 1e-10, "x[0] should be 1.8")
|
||||
testing.expect(t, math.abs(x[1] - 1.4) < 1e-10, "x[1] should be 1.4")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_solve_identity :: proc(t: ^testing.T) {
|
||||
A := [][]f64{{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}}
|
||||
b := []f64{3.0, 7.0, -2.0}
|
||||
|
||||
x := solve_linear_system(A, b)
|
||||
defer delete(x)
|
||||
|
||||
for i in 0 ..< 3 {
|
||||
testing.expect(t, math.abs(x[i] - b[i]) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_solve_needs_pivoting :: proc(t: ^testing.T) {
|
||||
// System that requires pivoting for numerical stability
|
||||
A := [][]f64{{0.0001, 1.0}, {1.0, 1.0}}
|
||||
b := []f64{1.0, 2.0}
|
||||
|
||||
x := solve_linear_system(A, b)
|
||||
defer delete(x)
|
||||
|
||||
// Verify Ax = b
|
||||
result := make([]f64, 2)
|
||||
defer delete(result)
|
||||
|
||||
for i in 0 ..< 2 {
|
||||
sum := 0.0
|
||||
for j in 0 ..< 2 {
|
||||
sum += A[i][j] * x[j]
|
||||
}
|
||||
result[i] = sum
|
||||
}
|
||||
|
||||
testing.expect(t, math.abs(result[0] - b[0]) < 1e-6)
|
||||
testing.expect(t, math.abs(result[1] - b[1]) < 1e-6)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_solve_singular_matrix :: proc(t: ^testing.T) {
|
||||
// Singular matrix (rows are linearly dependent)
|
||||
A := [][]f64{{1.0, 2.0}, {2.0, 4.0}}
|
||||
b := []f64{3.0, 6.0}
|
||||
|
||||
x := solve_linear_system(A, b)
|
||||
defer delete(x)
|
||||
|
||||
// Should return zero vector for singular matrix
|
||||
testing.expect_value(t, len(x), 2)
|
||||
testing.expect(t, math.abs(x[0]) < 1e-10)
|
||||
testing.expect(t, math.abs(x[1]) < 1e-10)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_solve_larger_system :: proc(t: ^testing.T) {
|
||||
A := [][]f64 {
|
||||
{4.0, 1.0, 2.0, 1.0},
|
||||
{1.0, 5.0, 1.0, 2.0},
|
||||
{2.0, 1.0, 6.0, 1.0},
|
||||
{1.0, 2.0, 1.0, 7.0},
|
||||
}
|
||||
b := []f64{10.0, 12.0, 14.0, 16.0}
|
||||
|
||||
x := solve_linear_system(A, b)
|
||||
defer delete(x)
|
||||
|
||||
// Verify Ax ≈ b
|
||||
for i in 0 ..< 4 {
|
||||
sum := 0.0
|
||||
for j in 0 ..< 4 {
|
||||
sum += A[i][j] * x[j]
|
||||
}
|
||||
testing.expect(t, math.abs(sum - b[i]) < 1e-8)
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_train_linear_regression :: proc(t: ^testing.T) {
|
||||
X := [][]f64{{1.0}, {2.0}, {3.0}, {4.0}, {5.0}}
|
||||
y := []f64{5.0, 7.0, 9.0, 11.0, 13.0}
|
||||
|
||||
beta := train_linear_regression(X, y)
|
||||
defer delete(beta)
|
||||
|
||||
fmt.printfln("beta = %v", beta)
|
||||
|
||||
// For y = 2x + 3 with no intercept term:
|
||||
// Best fit through origin: minimize Σ(y - βx)²
|
||||
// β = Σ(xy) / Σ(x²) = (1*5 + 2*7 + 3*9 + 4*11 + 5*13) / (1 + 4 + 9 + 16 + 25)
|
||||
// = (5 + 14 + 27 + 44 + 65) / 55 = 155 / 55 ≈ 2.818
|
||||
testing.expect(t, math.abs(beta[0] - 2.818) < 0.01, "slope should be ~2.818")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_train_with_intercept :: proc(t: ^testing.T) {
|
||||
// Dataset with intercept column: y = 2x + 3
|
||||
X := [][]f64 {
|
||||
{1.0, 1.0}, // [intercept, x]
|
||||
{1.0, 2.0},
|
||||
{1.0, 3.0},
|
||||
{1.0, 4.0},
|
||||
{1.0, 5.0},
|
||||
}
|
||||
y := []f64{5.0, 7.0, 9.0, 11.0, 13.0}
|
||||
|
||||
beta := train_linear_regression(X, y)
|
||||
defer delete(beta)
|
||||
|
||||
testing.expect(t, math.abs(beta[0] - 3.0) < 1e-6, "intercept should be 3.0")
|
||||
testing.expect(t, math.abs(beta[1] - 2.0) < 1e-6, "slope should be 2.0")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_predict :: proc(t: ^testing.T) {
|
||||
X := [][]f64{{1.0, 1.0}, {1.0, 2.0}, {1.0, 3.0}}
|
||||
beta := []f64{3.0, 2.0} // y = 2x + 3
|
||||
|
||||
predictions := predict(X, beta)
|
||||
defer delete(predictions)
|
||||
|
||||
expected := []f64{5.0, 7.0, 9.0}
|
||||
for i in 0 ..< len(predictions) {
|
||||
testing.expect(t, math.abs(predictions[i] - expected[i]) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_rmse :: proc(t: ^testing.T) {
|
||||
predictions := []f64{5.0, 7.0, 9.0}
|
||||
actual := []f64{5.1, 6.9, 9.2}
|
||||
|
||||
error := rmse(predictions, actual)
|
||||
|
||||
// RMSE = sqrt((0.1² + 0.1² + 0.2²) / 3) = sqrt(0.06/3) ≈ 0.141
|
||||
testing.expect(t, math.abs(error - 0.141) < 0.01, "RMSE should be ~0.141")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_rmse_perfect_fit :: proc(t: ^testing.T) {
|
||||
predictions := []f64{1.0, 2.0, 3.0}
|
||||
actual := []f64{1.0, 2.0, 3.0}
|
||||
|
||||
error := rmse(predictions, actual)
|
||||
|
||||
testing.expect(t, error < 1e-10, "RMSE should be 0 for perfect fit")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_full_pipeline :: proc(t: ^testing.T) {
|
||||
// Train on y = 3x + 2
|
||||
X_train := [][]f64{{1.0, 1.0}, {1.0, 2.0}, {1.0, 3.0}, {1.0, 4.0}}
|
||||
y_train := []f64{5.0, 8.0, 11.0, 14.0}
|
||||
|
||||
// Test data
|
||||
X_test := [][]f64{{1.0, 5.0}, {1.0, 6.0}}
|
||||
y_test := []f64{17.0, 20.0}
|
||||
|
||||
// Train
|
||||
beta := train_linear_regression(X_train, y_train)
|
||||
defer delete(beta)
|
||||
|
||||
// Predict
|
||||
predictions := predict(X_test, beta)
|
||||
defer delete(predictions)
|
||||
|
||||
// Evaluate
|
||||
error := rmse(predictions, y_test)
|
||||
|
||||
testing.expect(t, error < 1e-6, "Should have near-zero error on linear data")
|
||||
}
|
||||
253
src/main.odin
253
src/main.odin
@@ -10,30 +10,8 @@ import "core:slice"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
|
||||
OUTPUT_FILE :: "output/data.csv"
|
||||
DATA_FILE :: "res/knapPI_12_500_1000_82.csv"
|
||||
NUMBER_OF_ITEMS :: 500
|
||||
CAPACITY :: 280785
|
||||
POPULATION_SIZE :: 100
|
||||
GENERATIONS :: 100
|
||||
TOURNAMENT_SIZE :: 3
|
||||
CROSSOVER_RATE :: 0.8
|
||||
MUTATION_RATE :: 0.01
|
||||
|
||||
Item :: struct {
|
||||
profit, weight: int,
|
||||
}
|
||||
|
||||
Chromosome :: ^bit_array.Bit_Array
|
||||
Population :: [POPULATION_SIZE]Chromosome
|
||||
|
||||
dataset: Dataset
|
||||
items: [NUMBER_OF_ITEMS]Item
|
||||
|
||||
Data :: struct {
|
||||
best, worst: int,
|
||||
mean: f32,
|
||||
}
|
||||
|
||||
stats: [GENERATIONS]Data
|
||||
|
||||
read_data :: proc(file: string) -> (res: [NUMBER_OF_ITEMS]Item, ok := true) {
|
||||
@@ -47,6 +25,38 @@ read_data :: proc(file: string) -> (res: [NUMBER_OF_ITEMS]Item, ok := true) {
|
||||
return
|
||||
}
|
||||
|
||||
load_dataset :: proc(filename: string) -> (data: Dataset, ok := true) {
|
||||
file_data := os.read_entire_file(filename) or_return
|
||||
defer delete(file_data)
|
||||
|
||||
r: csv.Reader
|
||||
csv.reader_init_with_string(&r, string(file_data))
|
||||
defer csv.reader_destroy(&r)
|
||||
|
||||
r.trim_leading_space = true
|
||||
r.reuse_record = true
|
||||
|
||||
idx := 0
|
||||
for {
|
||||
record, err := csv.read(&r)
|
||||
if err != nil {break}
|
||||
if idx >= DATASET_ROWS {break}
|
||||
|
||||
// Parse features (columns 0-99)
|
||||
for i in 0 ..< NUMBER_OF_FEATURES {
|
||||
data[idx].features[i] = strconv.parse_f64(record[i]) or_return
|
||||
}
|
||||
|
||||
// Parse target (column 100)
|
||||
data[idx].target = strconv.parse_f64(record[NUMBER_OF_FEATURES]) or_return
|
||||
|
||||
idx += 1
|
||||
}
|
||||
|
||||
return data, idx == DATASET_ROWS
|
||||
}
|
||||
|
||||
|
||||
write_results :: proc(filename: string, stats: []Data) -> bool {
|
||||
handle, err := os.open(filename, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0o644)
|
||||
if err != os.ERROR_NONE {return false}
|
||||
@@ -84,9 +94,13 @@ fitness :: proc(chrom: Chromosome) -> int {
|
||||
return tot_profit - 500 * max(tot_weight - CAPACITY, 0)
|
||||
}
|
||||
|
||||
create_random_chromosome :: proc() -> Chromosome {
|
||||
chrom := bit_array.create(NUMBER_OF_ITEMS)
|
||||
for i in 0 ..< NUMBER_OF_ITEMS {
|
||||
fitness_rmse :: proc(chrom: Chromosome) -> f64 {
|
||||
return fitness_feature_selection(dataset, chrom, RANDOM_SEED)
|
||||
}
|
||||
|
||||
create_random_chromosome :: proc(size: int = NUMBER_OF_ITEMS) -> Chromosome {
|
||||
chrom := bit_array.create(size)
|
||||
for i in 0 ..< size {
|
||||
bit_array.set(chrom, i, rand.int_max(2) == 1)
|
||||
}
|
||||
return chrom
|
||||
@@ -108,6 +122,14 @@ generate_population :: proc() -> Population {
|
||||
return pop
|
||||
}
|
||||
|
||||
generate_population_features :: proc() -> Population {
|
||||
pop: Population
|
||||
for i in 0 ..< POPULATION_SIZE {
|
||||
pop[i] = create_random_chromosome(NUMBER_OF_FEATURES)
|
||||
}
|
||||
return pop
|
||||
}
|
||||
|
||||
destroy_population :: proc(pop: ^Population) {
|
||||
for chrom in pop {
|
||||
bit_array.destroy(chrom)
|
||||
@@ -122,6 +144,14 @@ evaluate_population :: proc(pop: ^Population) -> [POPULATION_SIZE]int {
|
||||
return fitnesses
|
||||
}
|
||||
|
||||
evaluate_population_rmse :: proc(pop: ^Population) -> [POPULATION_SIZE]f64 {
|
||||
fitnesses: [POPULATION_SIZE]f64
|
||||
for chrom, i in pop {
|
||||
fitnesses[i] = fitness_rmse(chrom)
|
||||
}
|
||||
return fitnesses
|
||||
}
|
||||
|
||||
tournament_selection :: proc(
|
||||
pop: ^Population,
|
||||
fitnesses: []int,
|
||||
@@ -141,6 +171,22 @@ tournament_selection :: proc(
|
||||
return pop[best_idx]
|
||||
}
|
||||
|
||||
tournament_selection_rmse :: proc(pop: ^Population, fitnesses: []f64) -> Chromosome {
|
||||
best_idx := rand.int_max(POPULATION_SIZE)
|
||||
best_fitness := fitnesses[best_idx]
|
||||
|
||||
for _ in 1 ..< TOURNAMENT_SIZE {
|
||||
idx := rand.int_max(POPULATION_SIZE)
|
||||
if fitnesses[idx] < best_fitness { // Lower is better
|
||||
best_idx = idx
|
||||
best_fitness = fitnesses[idx]
|
||||
}
|
||||
}
|
||||
|
||||
return pop[best_idx]
|
||||
}
|
||||
|
||||
|
||||
roulette_selection :: proc(pop: ^Population, fitnesses: []int) -> Chromosome {
|
||||
total_fitness := 0
|
||||
for f in fitnesses {
|
||||
@@ -336,6 +382,21 @@ compute_stats :: proc(fitnesses: []int) -> Data {
|
||||
return {best, worst, f32(sum) / f32(len(fitnesses))}
|
||||
}
|
||||
|
||||
compute_stats_rmse :: proc(fitnesses: []f64) -> [3]f64 {
|
||||
best := math.F64_MAX
|
||||
worst := -math.F64_MAX
|
||||
sum := 0.0
|
||||
|
||||
for f in fitnesses {
|
||||
best = min(best, f) // Lower is better
|
||||
worst = max(worst, f) // Higher is worse
|
||||
sum += f
|
||||
}
|
||||
|
||||
mean := sum / f64(len(fitnesses))
|
||||
return {best, mean, worst}
|
||||
}
|
||||
|
||||
run_ga :: proc() {
|
||||
population := generate_population()
|
||||
defer destroy_population(&population)
|
||||
@@ -397,22 +458,138 @@ run_ga :: proc() {
|
||||
fmt.println("successfully wrote data to", OUTPUT_FILE)
|
||||
}
|
||||
|
||||
run_baseline :: proc() -> f64 {
|
||||
all_features := bit_array.create(NUMBER_OF_FEATURES)
|
||||
defer bit_array.destroy(all_features)
|
||||
|
||||
// Select all features
|
||||
for i in 0 ..< NUMBER_OF_FEATURES {
|
||||
bit_array.set(all_features, i, true)
|
||||
}
|
||||
|
||||
return fitness_feature_selection(dataset, all_features, RANDOM_SEED)
|
||||
}
|
||||
|
||||
create_offspring_rmse :: proc(pop: ^Population, fitnesses: []f64) -> Population {
|
||||
offspring: Population
|
||||
|
||||
for i := 0; i < POPULATION_SIZE; i += 2 {
|
||||
parent1 := tournament_selection_rmse(pop, fitnesses)
|
||||
parent2 := tournament_selection_rmse(pop, fitnesses)
|
||||
|
||||
child1, child2 := two_point_crossover(parent1, parent2)
|
||||
|
||||
swap_mutation(child1)
|
||||
if i + 1 < POPULATION_SIZE {
|
||||
swap_mutation(child2)
|
||||
}
|
||||
|
||||
offspring[i] = child1
|
||||
if i + 1 < POPULATION_SIZE {
|
||||
offspring[i + 1] = child2
|
||||
} else {
|
||||
bit_array.destroy(child2)
|
||||
}
|
||||
}
|
||||
|
||||
return offspring
|
||||
}
|
||||
|
||||
write_results_rmse :: proc(filename: string, stats: [][3]f64) -> bool {
|
||||
handle, err := os.open(filename, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0o644)
|
||||
if err != os.ERROR_NONE {return false}
|
||||
defer os.close(handle)
|
||||
|
||||
w: csv.Writer
|
||||
csv.writer_init(&w, os.stream_from_handle(handle))
|
||||
|
||||
csv.write(&w, []string{"Generation", "Best", "Mean", "Worst"})
|
||||
|
||||
for stat, gen in stats {
|
||||
csv.write(
|
||||
&w,
|
||||
[]string {
|
||||
fmt.tprintf("%d", gen),
|
||||
fmt.tprintf("%.6f", stat[0]),
|
||||
fmt.tprintf("%.6f", stat[1]),
|
||||
fmt.tprintf("%.6f", stat[2]),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
csv.writer_flush(&w)
|
||||
return true
|
||||
}
|
||||
|
||||
run_ga_feature_selection :: proc() {
|
||||
population := generate_population_features()
|
||||
defer destroy_population(&population)
|
||||
|
||||
generation_stats := make([dynamic][3]f64, 0, GENERATIONS)
|
||||
defer delete(generation_stats)
|
||||
|
||||
for gen in 0 ..< GENERATIONS {
|
||||
fitnesses := evaluate_population_rmse(&population)
|
||||
stats := compute_stats_rmse(fitnesses[:])
|
||||
append(&generation_stats, stats)
|
||||
|
||||
fmt.printfln("Gen %d: Best=%.4f Mean=%.4f Worst=%.4f", gen, stats[0], stats[1], stats[2])
|
||||
|
||||
// Create offspring
|
||||
offspring := create_offspring_rmse(&population, fitnesses[:])
|
||||
defer destroy_population(&offspring)
|
||||
|
||||
// Replace population
|
||||
destroy_population(&population)
|
||||
population = offspring
|
||||
}
|
||||
|
||||
// Write results
|
||||
write_results_rmse(OUTPUT_FILE, generation_stats[:])
|
||||
|
||||
// Final best solution
|
||||
final_fitnesses := evaluate_population_rmse(&population)
|
||||
best_idx := 0
|
||||
best_rmse := final_fitnesses[0]
|
||||
for f, i in final_fitnesses {
|
||||
if f < best_rmse {
|
||||
best_rmse = f
|
||||
best_idx = i
|
||||
}
|
||||
}
|
||||
|
||||
// Count selected features
|
||||
selected_count := 0
|
||||
for i in 0 ..< NUMBER_OF_FEATURES {
|
||||
if bit_array.get(population[best_idx], i) {
|
||||
selected_count += 1
|
||||
}
|
||||
}
|
||||
|
||||
fmt.printfln("\nBest solution: %d features selected, RMSE=%.4f", selected_count, best_rmse)
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
data, ok := read_data(DATA_FILE)
|
||||
// Load knapsack data
|
||||
knapsack_data, ok := read_data(DATA_FILE)
|
||||
if !ok {
|
||||
fmt.eprintln("Failed to read data from", DATA_FILE)
|
||||
fmt.eprintln("Failed to load knapsack data")
|
||||
return
|
||||
}
|
||||
items = data
|
||||
items = knapsack_data
|
||||
|
||||
fmt.println("Running Genetic Algorithm for Binary Knapsack Problem")
|
||||
fmt.printfln(
|
||||
"Items: %d, Capacity: %d, Population: %d, Generations: %d\n",
|
||||
NUMBER_OF_ITEMS,
|
||||
CAPACITY,
|
||||
POPULATION_SIZE,
|
||||
GENERATIONS,
|
||||
)
|
||||
// Load feature selection dataset
|
||||
feature_data, dataset_ok := load_dataset(DATASET_FILE)
|
||||
if !dataset_ok {
|
||||
fmt.eprintln("Failed to load dataset from:", DATASET_FILE)
|
||||
return
|
||||
}
|
||||
dataset = feature_data
|
||||
|
||||
run_ga()
|
||||
fmt.println("=== Baseline (All Features) ===")
|
||||
baseline_rmse := run_baseline()
|
||||
fmt.printfln("RMSE with all features: %.4f\n", baseline_rmse)
|
||||
|
||||
fmt.println("=== GA Feature Selection ===")
|
||||
run_ga_feature_selection()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user