From 72454c395c991fdbbeb00c2beb40a0486ccf9e92 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Mon, 3 Apr 2023 16:00:37 +0200 Subject: [PATCH] Add lists+vars->tbfs/state. --- scribblings/tbn.scrbl | 16 ++++++++++++++++ tbn.rkt | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/scribblings/tbn.scrbl b/scribblings/tbn.scrbl index 80a3fa1..feb6e4a 100644 --- a/scribblings/tbn.scrbl +++ b/scribblings/tbn.scrbl @@ -114,3 +114,19 @@ input values. (apply-tbf/state (tbf/state (hash 'a 2 'b -2) 1) (hash 'a 1 'b 0 'c 1)) ]} + +@section{Reading TBFs and SBFs} + +@defproc[(lists+vars->tbfs/state [vars (Listof Variable)] + [lsts (Listof (Listof Real))]) + (Listof TBF/State)]{ + +Reads a list of @racket[TBF/State] from a list of list of +@racket[Real]s. + +The last element of each list is taken to be the threshold of the +TBFs, and the rest of the elements are taken to be the weights. + +@ex[ +(lists+vars->tbfs/state '(x y) '((1 2 3) (1 1 2))) +]} diff --git a/tbn.rkt b/tbn.rkt index e3ac60d..8602c73 100644 --- a/tbn.rkt +++ b/tbn.rkt @@ -24,6 +24,8 @@ (struct-out tbf/state) TBF/State tbf/state-w tbf/state-θ make-tbf/state sbf/state? apply-tbf/state + + lists+vars->tbfs/state ) (: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One))) @@ -75,6 +77,22 @@ (define tbf (make-tbf/state '((a . 2) (b . -2)) 1)) (check-equal? (apply-tbf/state tbf st1) 1) (check-equal? (apply-tbf/state tbf st2) 0))) + + (: lists+vars->tbfs/state (-> (Listof Variable) (Listof (Listof Real)) + (Listof TBF/State))) + (define (lists+vars->tbfs/state vars lsts) + (for/list ([lst (in-list lsts)]) + (define-values (ws θ) (split-at-right lst 1)) + (make-tbf/state (for/list ([x (in-list vars)] + [w (in-list ws)]) + (cons x w)) + (car θ)))) + + (module+ test + (test-case "lists+vars->tbfs/state" + (check-equal? (lists+vars->tbfs/state '(x y) '((1 2 3) (1 1 2))) + (list (tbf/state '#hash((x . 1) (y . 2)) 3) + (tbf/state '#hash((x . 1) (y . 1)) 2))))) ) (module+ test