public Tuple<IEnumerable<string>, int> FindShortestRoute(IEnumerable<Route> routes, string from, string to)
        {
            Dijkstra.Dijkstra dijkstra;
            //Form adjacency matrix
            var stations = routes.Select(x => x.From)
                .Intersect(routes.Select(x => x.To))
                .Distinct()
                .OrderBy(x => x)
                .ToList();

            var numberOfStations = stations.Count();

            var adjacencyMatrix = new double[numberOfStations,numberOfStations];
            foreach (var route in routes)
            {
                adjacencyMatrix[stations.IndexOf(route.From), stations.IndexOf(route.To)] = route.TimeTaken;
            }

            var fromStationIndex = stations.IndexOf(from);
            var toStationIndex = stations.IndexOf(to);

            dijkstra = new Dijkstra.Dijkstra(adjacencyMatrix, fromStationIndex);

            var routeToDestination = new List<string>();
            for (var currentNode = toStationIndex; currentNode != -1; currentNode = dijkstra.path[currentNode])
            {
                routeToDestination.Add(stations[currentNode]);
            }

            routeToDestination.Reverse();
            return Tuple.Create<IEnumerable<string>, int>(routeToDestination, (int)dijkstra.dist[toStationIndex]);
        }
Esempio n. 2
0
        public static float Run(IList<double> dp0, IList<IList<double>> dpc1)
        {
            IList<LocalPattern> lps1 = new List<LocalPattern>();
            lps1 = dpc1.Aggregate(lps1,
                (current, ts) => current.Concat(GetLocalPatterns(ts, WINDOW_SIZE)).ToList());

            IList<LocalPattern> lps0 = GetLocalPatterns(dp0, WINDOW_SIZE);
            BUCKET_SIZE = (int) (TOP_BUCKET_RATIO*dp0.Count);

            List<List<LocalPatternMatch>> lpmBuckets = GetLocalPatternMatchBuckets(lps0, lps1);

            var allLpms = new List<LocalPatternMatch>();
            foreach (var bucket in lpmBuckets)
            {
                foreach (LocalPatternMatch lpm in bucket)
                {
                    allLpms.Add(lpm);
                }
            }

            int numVertices = allLpms.Count + 2;
            var costMatrix = new int[numVertices, numVertices];

            for (int i = 0; i < allLpms.Count; i++)
            {
                int distToStart = LocalPatternMatch.DistanceToPs(allLpms[i], allLpms.Count, WINDOW_SIZE);
                int distToEnd = LocalPatternMatch.DistanceToPe(allLpms[i], allLpms.Count, WINDOW_SIZE);

                costMatrix[0, i + 1] = distToStart;
                costMatrix[i + 1, numVertices - 1] = distToEnd;
            }

            for (int row = 0; row < numVertices - 2; row++)
            {
                for (int col = 0; col < numVertices - 2; col++)
                {
                    if (row == col)
                        costMatrix[row + 1, col + 1] = 0;
                    else
                        costMatrix[row + 1, col + 1] = LocalPatternMatch.Distance(allLpms[row], allLpms[col],
                            allLpms.Count, WINDOW_SIZE);
                }
            }

            var dijkstra = new Dijkstra.Dijkstra();
            dijkstra.Run(costMatrix, 0);
            return dijkstra.StartToEndDistance;
        }
Esempio n. 3
0
        public void ValidateSampleTwoFrom9To6()
        {
            var one    = new DirectionalNode("1");
            var two    = new DirectionalNode("2");
            var three  = new DirectionalNode("3");
            var four   = new DirectionalNode("4");
            var five   = new DirectionalNode("5");
            var six    = new DirectionalNode("6");
            var seven  = new DirectionalNode("7");
            var height = new DirectionalNode("8");
            var nine   = new DirectionalNode("9");
            var ten    = new DirectionalNode("10");

            one.ConnectTo(two, 10);
            one.ConnectTo(four, 20);
            one.ConnectTo(five, 20);
            one.ConnectTo(six, 5);
            one.ConnectTo(seven, 15);

            two.ConnectTo(four, 10);
            two.ConnectTo(three, 5);

            three.ConnectTo(four, 5);
            three.ConnectTo(two, 15);

            four.ConnectTo(five, 10);

            five.ConnectTo(six, 5);

            seven.ConnectTo(six, 10);

            height.ConnectTo(one, 5);
            height.ConnectTo(two, 20);
            height.ConnectTo(seven, 5);

            nine.ConnectTo(two, 15);
            nine.ConnectTo(height, 20);
            nine.ConnectTo(ten, 10);

            ten.ConnectTo(two, 5);
            ten.ConnectTo(three, 15);

            var dijkstra = new Dijkstra.Dijkstra();
            var result   = dijkstra.FindShortestPath(nine, six);

            Assert.NotNull(result);
            Assert.Equal(4, result.Length);
        }
Esempio n. 4
0
        public void ValidateSampleOneFromAToF()
        {
            var a = new SimpleNode("A");
            var b = new SimpleNode("B");
            var c = new SimpleNode("C");
            var d = new SimpleNode("D");
            var e = new SimpleNode("E");
            var f = new SimpleNode("F");

            a.ConnectTo(b, 4);
            a.ConnectTo(c, 2);
            b.ConnectTo(c, 1);
            b.ConnectTo(d, 5);
            c.ConnectTo(d, 8);
            c.ConnectTo(e, 10);
            d.ConnectTo(f, 6);
            d.ConnectTo(e, 2);
            e.ConnectTo(f, 2);

            var dijkstra = new Dijkstra.Dijkstra();
            var result   = dijkstra.FindShortestPath(a, f);

            Assert.NotNull(result);
            Assert.Equal(6, result.Length);
            Assert.Equal("A", result[0].Label);
            Assert.Equal("C", result[1].Label);
            Assert.Equal("B", result[2].Label);
            Assert.Equal("D", result[3].Label);
            Assert.Equal("E", result[4].Label);
            Assert.Equal("F", result[5].Label);

            var weight = 0;

            for (var i = 0; i < result.Length; i++)
            {
                var currentWeight = 0;
                if (i < result.Length - 1)
                {
                    currentWeight = result[i].Neighbors
                                    .Single(n => n.Node.Label.Equals(result[i + 1].Label))
                                    .WeightToNode;
                }
                weight += currentWeight;
            }

            Assert.Equal(12, weight);
        }