[racket] simple genetic algorithm

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Nov 10 14:39:11 EST 2013

I think this is the program you want: 

#lang racket

(module+ test 
  (require rackunit))

;; [List-of Real] -> [List-of Real]

(module+ test 
  (check-equal? (roulette-wheel-ratio '(1 2 3)) '(1/6 2/6 3/6)))

(define (roulette-wheel-ratio generation-fitness) 
  (define total-population-fitness (foldl + 0 generation-fitness))
  (map (lambda (fitness) (/ fitness total-population-fitness)) generation-fitness))




On Nov 10, 2013, at 2:28 PM, Rian Shams wrote:

> Hello All,
> 
> As part of the design for a simple genetic algorithm I have this function:
> 
> (define (roulette-wheel-ratio generation-fitness ) 
>   (cond [(empty? generation-fitness) empty]
>            [else (cons (/ (first generation-fitness)
>                                (total-population-fitness generation-fitness)) 
>                     (roulette-wheel-ratio (rest generation-fitness)))]))
> 
> where generation-fitness is a list of values that correspond to the fitness of each individual in a population. For example, in a generation with population size 12, generation fitness may look like:
> 
> '(7.8807275269175125
>   6.78896220864992
>   6.52572681075793
>   3.208263473483078
>   9.970710802683316
>   10.400703374724888
>   7.434703353949041
>   6.009574612909729
>   2.9503066173989634
>   6.07124467626777
>   2.1893449585751754
>   1.0741024515301607)
>  
> Here is how I have defined the auxiliary function, total-population-fitness:
> 
> (define (total-population-fitness generation-fitness)
>   (foldl + 0 generation-fitness))
> 
> I was wondering how can I modify the above function, roulette-wheel-ratio so that when it evaluates the helper function, total-population-fitness it only does so for the initial list of generation-fitness. Otherwise the total population fitness decreases for each recursive call which is what I don't want, it should remain constant throughout the entire function call.
> 
> Basically, is it possible to modify this function so that the auxiliary function call (total-population-fitness generation-fitness) remains unaffected by the natural recursion imposed by roulette-wheel-ratio? 
> 
> Any help would be appreciated as I am still learning the Racket basics.
> 
> Kind Regards,
> -- 
> Rian Shams
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.