Add dynamics%.

This commit is contained in:
Sergiu Ivanov 2022-08-30 17:41:44 +02:00
parent 811df5fe1e
commit 8c89bf810a
2 changed files with 72 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#lang racket
(module typed typed/racket
(require "utils.rkt" "functions.rkt"
(require "utils.rkt" "functions.rkt" "dynamics.rkt"
typed/graph racket/random)
(module+ test
@ -33,7 +33,7 @@
build-interaction-graph/form build-signed-interaction-graph
build-signed-interaction-graph/form
Modality Mode
Modality Mode dynamics%
)
(define-type (State a) (VariableMapping a))
@ -512,6 +512,41 @@
(define-type Modality (Listof Variable))
(define-type Mode (Listof Modality))
(define dynamics%
;; TODO: Fix the parameter of State when Typed Racket supports
;; passing type parameters to the parent.
(class (inst dds% (State Any) Modality)
#:forall (a)
(super-new)
(init-field [network : (Network a) network]
[mode : Mode mode])
(: step/annotated (-> (State a) (Listof (Pairof Modality (State a)))))
(define/override (step/annotated s)
(for/list ([m mode])
(cons m (update network s m))))))
(module+ test
(test-case "dynamics%"
(define n1 : (Network Boolean)
(forms->boolean-network (hash 'x '(not y)
'y 'x
'z '(and y z))))
(define syn : Mode '((x y z)))
(define asyn : Mode '((x) (y) (z)))
(define dyn-syn (new (inst dynamics% Boolean) [network n1] [mode syn]))
(define dyn-asyn (new (inst dynamics% Boolean) [network n1] [mode asyn]))
(define s1 (hash 'x #f 'y #f 'z #f))
(check-equal? (send dyn-syn step/annotated s1)
'(((x y z) . #hash((x . #t) (y . #f) (z . #f)))))
(check-equal? (send dyn-asyn step/annotated s1)
'(((x) . #hash((x . #t) (y . #f) (z . #f)))
((y) . #hash((x . #f) (y . #f) (z . #f)))
((z) . #hash((x . #f) (y . #f) (z . #f)))))
))
)
(require 'typed)

View File

@ -3,9 +3,11 @@
(for-label typed/racket/base
graph
(only-in typed/graph Graph)
(only-in racket/class send)
(submod "../networks.rkt" typed)
"../utils.rkt"
"../functions.rkt"))
"../functions.rkt"
"../dynamics.rkt"))
@(define networks-evaluator
(parameterize ([sandbox-output 'string]
@ -376,6 +378,38 @@ A mode is a list of modalities. This is a synonym of @racket[(Listof Modality)]
}
@defclass[dynamics% dds% ()]{
A model of dynamics of a network is a @racket[Network] with a @racket[Mode].
@defconstructor[([network (Network a)] [mode Mode])]{
Creates a new instance of @racket[dynamics%] from a @racket[network] and
a @racket[mode]. Both are available as public fields of the class.
}
@defmethod[(step/annotated [st (State a)]) (Listof (Pairof Modality (State a)))]{
Apply the network stored in this class to the state @racket[st] with all
modalities of the mode stored in this class.
This is the only method of @racket[dds%] overridden in this class.
@ex[
(let* ([n (forms->boolean-network (hash 'a '(and a b)
'b '(not b)))]
[syn '((a) (b))]
[syn-dynamics (new (inst dynamics% Boolean) [network n] [mode syn])]
[st (hash 'a #f 'b #f)])
(send syn-dynamics step/annotated st))
]
}
}
@section{Inferring interaction graphs}
This section provides inference of both unsigned and signed interaction graphs.