From da3f25922ae145aaabf99f05b6caddfa79c27ea5 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 22 Feb 2020 22:27:40 +0100 Subject: [PATCH] networks: Add build-all-states. --- networks-tests.rkt | 10 +++++++++- networks.rkt | 12 +++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/networks-tests.rkt b/networks-tests.rkt index 86d3c2b..7202416 100644 --- a/networks-tests.rkt +++ b/networks-tests.rkt @@ -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))))) diff --git a/networks.rkt b/networks.rkt index cdace2e..87761bc 100644 --- a/networks.rkt +++ b/networks.rkt @@ -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))))))