-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sequence.c
More file actions
130 lines (104 loc) · 3.9 KB
/
test_sequence.c
File metadata and controls
130 lines (104 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "gp.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
// Test: accumulate sequence
// Task: Given a sequence of numbers, output the running sum
// Example: inputs [3, 5, 2] -> outputs [3, 8, 10]
// Requires memory to track sum so far
float evaluate_sequence(Program* prog, void* data) {
(void)data;
float total_error = 0.0f;
int num_tests = 10;
for (int test = 0; test < num_tests; test++) {
Context ctx = {0}; // Zero-initialize including memory
// Generate random sequence of 5 numbers
int sequence[5];
for (int i = 0; i < 5; i++) {
sequence[i] = rand() % 10;
}
// Run program on each number, expect running sum
int running_sum = 0;
for (int i = 0; i < 5; i++) {
ctx.inputs[0] = sequence[i];
ctx.num_inputs = 1;
ctx.num_outputs = 0; // Reset outputs
execute_program(prog, &ctx, NULL);
running_sum += sequence[i];
int expected = running_sum;
int result = (ctx.num_outputs > 0) ? ctx.outputs[0] : 0;
int error = abs(result - expected);
total_error += error;
}
}
float avg_error = total_error / (num_tests * 5);
float fitness = 100.0f - avg_error;
fitness -= prog->size * 0.01f; // Parsimony
return fitness;
}
int main() {
srand(time(NULL));
printf("Tree-based GP - Sequence Accumulation Test\n");
printf("==========================================\n\n");
printf("Task: Output running sum of inputs\n");
printf("Example: inputs [3,5,2] -> outputs [3,8,10]\n");
printf("Requires memory to track sum\n\n");
Population* pop = pop_create();
int max_gen = 2000;
float best_ever = -INFINITY;
int no_improvement = 0;
for (int gen = 0; gen < max_gen; gen++) {
evolve_generation(pop, evaluate_sequence, NULL, 1);
if (pop->best_fitness > best_ever) {
best_ever = pop->best_fitness;
no_improvement = 0;
} else {
no_improvement++;
}
if (gen % 50 == 0 || pop->best_fitness >= 98.0f) {
printf("Gen %4d: Best=%.1f Avg=%.1f Size=%d Depth=%d\n",
gen,
pop->best_fitness,
pop->avg_fitness,
pop->best ? pop->best->size : 0,
pop->best ? pop->best->depth : 0);
}
if (pop->best_fitness >= 98.0f) {
printf("\n*** TASK SOLVED! ***\n");
printf("Final fitness: %.1f\n", pop->best_fitness);
printf("Solution size: %d nodes\n", pop->best->size);
printf("Solution depth: %d\n", pop->best->depth);
printf("\nSolution tree:\n");
print_tree(pop->best->root, 0);
// Test on specific example
printf("\nTesting on sequence [3, 5, 2, 7, 1]:\n");
Context ctx = {0};
int test_seq[] = {3, 5, 2, 7, 1};
int running_sum = 0;
for (int i = 0; i < 5; i++) {
ctx.inputs[0] = test_seq[i];
ctx.num_inputs = 1;
ctx.num_outputs = 0;
execute_program(pop->best, &ctx, NULL);
running_sum += test_seq[i];
int result = (ctx.num_outputs > 0) ? ctx.outputs[0] : 0;
printf(" Input=%d, Expected=%d, Got=%d %s\n",
test_seq[i], running_sum, result,
(result == running_sum) ? "OK" : "WRONG");
}
break;
}
if (no_improvement > 500) {
printf("\nNo improvement for 500 generations, stopping.\n");
break;
}
}
if (pop->best_fitness < 98.0f) {
printf("\nDid not fully solve (best: %.1f)\n", pop->best_fitness);
printf("Best solution:\n");
print_tree(pop->best->root, 0);
}
pop_destroy(pop);
return 0;
}