/// <summary> /// Method: getEuclideanDistance /// Input: reference and to locations /// Returns: double format of euclidean distance /// </summary> public static double getEuclideanDistance(LocationInfo locationReference, LocationInfo locationTo) { double xComponent = Math.Pow((locationReference.xCoOrdinate - locationTo.xCoOrdinate), 2); double yComponent = Math.Pow((locationReference.yCoOrdinate - locationTo.yCoOrdinate), 2); return Math.Sqrt(xComponent + yComponent); }
/// <summary> /// Method: getLocationsInfo /// Input: fileDescriptor /// Returns: list of locations /// </summary> public List<LocationInfo> getLocationsInfo(String fileDescriptor) { String[] parseLines = System.IO.File.ReadAllLines(fileDescriptor); //Read the file line by line String endOfFile = "END"; // variables used for storing the location information in the location bean String[] locs; int x, y; Node node; // to create a list of location objects List<LocationInfo> locations = new List<LocationInfo>(); // iterate over a foreach loop for each line in the file and store the details appropriately foreach (String eachLine in parseLines) { if (!eachLine.Equals(endOfFile)) { // END symbolises the end of file locs = eachLine.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries); node = new Node(locs[0]); x = Int32.Parse(locs[1]); y = Int32.Parse(locs[2]); // after getting the corresponding entries split by space, construct the location object // and add it to the location list LocationInfo locationInfo = new LocationInfo(node, x, y, new CostOfTraversal(0.0, 0.0), null); locations.Add(locationInfo); } } return locations; }
// Constructor with all the default input parameters. public LocationInfo(Node node, int x, int y, CostOfTraversal costOfTraversal, LocationInfo parentNode) { _node = node; xCoOrdinate = x; yCoOrdinate = y; _costOfTraversal = costOfTraversal; _parentNode = parentNode; }
public LocationInfo _startNode, _endNode; // class level variable for the start and end nodes initialized. #endregion Fields #region Constructors // constructor public ShortestDistanceSearchAlgorithm(LocationInfo startNode, LocationInfo endNode, List<LocationInfo> locationsInfo, List<ConnectionInfo> connectionsInfo) { _startNode = startNode; _endNode = endNode; _connectionsInfo = connectionsInfo; _locationsInfo = locationsInfo; _openNodes = new List<LocationInfo>(); _visitedNodes = new List<LocationInfo>(); }
// constructor public FewerLinksSearchAlgorithm(LocationInfo startNode, LocationInfo endNode, List<LocationInfo> locationsInfo, List<ConnectionInfo> connectionsInfo) { _startNode = startNode; _endNode = endNode; _connectionsInfo = connectionsInfo; _locationsInfo = locationsInfo; _openNodes = new List<LocationInfo>(); _visitedNodes = new List<LocationInfo>(); _minLinkPath = ""; }
/// <summary> /// Method: getConnectionsInfo /// Input: file descriptor and the location list /// Returns: list of connections /// </summary> public List<ConnectionInfo> getConnectionsInfo(String fileDescriptor, List<LocationInfo> locations) { String[] parseLines = System.IO.File.ReadAllLines(fileDescriptor); //parse the input file line by line String endOfFile = "END"; // variables used for getting the connections String[] cons; LocationInfo locationVertex, locationLinkedVertex; List<ConnectionInfo> connections = new List<ConnectionInfo>(); // run a for loop for each line and get corresponding details foreach (String eachLine in parseLines) { if (!eachLine.Equals(endOfFile)) { //END represents end of file cons = eachLine.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (locations.FirstOrDefault(item => item._node._node == cons[0]) != null) {// location specific details of the vertex node var vertex = locations.FirstOrDefault(item => item._node._node == cons[0]); int x1 = vertex.xCoOrdinate; int y1 = vertex.yCoOrdinate; // construct a location detail of the vertex locationVertex = new LocationInfo(vertex._node, x1, y1, new CostOfTraversal(0.0, 0.0), null); int edges = Int32.Parse(cons[1]); // get the number of neighbors for a particular node // iterate over a loop and get the location details of the neighbors for (int i = 1; i <= edges; i++) { if (locations.FirstOrDefault(item => item._node._node == cons[1 + i]) != null) { // location specific details of the neighbor node var linkedVertex = locations.FirstOrDefault(item => item._node._node == cons[1 + i]); int x2 = linkedVertex.xCoOrdinate; int y2 = linkedVertex.yCoOrdinate; // construct a location detail of the vertex locationLinkedVertex = new LocationInfo(linkedVertex._node, x2, y2, new CostOfTraversal(0.0, 0.0), null); // calculate the distance between both the nodes and store it in the connections bean double euclideanDistance = getEuclideanDistance(locationVertex, locationLinkedVertex); ConnectionInfo connectionInfo = new ConnectionInfo(vertex._node, linkedVertex._node, euclideanDistance); connections.Add(connectionInfo); // make a list of the various connection beans } } } } } return connections; }
public void enQueueFewerLinks(LocationInfo location) { bool present = false; for (int i = 0; i < genericQueue.Count; i++) { LocationInfo placeHolder = genericQueue[i]; if (placeHolder._costOfTraversal.getCostOfTraversal() >= location._costOfTraversal.getCostOfTraversal()) { genericQueue.Insert(i, location); present = true; break; } } if (!present) { genericQueue.Add(location); } }
public void SearchBasedHeuristic(object sender, EventArgs e) { String locFileName = (String)Session["locationFileName"]; _locationsInfo = _locNodes.getLocationsInfo(_absolutePath + locFileName); List <LocationInfo> _graphicalNodes = _locationsInfo; _graphicalNodes = _locNodes.getLocationsInfo(_absolutePath + locFileName); foreach (RadComboBoxItem excludeNodes in excludeLocationsNodes.Items) { if (!(excludeNodes.Checked == false)) { _graphicalNodes = _graphicalNodes.Where(query => query._node._node != excludeNodes.Text).ToList(); } } double totalCostOfTraversal = 0.0; //convert start and end to location obj _startNode = _graphicalNodes.FirstOrDefault(query => query._node._node == startLocationsList.SelectedItem.Text); _endNode = _graphicalNodes.FirstOrDefault(query => query._node._node == endLocationsList.SelectedItem.Text); String connFileName = (String)Session["connectionFileName"]; _connectionsInfo = _connEdges.getConnectionsInfo(_absolutePath + connFileName, _graphicalNodes); string locationsNodes = (string)Session["locationNodes"]; string connectionsEdges = (string)Session["connectionEdges"]; //Branch to either shortest distance heuristic based on the search heuristic dropdown if (selectHeuristicCombo.SelectedItem.Value == shortestDistance) { ShortestDistanceSearchAlgorithm aStarSearch = new ShortestDistanceSearchAlgorithm(_startNode, _endNode, _locationsInfo, _connectionsInfo); List <LocationInfo> resultantArray = aStarSearch.AStarTraversal(); String nodesAndCost = ""; foreach (LocationInfo locationInfo in resultantArray) { totalCostOfTraversal += locationInfo._costOfTraversal._g; nodesAndCost += ("<br>Node:" + locationInfo._node._node + "| Interim Traversal Cost:" + locationInfo._costOfTraversal._g + "| Cumulative Traversal Cost:" + totalCostOfTraversal); } String finalOutputNodes = ""; foreach (LocationInfo locationInfo in resultantArray) { finalOutputNodes += locationInfo._node._node + ','; nodesAndCost += ("-" + locationInfo._node._node); } finalOutputNodes = finalOutputNodes.TrimEnd(','); nodesAndCost += ("<br>Total Cost of Travel: " + resultantArray[resultantArray.Count - 1]._costOfTraversal._g); nodesAndCost = nodesAndCost.TrimStart('-'); path.Text = nodesAndCost; //Send the output, the locations and connections info so that vis populates the connected graph ScriptManager.RegisterStartupScript(this, typeof(Page), "GraphParameters", String.Format("populateNodesAndEdges('{0}','{1}','{2}');", finalOutputNodes, locationsNodes, connectionsEdges), true); } //Branch to the fewest links heuristic based on the search heuristic dropdown else if (selectHeuristicCombo.SelectedItem.Value == fewestLinks) { FewerLinksSearchAlgorithm aStarSearch = new FewerLinksSearchAlgorithm(_startNode, _endNode, _locationsInfo, _connectionsInfo); String aStarOutput = ""; String finalOutputNodes = ""; String resultWithMinLinks = aStarSearch.searchByLinks(); resultWithMinLinks = resultWithMinLinks.TrimStart(','); resultWithMinLinks = resultWithMinLinks.TrimEnd(','); finalOutputNodes = resultWithMinLinks.TrimEnd(','); int resultWithLinks = resultWithMinLinks.Count(query => query == ','); aStarOutput += ("<br>Path Final:" + resultWithMinLinks); aStarOutput += ("<br>Links travelled:" + resultWithLinks); path.Text = aStarOutput; //Send the output, the locations and connections info so that vis populates the connected graph ScriptManager.RegisterStartupScript(this, typeof(Page), "GraphParameters", String.Format("populateNodesAndEdges('{0}','{1}','{2}');", finalOutputNodes, locationsNodes, connectionsEdges), true); } }
// Method to remove an item from the priority queue public void remove(LocationInfo node) { genericQueue.Remove(node); }
// Method to check whether an element is present in the queue or not public bool exists(LocationInfo node) { return(genericQueue.Contains(node)); }
public void SearchBasedHeuristic(object sender, EventArgs e) { String locFileName = (String)Session["locationFileName"]; _locationsInfo = _locNodes.getLocationsInfo(_absolutePath + locFileName); List<LocationInfo> _graphicalNodes = _locationsInfo; _graphicalNodes = _locNodes.getLocationsInfo(_absolutePath + locFileName); foreach (RadComboBoxItem excludeNodes in excludeLocationsNodes.Items) { if (!(excludeNodes.Checked == false)) _graphicalNodes = _graphicalNodes.Where(query => query._node._node != excludeNodes.Text).ToList(); } double totalCostOfTraversal = 0.0; //convert start and end to location obj _startNode = _graphicalNodes.FirstOrDefault(query => query._node._node == startLocationsList.SelectedItem.Text); _endNode = _graphicalNodes.FirstOrDefault(query => query._node._node == endLocationsList.SelectedItem.Text); String connFileName = (String)Session["connectionFileName"]; _connectionsInfo = _connEdges.getConnectionsInfo(_absolutePath + connFileName, _graphicalNodes); string locationsNodes = (string)Session["locationNodes"]; string connectionsEdges = (string)Session["connectionEdges"]; //Branch to either shortest distance heuristic based on the search heuristic dropdown if (selectHeuristicCombo.SelectedItem.Value == shortestDistance) { ShortestDistanceSearchAlgorithm aStarSearch = new ShortestDistanceSearchAlgorithm(_startNode, _endNode, _locationsInfo, _connectionsInfo); List<LocationInfo> resultantArray = aStarSearch.AStarTraversal(); String nodesAndCost = ""; foreach (LocationInfo locationInfo in resultantArray) { totalCostOfTraversal += locationInfo._costOfTraversal._g; nodesAndCost += ("<br>Node:" + locationInfo._node._node + "| Interim Traversal Cost:" + locationInfo._costOfTraversal._g + "| Cumulative Traversal Cost:" + totalCostOfTraversal); } String finalOutputNodes = ""; foreach (LocationInfo locationInfo in resultantArray) { finalOutputNodes += locationInfo._node._node + ','; nodesAndCost += ("-" + locationInfo._node._node); } finalOutputNodes=finalOutputNodes.TrimEnd(','); nodesAndCost += ("<br>Total Cost of Travel: " + resultantArray[resultantArray.Count - 1]._costOfTraversal._g); nodesAndCost = nodesAndCost.TrimStart('-'); path.Text = nodesAndCost; //Send the output, the locations and connections info so that vis populates the connected graph ScriptManager.RegisterStartupScript(this, typeof(Page), "GraphParameters", String.Format("populateNodesAndEdges('{0}','{1}','{2}');", finalOutputNodes, locationsNodes, connectionsEdges), true); } //Branch to the fewest links heuristic based on the search heuristic dropdown else if (selectHeuristicCombo.SelectedItem.Value == fewestLinks) { FewerLinksSearchAlgorithm aStarSearch = new FewerLinksSearchAlgorithm(_startNode, _endNode, _locationsInfo, _connectionsInfo); String aStarOutput = ""; String finalOutputNodes = ""; String resultWithMinLinks = aStarSearch.searchByLinks(); resultWithMinLinks = resultWithMinLinks.TrimStart(','); resultWithMinLinks = resultWithMinLinks.TrimEnd(','); finalOutputNodes = resultWithMinLinks.TrimEnd(','); int resultWithLinks = resultWithMinLinks.Count(query => query == ','); aStarOutput += ("<br>Path Final:" + resultWithMinLinks); aStarOutput += ("<br>Links travelled:" + resultWithLinks); path.Text = aStarOutput; //Send the output, the locations and connections info so that vis populates the connected graph ScriptManager.RegisterStartupScript(this, typeof(Page), "GraphParameters", String.Format("populateNodesAndEdges('{0}','{1}','{2}');", finalOutputNodes, locationsNodes, connectionsEdges), true); } }
/// <summary> /// Method: getEdgeTraversalCost /// Input: location from and to /// Returns: g(n) + h(n) cost function in double /// </summary> public double getEdgeTraversalCost(LocationInfo locationReference, LocationInfo locationTo) { double xComponent = Math.Pow((locationReference.xCoOrdinate - locationTo.xCoOrdinate), 2); double yComponent = Math.Pow((locationReference.yCoOrdinate - locationTo.yCoOrdinate), 2); return Math.Sqrt(xComponent + yComponent); }
// Method to check whether an element is present in the queue or not public bool exists(LocationInfo node) { return genericQueue.Contains(node); }