From cd11bcc330a5d1b0dd8a3148eb62fbf6b4ff62f5 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 22 Mar 2020 19:28:44 +0100 Subject: [PATCH] networks: Export and test the functions for generation of random networks. --- networks-tests.rkt | 13 ++++++++++++- networks.rkt | 14 +++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/networks-tests.rkt b/networks-tests.rkt index 23b5764..0db273d 100644 --- a/networks-tests.rkt +++ b/networks-tests.rkt @@ -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))))) diff --git a/networks.rkt b/networks.rkt index 1df35b4..a8d4658 100644 --- a/networks.rkt +++ b/networks.rkt @@ -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)))))