networks: Add interaction?.

This commit is contained in:
Sergiu Ivanov 2020-11-14 23:15:39 +01:00
parent d24dadd4cd
commit 8a1f4c682e

View file

@ -31,6 +31,7 @@
network?)]
[list-syntactic-interactions (-> network-form? variable? (listof variable?))]
[build-syntactic-interaction-graph (-> network-form? graph?)]
[interaction? (-> network? domain-mapping/c variable? variable? boolean?)]
[build-all-states (-> domain-mapping/c (listof state?))]
[make-same-domains (-> (listof variable?) generic-set? domain-mapping/c)]
[make-boolean-domains (-> (listof variable?) (hash/c variable? (list/c #f #t)))]
@ -397,7 +398,41 @@
#hash((a . 1) (b . 0))
#hash((a . 1) (b . 1))))))
;;; Given two variables x and y of a network f, verifies if they
;;; interact, i.e. that there exists such a state s with the property
;;; that s' which is s with a different value for x yields such a new
;;; state f(s') in which the value for y is different from f(s).
(define (interaction? network doms x y)
(define states-not-x (build-all-states (hash-remove doms x)))
(define (different-ys-exist? st)
(define x-states (for/list ([x-val (in-list (hash-ref doms x))])
(hash-set st x x-val)))
(for*/first ([st1 x-states]
[st2 x-states]
#:unless (equal? (hash-ref st1 x) (hash-ref st2 x))
#:unless (equal? ((hash-ref network y) st1)
((hash-ref network y) st2)))
#t))
(for*/first ([st (in-list states-not-x)]
#:when (different-ys-exist? st))
#t))
(module+ test
(test-case "interaction?"
(define n-bool (network-form->network
(hash 'x '(not y)
'y 'x
'z '(and y z))))
(define bool-doms (make-boolean-domains '(x y z)))
(check-true (interaction? n-bool bool-doms 'x 'y))
(check-true (interaction? n-bool bool-doms 'y 'x))
(check-false (interaction? n-bool bool-doms 'x 'z))
(define n-multi (network-form->network
(hash 'x '(max (+ y 1) 2)
'y '(min (- y 1) 0))))
(define 123-doms (make-same-domains '(x y) '(0 1 2)))
(check-false (interaction? n-multi 123-doms 'x 'y))
(check-true (interaction? n-multi 123-doms 'y 'x))))
;;; ====================
;;; Dynamics of networks