|
|
@@ -1,4 +1,4 @@
|
|
|
-#lang typed/racket
|
|
|
+#lang racket
|
|
|
|
|
|
;; A simple genetic algorithm
|
|
|
;; Computer Exercise #1 from Intro to GA by Mitchell
|
|
|
@@ -21,13 +21,11 @@
|
|
|
|
|
|
|
|
|
; Fitness assessment is the number of 1's in the population member
|
|
|
-(: fitness (-> (Listof Integer) Real))
|
|
|
(define (fitness member)
|
|
|
(/ (sum member)
|
|
|
(length member)))
|
|
|
|
|
|
; Flip a chromosome to its opposite
|
|
|
-(: flip-chrom (-> Integer Integer))
|
|
|
(define (flip-chrom val)
|
|
|
(if (eq? val 1) 0 1))
|
|
|
|
|
|
@@ -35,9 +33,8 @@
|
|
|
; mutate
|
|
|
; because of the way the population is stored and that mutations happen after
|
|
|
; offspring are produced, we can mutate the whole list together
|
|
|
-(: mutate (-> (Listof Integer) (Listof Integer)))
|
|
|
(define (mutate pop)
|
|
|
- (for/list: : Listof Integer ([i : Index pop])
|
|
|
+ (for/list ([i pop])
|
|
|
((λ (chrom)
|
|
|
(if (<= (random) p-mutate)
|
|
|
(flip-chrom i)
|
|
|
@@ -45,7 +42,6 @@
|
|
|
i)))
|
|
|
|
|
|
; Initialize a random population.
|
|
|
-;(: initpop (-> (Listof Integer)))
|
|
|
-;(define initpop
|
|
|
-; (for/list ([i (in-range (* popsize chromlen))])
|
|
|
-; (random-integer 0 1)))
|
|
|
+(define (initpop)
|
|
|
+ (for/list ([i (in-range (* popsize chromlen))])
|
|
|
+ (random-integer 0 2)))
|