2005-08-17 14:34:56 +01:00
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
%:- style_check(all).
|
2005-08-17 14:34:56 +01:00
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
:- module(viterbi, [viterbi/5]).
|
2005-08-17 14:34:56 +01:00
|
|
|
|
|
|
|
:- use_module(library(lists),
|
|
|
|
[nth/3]).
|
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
:- use_module(library(clpbn), []).
|
2005-08-17 14:34:56 +01:00
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
:- use_module(library('clpbn/dists'), [
|
|
|
|
get_dist_params/2]).
|
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
:- attribute prob/1, emission/1, backp/1, ancestors/1.
|
2005-08-17 14:34:56 +01:00
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
viterbi(Start,End,Trace,Ticks,Slices) :-
|
2005-08-17 14:34:56 +01:00
|
|
|
attributes:all_attvars(Vars0),
|
2006-01-02 02:16:19 +00:00
|
|
|
group_vars_by_key_and_parents(Vars0,Ticks,Slices),
|
2005-08-17 14:34:56 +01:00
|
|
|
init_viterbi(Start),
|
2005-09-09 18:22:33 +01:00
|
|
|
viterbi_alg([Start|R],R),
|
2005-08-17 14:34:56 +01:00
|
|
|
backtrace(Start,End,[],Trace).
|
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
group_vars_by_key_and_parents(AVars, NTicks, Slices) :-
|
|
|
|
NTicks1 is NTicks+2,
|
|
|
|
functor(Hashes,slots,NTicks1),
|
|
|
|
NSlices is Slices+2,
|
|
|
|
build_slices(0,NTicks1,NSlices,Hashes),
|
|
|
|
get_keys(AVars, Hashes),
|
|
|
|
get_parents(AVars, Hashes).
|
|
|
|
|
|
|
|
build_slices(NTicks,NTicks,_,_) :- !.
|
|
|
|
build_slices(I0,NTicks,NSlices,Hashes) :-
|
|
|
|
functor(Slice,slices,NSlices),
|
|
|
|
I is I0+1,
|
|
|
|
arg(I,Hashes,Slice),
|
|
|
|
build_slices(I,NTicks,NSlices,Hashes).
|
|
|
|
|
|
|
|
get_keys([], _).
|
|
|
|
get_keys([V|AVars], Trees) :-
|
|
|
|
clpbn:get_atts(V, [key(K)]), !,
|
|
|
|
arg(1,K,Time0),
|
|
|
|
Time is Time0+1,
|
|
|
|
arg(Time, Trees, Tree),
|
|
|
|
make_key(K, TKey),
|
|
|
|
arg(TKey, Tree, List),
|
|
|
|
lookup(List, K, V),
|
|
|
|
get_keys(AVars, Trees).
|
|
|
|
get_keys([_|AVars], Trees) :- % may be non-CLPBN vars.
|
|
|
|
get_keys(AVars, Trees).
|
|
|
|
|
|
|
|
get_parents([], _).
|
|
|
|
get_parents([V|AVars], Trees) :-
|
2007-07-13 01:52:54 +01:00
|
|
|
clpbn:get_atts(V, [dist(Id,Parents)]), !,
|
2005-09-09 18:22:33 +01:00
|
|
|
%clpbn:get_atts(V, [key(K)]), format('~w (~w): ~w~n',[V,K,Parents]),
|
2007-07-13 01:52:54 +01:00
|
|
|
add_parents(Parents,V,Id,Trees),
|
2005-09-09 18:22:33 +01:00
|
|
|
get_parents(AVars, Trees).
|
|
|
|
get_parents([_|AVars], Trees) :- % may be non-CLPBN vars.
|
|
|
|
get_parents(AVars, Trees).
|
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
add_parents(Parents,V,Id,Trees) :-
|
2005-09-09 18:22:33 +01:00
|
|
|
transform_parents(Parents,NParents,Copy,Trees),
|
2007-07-13 01:52:54 +01:00
|
|
|
( var(Copy) -> true ; clpbn:put_atts(V, [dist(Id,NParents)]) ).
|
2005-09-09 18:22:33 +01:00
|
|
|
|
|
|
|
transform_parents([],[],_,_).
|
|
|
|
transform_parents([P|Parents0],[P|NParents],Copy,Trees) :-
|
|
|
|
var(P), !,
|
|
|
|
inc_ancestors(P),
|
|
|
|
transform_parents(Parents0,NParents,Copy,Trees).
|
|
|
|
transform_parents([P|Parents0],[V|NParents],copy,Trees) :-
|
|
|
|
arg(1,P,Time0),
|
|
|
|
Time is Time0+1,
|
|
|
|
arg(Time, Trees, Tree),
|
|
|
|
make_key(P, TKey),
|
|
|
|
arg(TKey, Tree, List),
|
|
|
|
lookup(List, P, V),
|
|
|
|
inc_ancestors(V),
|
|
|
|
transform_parents(Parents0,NParents,copy,Trees).
|
|
|
|
|
|
|
|
inc_ancestors(P) :-
|
|
|
|
get_atts(P,[ancestors(N)]), !,
|
|
|
|
N1 is N+1,
|
|
|
|
%format(' ~w->~d:~n',[P,N1]),
|
|
|
|
put_atts(P,[ancestors(N1)]).
|
|
|
|
inc_ancestors(P) :-
|
|
|
|
%format(' ~w->1:~n',[P]),
|
|
|
|
put_atts(P,[ancestors(1)]).
|
|
|
|
|
|
|
|
make_key(T,K) :-
|
|
|
|
arg(2,T,I), !,
|
|
|
|
K is I+2.
|
|
|
|
make_key(_,1).
|
|
|
|
|
|
|
|
lookup(Tree, K, V) :- var(Tree), !,
|
|
|
|
Tree = [[K|V]|_].
|
|
|
|
lookup([[K1|V]|_],K2,V) :- K1 == K2, !.
|
|
|
|
lookup([_|List],K,V) :-
|
|
|
|
lookup(List,K,V).
|
2005-08-17 14:34:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
init_viterbi(V) :-
|
|
|
|
put_atts(V,[prob(0)]).
|
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
viterbi_alg(L0, Lf) :- L0 == Lf, !.
|
|
|
|
viterbi_alg([V|Vs], Rs) :-
|
2007-07-13 01:52:54 +01:00
|
|
|
% format('<< ~w~n',[V]),
|
2005-08-17 14:34:56 +01:00
|
|
|
% get the current status
|
|
|
|
get_atts(V,[prob(P0)]), !,
|
2007-07-13 01:52:54 +01:00
|
|
|
clpbn:get_atts(V,[dist(Id,States)]),
|
|
|
|
get_dist_params(Id,Probs),
|
2005-08-17 14:34:56 +01:00
|
|
|
% adjust to consider emission probabilities
|
|
|
|
adjust_for_emission(V, P0, Pf),
|
2005-09-09 18:22:33 +01:00
|
|
|
propagate(Probs,States,Pf,V,Rs,NRs),
|
|
|
|
viterbi_alg(Vs,NRs).
|
2005-08-17 14:34:56 +01:00
|
|
|
|
|
|
|
adjust_for_emission(V, P0, Pf) :-
|
2005-09-09 18:22:33 +01:00
|
|
|
hmm:get_atts(V,[emission(P)]), !,
|
|
|
|
Pf is P+P0,
|
2005-08-17 14:34:56 +01:00
|
|
|
put_atts(V,[prob(Pf)]).
|
|
|
|
adjust_for_emission(_, P, P).
|
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
propagate([],[],_,_,Rs,Rs).
|
|
|
|
propagate([Prob|Probs],[State|States],Pf,V,Rs,Rs0) :-
|
|
|
|
%format(' ~w~n',[State]),
|
|
|
|
get_atts(State,[prob(P0),ancestors(N)]), !,
|
2005-08-17 14:34:56 +01:00
|
|
|
mprob(Pf,Prob,P),
|
2005-09-09 18:22:33 +01:00
|
|
|
N1 is N-1,
|
2005-08-17 14:34:56 +01:00
|
|
|
(P > P0 ->
|
2005-09-09 18:22:33 +01:00
|
|
|
put_atts(State,[prob(P),backp(V),ancestors(N1)])
|
2005-08-17 14:34:56 +01:00
|
|
|
;
|
2005-09-09 18:22:33 +01:00
|
|
|
put_atts(State,[ancestors(N1)])
|
2005-08-17 14:34:56 +01:00
|
|
|
),
|
2005-09-09 18:22:33 +01:00
|
|
|
(N1 == 0 -> Rs = [State|NRs] ; Rs = NRs),
|
|
|
|
propagate(Probs,States,Pf,V,NRs,Rs0).
|
|
|
|
propagate([Prob|Probs],[State|States],Pf,V,Rs,Rs0) :-
|
|
|
|
get_atts(State,[ancestors(N)]), !,
|
|
|
|
N1 is N-1,
|
2005-08-17 14:34:56 +01:00
|
|
|
mprob(Pf,Prob,P),
|
2005-09-09 18:22:33 +01:00
|
|
|
put_atts(State,[prob(P),backp(V),ancestors(N1)]),
|
|
|
|
(N1 == 0 -> Rs = [State|NRs] ; Rs = NRs),
|
|
|
|
propagate(Probs,States,Pf,V,NRs,Rs0).
|
2005-08-17 14:34:56 +01:00
|
|
|
|
|
|
|
backtrace(Start,Var,Trace,Trace) :- Start == Var, !.
|
|
|
|
backtrace(Start,Var,Trace0,Trace) :-
|
|
|
|
get_atts(Var,[backp(V)]),
|
|
|
|
clpbn:get_atts(Var, [key(K)]),
|
|
|
|
backtrace(Start,V,[K|Trace0],Trace).
|
|
|
|
|
2005-09-09 18:22:33 +01:00
|
|
|
mprob(P0,P1,Pf) :- Pf is P0+P1.
|