Esempio n. 1
0
        private void RunDijkstra()
        {
            Satsuma.PriorityQueue <HiddenMarkovState, double> Q        = new Satsuma.PriorityQueue <HiddenMarkovState, double>();
            Dictionary <HiddenMarkovState, double>            distance = new Dictionary <HiddenMarkovState, double>();
            Dictionary <HiddenMarkovState, HiddenMarkovState> parent   = new Dictionary <HiddenMarkovState, HiddenMarkovState>();

            if (!parent.ContainsKey(s))
            {
                parent.Add(s, null);
            }
            else
            {
                parent[s] = null;
            }

            Q[s] = 0.0;

            while (Q.Count != 0)
            {
                // find the closest reached but unfixed node
                double            minDist;
                HiddenMarkovState min = Q.Peek(out minDist);
                Q.Pop();

                /// fix the node.
                if (!distance.ContainsKey(min))
                {
                    distance.Add(min, minDist);
                }
                else
                {
                    distance[min] = minDist; // fix the node
                }
                if (min == t)
                {
                    break;           // target node found and fixed.
                }
                foreach (HiddenMarkovState v in min.AdjacencyList)
                {
                    if (distance.ContainsKey(v))
                    {
                        continue;                         // already processed
                    }
                    double newDist = minDist + min.StateCost() + EdgeCost(min, v);

                    double oldDist;
                    if (!Q.TryGetPriority(v, out oldDist))
                    {
                        oldDist = double.PositiveInfinity;
                    }

                    if (newDist < oldDist)
                    {
                        Q[v] = newDist;
                        if (parent.ContainsKey(v))
                        {
                            parent[v] = min;
                        }
                        else
                        {
                            parent.Add(v, min);
                        }
                    }
                }
            }

            // Construct Solution.
            HiddenMarkovState S = parent[t];

            while (S != null)
            {
                //if (S.GetUnderlyingRNode() != null);
                MatchingSolution.AddFirst(S.GetUnderlyingRNode());
                S = parent[S];
            }
        }