Skip to content

Write a program in Lisp to demonstrate the crossover genetic operator

AIM: Write a program in Lisp to demonstrate the crossover genetic operator. 

Post by: Sohrab Vakharia

(Enter population of binary strings. Perform crossover based on a random crossing site value. Consider fitness value as the number of repetitions of specified bit in the string. Perform the evolution for a specified number generation).

Source Code:

(defun crossover(List1 List2)
    (setq cnt 0)
    (setq A ‘())
    (setq B ‘())
    (setq cpt (random(length List1)))
   
    (loop
        (when (and (equal (first List1) nil) (equal (first List2) nil))(return))
            (if (<= cpt cnt)
                (block blk1
                    (setq A(cons(first List2) A))
                    (setq B(cons(first List1) B))           
                )
                (block blk2
                    (setq A(cons(first List1) A))
                    (setq B(cons(first List2) B))           
                )
            )
        (incf cnt)
        (setq List1 (rest List1))
        (setq List2 (rest List2))
    )
    (setq List1 (append A  B))
    (setq List2 (append B  A))
)

(defun actualcrossover(LAlpha LBeta  LGamma )
    (setq Lgencntr 0)(setq i 1)
    (setq TempLst ‘())
    (loop
        (when (equal Lgencntr LGamma)(return))
            (setq Postcntr 0)(setq TempLst ‘())
            (setq TempLst (crossover LAlpha LBeta  ))
            (setq LenTempLst (length TempLst))
            (setq LAlpha ‘())
            (setq LBeta ‘())
            (loop
                (when (equal (first TempLst) nil)(return))
                (if( < Postcntr (/ LenTempLst 2))
                        (setq LAlpha (cons (first TempLst ) LAlpha))
                        (setq LBeta (cons (first TempLst ) LBeta))
                )
                (incf Postcntr)
                (setq TempLst (rest TempLst ))
            )
            (incf Lgencntr)(incf i)
            (format t “~% ~A Point of Crossover in list ~A” (- i 1) cpt)   
            (format t “~% “)   
            (format t “~% After Crossover the output shows :” )
            (print LAlpha)
            (print LBeta)
            (print ‘*****************************)
    ))

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!