in reply to Viterbi application

Hi, I tried to run your first piece of code to better visualize the data, but even that part does not work properly and probably never ends. These nested loops don't work:
for(my $i=1; $i<=$x; ++$i){ for(my $j=0; $j<$y; ++$j){ for(my $k=0; $k<$y; ++$j){ $distance_mat -> [$i][$j][$k] = rand 1; } } }
because the third line increments $j and tests max value on $k, so that will probably never ends. A minimum correction on the first part would be:
use strict; use warnings; use Data::Dumper; my $x = 4; my $y = 5; my $distance_mat; #################### for(my $i=1; $i<=$x; ++$i){ for(my $j=0; $j<$y; ++$j){ for(my $k=0; $k<$y; ++$k){ $distance_mat -> [$i][$j][$k] = rand 1; } } } ##############Start and end nodes have zero cost for(my $i=0; $i<scalar(@{$distance_mat->[1]}); ++$i){ $distance_mat -> [0][0][$i] = 0; } for(my $i=0; $i<scalar(@{$distance_mat->[-1]}); ++$i){ $distance_mat -> [-1][0][$i] = 0; } print Dumper $distance_mat;
which will print out:
$VAR1 = [ [ [ 0, 0, 0, 0, 0 ] ], [ [ '0.212283829766424', '0.693160487196536', '0.721569572380883', '0.0147371758675447', '0.508071977540641' ], [ '0.607456981189884', '0.413801502805963', '0.962572895729942', '0.938935399978082', '0.717291943322152' ], [ '0.306279826459825', '0.89992134970587', '0.690479726793445', '0.733121785882545', '0.487829205187605' ], [ '0.711943004833408', '0.254652102915149', '0.267465127747556', '0.302759025077147', '0.32486696921206' ], [ '0.489091147270205', '0.0735180399392874', '0.235300433159548', '0.746479779929494', '0.905501634741288' ] ], [ [ '0.688008245370213', '0.113680627990203', '0.99717709784958', '0.510060769658413', '0.767946103966395' ], [ '0.947138589977666', '0.369698435985974', '0.651511387205236', '0.211410103277188', '0.21735370417333' ], [ '0.734569009367142', '0.598363562918809', '0.431561123751734', '0.712531952111899', '0.293976071886583' ], [ '0.517267742541694', '0.55231451959105', '0.45234384270081', '0.147473762126175', '0.290022437346682' ], [ '0.470738612969299', '0.0427293275962448', '0.777329578040955', '0.144845576196932', '0.608144103958836' ] ], [ [ '0.012097916474044', '0.488911202200264', '0.424603577528178', '0.187457713190881', '0.608602384605799' ], [ '0.492303567493103', '0.334913914644719', '0.232936661637211', '0.929022161683616', '0.616006850324688' ], [ '0.150798208707261', '0.409304914407997', '0.553553019401473', '0.173385566856545', '0.882371508058217' ], [ '0.786329035626949', '0.480783348396823', '0.719431190274186', '0.656542212578945', '0.632677581767197' ], [ '0.699789445283013', '0.941904765904233', '0.239623890103449', '0.176224763809842', '0.261173130161335' ] ], [ [ 0, 0, 0, 0, 0 ], [ '0.373621309619981', '0.411533490541188', '0.623695973015582', '0.00773593933377725', '0.00893442880364503' ], [ '0.837186783303693', '0.584918955098058', '0.0295591991893147', '0.422034731454435', '0.260471634650969' ], [ '0.82811137679651', '0.49858938564811', '0.153148378721244', '0.500505552716803', '0.679154015831099' ], [ '0.0258503603085316', '0.399454916212452', '0.470265378306834', '0.498481414135323', '0.832463141199078' ] ] ];
Notice how Data::Dumper makes it possible to visualize your data structure. Looking at the above structure, it would seem (but I cannot be sure) that you would probably want to use the post increment operators, rather than the pre increment operator. Or, better, use the
for my $i (0..$max_val)
syntax. Please fix this part of your code so that we can understand what you are trying to do and agree on your input, then only can we think really on the rest of your program. And, at this point, you probably want to open a new thread (referring to this one) with the input part properly fixed, son that we can then focus on the rest.

Replies are listed 'Best First'.
Re^2: Viterbi application
by azheid (Sexton) on Apr 08, 2014 at 20:18 UTC

    oops, sorry. I incorrectly copied the third line of that loop. In my program, the data structure is more complicated and depends on a large file. I tried to simplify the code so that the details were easier to understand.

    At this point, you are right, I should probably make a new post. I am considering asking one of my computer science colleagues to help, however in the past they have been reluctant.

      No problem, this can happen, but please make sure, before opening a new thread, that at least the part of your code that produces the input data is really correct. This way, we can try to work on your Viterbi algorithm itself with good data. For the time being, the Dumper output shows for example some inner arrays with only 0 values, I am not completely sure that's really what you want to start with. Don't hesitate to use the Dumper function of the Data::Dumper module (the way I did it) to better visualize your data and be sure that it is what you wish us to work on. Also note that the two zero-cost code segments were bugged and could not compile; I fixed them so that they would at least compile and do something, but I am not completely sure that what they do now is really what you wanted them to do. Well, in brief, please provide really good input data, then, once we have that, I am sure that some gentle monks among us will be willing to go through the Viterbi algorithm with you. (If you have trouble producing really good input data, I would suggest that you ask for help here, and create a new thread only once this part of the work is completely solved.)