dds/bn.rkt

116 lines
4 KiB
Racket
Raw Normal View History

#lang racket
2020-02-15 13:32:54 +01:00
;;; dds/bn
;;; This module provides some quick definitions for running Boolean
;;; networks. A Boolean network is a set of Boolean variables which
;;; are updated according to their corresponding update functions.
;;; The variables to be updated at each step are given by the mode.
2020-02-18 11:41:16 +01:00
(require "utils.rkt")
2020-02-18 12:36:44 +01:00
(provide
;; Functions
update make-state make-bn-funcs update-func-form->update-func
bn-form->bn make-bn-forms
;; Syntax
2020-02-18 12:39:11 +01:00
st bn)
;;; =================
;;; Basic definitions
;;; =================
(define-type Variable Symbol)
2020-02-15 13:32:54 +01:00
2020-02-15 15:16:20 +01:00
;;; A state of a Boolean network is a mapping from the variables of the
;;; network to their Boolean values.
(define-type State (HashTable Variable Boolean))
2020-02-15 13:32:54 +01:00
2020-02-15 15:16:20 +01:00
;;; An update function is a Boolean function computing a Boolean value
;;; from the given state.
2020-02-15 13:32:54 +01:00
(define-type UpdateFunc (-> State Boolean))
2020-02-15 15:16:20 +01:00
;;; A Boolean network is a mapping from its variables to its update
;;; functions.
(define-type Network (HashTable Variable UpdateFunc))
2020-02-15 20:30:46 +01:00
;;; Given a state s updates all the variables from xs. This
;;; corresponds to a parallel mode.
(: update (-> Network State (Listof Variable) State))
(define (update bn ; the Boolean network
s ; the state to operate on
xs) ; the variables to update
2020-02-15 20:30:46 +01:00
(let ([new-s : State (hash-copy s)])
(for ([x xs])
(let ([f (hash-ref bn x)])
(hash-set! new-s x (f s))))
new-s))
2020-02-15 20:51:52 +01:00
;;; A version of make-hash restricted to creating Boolean states.
(: make-state (-> (Listof (Pairof Variable Boolean)) State))
(define (make-state mappings)
2020-02-15 20:51:52 +01:00
(make-hash mappings))
2020-02-15 21:00:20 +01:00
2020-02-18 12:39:11 +01:00
;;; A shortcut for make-state.
(define-syntax-rule (st mappings) (make-state mappings))
2020-02-15 21:00:20 +01:00
;;; A version of make-hash restricted to creating Boolean networks.
2020-02-18 12:09:07 +01:00
(: make-bn-funcs (-> (Listof (Pairof Variable UpdateFunc)) Network))
(define (make-bn-funcs funcs)
2020-02-15 21:00:20 +01:00
(make-hash funcs))
;;; =========================================
;;; Syntactic description of Boolean networks
;;; =========================================
;;; An UpdateFuncForm is any form which can appear as a body of a
;;; Boolean function and which can be evaluated with eval. For
;;; example, '(and x y (not z)).
(define-type UpdateFuncForm Any)
;;; A Boolean network form is a mapping from its variables to the
;;; forms of their update functions.
(define-type NetworkForm (HashTable Variable UpdateFuncForm))
2020-02-18 11:41:16 +01:00
;;; Build an update function from an update function form.
#;(: update-func-form->update-func (-> UpdateFuncForm UpdateFunc))
(define (update-func-form->update-func form)
(lambda ([s : State])
(cast (eval-with1 (cast s (HashTable Variable Any)) form) Boolean)))
2020-02-18 12:07:38 +01:00
;;; Build a Network from a Network form.
(: bn-form->bn (-> NetworkForm Network))
(define (bn-form->bn bnf)
(make-hash
(hash-map bnf (lambda ([x : Variable] [form : UpdateFuncForm])
(cons x (update-func-form->update-func form))))))
2020-02-18 12:16:03 +01:00
;;; Build a network from a list of pairs of forms of update functions.
(: make-bn-forms (-> (Listof (Pairof Variable UpdateFuncForm)) Network))
(define (make-bn-forms forms)
(bn-form->bn (make-hash forms)))
2020-02-18 12:36:26 +01:00
;;; A shortcut for make-bn-forms.
(define-syntax-rule (bn forms) (make-bn-forms forms))
;;; ============================
;;; Inferring interaction graphs
;;; ============================
;;; I allow any syntactic forms in definitions of Boolean functions.
;;; I can still find out which Boolean variables appear in those
;;; syntactic form, but I have no reliable syntactic means of finding
;;; out what kind of action do they have (inhibition or activation)
;;; since I cannot do Boolean minimisation (e.g., I cannot rely on not
;;; appearing before a variable, since (not (not a)) is equivalent
;;; to a). On the other hand, going through all Boolean states is
;;; quite resource-consuming and thus not always useful.
;;;
;;; In this section I provide inference of both unsigned and signed
;;; interaction graphs, but since the inference of signed interaction
;;; graphs is based on analysing the dynamics of the networks, it may
;;; be quite resource-consuming.