示例#1
0
        /*****
        # A empirical distribution
        #  def transition_prob(u, v):
        #   c = 1 / BETA
        # Calculating route distance is expensive.
        # We will discuss how to reduce the number of calls to this function later.
        #   delta = math.abs(route_distance(u, v) - great_circle_distance(u.measurement, v.measurement))
        #   return c * math.exp(-delta)
        *****/
        private double TransitionProbability(HiddenMarkovState u, HiddenMarkovState v)
        {
            if (u == s)
            {
                return(1.0f);
            }
            else if (v == t)
            {
                return(1.0f);
            }

            Node Source = RoadNetwork.ResolvePoint(u.P, 0.1f).First();
            Node Target = RoadNetwork.ResolvePoint(v.P, 0.1f).First();

            Router router = new Router(Source, Target, string.Empty, 0, TravelMode.Car, ObjFunction.Distance);

            router.Start();

            double routeDistance = router.SolutionCost;//router.Solution.Where(x => x.Id != -1).Sum(x => x.GetDistance(TravelMode.Car));
            //
            double beta  = 3;
            double c     = 1 / beta;
            double delta = Math.Abs(routeDistance - u.P.DistanceFrom(v.P));

            return(c * Math.Exp(-delta));
        }
示例#2
0
 private void Init()
 {
     s = new HiddenMarkovState(null, null, 0.0f, true);
     t = new HiddenMarkovState(null, null, 0.0f, true);
     foreach (Point P in Points)
     {
         CreateState(P, Points.IndexOf(P));
     }
     LinkStates();
 }
示例#3
0
 private double EdgeCost(HiddenMarkovState u, HiddenMarkovState v)
 {
     return(-1 * Math.Log10(TransitionProbability(u, v)));
 }
示例#4
0
 public void AddToAdjList(HiddenMarkovState State)
 {
     AdjacencyList.Add(State);
 }
示例#5
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];
            }
        }