From c00d50d4ae89219a2198c8a511fe24c440f29b9e Mon Sep 17 00:00:00 2001 From: Bryan Jurish Date: Tue, 30 Jan 2007 15:47:31 +0000 Subject: v0.0402: + fixed generation in gfsm_markov.pd + added total_weight and arc_gen methods to gfsm_state svn path=/trunk/externals/moocow/; revision=7396 --- gfsm/configure.in | 2 +- gfsm/src/gfsm_markov.pd | 156 +++++++++++++------------------------------- gfsm/src/gfsm_state-help.pd | 61 +++++++++-------- gfsm/src/pd_state.c | 95 ++++++++++++++++++++++++++- 4 files changed, 176 insertions(+), 138 deletions(-) diff --git a/gfsm/configure.in b/gfsm/configure.in index 422a340..7c3f948 100644 --- a/gfsm/configure.in +++ b/gfsm/configure.in @@ -4,7 +4,7 @@ AC_PREREQ(2.5) dnl Some handy macros define([THE_PACKAGE_NAME], [pd-gfsm]) -define([THE_PACKAGE_VERSION], [0.0401]) +define([THE_PACKAGE_VERSION], [0.0402]) define([THE_PACKAGE_AUTHOR], [moocow@ling.uni-potsdam.de]) AC_INIT(THE_PACKAGE_NAME, THE_PACKAGE_VERSION, THE_PACKAGE_AUTHOR) diff --git a/gfsm/src/gfsm_markov.pd b/gfsm/src/gfsm_markov.pd index ab7142d..fa2ea8b 100644 --- a/gfsm/src/gfsm_markov.pd +++ b/gfsm/src/gfsm_markov.pd @@ -284,127 +284,63 @@ clear fsm alph state add2; #X connect 35 0 34 1; #X connect 36 0 34 1; #X restore 21 186 pd gfsm_markov_add_guts; -#N canvas 27 0 738 524 gfsm_markov_next_guts 0; -#X obj 86 25 r \$0-next; +#N canvas 29 370 662 532 gfsm_markov_next_guts 0; +#X obj 28 4 r \$0-next; #X obj 243 464 outlet; -#X obj 86 47 route bang; -#X obj 151 158 * -1; -#X obj 151 136 * 1; -#X obj 559 376 t b b; -#N canvas 75 18 312 424 weight_sum 0; -#X obj 24 9 inlet; -#X msg 24 114 arc_next; -#X obj 24 319 f 0; -#X obj 199 92 0; -#X obj 89 238 unpack 0 0 0 0; -#X obj 183 262 + 0; -#X obj 88 263 t f f; -#X obj 24 350 outlet; -#X obj 60 146 r \$0-state; -#X obj 24 90 until; -#X obj 24 214 route bang; -#X obj 24 235 t b b; -#X obj 24 192 route arc_next; -#X obj 24 43 t b b b b; -#X msg 91 114 arc_reset; -#X obj 24 167 gfsm_state \$1-fsm 0; -#X connect 0 0 13 0; -#X connect 1 0 15 0; -#X connect 2 0 7 0; -#X connect 3 0 5 1; -#X connect 4 3 5 0; -#X connect 5 0 6 0; -#X connect 6 0 5 1; -#X connect 6 1 2 1; -#X connect 8 0 15 0; -#X connect 9 0 1 0; -#X connect 10 0 11 0; -#X connect 10 1 4 0; -#X connect 11 0 2 0; -#X connect 11 1 9 1; -#X connect 12 0 10 0; -#X connect 13 0 9 0; -#X connect 13 1 3 0; -#X connect 13 2 14 0; -#X connect 13 3 9 1; -#X connect 14 0 15 0; -#X connect 15 0 12 0; -#X restore 167 115 pd weight_sum; -#X obj 25 95 random 131072; -#X obj 25 116 / 131072; -#X obj 86 69 t b b b; -#X obj 151 69 t f b b; -#X msg 559 227 arc_next; -#X obj 594 269 r \$0-state; -#X obj 559 205 until; -#X obj 559 349 route bang; -#X obj 559 326 route arc_next; -#X msg 625 226 arc_reset; -#X obj 151 180 t b b f; +#X obj 28 26 route bang; +#X obj 167 138 * 1; +#X obj 28 70 random 131072; +#X obj 28 91 / 131072; #X obj 405 461 outlet; #X obj 101 429 gfsm_alphabet \$1-alph; #X msg 101 407 char2atom! \$1; -#X obj 179 296 moses 0; -#X obj 179 251 + 0; -#X obj 179 274 t f f; #X obj 86 225 unpack 0 0 0 0; -#X obj 18 386 f 0; #X msg 18 411 set \$1; #X obj 18 432 s \$0-state; -#X obj 559 292 gfsm_state \$1-fsm 0; -#X obj 559 457 outlet; +#X obj 512 461 outlet; #X obj 263 428 gfsm_alphabet \$1-alph; #X msg 263 406 char2atom! \$1; -#X obj 263 384 f 0; -#X obj 101 385 f 0; -#X obj 225 316 t b b b b; +#X obj 424 101 gfsm_state \$1-fsm 0; +#X obj 424 20 r \$0-state; +#X obj 512 150 route bang; +#X obj 512 172 t b; +#X obj 424 124 route total_weight arc_gen; +#X obj 28 48 t b; +#X obj 153 71 t b f; +#X obj 167 115 f 0; +#X obj 196 114 t b f; +#X msg 167 160 arc_gen \$1; +#X msg 153 91 arc_reset \, total_weight; +#X obj 153 187 s \$0-state; #X connect 0 0 2 0; -#X connect 2 0 9 0; -#X connect 2 1 10 0; -#X connect 3 0 17 0; -#X connect 4 0 3 0; -#X connect 5 0 29 0; -#X connect 5 1 13 1; -#X connect 6 0 4 1; -#X connect 7 0 8 0; -#X connect 8 0 4 0; -#X connect 9 0 7 0; -#X connect 9 1 6 0; -#X connect 9 2 13 1; -#X connect 10 0 4 0; -#X connect 10 1 6 0; -#X connect 10 2 13 1; -#X connect 11 0 28 0; -#X connect 12 0 28 0; -#X connect 13 0 11 0; -#X connect 14 0 5 0; -#X connect 14 1 24 0; -#X connect 15 0 14 0; -#X connect 16 0 28 0; -#X connect 17 0 13 0; -#X connect 17 1 16 0; -#X connect 17 2 22 1; -#X connect 19 1 1 0; -#X connect 20 0 19 0; -#X connect 21 1 34 0; -#X connect 22 0 23 0; -#X connect 23 0 21 0; -#X connect 23 1 22 1; -#X connect 24 0 25 1; -#X connect 24 1 33 1; -#X connect 24 2 32 1; -#X connect 24 3 22 0; +#X connect 2 0 20 0; +#X connect 2 1 21 0; +#X connect 3 0 24 0; +#X connect 4 0 5 0; +#X connect 5 0 21 0; +#X connect 7 1 1 0; +#X connect 8 0 7 0; +#X connect 9 0 10 0; +#X connect 9 1 8 0; +#X connect 9 2 14 0; +#X connect 10 0 11 0; +#X connect 13 1 6 0; +#X connect 14 0 13 0; +#X connect 15 0 19 0; +#X connect 16 0 15 0; +#X connect 17 0 18 0; +#X connect 17 1 9 0; +#X connect 18 0 12 0; +#X connect 19 0 23 0; +#X connect 19 1 17 0; +#X connect 20 0 4 0; +#X connect 21 0 25 0; +#X connect 21 1 22 1; +#X connect 22 0 3 0; +#X connect 23 0 22 0; +#X connect 23 1 3 1; +#X connect 24 0 26 0; #X connect 25 0 26 0; -#X connect 26 0 27 0; -#X connect 28 0 15 0; -#X connect 30 1 18 0; -#X connect 31 0 30 0; -#X connect 32 0 31 0; -#X connect 33 0 20 0; -#X connect 34 0 25 0; -#X connect 34 1 33 0; -#X connect 34 2 32 0; -#X connect 34 3 13 1; #X restore 22 216 pd gfsm_markov_next_guts; #X obj 185 260 outlet; #X connect 3 0 6 0; diff --git a/gfsm/src/gfsm_state-help.pd b/gfsm/src/gfsm_state-help.pd index b798c0a..96d07b4 100644 --- a/gfsm/src/gfsm_state-help.pd +++ b/gfsm/src/gfsm_state-help.pd @@ -1,4 +1,4 @@ -#N canvas 232 0 651 599 10; +#N canvas 154 10 651 599 10; #X obj 85 569 gfsm; #X text 15 569 SEE ALSO:; #X text 41 6 gfsm_state : position "pointer" for finite-state automata @@ -12,38 +12,45 @@ #X text 52 47 FSM_NAME - symbolic name of a gfsm_automaton; #X text 52 61 STATE_ID - numeric Id of the target state; #X obj 26 531 print gfsm-state-out; -#X msg 44 182 id; -#X text 164 185 "id" : get current state Id; -#X text 135 205 "set ID" : set current state Id; -#X msg 50 205 set 0; +#X msg 44 168 id; +#X text 164 171 "id" : get current state Id; +#X text 135 185 "set ID" : set current state Id; +#X msg 50 185 set 0; #X msg 26 127 automaton; -#X msg 31 149 automaton fsm-help; +#X msg 31 144 automaton fsm-help; #X text 197 128 "automaton" : use an object-local machine; -#X text 163 151 "automaton NAME" : use a shared machine named NAME +#X text 163 146 "automaton NAME" : use a shared machine named NAME ; -#X msg 58 239 degree; -#X text 137 240 "degree" : get number of outgoing arcs (linear time) +#X msg 58 212 degree; +#X text 137 213 "degree" : get number of outgoing arcs (linear time) ; -#X msg 61 295 arc_first; -#X msg 61 317 arc_next; -#X msg 59 382 arc_reset; +#X msg 61 325 arc_first; +#X msg 61 342 arc_next; +#X msg 65 412 arc_reset; #X obj 26 508 gfsm_state fsm-help 0; -#X text 229 398 arcs are output as a list:; -#X text 277 414 NEXT_STATEID LO HI WEIGHT; -#X text 306 359 (negative labels are ignored); -#X msg 58 345 arc_seek 202 -1; -#X text 208 294 "arc_first" : get first outgoing arc; -#X text 214 318 "arc_next" : get next available outgoing arc (maybe +#X text 75 258 arcs are output as a list:; +#X text 265 258 NEXT_STATEID LO HI WEIGHT; +#X text 306 392 (negative labels are ignored); +#X msg 64 376 arc_seek 202 -1; +#X text 208 326 "arc_first" : get first outgoing arc; +#X text 215 342 "arc_next" : get next available outgoing arc (maybe first); -#X text 173 344 "arc_seek LO HI" : get next arc with labels LO:HI; -#X text 207 383 "arc_reset" : close internal arc iterator; -#X msg 54 447 add_weight 1 -1 101 0.9; -#X text 233 450 "add_weight ID LO HI W" : add W to 1st arc to state +#X text 173 377 "arc_seek LO HI" : get next arc with labels LO:HI; +#X text 213 413 "arc_reset" : close internal arc iterator; +#X msg 54 467 add_weight 1 -1 101 0.9; +#X text 233 470 "add_weight ID LO HI W" : add W to 1st arc to state ID; -#X text 416 463 with labels LO:HI; -#X text 271 479 (implicitly resets arc iterator); -#X msg 59 263 cyclic; -#X text 141 263 "cyclic" : does this state lead to any cycles?; +#X text 416 483 with labels LO:HI; +#X text 271 499 (implicitly resets arc iterator); +#X msg 61 229 cyclic; +#X text 137 229 "cyclic" : does this state lead to any cycles?; +#X msg 64 359 arc_nth 0; +#X text 208 360 "arc_nth N" : seek to n-th arc (counting from zero) +; +#X msg 61 278 arc_gen 42 1; +#X text 186 281 "arc_gen W SR" : generate an arc; +#X text 312 295 W - weight hint (0..sum(weights(state)); +#X text 304 309 SR - boolean - whether to use gfsmSemiring; #X connect 12 0 25 0; #X connect 15 0 25 0; #X connect 16 0 25 0; @@ -56,3 +63,5 @@ ID; #X connect 29 0 25 0; #X connect 34 0 25 0; #X connect 38 0 25 0; +#X connect 40 0 25 0; +#X connect 42 0 25 0; diff --git a/gfsm/src/pd_state.c b/gfsm/src/pd_state.c index 63b2e27..621979d 100644 --- a/gfsm/src/pd_state.c +++ b/gfsm/src/pd_state.c @@ -3,7 +3,7 @@ * Author: Bryan Jurish * Description: finite state automata for Pd * - * Copyright (c) 2004-2006 Bryan Jurish. + * Copyright (c) 2004-2007 Bryan Jurish. * * For information on usage and redistribution, and for a DISCLAIMER OF ALL * WARRANTIES, see the file, "LICENSE.txt," in this distribution. @@ -201,6 +201,91 @@ static void pd_gfsm_state_arc_seek(t_pd_gfsm_state *x, t_float flo, t_float fhi) pd_gfsm_state_outlet_arc(x, gensym("arc_seek")); } +/*-------------------------------------------------------------------- + * arc_nth(n) + */ +static void pd_gfsm_state_arc_nth(t_pd_gfsm_state *x, t_float n) +{ + int i = n; + gfsmState *s = gfsm_automaton_find_state(x->x_automaton_pd->x_automaton, x->x_id); + + if (s) { + gfsm_arciter_close(&x->x_arci); + for (gfsm_arciter_open(&x->x_arci, x->x_automaton_pd->x_automaton, x->x_id); + i > 0 && gfsm_arciter_ok(&x->x_arci); + gfsm_arciter_next(&x->x_arci)) + ; + } + + pd_gfsm_state_outlet_arc(x, gensym("arc_nth")); +} + +/*-------------------------------------------------------------------- + * get_total_weight(bool use_semiring) + * + low-level + */ +static t_float pd_gfsm_state_get_total_weight(t_pd_gfsm_state *x, int use_semiring) +{ + gfsmAutomaton *fsm = x->x_automaton_pd->x_automaton; + gfsmState *s = gfsm_automaton_find_state(fsm, x->x_id); + gfsmSemiring *sr = fsm->sr; + gfsmWeight w = use_semiring ? sr->zero : 0.0; + + if (s) { + /*w = gfsm_sr_plus(sr,w,gfsm_automaton_get_final_weight(fsm,x->x_id));*/ //--ignore final weights! + gfsm_arciter_close(&x->x_arci); + for (gfsm_arciter_open(&x->x_arci, x->x_automaton_pd->x_automaton, x->x_id); + gfsm_arciter_ok(&x->x_arci); + gfsm_arciter_next(&x->x_arci)) + { + gfsmArc *a = gfsm_arciter_arc(&x->x_arci); + w = use_semiring ? gfsm_sr_plus(sr,w,a->weight) : (w+a->weight); + } + } + return w; +} + +/*-------------------------------------------------------------------- + * total_weight() + * + pd level + */ +static void pd_gfsm_state_total_weight(t_pd_gfsm_state *x, t_float use_semiring) +{ + t_float w = pd_gfsm_state_get_total_weight(x, (int)use_semiring); + SETFLOAT(x->x_argv, (t_float)(w)); + outlet_anything(x->x_valout, gensym("total_weight"), 1, x->x_argv); +} + + +/*-------------------------------------------------------------------- + * arc_gen(weight_hint) + */ +static void pd_gfsm_state_arc_gen(t_pd_gfsm_state *x, t_float weight_hint, t_float use_semiring_f) +{ + gfsmState *s = gfsm_automaton_find_state(x->x_automaton_pd->x_automaton, x->x_id); + int use_semiring = (int)use_semiring_f; + if (s) { + gfsmSemiring *sr = x->x_automaton_pd->x_automaton->sr; + gfsmWeight w = use_semiring ? sr->zero : 0; + + for (gfsm_arciter_open(&x->x_arci, x->x_automaton_pd->x_automaton, x->x_id); + gfsm_arciter_ok(&x->x_arci); + gfsm_arciter_next(&x->x_arci)) + { + gfsmArc *a = gfsm_arciter_arc(&x->x_arci); + if (use_semiring) { + w = gfsm_sr_plus(sr,w,a->weight); + if (!gfsm_sr_less(sr,w,weight_hint)) break; + } else { + w += a->weight; + if (w >= weight_hint) break; + } + } + } + pd_gfsm_state_outlet_arc(x, gensym("arc_gen")); +} + + /*-------------------------------------------------------------------- * arc_reset @@ -315,6 +400,14 @@ void pd_gfsm_state_setup(void) class_addmethod(pd_gfsm_state_class, (t_method)pd_gfsm_state_arc_reset, gensym("arc_reset"), A_NULL); + //-- new arc methods + class_addmethod(pd_gfsm_state_class, (t_method)pd_gfsm_state_arc_nth, + gensym("arc_nth"), A_DEFFLOAT, A_NULL); + class_addmethod(pd_gfsm_state_class, (t_method)pd_gfsm_state_total_weight, + gensym("total_weight"), A_DEFFLOAT, A_DEFFLOAT, A_NULL); + class_addmethod(pd_gfsm_state_class, (t_method)pd_gfsm_state_arc_gen, + gensym("arc_gen"), A_DEFFLOAT, A_DEFFLOAT, A_NULL); + //-- methods: manipulation class_addmethod(pd_gfsm_state_class, (t_method)pd_gfsm_state_add_weight, gensym("add_weight"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL); -- cgit v1.2.1