Пример #1
0
        public Task <string> GetBestRoute(BestRouteRequest action)
        {
            StartNode = new Node()
            {
                Name = action.From.ToUpper()
            };
            EndNode = new Node()
            {
                Name = action.To.ToUpper()
            };
            DataSource = action.DataSource;

            DijkstraSearch();
            BuildShortestPath(BestRoute, EndNode);
            BestRoute.AddFirst(StartNode);

            var itinerary = new BestRouteResponse()
            {
                BestRoute = BestRoute
            };

            var message = itinerary.BestRoute.Last() == EndNode
                ? $"Best route found: {itinerary} > $ {itinerary.Total}"
                : "No routes found for this params :(";

            return(Task.FromResult(message));
        }
Пример #2
0
        public void WhenExistsFromAndToDestinyThenFindByDijkstraImplementation()
        {
            var listRouteValue = new List <RouteValue>();

            routeRepository.GetAll().Returns(listRouteValue);

            var listNode = new List <Node>();

            var nodeFrom = new Node(FROM);
            var nodeTo   = new Node(TO);

            listNode.Add(nodeFrom);
            listNode.Add(nodeTo);

            graphCreator.CreateNodes(listRouteValue).Returns(listNode);

            var bestRoute = new BestRoute();

            dijkstraImplementation.FindBestRoute(nodeFrom, nodeTo).Returns(bestRoute);

            var route  = new Route(FROM, TO);
            var result = dijkstraLowestPriceFinder.Find(route);

            Assert.AreEqual(bestRoute, result);

            routeRepository.Received().GetAll();
            graphCreator.Received().CreateNodes(listRouteValue);
            dijkstraImplementation.Received().FindBestRoute(nodeFrom, nodeTo);
        }
Пример #3
0
        public ActionResult Index(string pathFile)
        {
            if (string.IsNullOrEmpty(pathFile))
            {
                return(View());
            }

            int[,] jsonMatrix;

            using (StreamReader r = new StreamReader(pathFile))
            {
                string json = r.ReadToEnd();
                jsonMatrix = JsonConvert.DeserializeObject <int[, ]>(json);
            }

            FBAlgoritm fbAlgoritm = new FBAlgoritm(jsonMatrix);
            DAlgoritm  dAlgoritm  = new DAlgoritm(jsonMatrix);

            BestRoute fbBestRoute = fbAlgoritm.Work();
            BestRoute dBestRoute  = dAlgoritm.Work();

            View viewModel = new View
            {
                dBestRoute  = dBestRoute,
                fbBestRoute = fbBestRoute,
                jsonNodos   = null
            };

            return(View(viewModel));
        }
        public void CalculateNearestNeighbour()
        {
            while (BestRoute.Count != Cities.Count)
            {
                double bestDistance     = int.MaxValue;
                int    closestCityIndex = 0;

                for (int i = 0; i < Cities.Count; i++)
                {
                    double distance = CalculateDistance(Cities[_vertex], Cities[i]);
                    if (bestDistance > distance && !Cities[i].Visited)
                    {
                        bestDistance     = distance;
                        closestCityIndex = i;
                    }
                    ReportProgress();
                }

                //Thread.Sleep(10);

                if (BestRoute.Count < Cities.Count)
                {
                    Cities[closestCityIndex].Visited = true;
                    _vertex = closestCityIndex;
                    BestRoute.Add(Cities[_vertex]);
                }

                //TwoOptSwap();

                Iterations++;
            }
        }
Пример #5
0
    public void CalculateBestRoute()
    {
        foreach (Transform child in RouteParts.transform)
        {
            GameObject.Destroy(child.gameObject);
        }

        if (StartStation.value == EndStation.value)
        {
            return;
        }

        bestRoute = FindObjectOfType <RouteScript>().GetBestRoute(Stops.pysakit[StartStation.value], Stops.pysakit[EndStation.value]);
        for (int i = 0; i < bestRoute.Route.Count - 1; i++)
        {
            GameObject temp = Instantiate(RoutePrefab, RouteParts.transform);
            Debug.Log(i);
            temp.GetComponentInChildren <Image>().overrideSprite = Buslines[bestRoute.RouteColor[i]];
            if (i < bestRoute.Route.Count - 1)
            {
                temp.GetComponentInChildren <TMP_Text>().text = bestRoute.Route[i] + " -> " + bestRoute.Route[i + 1];
            }
            else
            {
                temp.GetComponentInChildren <TMP_Text>().text = bestRoute.Route[i];
            }

            RoutePartsList.Add(temp);
        }
    }
Пример #6
0
 public DAlgoritm(int[,] matriz)
 {
     matrizNodos = matriz;
     cantNodos   = Convert.ToInt32(Math.Sqrt(matriz.Length));
     bestRoute   = new BestRoute
     {
         selectedRoute = "",
         distance      = 0
     };
 }
        public void TestConsolePrintBestRouteMessage()
        {
            var output = new StringWriter();

            Console.SetOut(output);

            var bestRoute = new BestRoute {
                Route = "ASD-AQW-QWE", Value = 10
            };

            userInterfaceConsole.PrintBestRoute(bestRoute);

            Assert.AreEqual($"best route: ASD-AQW-QWE > $10{Environment.NewLine}", output.ToString());
        }
Пример #8
0
        public FBAlgoritm(int[,] matriz)
        {
            matrizNodos = matriz;
            cantNodos   = Convert.ToInt32(Math.Sqrt(matriz.Length));

            // Calculo la cantidad de veces que voy a repetir la búsqueda del camino más óptimo.
            // En un grafo de "n" nodos, hay (n-1)!/2 caminos posibles. Se divide por 2 porque hay caminos inversos -duplicados-.
            repeats = Factorial(cantNodos - 1);

            bestRoute = new BestRoute
            {
                selectedRoute = "",
                distance      = Int32.MaxValue
            };
        }
        public void WhenCalcBestRoutThenGetFinderWithValueAndFindRoute()
        {
            var route = new Route("FRO", "TO");

            var bestRoute = new BestRoute();

            bestRoutFinder.Find(route).Returns(bestRoute);

            findFactory.GetFinder(FindBestBy.VALUE).Returns(bestRoutFinder);

            var result = bestRouteService.CalcBestRoute(route);

            findFactory.Received().GetFinder(FindBestBy.VALUE);
            bestRoutFinder.Received().Find(route);

            Assert.AreEqual(bestRoute, result);
        }
Пример #10
0
    public BestRoute GetBestRoute(string from, string to)
    {
        PossibleRoutes.Clear();
        List <string> currentRoute = new List <string>();

        GetRoute(from, to, currentRoute, 0);

        BestRoute usethisroute = new BestRoute();

        usethisroute.Lenght = 1000;

        foreach (var item in PossibleRoutes)
        {
            int routelenght = 0;
            for (int i = 1; i < item.Count; i++)
            {
                routelenght += GetLenght(item[i - 1], item[i]);
            }

            if (routelenght < usethisroute.Lenght)
            {
                usethisroute.Route  = item;
                usethisroute.Lenght = routelenght;
            }
        }

        for (int i = 1; i < usethisroute.Route.Count; i++)
        {
            int line;
            ////check first the one line being used
            line = checkCurrentLinjasto(usethisroute.Route[i - 1], usethisroute.Route[i], CurrentLinjasto);

            usethisroute.RouteColor.Add(line);
            CurrentLinjasto = line;
        }


        return(usethisroute);
    }
Пример #11
0
    // Start is called before the first frame update
    void Start()
    {
        CurrentLinjasto = 0;
        TextAsset asset = Resources.Load("reittiopas") as TextAsset;

        if (asset != null)
        {
            RoadsList   = JsonUtility.FromJson <tietlist>(asset.text);
            pysakitlist = JsonUtility.FromJson <pysakitlist>(asset.text);
            linjastot   = JsonUtility.FromJson <linjastotlist>(asset.text);
        }

        //foreach (var item in linjastot.keltainen)
        //{
        //    print(item);
        //}

        List <string> currentRoute = new List <string>();

        GetRoute("E", "F", currentRoute, 0);
        BestRoute usethisroute = new BestRoute();

        usethisroute.Lenght = 1000;

        //print all routes
        foreach (var item in PossibleRoutes)
        {
            print("complete route:");
            Printstr = "";
            foreach (var route in item)
            {
                Printstr += " -> " + route;
            }

            int routelenght = 0;
            for (int i = 1; i < item.Count; i++)
            {
                routelenght += GetLenght(item[i - 1], item[i]);
            }
            print(Printstr);
            print("Lenght: " + routelenght);

            if (routelenght < usethisroute.Lenght)
            {
                usethisroute.Route  = item;
                usethisroute.Lenght = routelenght;
            }
        }

        print("Best route:");
        Printstr = "";
        foreach (var route in usethisroute.Route)
        {
            Printstr += " -> " + route;
        }
        print(Printstr);
        print("Lenght: " + usethisroute.Lenght);

        for (int i = 1; i < usethisroute.Route.Count; i++)
        {
            int line;
            ////check first the one line being used
            line = checkCurrentLinjasto(usethisroute.Route[i - 1], usethisroute.Route[i], CurrentLinjasto);

            usethisroute.RouteColor.Add(line);
            CurrentLinjasto = line;
            print("Color: " + usethisroute.RouteColor[i - 1]);
        }
    }
 public void PrintBestRoute(BestRoute bestRoute)
 {
     Console.WriteLine($"best route: {bestRoute.Route} > ${bestRoute.Value}");
 }
Пример #13
0
 public override string ToString()
 => string.Join(" -> ", BestRoute.Select(s => s.Name.ToString()));