コード例 #1
0
        public void ReturnDijkstraRouteTest(int totalLoc)
        {
            int totalLocations = totalLoc;

            if (totalLoc < 2)
            {
                totalLocations = rgn.Next(2, 11);
            }

            List <Location> locations = new List <Location>();
            List <Road>     roads     = new List <Road>();

            for (int i = 0; i < totalLocations; i++)
            {
                Location temp = new Location(1, rgn.Next(0, 100), rgn.Next(0, 100));
                locations.Add(temp);
            }
            foreach (Location l1 in locations)
            {
                foreach (Location l2 in locations)
                {
                    if (l1 != l2)
                    {
                        Road temp = new Road(l1, l2);
                        roads.Add(temp);
                    }
                }
            }
            int           split = totalLocations / 2;
            Dijkstra      d     = new Dijkstra(roads);
            DijkstraRoute dr    = d.GetRouteTo(locations[rgn.Next(0, split)], locations[rgn.Next(split, totalLocations - 1)]);

            Assert.True(dr != null);
        }
コード例 #2
0
        private void btnDrawRoute_Click(object sender, EventArgs e)
        {
            int locationNumber1;
            int locationNumber2;

            if (int.TryParse(tbDrawRouteFrom.Text, out locationNumber1) && int.TryParse(tbDrawRouteTo.Text, out locationNumber2))
            {
                if (map != null)
                {
                    Location l1 = map.GetLocationByID(locationNumber1);
                    Location l2 = map.GetLocationByID(locationNumber2);
                    if (l1 != null && l2 != null)
                    {
                        if (dijkstraForDrawing != null)
                        {
                            DijkstraRoute myRoute = dijkstraForDrawing.GetRouteTo(l1, l2);
                            if (myRoute != null)
                            {
                                List <Road> allRoads = map.Edges;
                                foreach (Road r in allRoads)
                                {
                                    r.ResetDrawFields();
                                }
                                foreach (Road r in myRoute.Route)
                                {
                                    r.LineColor = Color.Green;
                                }
                                Map.RedrawMap();
                            }
                            else
                            {
                                MessageBox.Show("No route could be found for these numbers/locations");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Map has not been analyzed yet");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Not all locations could be found for the given numbers");
                    }
                }
            }
            else
            {
                MessageBox.Show("Location Number is in a wrong format");
            }
        }
コード例 #3
0
        public void ReturnShortestDijkstraRouteTest(int totalLoc)
        {
            int totalLocations = totalLoc;

            if (totalLoc < 2)
            {
                totalLocations = rgn.Next(2, 11);
            }

            List <Location> locations = new List <Location>();
            List <Road>     roads     = new List <Road>();

            for (int i = 0; i < totalLocations; i++)
            {
                Location temp = new Location(2, rgn.Next(0, 100), rgn.Next(0, 100));
                locations.Add(temp);
            }
            for (int i = 0; i < locations.Count - 1; i++)
            {
                Road temp = new Road(locations[i], locations[i + 1]);
                temp.InitialCost = rgn.Next(1, 4);
                roads.Add(temp);
            }
            int randomRoadsCount = (int)Math.Floor((double)totalLocations / 2);

            for (int i = 1; i <= randomRoadsCount; i++)
            {
                int  num1 = rgn.Next(0, randomRoadsCount);
                int  num2 = rgn.Next(randomRoadsCount, totalLocations);
                Road temp = new Road(locations[num1], locations[num2]);
                temp.InitialCost = rgn.Next(50, 100);
                roads.Add(temp);
            }
            Dijkstra      d  = new Dijkstra(roads);
            DijkstraRoute dr = d.GetRouteTo(locations[0], locations[totalLocations - 1]);

            Assert.True(dr.RouteLenght < 50);
        }
コード例 #4
0
        public void UnreachableLocationTest(int totalLoc)
        {
            int totalLocations = totalLoc;

            if (totalLoc < 4)
            {
                totalLocations = rgn.Next(2, 11);
            }

            List <Location> locations = new List <Location>();
            List <Road>     roads     = new List <Road>();

            for (int i = 0; i < totalLocations; i++)
            {
                Location temp = new Location(3, rgn.Next(0, 100), rgn.Next(0, 100));
                locations.Add(temp);
            }
            Road r = new Road(locations[0], locations[1]);

            roads.Add(r);
            for (int i = 2; i < totalLocations; i++)
            {
                for (int j = 2; j < totalLocations; j++)
                {
                    if (i != j)
                    {
                        Road temp = new Road(locations[i], locations[j]);
                        roads.Add(temp);
                    }
                }
            }
            Location      from = locations[rgn.Next(2, totalLocations)];
            Location      to   = locations[rgn.Next(0, 2)];
            Dijkstra      d    = new Dijkstra(roads);
            DijkstraRoute dr   = d.GetRouteTo(from, to);

            Assert.True(dr == null);
        }
コード例 #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            int locationNumber1;

            if (int.TryParse(tbDrawShop.Text, out locationNumber1))
            {
                if (map != null)
                {
                    if (map.Shops.Count > 0)
                    {
                        Location l1 = map.GetLocationByID(locationNumber1);
                        if (l1 != null)
                        {
                            if (dijkstraForDrawing != null)
                            {
                                //find best route
                                DijkstraRoute temp      = null;
                                DijkstraRoute bestRoute = null;
                                foreach (Location s in map.Shops)
                                {
                                    temp = dijkstraForDrawing.GetRouteTo(l1, s);
                                    if (temp != null)
                                    {
                                        if (bestRoute == null)
                                        {
                                            bestRoute = temp;
                                        }
                                        else if (temp.RouteLenght < bestRoute.RouteLenght)
                                        {
                                            bestRoute = temp;
                                        }
                                    }
                                }

                                //display best route
                                if (bestRoute != null)
                                {
                                    List <Road> allRoads = map.Edges;
                                    foreach (Road r in allRoads)
                                    {
                                        r.ResetDrawFields();
                                    }
                                    foreach (Road r in bestRoute.Route)
                                    {
                                        r.LineColor = Color.Green;
                                    }
                                    Map.RedrawMap();
                                }
                                else
                                {
                                    MessageBox.Show("No route could be found from shop to given location");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Map has not been analyzed yet");
                            }
                        }
                        else
                        {
                            MessageBox.Show("No location could be found for the given number");
                        }
                    }
                    else
                    {
                        MessageBox.Show("At least 1 shop should be placed in order to make use of this functionality");
                    }
                }
            }
            else
            {
                MessageBox.Show("Location Number is in a wrong format");
            }
        }
コード例 #6
0
        private void DrawAllClosests_Click(object sender, EventArgs e)
        {
            if (map != null)
            {
                if (dijkstraForDrawing != null)
                {
                    if (map.Shops.Count > 0 && map.Warehouses.Count > 0)
                    {
                        List <DijkstraRoute> bestRoutes      = new List <DijkstraRoute>();
                        bool allShopsAreConnectedToWarehouse = true;
                        //find best route
                        foreach (Location s in map.Shops)
                        {
                            DijkstraRoute temp      = null;
                            DijkstraRoute bestRoute = null;
                            foreach (Location w in map.Warehouses)
                            {
                                temp = dijkstraForDrawing.GetRouteTo(w, s);
                                if (temp != null)
                                {
                                    if (bestRoute == null)
                                    {
                                        bestRoute = temp;
                                    }
                                    else if (temp.RouteLenght < bestRoute.RouteLenght)
                                    {
                                        bestRoute = temp;
                                    }
                                }
                            }
                            if (bestRoute != null)
                            {
                                bestRoutes.Add(bestRoute);
                            }
                            else
                            {
                                allShopsAreConnectedToWarehouse = false;
                            }
                        }

                        //display best routes
                        if (bestRoutes.Count > 0)
                        {
                            List <Road> allRoads = map.Edges;
                            foreach (Road r in allRoads)
                            {
                                r.ResetDrawFields();
                            }
                            foreach (DijkstraRoute dr in bestRoutes)
                            {
                                foreach (Road r in dr.Route)
                                {
                                    r.LineColor = Color.Green;
                                }
                            }
                            if (!allShopsAreConnectedToWarehouse)
                            {
                                MessageBox.Show("Not all shops have a connection to some warehouse");
                            }
                            Map.RedrawMap();
                        }
                        else
                        {
                            MessageBox.Show("No route could be found from any shop to any warehouse");
                        }
                    }
                    else
                    {
                        MessageBox.Show("At least 1 shop and warehouse should be placed in order to make use of this functionality");
                    }
                }
                else
                {
                    MessageBox.Show("Map has not been analyzed yet");
                }
            }
        }