Guest User

Using Genetic Algorithm

a guest
Sep 16th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.91 KB | None | 0 0
  1. (seed (time))
  2.  
  3. (load "@lib/simul.l")
  4. (load "@lib/math.l")
  5.  
  6. (symbols '(simul pico))
  7.  
  8. # goal   ax+b=y   find a and b when y(5.0)=13.0
  9. (setq X 5.0 Y 13.0)
  10.  
  11. (de function (A)
  12.   (+ (*/ (car A) X 1.0) (cadr A)) )
  13.  
  14. (setq Population
  15.   (make
  16.     (do 10
  17.       (link (list (rand -10.0 10.0) (rand -10.0 10.0))))) )
  18.  
  19. (de termination (A)
  20.   (let M (maxi fitness A) F @@
  21.   (prinl
  22.     (pack
  23.       (format (car M) *Scl)
  24.       " X "
  25.       (format (cadr M) *Scl)
  26.       " = "
  27.       (format (function M) *Scl)
  28.       "\t\tError: " (fitness M) ))
  29.     (> (fitness M) -5) ))
  30.  
  31. (de combine (A B)
  32.   (if (rand T) A B) )
  33. (de recombination (A B)
  34.   (mapcar combine A B) )
  35.  
  36. (de mutate (A)
  37.   (if (=0 (rand 0 10))
  38.     (+ A (rand -2.0 2.0))
  39.     A) )
  40. (de mutation (A)
  41.   (mapcar mutate A) )
  42.  
  43. (de fitness (A)
  44.   (- (pow (- Y (function A)) 2.0)) )
  45.  
  46. (gen
  47.   Population
  48.   termination
  49.   recombination
  50.   mutation
  51.   fitness)
  52.  
  53. (bye)
Advertisement
Add Comment
Please, Sign In to add comment