public LogicManager(MainWindow _window) { graphToVisualize = null; citiesLocations = null; citiesConnections = null; defaultCitiesLocations = new CitiesLocations(); defaultCitiesConnections = new CitiesConnections(); algCitiesLocations = new CitiesLocations(); startCity = null; finalCity = null; algSpeed = Alg_Speed.Fast; algHeuristic = Heuristic.Distance; resources = _window.Resources; window = _window; graphManager = new GraphManager(window); algorithm = null; algThread = null; IsRunningAlg = false; }
public bool Equals(CitiesLocations toCompare) { if (toCompare == null) { return(false); } if (locations.Count != toCompare.locations.Count) { return(false); } foreach (KeyValuePair <City, Coordinates> dictPair in locations) { if (!toCompare.locations.ContainsKey(dictPair.Key)) { return(false); } if (!dictPair.Value.Equals(toCompare.locations[dictPair.Key])) { return(false); } } return(true); }
private bool result; // Result of some operation (used in dialogs) #endregion Fields #region Constructors /// <summary> /// Main constructor /// </summary> public OpenGraphWindow() { citiesLocations = null; citiesConnections = null; InitializeComponent(); }
// Main constructor that initializes everything except window public Algorithm(CitiesLocations _citiesLocations, CitiesConnections _citiesConnectios, City _startCity, City _finalCity, Alg_Speed _algSpeed, Heuristic _algHeuristic, ref GraphLayoutCity _graphLayout, ref TextBox _textBoxLog, ResourceDictionary _resourceDictionary, GraphManager _graphManager) { citiesLocations = _citiesLocations; citiesConnecitons = _citiesConnectios; StartCity = _startCity; FinalCity = _finalCity; AlgSpeed = _algSpeed; AlgHeuristic = _algHeuristic; graphLayout = _graphLayout; textBoxLog = _textBoxLog; resourceDictionary = _resourceDictionary; IsRunning = false; Window = null; CanContinue = false; graphManager = _graphManager; }
public GraphCity CreateGraph( CitiesLocations citiesLocations, CitiesConnections citiesConnections) { var graph = new GraphCity(); Coordinates tempStartCityCoordinates, tempEndCityCoordinates; foreach (City city in citiesLocations.locations.Keys) { citiesLocations.locations.TryGetValue(city, out tempStartCityCoordinates); graph.AddVertex(new VertexCity(new City(city), new Coordinates(tempStartCityCoordinates))); } List<City> tempCityConnections = new List<City>(); foreach (City startCity in citiesConnections.connections.Keys) { citiesLocations.locations.TryGetValue(startCity, out tempStartCityCoordinates); citiesConnections.connections.TryGetValue(startCity, out tempCityConnections); foreach (City endCity in tempCityConnections) { citiesLocations.locations.TryGetValue(endCity, out tempEndCityCoordinates); graph.AddEdge(new EdgeCity( new VertexCity(new City(startCity), new Coordinates(tempStartCityCoordinates)), new VertexCity(new City(endCity), new Coordinates(tempEndCityCoordinates)))); } } return (graph); }
private bool result; // Result of some operation (used in dialogs) #endregion Data #region Constructors /// <summary> /// Main constructor /// </summary> public OpenGraphWindow() { citiesLocations = null; citiesConnections = null; InitializeComponent(); }
public GraphCity CreateGraph( CitiesLocations citiesLocations, CitiesConnections citiesConnections) { var graph = new GraphCity(); Coordinates tempStartCityCoordinates, tempEndCityCoordinates; foreach (City city in citiesLocations.locations.Keys) { citiesLocations.locations.TryGetValue(city, out tempStartCityCoordinates); graph.AddVertex(new VertexCity(new City(city), new Coordinates(tempStartCityCoordinates))); } List <City> tempCityConnections = new List <City>(); foreach (City startCity in citiesConnections.connections.Keys) { citiesLocations.locations.TryGetValue(startCity, out tempStartCityCoordinates); citiesConnections.connections.TryGetValue(startCity, out tempCityConnections); foreach (City endCity in tempCityConnections) { citiesLocations.locations.TryGetValue(endCity, out tempEndCityCoordinates); graph.AddEdge(new EdgeCity( new VertexCity(new City(startCity), new Coordinates(tempStartCityCoordinates)), new VertexCity(new City(endCity), new Coordinates(tempEndCityCoordinates)))); } } return(graph); }
// Constructors public AAlgorithm(CitiesLocations newCitiesLocations, CitiesConnections newCitiesConnectios, GraphLayout _graphLayout, int _algSpeed) { citiesLocations = newCitiesLocations; citiesConnecitons = newCitiesConnectios; algSpeed = _algSpeed; graphLayout = _graphLayout; }
public void Copy(CitiesLocations newCitiesLocations) { Reset(); locations = new Dictionary <City, Coordinates>(); Coordinates tempCoordinates; foreach (City city in newCitiesLocations.locations.Keys) { newCitiesLocations.locations.TryGetValue(city, out tempCoordinates); locations.Add(new City(city), new Coordinates(tempCoordinates)); } }
/// <summary> /// Load graph from the file /// </summary> /// <param name="graphLayout"></param> public void LoadGraphFromFile(ref GraphLayoutCity graphLayout) { var openGraphWindow = new OpenGraphWindow(); openGraphWindow.ShowDialog(); if (openGraphWindow.getResult()) { citiesLocations = openGraphWindow.getLocations(); citiesConnections = openGraphWindow.getConnections(); defaultCitiesLocations.Copy(citiesLocations); defaultCitiesConnections.Copy(citiesConnections); CreateGraph(); RefreshGraph(ref graphLayout); EstablishGraphCoordinates(ref graphLayout); } }
public GraphManager graphManager; // Manager for the graph #endregion Data #region Constructors // Null constructor public Algorithm() { citiesLocations = null; citiesConnecitons = null; StartCity = null; FinalCity = null; AlgSpeed = Alg_Speed.Fast; AlgHeuristic = Heuristic.Distance; graphLayout = null; textBoxLog = null; resourceDictionary = null; IsRunning = false; Window = null; CanContinue = false; }
/// <summary> /// Calculates the path between the sequence of cities. /// </summary> public static double CalculateTotalPath(List <City> cities, CitiesLocations citiesLocations) { if (cities.Count < 2) { return(-1); } double pathLength = 0; Coordinates tempCityCoordinates_1, tempCityCoordinates_2; citiesLocations.locations.TryGetValue(cities[0], out tempCityCoordinates_1); for (int i = 1; i < cities.Count; i++) { citiesLocations.locations.TryGetValue(cities[i], out tempCityCoordinates_2); pathLength += Algorithm.FindDistance(tempCityCoordinates_1, tempCityCoordinates_2); tempCityCoordinates_1 = tempCityCoordinates_2; } return(pathLength); }
private TextBox textBoxLog; // Textbox for log #endregion Fields #region Constructors // Null constructor public Algorithm() { citiesLocations = null; citiesConnecitons = null; StartCity = null; FinalCity = null; AlgSpeed = Alg_Speed.Fast; AlgHeuristic = Heuristic.Distance; graphLayout = null; textBoxLog = null; resourceDictionary = null; IsRunning = false; Window = null; CanContinue = false; }
/// <summary> /// Get the data about the current graph's /// node, edges and their coordinates. Save this /// data to citiesLocations and citiesConnections. /// </summary> public void GetCurrentGraph(GraphLayoutCity graphLayout, ref CitiesLocations citiesLocations) { VertexControl vertexControl = new VertexControl(); Coordinates tempVertexCoordinates; int tempXCoord; int tempYCoord; foreach (VertexCity vertex in graphLayout.Graph.Vertices) { // Get the vertex data and a vertex control citiesLocations.locations.TryGetValue(vertex.City, out tempVertexCoordinates); vertexControl = graphLayout.GetVertexControl(vertex); // Get current coordinates tempXCoord = (int)GraphCanvas.GetX(vertexControl); tempYCoord = (int)GraphCanvas.GetY(vertexControl); // Update the information tempVertexCoordinates.setX(tempXCoord); tempVertexCoordinates.setY(tempYCoord); } }
/// <summary> /// Load the graph if all the input data is chosen correctly /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonLoadGraph_Click(object sender, RoutedEventArgs e) { // Get the files' paths string locationsPath = this.textBoxLocations.Text; string connectionsPath = this.textBoxConnections.Text; // Load data if the files are not corrupted if (FileManager.VerifyLocationsFile(locationsPath) && FileManager.VerifyConnectionsFile(connectionsPath)) { citiesLocations = FileManager.ReadLocations(locationsPath); citiesConnections = FileManager.ReadConnections(connectionsPath); result = true; this.Close(); } // If file is corrupted else { result = false; MessageBox.Show("ERROR! Chosen files are corrupted. Choose another files.", "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Set the nodes coordinates to the coordinates from the /// locations file. /// </summary> /// <param name="graphLayout"> /// Graph layout /// </param> /// <param name="citiesLocations"> /// Locations file that has the desired coordinates for the graph's vertices /// </param> public void EstablishCoordinates(ref GraphLayoutCity graphLayout, CitiesLocations citiesLocations) { VertexControl vertexControl = new VertexControl(); Coordinates tempVertexCoordinates; if (citiesLocations != null) { foreach (VertexCity vertex in graphLayout.Graph.Vertices) { // Get the coordinates from the locations file citiesLocations.locations.TryGetValue(vertex.City, out tempVertexCoordinates); // Update the vertex data vertex.CityCoordinates.setX(tempVertexCoordinates.getX()); vertex.CityCoordinates.setY(tempVertexCoordinates.getY()); // Update the vertex position on the screen vertexControl = graphLayout.GetVertexControl(vertex); GraphCanvas.SetX(vertexControl, vertex.CityCoordinates.getX()); GraphCanvas.SetY(vertexControl, vertex.CityCoordinates.getY()); } } }
/* DESCRIPTION: * Reads the cities' locations from the file */ public static CitiesLocations ReadLocations(string filePath) { // Read the data of file exists if (File.Exists(filePath)) { string nextLine; // Read line from the file string[] lineData; // Data that the read line contains char[] delimeter = " ".ToCharArray(); // Delimiter that separates data in the file string tempCityName; // City's name during each iteration int tempXCoord, tempYCoord; // City's coordinates for each city StreamReader inputFile = null; // File to read from CitiesLocations citiesLocations; // Locations of the cities // Exceptions may arise while reading from the file try { // Initialize inputFile = new StreamReader(filePath); citiesLocations = new CitiesLocations(); // Read till the end of the file while ((nextLine = inputFile.ReadLine()) != null) { // If the END flag is reached, then all data has been read if (nextLine.Equals("END")) { return(citiesLocations); } // If it is not the end, then read the next line from the file else { lineData = nextLine.Split(delimeter, 3); // Read the next line tempCityName = lineData[0]; // First data - city's name tempXCoord = Int32.Parse(lineData[1]); // Second - X coordinate tempYCoord = Int32.Parse(lineData[2]); // Third - // Add a new city to the list citiesLocations.locations.Add(new City(tempCityName), new Coordinates(tempXCoord, tempYCoord)); } } // No END flag. Output an error. File is corrupted. MessageBox.Show("ERROR! File is corrupted.", "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } // In case of exception, output its message catch (Exception exc) { MessageBox.Show(exc.ToString(), "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } // Close the input file in the end finally { if (inputFile != null) { inputFile.Close(); } } } // If the input file does not exist, output an error. else { MessageBox.Show("ERROR! File not found.", "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } }
/// <summary> /// Calculates the path between the sequence of cities. /// </summary> public static double CalculateTotalPath(List<City> cities, CitiesLocations citiesLocations) { if (cities.Count < 2) { return (-1); } double pathLength = 0; Coordinates tempCityCoordinates_1, tempCityCoordinates_2; citiesLocations.locations.TryGetValue(cities[0], out tempCityCoordinates_1); for (int i = 1; i < cities.Count; i++) { citiesLocations.locations.TryGetValue(cities[i], out tempCityCoordinates_2); pathLength += Algorithm.FindDistance(tempCityCoordinates_1, tempCityCoordinates_2); tempCityCoordinates_1 = tempCityCoordinates_2; } return (pathLength); }
/* DESCRIPTION: * Reads the cities' locations from the file */ public static CitiesLocations ReadLocations(string filePath) { // Read the data of file exists if (File.Exists(filePath)) { string nextLine; // Read line from the file string[] lineData; // Data that the read line contains char[] delimeter = " ".ToCharArray(); // Delimiter that separates data in the file string tempCityName; // City's name during each iteration int tempXCoord, tempYCoord; // City's coordinates for each city StreamReader inputFile = null; // File to read from CitiesLocations citiesLocations; // Locations of the cities // Exceptions may arise while reading from the file try { // Initialize inputFile = new StreamReader(filePath); citiesLocations = new CitiesLocations(); // Read till the end of the file while ((nextLine = inputFile.ReadLine()) != null) { // If the END flag is reached, then all data has been read if (nextLine.Equals("END")) { return (citiesLocations); } // If it is not the end, then read the next line from the file else { lineData = nextLine.Split(delimeter, 3); // Read the next line tempCityName = lineData[0]; // First data - city's name tempXCoord = Int32.Parse(lineData[1]); // Second - X coordinate tempYCoord = Int32.Parse(lineData[2]); // Third - // Add a new city to the list citiesLocations.locations.Add(new City(tempCityName), new Coordinates(tempXCoord, tempYCoord)); } } // No END flag. Output an error. File is corrupted. MessageBox.Show("ERROR! File is corrupted.", "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); return (null); } // In case of exception, output its message catch (Exception exc) { MessageBox.Show(exc.ToString(), "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); return (null); } // Close the input file in the end finally { if (inputFile != null) { inputFile.Close(); } } } // If the input file does not exist, output an error. else { MessageBox.Show("ERROR! File not found.", "Path Finder", MessageBoxButton.OK, MessageBoxImage.Error); return (null); } }