Roulette Wheel

Write a program in Lisp to perform roulette wheel selection. (Enter a population of binary strings. Consider fitness value as the number of repetitions of a specified bit (say 1) in the string.)


CODE:

(defun strStg (strWord)
(setq stgCnt 0)
(setq lenCnt 0)
                (loop
                                (when (equal lenCnt (length strWord)) (return stgCnt))
                                (if (equal (char "1" 0) (char strWord lenCnt)) (incf stgCnt))
                                (incf lenCnt)
                )
)

(strStg "10001")

(defun RW (strOne strTwo strThree strFour strFive)
(setq Cnt 0)
(setq stgList '())

                (setq strOneStg (strStg strOne))
                (setq strTwoStg (strStg strTwo))
                (setq strThreeStg (strStg strThree))
                (setq strFourStg (strStg strFour))
                (setq strFiveStg (strStg strFive))

                (setq stgList (push strOneStg stgList))
                (setq stgList (push (+ strOneStg strTwoStg) stgList))       
                (setq stgList (push (+ strOneStg strTwoStg strThreeStg) stgList))
                (setq stgList (push (+ strOneStg strTwoStg strThreeStg strFourStg) stgList))
                (setq stgList (push (+ strOneStg strTwoStg strThreeStg strFourStg strFiveStg) stgList))

                (setq strSel (Mod (random 1000) (first stgList)))
                (print strSel)

                (loop
                                (when (equal (first stgList) nil) (return))
                                (if (>= strSel (first stgList))
                                                (block blkIf
                                                                (if(equal Cnt 0)(return strOne))
                                                                (if(equal Cnt 1)(return strTwo))
                                                                (if(equal Cnt 2)(return strThree))
                                                                (if(equal Cnt 3)(return strFour))
                                                                (if(equal Cnt 4)(return strFive))
                                                )
                                )
                                (setq stgList (rest stgList))
                                (incf Cnt)
                )
)

(RW "100110" "100101" "001010" "010101" "100001")

OUTPUT:
---- Done ----

1

---- Done ----

7
"010101"

0 comments: