networks: Add build-all-states.

This commit is contained in:
Sergiu Ivanov 2020-02-22 22:27:40 +01:00
parent 511aa60c31
commit da3f25922a
2 changed files with 20 additions and 2 deletions

View File

@ -58,4 +58,12 @@
(check-true (has-edge? ig 'b 'a))
(check-true (has-edge? ig 'b 'b))
(check-false (has-edge? ig 'c 'b))
(check-false (has-edge? ig 'c 'a))))
(check-false (has-edge? ig 'c 'a)))
(check-equal? (map hash->list (build-all-states '((a . (#t #f)) (b . (1 2 3)))))
'(((a . #t) (b . 1))
((a . #t) (b . 2))
((a . #t) (b . 3))
((a . #f) (b . 1))
((a . #f) (b . 2))
((a . #f) (b . 3)))))

View File

@ -22,7 +22,8 @@
[make-network-from-forms (-> (listof (cons/c symbol? update-function-form?))
network?)]
[list-interactions (-> network-form? variable? (listof variable?))]
[build-interaction-graph (-> network-form? graph?)])
[build-interaction-graph (-> network-form? graph?)]
[build-all-states (-> (listof (cons/c variable? generic-set?)) (listof state?))])
;; Predicates
(contract-out [variable? (-> any/c boolean?)]
[state? (-> any/c boolean?)]
@ -141,3 +142,12 @@
(unweighted-graph/adj
(hash-map n (λ (var _)
(cons var (list-interactions n var)))))))
;;; Given a list of pairs mapping variables to generic sets of their
;;; possible values, constructs the list of all possible states.
(define (build-all-states vars-domains)
(let ([vars (map car vars-domains)]
[domains (map cdr vars-domains)])
(for/list ([s (apply cartesian-product domains)])
(make-state (for/list ([var vars] [val s])
(cons var val))))))