Compare commits
2 Commits
ed67927803
...
e8ebab58ca
Author | SHA1 | Date |
---|---|---|
Sergiu Ivanov | e8ebab58ca | |
Sergiu Ivanov | b8b9fee9ce |
|
@ -81,3 +81,34 @@ and weights, as well as a threshold.
|
||||||
@ex[
|
@ex[
|
||||||
(make-tbf/state '((x1 . 1) (x2 . 1)) 1)
|
(make-tbf/state '((x1 . 1) (x2 . 1)) 1)
|
||||||
]}
|
]}
|
||||||
|
|
||||||
|
@defproc[(sbf/state? [tbfs TBF/State]) Boolean]{
|
||||||
|
|
||||||
|
A state sign Boolean function (SBF) is a @racket[TBF/State] whose
|
||||||
|
threshold is 0.
|
||||||
|
|
||||||
|
@ex[
|
||||||
|
(sbf/state? (tbf/state (hash 'a -1 'b 1) 0))
|
||||||
|
(sbf/state? (tbf/state (hash 'a -1 'b 1) 1))
|
||||||
|
]}
|
||||||
|
|
||||||
|
@defproc[(apply-tbf/state [tbfs TBF/State]
|
||||||
|
[st (State (U Zero One))])
|
||||||
|
(U Zero One)]{
|
||||||
|
|
||||||
|
Applies a @racket[TBF/State] to its inputs given by the state
|
||||||
|
@racket[st].
|
||||||
|
|
||||||
|
Applying a TBF consists in multiplying the weights by the
|
||||||
|
corresponding inputs and comparing the sum of the products to
|
||||||
|
the threshold.
|
||||||
|
|
||||||
|
This function is similar to @racket[apply-tbf], but because it applies
|
||||||
|
a @racket[TBF/State] to a @racket[(State (U Zero One))], it avoids
|
||||||
|
potential mismatches between weights and the corresponding
|
||||||
|
input values.
|
||||||
|
|
||||||
|
@ex[
|
||||||
|
(apply-tbf/state (tbf/state (hash 'a 2 'b -2) 1)
|
||||||
|
(hash 'a 1 'b 0 'c 1))
|
||||||
|
]}
|
||||||
|
|
30
tbn.rkt
30
tbn.rkt
|
@ -9,6 +9,13 @@
|
||||||
"utils.rkt" "functions.rkt" "networks.rkt"
|
"utils.rkt" "functions.rkt" "networks.rkt"
|
||||||
typed/graph typed/racket/random)
|
typed/graph typed/racket/random)
|
||||||
|
|
||||||
|
(require/typed racket/hash
|
||||||
|
[hash-intersect
|
||||||
|
(->* ((HashTable Variable Real))
|
||||||
|
(#:combine (-> Real Real Real))
|
||||||
|
#:rest (HashTable Variable Real)
|
||||||
|
(HashTable Variable Real))])
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
(require typed/rackunit))
|
(require typed/rackunit))
|
||||||
|
|
||||||
|
@ -16,6 +23,7 @@
|
||||||
apply-tbf-to-state
|
apply-tbf-to-state
|
||||||
|
|
||||||
(struct-out tbf/state) TBF/State tbf/state-w tbf/state-θ make-tbf/state
|
(struct-out tbf/state) TBF/State tbf/state-w tbf/state-θ make-tbf/state
|
||||||
|
sbf/state? apply-tbf/state
|
||||||
)
|
)
|
||||||
|
|
||||||
(: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One)))
|
(: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One)))
|
||||||
|
@ -45,6 +53,28 @@
|
||||||
(define f (make-tbf/state '((x1 . 1) (x2 . 1)) 1))
|
(define f (make-tbf/state '((x1 . 1) (x2 . 1)) 1))
|
||||||
(check-equal? (tbf/state-w f) #hash((x1 . 1) (x2 . 1)))
|
(check-equal? (tbf/state-w f) #hash((x1 . 1) (x2 . 1)))
|
||||||
(check-equal? (tbf/state-θ f) 1)))
|
(check-equal? (tbf/state-θ f) 1)))
|
||||||
|
|
||||||
|
(: sbf/state? (-> TBF/State Boolean))
|
||||||
|
(define (sbf/state? tbfs) (zero? (tbf/state-θ tbfs)))
|
||||||
|
|
||||||
|
(module+ test
|
||||||
|
(test-case "sbf/state?"
|
||||||
|
(check-true (sbf/state? (tbf/state (hash 'a -1 'b 1) 0)))
|
||||||
|
(check-false (sbf/state? (tbf/state (hash 'a -1 'b 1) 1)))))
|
||||||
|
|
||||||
|
(: apply-tbf/state (-> TBF/State (State (U Zero One)) (U Zero One)))
|
||||||
|
(define (apply-tbf/state tbfs st)
|
||||||
|
(any->01
|
||||||
|
(> (apply + (hash-values (hash-intersect (tbf/state-w tbfs) st #:combine *)))
|
||||||
|
(tbf/state-θ tbfs))))
|
||||||
|
|
||||||
|
(module+ test
|
||||||
|
(test-case "apply-tbf/state"
|
||||||
|
(define st1 (hash 'a 1 'b 0 'c 1))
|
||||||
|
(define st2 (hash 'a 1 'b 1 'c 0))
|
||||||
|
(define tbf (make-tbf/state '((a . 2) (b . -2)) 1))
|
||||||
|
(check-equal? (apply-tbf/state tbf st1) 1)
|
||||||
|
(check-equal? (apply-tbf/state tbf st2) 0)))
|
||||||
)
|
)
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
|
|
Loading…
Reference in New Issue