diff --git a/networks.rkt b/networks.rkt index e8641f3..42c0d70 100644 --- a/networks.rkt +++ b/networks.rkt @@ -2,7 +2,7 @@ (module typed typed/racket (require "utils.rkt" "functions.rkt" "dynamics.rkt" - typed/graph racket/random + typed/graph typed/racket/random syntax/parse/define) (require/typed racket/syntax [format-symbol (-> String Any * Symbol)]) @@ -1048,6 +1048,22 @@ (check-equal? (network-domains n) #hash((x1 . (#f #t)) (x2 . (#f #t)))))) + + (: random-function/state (All (a) (-> (DomainMapping a) (Domain a) + (-> (State a) a)))) + (define (random-function/state arg-domains func-domain) + (table->unary-function + (for/list : (Listof (List (State a) a)) + ([st (build-all-states arg-domains)]) + (list st (random-ref func-domain))))) + + (module+ test + (test-case "random-function/state" + (random-seed 1) + (define doms (hash 'a '(1 2) 'b '(3 4))) + (define f (random-function/state doms '(e f))) + (check-equal? (tabulate-state+headers f doms) + '((a b f1) (1 3 e) (1 4 e) (2 3 f) (2 4 e))))) ) (require 'typed) diff --git a/scribblings/networks.scrbl b/scribblings/networks.scrbl index 40eb46d..158001b 100644 --- a/scribblings/networks.scrbl +++ b/scribblings/networks.scrbl @@ -893,6 +893,19 @@ other half function names. Function names are discarded. @section{Random functions and networks} +@defproc[(random-function/state [arg-domains (DomainMapping a)] + [func-doman (Domain a)]) + (-> (State a) a)]{ + +Generates a random function accepting a state over the domains given +by @racket[arg-domains] and producing values in @racket[func-domain]. + +@ex[ +(let* ([doms (hash 'a '(1 2) 'b '(3 4))] + [f (random-function/state doms '(e f))]) + (tabulate-state+headers f doms)) +]} + @section{TBF/TBN and SBF/SBN} This section defines threshold Boolean functions (TBF) and networks (TBN), as