Advertisement
Guest User

Untitled

a guest
Jul 31st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 7.59 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define pi 3.141592)
  4.  
  5. (define-namespace-anchor anc)
  6.  
  7. (define ns (namespace-anchor->namespace anc))
  8.  
  9. (define (char->symbol c) (string->symbol (string c)))
  10.  
  11. (define (nvector . vars) (apply list vars))
  12.  
  13. (define (interval low high) (cons low high))
  14.  
  15. (define (inrange range) (+ (car range) (/ (random (* 10000 (- (cdr range) (car range)))) 10000)))
  16.  
  17. (define (particle pos velocity) (cons pos velocity))
  18.  
  19. (define (newparticle ranges) (particle (map (lambda (range) (inrange range)) ranges) (map (lambda (range) (* .1 (inrange range))) ranges)))
  20.  
  21. (define (newcandidate ranges) (candidate (map (lambda (range) (inrange range)) ranges) (map (lambda (range) (* .1 (inrange range))) ranges) #f))
  22.  
  23. (define maximise #t) (define minimise #f)
  24.  
  25. (define (candidate pos velocity best) (cons (particle pos velocity) best))
  26.  
  27. (define (index l n) (if (zero? n) (car l) (index (cdr l) (- n 1))))
  28.  
  29. (define (newpopulation populationsize ranges) (map (lambda (x) (newcandidate ranges)) (range populationsize)))
  30.  
  31. (define (iteratecandidate c globalextreme) (let ((localextreme (cdr candidate))) (candidate globalextreme)))
  32.  
  33. (define (applyparticle p f) (apply f (car p)))
  34.  
  35. (define (ratecandidate c f) (applyparticle (car c) f))
  36.  
  37. (define (best pop currentextreme direction) (if (null? pop) currentextreme (if (< (cadr (car pop)) (car currentextreme)) (best (cdr pop) (cdar pop) direction) (best (cdr pop) currentextreme direction) )))
  38.  
  39. (define (stepcandidatepos p v) (if (null? p) '() (cons (+ (car v) (car p)) (stepcandidatepos (cdr p) (cdr v)))))
  40.  
  41. (define (stepcandidatevelocity p v ge le) (if (null? v) '() (cons (+ (* (car v) .9) (* .01 (- (car ge) (car p))) (* .01 (- (car le) (car p)))) (stepcandidatevelocity (cdr p) (cdr v) (cdr ge) (cdr le)))))
  42.  
  43. (define (stepcandidate c ge) (candidate (stepcandidatepos (caar c) (cdar c)) (stepcandidatevelocity (caar c) (cdar c) (cdr ge) (cddr c)) (cdr c)))
  44.  
  45. (define (movepop pop globalextreme) (map (lambda (a) (stepcandidate a globalextreme)) pop))
  46.  
  47. (define (iteratepopulation candidates f globalextreme direction n)
  48.   (if (= 0 n) candidates
  49. (let ((fitnesses (map (lambda (x) ((lambda (y) (candidate (caar x) (cdar x) (cons y (caar x)))) (ratecandidate x f))) candidates)))
  50. (let ((GE (best fitnesses globalextreme direction)))
  51.   (iteratepopulation (movepop fitnesses globalextreme) f GE direction (- n 1))))))
  52.  
  53. (define (varnames n) (map (lambda (x) (char->symbol (integer->char (+ 97 x)))) (range n)))
  54.  
  55. (define (controlnames n) (map (lambda (x) (string->symbol (string-append "c" (format "~v" x)))) (range n)))
  56.  
  57. (define-syntax-rule (funcify inps val)
  58.    `(lambda ,inps ,val))
  59.  
  60. (define-syntax-rule (difference a b) `(abs (- ,a ,b)))
  61.  
  62. (define-syntax-rule (a=b a b varcount) (funcify (varnames varcount) (difference a b)))
  63.  
  64. (define-syntax-rule (quickPSO f vars direction)
  65.   (best (iteratepopulation (newpopulation 75 (map (lambda (x) (interval -100 100)) (range vars))) (eval f ns) '(99999 0 0 0 0 0 0 0 0) direction 90) '(99999 0 0 0 0 0 0 0 0) minimise))
  66.  
  67. (define (listdifference a b) (if (null? a) 0 (+ (abs (- (car a) (car b))) (listdifference (cdr a) (cdr b)))))
  68.  
  69. (define (IOmatch trainingset f)
  70. (let ((inputvars (if (list? (caar trainingset)) (length (caar trainingset)) 1))
  71.       (outputvars (if (list? (cdar trainingset)) (length (cdar trainingset)) 1)))
  72.  (quickPSO  (funcify (varnames 8) `(listdifference (map (lambda ,(controlnames inputvars) f) ',(map car trainingset)) ',(map cdr trainingset))) 8 minimise)
  73.   ))
  74.  
  75. (define (randleaf psovars controls)
  76.     (if (zero? (random 3))
  77.                                        
  78.                                         (string->symbol (string-append "c" (format "~v" (random controls))))
  79.                                         (char->symbol (integer->char (+ 97 (random psovars))))))
  80. (define (randunary) (index '(sin exp cos) (random 3)))
  81.  
  82. (define (ainb? a b) (foldr (lambda (x y) (or (equal? x a) y)) false b))
  83.  
  84. (define (unary? value) (ainb? value '(sin exp cos)))
  85.  
  86. (define (randbinary) (index '(+ - *) (random 3)))
  87.  
  88.  
  89. (define (binary? value) (ainb? value '(+ - *)))
  90. (define (randfunc depth psovars controls) (if (zero? depth)
  91.                                                (randleaf psovars controls)
  92.                                                (if (zero? (random 4))
  93.                                                    (randleaf psovars controls)
  94.                                                    (if (zero? (random 2))
  95.                                                        `(,(randunary) ,(randfunc (- depth 1) psovars controls))
  96.                                                        `(,(randbinary) ,(randfunc (- depth 1) psovars controls) ,(randfunc (- depth 1) psovars controls))))))
  97. (define (functionpool size depth args) (if (zero? size) '() (cons (randfunc depth 8 args) (functionpool (- size 1) depth args))))
  98.  
  99. (define (iofunc trainingset f)
  100.   (let ((input (map car trainingset))
  101.         (output (map cdr trainingset)))
  102.    
  103.     (cons (funcify (varnames 8) `(listdifference  ',output (map (lambda (c0) ,f)  ',input))) f)))
  104.  
  105. (define (formatfunctions trainingset population direction)
  106.   (map (lambda (x) (let ((iof (iofunc trainingset x))) (list iof (quickPSO (car iof) 8 direction)))) population))
  107.  
  108. (define (1stgenio trainingset)
  109.   (let ((start (functionpool 500 20 1)))  
  110. (formatfunctions trainingset start minimise)
  111.  ))
  112.  
  113. (define ts `(
  114.     (1 . 3)
  115.     (2 . 5)
  116.     (3 . 7)
  117.     (4 . 11)
  118.     (5 . 13)
  119.     (6 . 17)
  120.     (7 . 19)
  121.     (8 . 23)
  122.     (9 . 27)
  123.     (10 . 31)
  124.     (11 . 37)))
  125.  
  126. (define 1stg (1stgenio ts))
  127.  
  128. (define (formatted x) (caar x))
  129.  
  130. (define (score x) (caadr x))
  131.  
  132. (define (bestcandidate x) (cdadr x))
  133.  
  134. (define (unformatted x) (cdar x))
  135.  
  136. (define (sortbyfitness genepop direction)
  137.   (sort genepop (if direction >= <=) #:key score))
  138.  
  139. (define (AorB a b) (if (zero? (random 2)) a b))
  140.  
  141. (define (left l n) (if (<= n 0)
  142.                        '()
  143.                        (cons
  144.                         (car l)
  145.                         (left (cdr l) (- n 1)))))
  146.  
  147. (define (right l n) (if (null? l) '() (if (<= 0 n) (cdr l) (right (cdr l) (- n 1)))))
  148.  
  149. (define (mapAB+remainder f a b)
  150.   (if (= (length a) (length b)) (map f a b)
  151.   (let ((m (min (length a) (length b))) (abigger (> (length a) (length b))))
  152.     (append (map f (left a m) (left b m)) (if abigger (right a (- (length a) m)) (right b (- (length b) m)))))))
  153.  
  154. (define (biasselect l) (index l (floor (* (expt (/ (random 1000) 1000) 3) (- (length l) 1)))))
  155.  
  156. (define (combine a b) (combineinner (unformatted a) (unformatted b)))
  157.  
  158. (define (combineinner a b)
  159.   (if (or (not (list? a)) (not (list? b)))
  160.       (AorB a b)
  161.       (if (zero? (random 2))
  162.           (if (binary? a)
  163.               (cons (car a) (mapAB+remainder AorB (cdr a) (cdr b)))
  164.               (list (car a) (AorB (cadr a) (cadr b))))
  165.           (if (binary? b)
  166.               (cons (car b) (mapAB+remainder AorB (cdr a) (cdr b)))
  167.               (list (car b) (AorB (cadr a) (cadr b)))))))
  168.  
  169. (define (iterategeneration trainingset population direction iterations)
  170.   (if (zero? iterations) population
  171.   (let ((sortedpop (sortbyfitness population direction))
  172.         (lp (length population)))
  173.     (begin
  174.       (write (displaybest sortedpop))
  175.       (write "\n\n")
  176.      (iterategeneration trainingset (formatfunctions trainingset (cons (unformatted (car sortedpop)) (map (lambda (x) (combine (biasselect sortedpop) (biasselect sortedpop))) (range (- (length population) 1)))) minimise) minimise (- iterations 1)))
  177.     )))
  178.  
  179. (define (displaybest pop)
  180.   (car (sortbyfitness pop minimise)))
  181.  
  182. (iterategeneration ts 1stg minimise 100)
  183.  
  184. "(define (exportpopulation population foldername)
  185.  
  186.  )"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement