Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (define-namespace-anchor anc)
- (define ns (namespace-anchor->namespace anc))
- (define (char->symbol c) (string->symbol (string c)))
- (define (nvector . vars) (apply list vars))
- (define (interval low high) (cons low high))
- (define (inrange range) (+ (car range) (random (- (cdr range) (car range)))))
- (define (particle pos velocity) (cons pos velocity))
- (define (newparticle ranges) (particle (map (lambda (range) (inrange range)) ranges) (map (lambda (range) (* .1 (inrange range))) ranges)))
- (define (newcandidate ranges) (candidate (map (lambda (range) (inrange range)) ranges) (map (lambda (range) (* .1 (inrange range))) ranges) #f))
- (define maximise #t) (define minimise #f)
- (define (candidate pos velocity best) (cons (particle pos velocity) best))
- (define (newpopulation populationsize ranges) (map (lambda (x) (newcandidate ranges)) (range populationsize)))
- (define (iteratecandidate c globalextreme) (let ((localextreme (cdr candidate))) (candidate globalextreme)))
- (define (applyparticle p f) (apply f (car p)))
- (define (ratecandidate c f) (applyparticle (car c) f))
- (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) )))
- (define (stepcandidatepos p v) (if (null? p) '() (cons (+ (car v) (car p)) (stepcandidatepos (cdr p) (cdr v)))))
- (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)))))
- (define (stepcandidate c ge) (candidate (stepcandidatepos (caar c) (cdar c)) (stepcandidatevelocity (caar c) (cdar c) (cdr ge) (cddr c)) (cdr c)))
- (define (movepop pop globalextreme) (map (lambda (a) (stepcandidate a globalextreme)) pop))
- (define (iteratepopulation candidates f globalextreme direction n)
- (if (= 0 n) candidates
- (let ((fitnesses (map (lambda (x) ((lambda (y) (candidate (caar x) (cdar x) (cons y (caar x)))) (ratecandidate x f))) candidates)))
- (let ((GE (best fitnesses globalextreme direction)))
- (iteratepopulation (movepop fitnesses globalextreme) f GE direction (- n 1))))))
- (define (varnames n) (map (lambda (x) (char->symbol (integer->char (+ 97 x)))) (range n)))
- (define (controlnames n) (map (lambda (x) (string->symbol (string-append "c" (format "~v" x)))) (range n)))
- (define mypop
- (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) )
- (define-syntax-rule (funcify inps val)
- `(lambda ,inps ,val))
- (define-syntax-rule (difference a b) `(abs (- ,a ,b)))
- (define-syntax-rule (a=b a b varcount) (funcify (varnames varcount) (difference a b)))
- (define-syntax-rule (quickPSO f vars direction)
- (best (iteratepopulation (newpopulation 30 (map (lambda (x) (interval -100 100)) (range vars))) (eval f ns) '(99999 0 0 0) direction 300) '(99999 0 0 0) minimise))
- (quickPSO (a=b '(+ a b) '(+ (* .5 a b) a (* 3 b)) 2) 2 minimise)
- (define-syntax-rule (IOmatch trainingset optimisedvars f)
- (let ((inputvars (if (list? (caar trainingset)) (length (caar trainingset)) 1))
- (outputvars (if (list? (cdar trainingset)) (length (cdar trainingset)) 1)))
- (funcify (varnames optimisedvars) `(lambda ,(controlnames inputvars) f))
- ))
- (IOmatch '((0 . 0) (1 . 1) (2 . 1) (3 . 2) (4 . 3) (5 . 4)) 8 '(+ a c0 (* (+ d c0) (+ e c0))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement