Kaynağa Gözat

mutation (chromosome flip) and population initlization. non-working because of typing issues

George C. Privon 6 yıl önce
ebeveyn
işleme
307edaf54c
1 değiştirilmiş dosya ile 23 ekleme ve 7 silme
  1. 23 7
      chap1/comp1/simpleGA.rkt

+ 23 - 7
chap1/comp1/simpleGA.rkt

@@ -7,6 +7,8 @@
 ;; - fitness-proportionate selection
 ;; - routlette-wheel sampling
 
+; The population is stored as a single list of popsize*chromlen elements
+
 (require math)
 
 ; population size and individual chromosome length
@@ -24,12 +26,26 @@
   (/ (sum member)
      (length member)))
 
+; Flip a chromosome to its opposite
+(: flip-chrom (-> Integer Integer))
+(define (flip-chrom val)
+  (if (eq? val 1) 0 1))
 
-; mutate an individual
-(: mutate (-> (Listof Integer) (Listof Integer)))
-(define (mutate member)
-  )
 
-(: genpop (-> (Listof Integer)))
-(define genpop
-  )
+; 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])
+    ((λ (chrom)
+       (if (<= (random) p-mutate)
+           (flip-chrom i)
+           i))
+     i)))
+
+; Initialize a random population.
+;(: initpop (-> (Listof Integer)))
+;(define initpop
+;  (for/list ([i (in-range (* popsize chromlen))])
+;    (random-integer 0 1)))