networks: Export and test the functions for generation of random networks.

This commit is contained in:
Sergiu Ivanov 2020-03-22 19:28:44 +01:00
parent c823001492
commit cd11bcc330
2 changed files with 19 additions and 8 deletions

View File

@ -249,4 +249,15 @@
(check-equal? (tabulate-state/boolean f '(x1 x2))
'((x1 x2 f) (#f #f #f) (#t #f #f) (#f #t #t) (#t #t #t)))
(check-equal? (tabulate-state/boolean f '(x1 x2) #:headers #f)
'((#f #f #f) (#t #f #f) (#f #t #t) (#t #t #t)))))
'((#f #f #f) (#t #f #f) (#f #t #t) (#t #t #t)))
(define bn (random-boolean-network/vars 3))
(check-equal? (tabulate-boolean-network bn)
'((x0 x1 x2 f-x0 f-x1 f-x2)
(#f #f #f #f #t #f)
(#f #t #f #t #f #f)
(#f #f #t #f #t #t)
(#f #t #t #t #f #f)
(#t #f #f #t #f #t)
(#t #t #f #f #f #t)
(#t #f #t #f #f #f)
(#t #t #t #t #t #t)))))

View File

@ -78,7 +78,10 @@
[random-boolean-function (-> number? procedure?)]
[random-boolean-function/list (-> number? procedure?)]
[random-function/state (domain-mapping/c generic-set? . -> . procedure?)]
[random-boolean-function/state ((listof variable?) . -> . procedure?)])
[random-boolean-function/state ((listof variable?) . -> . procedure?)]
[random-network (domain-mapping/c . -> . network?)]
[random-boolean-network ((listof variable?) . -> . network?)]
[random-boolean-network/vars (number? . -> . network?)])
;; Predicates
(contract-out [variable? (-> any/c boolean?)]
[state? (-> any/c boolean?)]
@ -570,19 +573,16 @@
(random-function/state (make-boolean-domains args) '(#f #t)))
;;; Generates a random network from the given domain mapping.
(define/contract (random-network domains)
(domain-mapping/c . -> . network?)
(define (random-network domains)
(for/hash ([(x x-dom) (in-hash domains)])
(values x (random-function/state domains x-dom))))
;;; Generates a random Boolean network with the given variables.
(define/contract (random-boolean-network vars)
((listof variable?) . -> . network?)
(define (random-boolean-network vars)
(random-network (make-boolean-domains vars)))
;;; Like random-boolean-network, but also generates the names of the
;;; variables for the network. The variables have the names x0 to xk,
;;; where k = n - 1.
(define/contract (random-boolean-network/vars n)
(number? . -> . network?)
(define (random-boolean-network/vars n)
(random-boolean-network (for/list ([i (in-range n)]) (string->symbol (format "x~a" i)))))