Crossover
Write a program in Lisp to demonstrate the crossover genetic operator.
(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).
CODE:
(defun
Cross(List1 List2)
(setq Cnt 0)
(setq newList1
'())
(setq newList2
'())
(setq cp (random
(length List1)))
(print
"CrossOver Point")
(prin1 cp)
(loop
(when (equal
(first List1) nil)(return))
(if (<= cp
Cnt)
(block
blkOne
(setq
newList1 (cons (first List2) newList1))
(setq
newList2 (cons (first List1) newList2))
)
(block
blkTwo
(setq
newList1 (cons (first List1) newList1))
(setq
newList2 (cons (first List2) newList2))
)
)
(incf Cnt)
(setq List1
(rest List1))
(setq List2
(rest List2))
)
(setq List1 (append newList1
newList2))
)
(defun mainCross
(listOne listTwo Gen)
(setq genCntr 0)
(setq tempList
'())
(loop
(when(equal
genCntr Gen)(return))
(setq posCntr 0)(setq tempList '())
(setq tempList
(Cross listOne listTwo))
(setq lenTempList (length templist))
(setq listOne
'())
(setq listTwo '())
(loop
(when (equal (first tempList) nil)(return))
(if
(< posCntr (/ lenTempList 2))
(setq
listOne (cons (first tempList) listOne))
(setq listTwo (cons (first tempList)
listTwo))
)
(incf
posCntr)
(setq
tempList (rest tempList))
)
(incf genCntr)
(print "Result of Crossing")
(print listOne)
(print listTwo) ))
(mainCross '(1 2 3 4) '(5 6 7 8) 3)
OUTPUT:
"CrossOver
Point" 0
"Result of
Crossing"
(5 6 7 8)
(1 2 3 4)
"CrossOver
Point" 0
"Result of
Crossing"
(1 2 3 4)
(5 6 7 8)
"CrossOver
Point" 3
"Result of
Crossing"
(1 2 3 8)
(5 6 7 4)
NIL
0 comments: