diff --git a/networks-tests.rkt b/networks-tests.rkt index a5ec133..1a82d91 100644 --- a/networks-tests.rkt +++ b/networks-tests.rkt @@ -235,4 +235,11 @@ (check-true (f #f #f)) (check-false (f #f #t)) (check-true (f #t #f)) (check-false (f #t #t))) (let ([f (random-boolean-function/list 2)]) (check-false (f '(#f #f))) (check-true (f '(#f #t))) - (check-true (f '(#t #f))) (check-false (f '(#t #t))))) + (check-true (f '(#t #f))) (check-false (f '(#t #t)))) + (begin + (random-seed 0) + (define f (random-boolean-function/state '(x1 x2))) + (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))))) diff --git a/networks.rkt b/networks.rkt index 8ea1b13..7cc012f 100644 --- a/networks.rkt +++ b/networks.rkt @@ -10,7 +10,7 @@ ;;; This model can generalise Boolean networks, TBANs, multivalued ;;; networks, etc. -(require "utils.rkt" "generic.rkt" graph) +(require "utils.rkt" "generic.rkt" graph racket/random) (provide ;; Structures @@ -72,7 +72,9 @@ [enumerate-boolean-functions/list (-> number? (stream/c procedure?))] [random-boolean-table (-> number? (listof (*list/c boolean? boolean?)))] [random-boolean-function (-> number? procedure?)] - [random-boolean-function/list (-> number? procedure?)]) + [random-boolean-function/list (-> number? procedure?)] + [random-function/state (domain-mapping/c generic-set? . -> . procedure?)] + [random-boolean-function/state ((listof variable?) . -> . procedure?)]) ;; Predicates (contract-out [variable? (-> any/c boolean?)] [state? (-> any/c boolean?)] @@ -520,3 +522,15 @@ ;;; Like random-boolean-function, but the constructed function takes a ;;; list of arguments. (define random-boolean-function/list (compose table->function/list random-boolean-table)) + +;;; Generates a random function accepting a state over the domains +;;; given by arg-domains and producing values in func-domain. +(define (random-function/state arg-domains func-domain) + (table->function (for/list ([st (build-all-states arg-domains)]) + (list st (random-ref func-domain))))) + +;;; Like random-function/state, but the domains of the arguments and +;;; of the function are Boolean. args is a list of names of the +;;; variables appearing in the state. +(define (random-boolean-function/state args) + (random-function/state (make-boolean-domains args) '(#f #t)))