Add random-function/state.

This commit is contained in:
Sergiu Ivanov 2023-03-24 14:43:59 +01:00
parent 78c638a886
commit f5349a3659
2 changed files with 30 additions and 1 deletions

View File

@ -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)

View File

@ -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