bn: update: Add and test.

This commit is contained in:
Sergiu Ivanov 2020-02-15 20:30:46 +01:00
parent 51a7dc8498
commit faf8f70b2e
2 changed files with 40 additions and 2 deletions

View File

@ -4,4 +4,30 @@
(require typed/rackunit "bn.rkt")
(check-equal? (+ 1 2 3) 6)
;;; This test case sets up the following Boolean network:
;;; x1 = x1 AND NOT x2
;;; x2 = NOT x2
(test-begin
(test-case "Simple two-variable Boolean network"
(let* ([f1 (λ ([s : State])
(let ([x1 (hash-ref s 'x1)]
[x2 (hash-ref s 'x2)])
(and x1 (not x2))))]
[f2 (λ ([s : State])
(let ([x2 (hash-ref s 'x2)])
(not x2)))]
[bn (hash 'x1 f1 'x2 f2)])
(test-case "One-step syncronous update"
(let* ([s (hash 'x1 #t 'x2 #f)]
[new-s (update bn s '(x2 x1))])
(check-equal? (hash-ref new-s 'x1) #t)
(check-equal? (hash-ref new-s 'x2) #t)
(check-equal? (length (hash-keys new-s)) 2)))
(test-case "One-step asynchronous update"
(let* ([s (hash 'x1 #f 'x2 #f)]
[new-s (update bn s '(x2 x1))])
(check-equal? (hash-ref new-s 'x1) #f)
(check-equal? (hash-ref new-s 'x2) #t)
(check-equal? (length (hash-keys new-s)) 2))))))

14
bn.rkt
View File

@ -7,7 +7,8 @@
;;; are updated according to their corresponding update functions.
;;; The variables to be updated at each step are given by the mode.
(provide Variable State UpdateFunc Network)
(provide Variable State UpdateFunc Network
update)
(define-type Variable Symbol)
@ -22,3 +23,14 @@
;;; A Boolean network is a mapping from its variables to its update
;;; functions.
(define-type Network (HashTable Variable UpdateFunc))
;;; Given a state s updates all the variables from xs. This
;;; corresponds to a parallel mode.
(define (update [bn : Network] ; the Boolean network
[s : State] ; the state to operate on
[xs : (Listof Variable)]) ; the variables to update
(let ([new-s : State (hash-copy s)])
(for ([x xs])
(let ([f (hash-ref bn x)])
(hash-set! new-s x (f s))))
new-s))