浏览代码

some testing code to ensure the mutation works as needed

George C. Privon 6 年之前
父节点
当前提交
b78e55b7d5
共有 1 个文件被更改,包括 19 次插入1 次删除
  1. 19 1
      chap1/comp1/simpleGA.rkt

+ 19 - 1
chap1/comp1/simpleGA.rkt

@@ -44,4 +44,22 @@
 ; Initialize a random population.
 (define (initpop)
   (for/list ([i (in-range (* popsize chromlen))])
-    (random-integer 0 2)))
+    (random-integer 0 2)))
+
+; check mutation rate
+; return the number of members that are different between pop1 and pop2
+(define (mcheck pop1 pop2)
+     (for/list ([i (length pop1)])
+       (if (eq? (list-ref pop1 i) (list-ref pop2 i))
+           0
+           1)))
+
+; testing/debugging code below
+(define pop (initpop))
+(define mutated (mutate pop))
+
+(define (actualmutrate pop mutated)
+  (/ (sum (mcheck pop mutated)) (* popsize chromlen)))
+
+(write-string (string-append (real->decimal-string (actualmutrate pop mutated))
+               "\n"))