private void FileLoaderButton_Click(object sender, RoutedEventArgs e) { var openFileDialog = new OpenFileDialog(); openFileDialog.DefaultExt = ".txt"; openFileDialog.Filter = "Text Files (*.txt)|*.txt"; var result = openFileDialog.ShowDialog(); if (result == true) { var filePath = openFileDialog.FileName; FileLoaderLabel.Content = $"Loading file..."; FileLoader fileLoader = new FileLoader(); fileLoaderResult = fileLoader.LoadFile(filePath, isAStarSelected); string labelText; if (!fileLoaderResult.IsLoadingSuccess) { MessageBox.Show("An error occured while loading a file, try again", "Error", MessageBoxButton.OK, MessageBoxImage.Error); AlghorithmAButton.IsEnabled = false; labelText = "Loading error"; } else { var message = $"Successfully loaded file {Path.GetFileName(filePath)}.\n" + $"Amount of cities: {fileLoaderResult.CityAmount}, connections: {fileLoaderResult.ConnectionsAmount}\n" + $"Time taken: {fileLoaderResult.LoadTime}ms"; MessageBox.Show(message, "Success", MessageBoxButton.OK, MessageBoxImage.Information); outputFileName = Path.GetFileName(filePath).Replace("in", "out"); outputFilePath = Path.Combine(Path.GetDirectoryName(filePath), outputFileName); AlghorithmAButton.IsEnabled = true; labelText = "Loading complete!"; } algorithmsTimes.Clear(); FileLoaderLabel.Content = labelText; } }
AlghorithmCalculationResult IAlghorithm.CalculateRoute(FileLoaderResult fileLoaderResult) { var arraysLength = fileLoaderResult.CityAmount + 1; var distanceArray = new Dictionary <int, BFSDistanceVertex>(); var predecessorsArray = new int[arraysLength]; var searchedCitiesQueue = new Queue <int>(arraysLength); var startCity = fileLoaderResult.StartCity; var endCity = fileLoaderResult.EndCity; for (int i = 1; i <= arraysLength; ++i) { distanceArray.Add(i, new BFSDistanceVertex(int.MaxValue, int.MaxValue)); } distanceArray[startCity] = new BFSDistanceVertex(0, 0); searchedCitiesQueue.Enqueue(startCity); while (searchedCitiesQueue.Count != 0) { var currentlySearchedCity = searchedCitiesQueue.Dequeue(); foreach (var neighbour in fileLoaderResult.IncidenceList[currentlySearchedCity]) { var newPossibleDistance = distanceArray[currentlySearchedCity].DistanceInCities + 1; var currentDistanceToNeighbour = distanceArray[neighbour.ConnectedCity].DistanceInCities; if (currentDistanceToNeighbour >= newPossibleDistance) { var newRealPossibleDistance = distanceArray[currentlySearchedCity].RealDistance + neighbour.Distance; var currentRealDistance = distanceArray[neighbour.ConnectedCity].RealDistance; if (currentRealDistance > newRealPossibleDistance) { searchedCitiesQueue.Enqueue(neighbour.ConnectedCity); distanceArray[neighbour.ConnectedCity].DistanceInCities = newPossibleDistance; distanceArray[neighbour.ConnectedCity].RealDistance = newRealPossibleDistance; predecessorsArray[neighbour.ConnectedCity] = currentlySearchedCity; } } } } var path = new List <int>(); path.Add(endCity); var pathReadCurrentCity = predecessorsArray[endCity]; path.Add(pathReadCurrentCity); do { pathReadCurrentCity = predecessorsArray[pathReadCurrentCity]; path.Add(pathReadCurrentCity); } while (pathReadCurrentCity != startCity); return(new BFSResult(path.Count - 2, distanceArray[endCity].RealDistance, path)); }
public AlghorithmCalculationResult CalculateRoute(FileLoaderResult fileLoaderResult) { var incidenceList = fileLoaderResult.IncidenceList; var arraysLength = fileLoaderResult.CityAmount + 1; var visitedArray = new bool[arraysLength]; var distancesArray = new int[arraysLength]; var predecessorsArray = new int[arraysLength]; var isAlreadyAddedInCityDistances = new bool[arraysLength]; for (int i = 1; i < arraysLength; ++i) { distancesArray[i] = int.MaxValue; } var cityDistances = new SortedSet <AlgorithmCity>(new AlgorithmCityComparer()); var startCity = fileLoaderResult.StartCity; var endCity = fileLoaderResult.EndCity; cityDistances.Add(new AlgorithmCity(startCity, 0)); distancesArray[startCity] = 0; while (cityDistances.Count != 0) { var currentCity = cityDistances.Min; cityDistances.Remove(currentCity); isAlreadyAddedInCityDistances[currentCity.City] = false; visitedArray[currentCity.City] = true; foreach (var neighbour in incidenceList[currentCity.City]) { var possibleNewDistance = currentCity.Distance + neighbour.Distance; if (possibleNewDistance < distancesArray[neighbour.ConnectedCity]) { cityDistances.Remove(new AlgorithmCity(neighbour.ConnectedCity, distancesArray[neighbour.ConnectedCity])); isAlreadyAddedInCityDistances[neighbour.ConnectedCity] = false; distancesArray[neighbour.ConnectedCity] = possibleNewDistance; predecessorsArray[neighbour.ConnectedCity] = currentCity.City; } if (visitedArray[neighbour.ConnectedCity] == false && isAlreadyAddedInCityDistances[neighbour.ConnectedCity] == false) { cityDistances.Add(new AlgorithmCity(neighbour.ConnectedCity, distancesArray[neighbour.ConnectedCity])); isAlreadyAddedInCityDistances[currentCity.City] = true; } } } var path = new List <int>(); path.Add(endCity); var pathReadCurrentCity = predecessorsArray[endCity]; path.Add(pathReadCurrentCity); do { pathReadCurrentCity = predecessorsArray[pathReadCurrentCity]; path.Add(pathReadCurrentCity); } while (pathReadCurrentCity != startCity); return(new DijkstraResult(distancesArray[endCity], path)); }
public AlghorithmCalculationResult CalculateRoute(FileLoaderResult fileLoaderResult) { var incidenceList = fileLoaderResult.IncidenceList; var startCity = fileLoaderResult.StartCity; var endCity = fileLoaderResult.EndCity; if (fileLoaderResult.CitiesCoordinates == null || fileLoaderResult.CitiesCoordinates.Count != fileLoaderResult.CityAmount) { return(new AStarResult(0, new List <int>())); } var heuristicValuation = GetHeuristicsValuation(endCity, fileLoaderResult.CitiesCoordinates); var arraysLength = fileLoaderResult.CityAmount + 1; var estimatedDistanceArray = new int[arraysLength]; var distancesArray = new int[arraysLength]; for (int i = 0; i < arraysLength; ++i) { estimatedDistanceArray[i] = distancesArray[i] = int.MaxValue; } var predecessorsArray = new int[arraysLength]; var visitedArray = new bool[arraysLength]; var isAlreadyAddedInCityDistances = new bool[arraysLength]; distancesArray[startCity] = 0; estimatedDistanceArray[startCity] = heuristicValuation[startCity]; var processedCollection = new SortedSet <AlgorithmCity>(new AlgorithmCityComparer()); processedCollection.Add(new AlgorithmCity(startCity, estimatedDistanceArray[startCity])); while (processedCollection.Count != 0) { var currentCity = processedCollection.Min; processedCollection.Remove(currentCity); visitedArray[currentCity.City] = true; if (currentCity.City == endCity) { break; } foreach (var neighbour in incidenceList[currentCity.City]) { var newPossibleDistance = distancesArray[currentCity.City] + neighbour.Distance; if (newPossibleDistance < distancesArray[neighbour.ConnectedCity]) { processedCollection.Remove(new AlgorithmCity(neighbour.ConnectedCity, estimatedDistanceArray[neighbour.ConnectedCity])); isAlreadyAddedInCityDistances[neighbour.ConnectedCity] = false; distancesArray[neighbour.ConnectedCity] = newPossibleDistance; estimatedDistanceArray[neighbour.ConnectedCity] = newPossibleDistance + heuristicValuation[neighbour.ConnectedCity]; predecessorsArray[neighbour.ConnectedCity] = currentCity.City; if (visitedArray[neighbour.ConnectedCity] == false && isAlreadyAddedInCityDistances[neighbour.ConnectedCity] == false) { processedCollection.Add(new AlgorithmCity(neighbour.ConnectedCity, estimatedDistanceArray[neighbour.ConnectedCity])); isAlreadyAddedInCityDistances[neighbour.ConnectedCity] = true; } } } } var path = new List <int>(); path.Add(endCity); var pathReadCurrentCity = predecessorsArray[endCity]; path.Add(pathReadCurrentCity); do { pathReadCurrentCity = predecessorsArray[pathReadCurrentCity]; path.Add(pathReadCurrentCity); } while (pathReadCurrentCity != startCity); return(new AStarResult(distancesArray[endCity], path)); }