networks: Add random-function/state and random-boolean-function/state.

This commit is contained in:
Sergiu Ivanov 2020-03-22 14:35:26 +01:00
parent 5358f9bf57
commit a40106e47c
2 changed files with 24 additions and 3 deletions

View File

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

View File

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