
Kforth Activity 2: Me Thinks it is a Function
These three sample KFORTH programs can be used with the [MUTATE!] button to see simple evolution occur. The first example evolves a prorgam which returns the largest number.
The second program uses tricks to see evolution attempt to find a number.
The third program continues this idea and shows how you could have evolution find a function.
This functionality is available on the Mutations tab. The MUTATE! button and associated fields can be configured to run simplistic evolution on mutating kforth.
- Times: How many times to repeat the whole process when MUTATE! clicked.
- Population: How many competing algorithms will be in the pool each test.
- Trials: Only when this field greater than zero will the selection algorithm kick in. This is how many times each program is run. the average score is used to pick the best.
Me Thinks it is MAX_INT
; protect these instructions: R0!, R1!, R2!, R0++, R1++, R2++, --R0, --R1, --R2, traps 1-9, HALT ; protected code blocks: 1 main: { evolve call } evolve: { } |
Me Thinks it is a number
; protect these instructions: R0!, R1!, R2!, R0++, R1++, R2++, --R0, --R1, --R2, traps 1-9, HALT ; protected code blocks: 3 main: { evolve call R2! { pop DSLEN ?loop } call goal call R3! max_int R2 R3 - abs ; diff = abs(evolve - goal) - ; score = max_int - difference_from_goal } ; compute f() = 123 goal: { 123 } evolve: { } |
Me Thinks it is a Function
; protect these instructions: R0!, R1!, R2!, R0++, R1++, R2++,--R0, --R1, --R2, traps 1-9, HALT ; protected code blocks: 3 main: { RND R0! 0 R2! R0 evolve call R2! { pop DSLEN ?loop } call R0 goal call R3! max_int R2 R3 - abs ; diff = abs(evolve(x,y) - goal(x,y)) - ; score = max_int - difference_from_goal } ; compute f(x) = 2 * x^2 - 32*x + 222 goal: { dup dup * 2 * swap 32 * - 222 + } evolve: { } |