Last AI Quiz!!
Writing the roulette function
Complete the functions necessary to pick a new population of individuals
based on roulette selection. In the example below, individual 1 was
picked once, individual 2 twice, and individual 4 once.
Initial Expected Actual
Population x Value pselect count Count
String (randomly (unsigned f(x) f(i)/sum(f) f(i)/avg(f) (roulette
No. generated) integer) x^2 wheel)
-----------------------------------------------------------------------------
1 0 1 1 0 1 13 169 0.14 0.58 1
2 1 1 0 0 0 24 576 0.49 1.97 2
3 0 1 0 0 0 8 64 0.06 0.22 0
4 1 0 0 1 1 19 361 0.31 1.23 1
-----------------------------------------------------------------------------
Sum 1170 1.00 4.00 4.0
Average 293 0.25 1.00 1.0
Max 576 0.49 1.97 2.0
(defvar *population*
'(((0 1 1 0 1) . 0.14)
((1 1 0 0 0) . 0.49)
((0 1 0 0 0) . 0.06)
((1 0 0 1 1) . 0.31) ) )
(defun roulette (pop)
(let ( (probList nil)
(n 0)
(sum 0)
(randomNum 0.0)
)
(setf randomNum (random 1.0))
(format t "RandomNum=~F~%" randomNum)
(dolist (___________)
;; if the randomNum is <= the sum, return the current
;; individual
)
)
)
(defun printPopulation (pop)
(format t "Population:~%")
(dolist (indiv pop)
(format t "~a~%" indiv)
)
)
(defun testRoulette ()
(let ((newPopulation nil)
(len (length *population*))
(newIndiv nil)
)
(printPopulation *population*)
(dotimes (________)
(setf newIndiv (roulette *population*) )
(format t "Individual picked:~a~%" newIndiv)
;; add the individual picked to the end of the
;; newPopulation list
)
(printPopulation newPopulation)
newPopulation ;; return the newPopulation
)
)
(testRoulette)