#!/usr/bin/env scheme-script (import (rnrs) (rnrs eval) (prefix (revolver) revolver:)) ; We want to find a function that is equivalent to: ; ; f(x) = 4x^2 + 3x + 7 ; ; We use the following 'training' data: ; ; x f(x) ; ------+------- ; -15 | 862 ; 1 | 14 ; 10 | 437 ; 42 | 7189 ; 100 | 40307 ; ; We use 1 / (1 + difference) where difference is the sum of absolute ; difference between the value of our function and the example data. (define (fitness-test individual) (define function (eval `(lambda (x) ,individual) (environment '(rnrs)))) (/ 1.0 (+ 1 (+ (abs (- (function -15) 862)) (abs (- (function 1) 14)) (abs (- (function 10) 437)) (abs (- (function 42) 7189)) (abs (- (function 100) 40307)))))) ;(define (% a b) (if (= b 0) 0 (/ a b))) ; Set up and run the experiment. (let ([size 10] [generations 100] [functions '#((+ . 2) (- . 2) (* . 2))] [terminals '#(random-int x)] [crossover-probability 0.8] [min-depth 2] [max-depth 4] [target-fitness 1.0]) (define population (revolver:make-population size functions terminals min-depth max-depth)) (revolver:show-best (revolver:evolve population fitness-test generations crossover-probability target-fitness)))