Mutation

Write a program in Lisp to demonstrate the genetic operator mutation.

(Enter a population of binary strings. Perform mutation based on a random mutation point. Consider fitness value as the number of repetitions of a specified bit (say 1) in the string. Perform the evolution for a specified number generation.)


CODE:

(defun Muta(List)
(setq Cnt 0)
(setq newList '())
(setq mp (random (length List)))

(print "Mutation Point")
(prin1 mp)

                (loop
                                (when (equal (first List) nil) (return))
                                (if (equal mp Cnt)
                                                (if(equal (first List) 0)
                                                                (setq newList (cons '1 newList))
                                                                (setq newList (cons '0 newList))
                                                )
                               
                                                (setq newList (cons (first List) newList))
                                )
                               
                                (incf Cnt)
                                (setq List (rest List))
                )
               
                (setq List (reverse newList))
        (setq newList '())
                (print List)
)

(defun mainMuta (List1 List2 Gen)
(setq FVList1 0)
(setq FVList2 0)
(setq GenCntList1 0)
(setq GenCntList2 0)
(setq dplList '())

                (loop
                                (when (equal GenCntList1 Gen) (return))
                                (when (equal GenCntList2 Gen) (return))
                               
                                (setq FVList1 0)
                                (setq dplList List1)
                                (loop
                                                (when (equal (first List1) nil) (return))
                                                (if(equal (first List1) 1)(incf FVList1))
                                                (setq List1 (rest List1))
                                )
                                (setq List1 dplList)
                               
                                (setq FVList2 0)
                                (setq dplList List2)
                                (loop
                                                (when (equal (first List2) nil) (return))
                                                (if(equal (first List2) 1) (incf FVList2))
                                                (setq List2 (rest List2))
                                )
                                (setq List2 dplList)

                                (if (> FVList1 FVList2)
                                                (block blkOne
                                                                (incf GenCntList1)
                                                                (setq dplList List1)
                                                                (setq List1 (Muta List1))
                                                                (print "List1:-")
                                                                (prin1 List1)
                                                                (prin1 "List1 Generation Count:-")
                                                                (prin1 GenCntList1)
                                                )
                                                (block blkTwo
                                                                (incf GenCntList2)
                                                                (setq dplList List2)
                                                                (setq List2 (muta List2))
                                                                (print "List2:-")
                                                                (prin1 List2)
                                                                (prin1 "List2 Generation Count:-")
                                                                (prin1 GenCntList2)
                                                )
                                )
                )
)


(mainMuta '(1 2 3 4) '(5 6 7 8) 3)

OUTPUT:

"Mutation Point" 0
(0 2 3 4)
"List1:-" (0 2 3 4)"List1 Generation Count:-"1
"Mutation Point" 3
(5 6 7 0)
"List2:-" (5 6 7 0)"List2 Generation Count:-"1
"Mutation Point" 1
(5 0 7 0)
"List2:-" (5 0 7 0)"List2 Generation Count:-"2
"Mutation Point" 1
(5 1 7 0)
"List2:-" (5 1 7 0)"List2 Generation Count:-"3
NIL

0 comments: