code wiki / _hdl_build / nx_grammatical_evolution_gate.nx
nx_grammatical_evolution_gate.nx source
↩ module page · 78 lines · 5927 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_grammatical_evolution_gate.nx -- GRAMMATICAL EVOLUTION (O'Neill & Ryan): map an integer GENOME to a PROGRAM via a
4// BNF GRAMMAR, codon-by-codon, rule = codon mod (number of productions) (operator: mechanistic-AI foundation, Java
5// Grammatical Evolution was a primary link). GE's key property: ANY genome yields a SYNTACTICALLY VALID program (the
6// grammar guarantees it) -- so a GA can evolve genomes in a fixed integer space while the grammar enforces structure.
7// This bridges the GA (nx_genetic_algorithm) and symbolic regression (nx_expr_*). Pure integer mapping, NO LLM.
8// T0 GRAMMAR: <expr> ::= <var> <op> <var> ; <var> ::= x|y|z ; <op> ::= + | * | - (BNF productions).
9// T1 MAPPING: rule = codon mod num_productions -- genome [0,1,1] -> var0=x, op1=*, var1=y -> "x * y".
10// T2 DISTINCT PHENOTYPES: genome [2,0,0] -> "z + x"; a different genome yields a different valid expression.
11// T3 ALWAYS VALID: even an arbitrary genome maps to a well-formed expression (GE's guarantee) -- no syntax errors.
12// T4 EVALUATE: the phenotype runs -- "x*y"=15, "z+x"=5 (x=3,y=5,z=2).
13// T5 = grammatical evolution maps genomes -> valid programs via a grammar; evolvable by the GA, no LLM.
14// license_tier: ORIGINAL
15import "nx_syscalls.nx"
16
17// vars x=3,y=5,z=2 ; ops 0=+ 1=* 2=-
18func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
19" as *u8); return ok }
20func varval(idx: i64) -> i64 { if idx==0 { return 3 } if idx==1 { return 5 } return 2 }
21func varname(idx: i64) -> *u8 { if idx==0 { return "x" as *u8 } if idx==1 { return "y" as *u8 } return "z" as *u8 }
22func opname(idx: i64) -> *u8 { if idx==0 { return "+" as *u8 } if idx==1 { return "*" as *u8 } return "-" as *u8 }
23func apply_op(op: i64, a: i64, b: i64) -> i64 { if op==0 { return a+b } if op==1 { return a*b } return a-b }
24// map genome (3 codons) -> phenotype: <var> <op> <var>. returns the evaluated value; writes chosen rules to out[0..2].
25func ge_map(genome: *i64, out: *i64) -> i64 {
26 out[0]=genome[0]%3 // <var>
27 out[1]=genome[1]%3 // <op>
28 out[2]=genome[2]%3 // <var>
29 return apply_op(out[1], varval(out[0]), varval(out[2]))
30}
31
32func main() -> i64 {
33 gw("=== nx_grammatical_evolution_gate: BNF grammar + codon mapping -> valid programs, no LLM ===\n" as *u8)
34 var pass: i64=0; var total: i64=0
35
36 // T0 grammar.
37 total=total+1; pass=pass+1
38 gw(" [PASS] T0 GRAMMAR: <expr>::=<var><op><var> ; <var>::=x|y|z ; <op>::=+|*|- (x=3,y=5,z=2)\n" as *u8)
39
40 // T1 mapping genome [0,1,1].
41 let gA: *i64=sys_mmap(32) as *i64; gA[0]=0; gA[1]=1; gA[2]=1
42 let rA: *i64=sys_mmap(32) as *i64; let vA: i64=ge_map(gA, rA)
43 total=total+1; if rA[0]==0 { if rA[1]==1 { if rA[2]==1 { if vA==15 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
44 gw("T1 MAPPING: genome[0,1,1] -> " as *u8); gw(varname(rA[0])); gw(" " as *u8); gw(opname(rA[1])); gw(" " as *u8); gw(varname(rA[2])); gw(" = " as *u8); gn(vA); gw(" (rule=codon mod num_productions)\n" as *u8)
45
46 // T2 distinct phenotype genome [2,0,0].
47 let gB: *i64=sys_mmap(32) as *i64; gB[0]=2; gB[1]=0; gB[2]=0
48 let rB: *i64=sys_mmap(32) as *i64; let vB: i64=ge_map(gB, rB)
49 total=total+1; if vB==5 { if rB[0]!=rA[0] { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
50 gw("T2 DISTINCT: genome[2,0,0] -> " as *u8); gw(varname(rB[0])); gw(" " as *u8); gw(opname(rB[1])); gw(" " as *u8); gw(varname(rB[2])); gw(" = " as *u8); gn(vB); gw(" (a different valid expression)\n" as *u8)
51
52 // T3 always valid: try arbitrary genomes, all map to valid expressions (rules always in range).
53 var allvalid: i64=1; var g: i64=0
54 while g<27 {
55 let gg: *i64=sys_mmap(32) as *i64; gg[0]=g; gg[1]=g*7+1; gg[2]=g*13+2
56 let rr: *i64=sys_mmap(32) as *i64; ge_map(gg, rr)
57 if rr[0]<0 { allvalid=0 } if rr[0]>2 { allvalid=0 } if rr[1]<0 { allvalid=0 } if rr[1]>2 { allvalid=0 } if rr[2]<0 { allvalid=0 } if rr[2]>2 { allvalid=0 }
58 g=g+1
59 }
60 total=total+1; if allvalid==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
61 gw("T3 ALWAYS VALID: 27 arbitrary genomes ALL mapped to well-formed expressions (the grammar guarantees validity)\n" as *u8)
62
63 // T4 evaluate (already done: vA=15, vB=5).
64 total=total+1; if vA==15 { if vB==5 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
65 gw("T4 EVALUATE: phenotypes run -- x*y=" as *u8); gn(vA); gw(", z+x=" as *u8); gn(vB); gw(" (the program executes)\n" as *u8)
66
67 // T5.
68 total=total+1; if allvalid==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
69 gw("T5 GRAMMATICAL EVOLUTION: genome -> grammar -> valid program mapping; evolvable by the GA over a fixed integer space, no LLM\n" as *u8)
70
71 gw("\n GRAMMATICAL EVOLUTION (O'Neill & Ryan): an integer genome maps to a program through a BNF grammar, rule = codon mod\n" as *u8)
72 gw(" num_productions. The grammar GUARANTEES every genome is a valid program (27/27 here) -- so the GA (nx_genetic_algorithm) can\n" as *u8)
73 gw(" evolve genomes in a plain integer space while structure is enforced by the grammar. This bridges the GA and symbolic\n" as *u8)
74 gw(" regression (nx_expr_tree): evolve grammars-of-laws. Pure integer mapping, NO LLM. Foundation rung.\n" as *u8)
75 gw("GRAMMATICAL-EVOLUTION verdict=" as *u8)
76 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- genome->grammar->valid-program mapping, evolvable, no LLM\n" as *u8); sys_exit(0); return 0 }
77 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
78}