Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 5.78 KB | None | 0 0
  1. #lang racket
  2. (define-namespace-anchor anc)
  3.  
  4. (define ns (namespace-anchor->namespace anc))
  5.  
  6. (define (char->symbol c) (string->symbol (string c)))
  7.  
  8. (define (nvector . vars) (apply list vars))
  9.  
  10. (define (interval low high) (cons low high))
  11.  
  12. (define (inrange range) (+ (car range) (random (- (cdr range) (car range)))))
  13.  
  14. (define (particle pos velocity) (cons pos velocity))
  15.  
  16. (define (newparticle ranges) (particle (map (lambda (range) (inrange range)) ranges) (map (lambda (range) (* .1 (inrange range))) ranges)))
  17.  
  18. (define (newcandidate ranges) (candidate (map (lambda (range) (inrange range)) ranges) (map (lambda (range) (* .1 (inrange range))) ranges) #f))
  19.  
  20. (define maximise #t) (define minimise #f)
  21.  
  22. (define (candidate pos velocity best) (cons (particle pos velocity) best))
  23.  
  24. (define (index l n) (if (zero? n) (car l) (index (cdr l) (- n 1))))
  25.  
  26. (define (newpopulation populationsize ranges) (map (lambda (x) (newcandidate ranges)) (range populationsize)))
  27.  
  28. (define (iteratecandidate c globalextreme) (let ((localextreme (cdr candidate))) (candidate globalextreme)))
  29.  
  30. (define (applyparticle p f) (apply f (car p)))
  31.  
  32. (define (ratecandidate c f) (applyparticle (car c) f))
  33.  
  34. (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) )))
  35.  
  36. (define (stepcandidatepos p v) (if (null? p) '() (cons (+ (car v) (car p)) (stepcandidatepos (cdr p) (cdr v)))))
  37.  
  38. (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)))))
  39.  
  40. (define (stepcandidate c ge) (candidate (stepcandidatepos (caar c) (cdar c)) (stepcandidatevelocity (caar c) (cdar c) (cdr ge) (cddr c)) (cdr c)))
  41.  
  42. (define (movepop pop globalextreme) (map (lambda (a) (stepcandidate a globalextreme)) pop))
  43.  
  44. (define (iteratepopulation candidates f globalextreme direction n)
  45.   (if (= 0 n) candidates
  46. (let ((fitnesses (map (lambda (x) ((lambda (y) (candidate (caar x) (cdar x) (cons y (caar x)))) (ratecandidate x f))) candidates)))
  47. (let ((GE (best fitnesses globalextreme direction)))
  48.   (iteratepopulation (movepop fitnesses globalextreme) f GE direction (- n 1))))))
  49.  
  50. (define (varnames n) (map (lambda (x) (char->symbol (integer->char (+ 97 x)))) (range n)))
  51.  
  52. (define (controlnames n) (map (lambda (x) (string->symbol (string-append "c" (format "~v" x)))) (range n)))
  53.  
  54. (define mypop
  55. (iteratepopulation (newpopulation 30 (list (interval -100 100) (interval -100 100) (interval -100 100))) (lambda (a b c) (abs (- 420 (+ a (* a (- b c)) c (* c a))))) '(99999 0 0 0) minimise 300) )
  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 30 (map (lambda (x) (interval -100 100)) (range vars))) (eval f ns) '(99999 0 0 0 0 0 0 0 0) direction 300) '(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 (randbinary) (index '(+ - *) (random 3)))
  83.  
  84. (define (randfunc depth psovars controls) (if (zero? depth)
  85.                                                (randleaf psovars controls)
  86.                                                (if (zero? (random 4))
  87.                                                    (randleaf psovars controls)
  88.                                                    (if (zero? (random 2))
  89.                                                        `(,(randunary) ,(randfunc (- depth 1) psovars controls))
  90.                                                        `(,(randbinary) ,(randfunc (- depth 1) psovars controls) ,(randfunc (- depth 1) psovars controls))))))
  91. (define (functionpool size depth args) (if (zero? size) '() (cons (randfunc depth 8 args) (functionpool (- size 1) depth args))))
  92.  
  93. (define (iofunc trainingset f)
  94.   (let ((input (map car trainingset))
  95.         (output (map cdr trainingset)))
  96.    
  97.     (cons (funcify (varnames 8) `(listdifference  ',output (map (lambda (c0) ,f)  ',input))) f)))
  98.  
  99. (define (1stgenio trainingset)
  100.   (let ((start (functionpool 10 4 1)))  
  101. (map (lambda (x) (let ((iof (iofunc trainingset x))) (list iof (quickPSO (car iof) 8 minimise)))) start)  
  102.  ))
  103.  
  104. (define ts '((0 . 1)
  105.     (1 . 2)
  106.     (2 . 4)
  107.     (3 . 8)
  108.     (4 . 16)
  109.     (5 . 32)
  110.     (6 . 64)))
  111.  
  112. (define 1stg (1stgenio ts))
  113.  
  114. (define (formatted x) (caar x))
  115.  
  116. (define (score x) (caadr x))
  117.  
  118. (define (bestcandidate x) (cdadr x))
  119.  
  120. (define (unformatted x) (cdar x))
  121.  
  122. (define (sortbyfitness genepop direction)
  123.   (sort genepop (if direction <= >=) #:key score))
  124.  
  125. (define (biasselect l) (index l (* (expt (/ (random 1000) 1000) 2) (length l))))
  126.  
  127. (define (iterategeneration population direction)
  128.   (let ((sortedpop (sortbyfitness population direction)))
  129.     (map  (range (length population)))
  130.     ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement