// draws the grid on console public void DrawGrid(GridFile gridList) { for (int i = 0; i < gridList.TotalRows; i++) { for (int j = 0; j < gridList.TotalCol; j++) { foreach (GridList g in gridList.GetGridLists) { if (g.RowNumber == i && g.ColNumber == j && g.BlockType == Blocks.White) { Console.Write("| "); } else if (g.RowNumber == i && g.ColNumber == j && g.BlockType == Blocks.Green) { Console.Write("|G"); } else if (g.RowNumber == i && g.ColNumber == j && g.BlockType == Blocks.Red) { Console.Write("|R"); } else if (g.RowNumber == i && g.ColNumber == j && g.BlockType == Blocks.Grey) { Console.Write("|W"); } else if (g.RowNumber == i && g.ColNumber == j && g.BlockType == Blocks.Path) { Console.Write("|-"); } } } Console.Write("|\n"); } }
public static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Please make sure command is in format <filename> <method>"); } string textFilePath = args[0]; GridFile gridList = new GridFile(textFilePath); Draw draw = new Draw(); draw.DrawGrid(gridList); Console.WriteLine("Choose a method: \nDFS\nBFS\nGBFS\nAS\nID\nBI"); string searchMethod = args[1]; switch (searchMethod.ToUpper()) { case "DFS": Console.WriteLine("Depth First Search"); DFS DFS = new DFS(gridList); break; case "BFS": Console.WriteLine("Breath First Search"); BFS BFS = new BFS(gridList); break; case "GBFS": Console.WriteLine("Greedy Best First Search"); GBFS GBFS = new GBFS(gridList); break; case "AS": Console.WriteLine("A Star"); AStar AStar = new AStar(gridList); break; case "ID": Console.WriteLine("Iterative Deepening"); ID_DFS ID = new ID_DFS(gridList); break; case "BI": Console.WriteLine("BI A*"); BI_AStar BI = new BI_AStar(gridList); break; default: Console.WriteLine("NO method selected"); break; } }
public ID_DFS(GridFile gridList) : base(gridList) { while (!breakloop) { curretNode = InitializePosition(); dfsVisited.Push(curretNode); while (!(dfsVisited.Count == 0) && !breakloop) { curretNode = dfsVisited.Peek(); if (PossibleToMoveUp(curretNode) && GetPath(curretNode).Length <= levelSearch - 1) { MoveUp(curretNode); } else if (PossibleToMoveLeft(curretNode) && GetPath(curretNode).Length <= levelSearch - 1) { Moveleft(curretNode); } else if (PossibleToMoveDown(curretNode) && GetPath(curretNode).Length <= levelSearch - 1) { MoveDown(curretNode); } else if (PossibleToMoveRight(curretNode) && GetPath(curretNode).Length <= levelSearch - 1) { MoveRight(curretNode); } else { RemovePathToGrid(curretNode); dfsVisited.Pop(); } foreach (Node n in dfsVisited) { if (n.blockType == Blocks.Green) { breakloop = true; Console.WriteLine("level {0}", levelSearch); Console.WriteLine("ID_DFS {0}", listOfVisited.Count); ReturnPath(); break; } } draw.DrawGrid(gridList); Console.WriteLine("\n-----------------\n"); } listOfVisited.Clear(); dfsVisited.Clear(); levelSearch++; } }
public DFS(GridFile gridList) : base(gridList) { dfsVisited.Push(curretNode); while (!breakloop) { curretNode = dfsVisited.Peek(); if (PossibleToMoveUp(curretNode)) { MoveUp(curretNode); } else if (PossibleToMoveLeft(curretNode)) { Moveleft(curretNode); } else if (PossibleToMoveDown(curretNode)) { MoveDown(curretNode); } else if (PossibleToMoveRight(curretNode)) { MoveRight(curretNode); } else { RemovePathToGrid(curretNode); dfsVisited.Pop(); } if (curretNode.blockType == Blocks.Green) { breakloop = true; } else { draw.DrawGrid(gridList); Console.WriteLine("\n-----------------\n"); } } Console.WriteLine("DFS {0}", listOfVisited.Count - 1); //bug ReturnPath(); }
public void PathToMeetingPoint(Node forward, Node reverse, GridFile gridList) { Console.WriteLine("\nBI {0}\n", listOfVisited.Count); Console.WriteLine("Meeting Point is Column {0}, Row {1}", forward.currentCol, forward.currentRow); Console.WriteLine("\nPath Forward is: "); pathDirection = GetPath(forward); for (int i = 0; i < pathDirection.Length; i++) { Console.Write("{0}; ", pathDirection[i]); } Console.WriteLine("\n\nPath Backward is: "); pathDirection = GetPath(reverse); for (int i = 0; i < pathDirection.Length; i++) { Console.Write("{0}; ", pathDirection[i]); } }
public BFS(GridFile gridList) : base(gridList) { bfsVisited.Enqueue(InitializePosition()); while (!breakloop) { curretNode = bfsVisited.Dequeue(); if (PossibleToMoveUp(curretNode)) { MoveUp(curretNode); } if (PossibleToMoveLeft(curretNode)) { Moveleft(curretNode); } if (PossibleToMoveDown(curretNode)) { MoveDown(curretNode); } if (PossibleToMoveRight(curretNode)) { MoveRight(curretNode); } //RemovePathToGrid(curretNode); draw.DrawGrid(gridList); Console.WriteLine("---------------"); foreach (Node n in bfsVisited) { if (n.blockType == Blocks.Green) { breakloop = true; } } } Console.WriteLine("BFS {0}", listOfVisited.Count - 1); ReturnPath(); }
public bool breakloop = false; // for breaking while loop public SearchMethod(GridFile gridList) { listOfVisited = new List <Node>(); grid = gridList; curretNode = InitializePosition(); }
public AStar(GridFile gridList) : base(gridList) { targetNode = FindGreenNode(); curretNode.heuristicCost = GetHeuristicCost(curretNode.currentCol, curretNode.currentRow); while (!breakloop) { // no cost can be greater than 55 (11*5) in this case // or more then column * rows in any case int fOfN = grid.TotalCol * grid.TotalRows; tempNodes.Clear(); foreach (Node n in listOfVisited) { if (n.expanded == false) { if ((n.heuristicCost + n.gOfN) < fOfN) { fOfN = (n.heuristicCost + n.gOfN); } } } // found a green node. break loop //tempNodes selects nodes with smallest F(n) that has not been expanded foreach (Node n in listOfVisited) { if (n.heuristicCost == 0) { curretNode = n; breakloop = true; break; } if (((n.heuristicCost + n.gOfN) == fOfN) && (n.expanded == false)) { tempNodes.Enqueue(n); } } foreach (Node n in tempNodes) { if (PossibleToMoveUp(n)) { MoveUp(n); } if (PossibleToMoveLeft(n)) { Moveleft(n); } if (PossibleToMoveDown(n)) { MoveDown(n); } if (PossibleToMoveRight(n)) { MoveRight(n); } n.expanded = true; } draw.DrawGrid(gridList); Console.WriteLine("---------------"); } Console.WriteLine("A Star {0}", listOfVisited.Count); ReturnPath(); }
public BI_AStar(GridFile gridList) : base(gridList) { listOfVisitedTarget = new List <Node>(); targetNode = FindGreenNode(); targetNode.targetHeuristic = GetHeuristicCostForTarget(targetNode.currentCol, targetNode.currentRow); listOfVisitedTarget.Add(targetNode); curretNode.heuristicCost = GetHeuristicCost(curretNode.currentCol, curretNode.currentRow); while (!breakloop) { // no cost can be greater than 55 (11*5) in this case // or more then column * rows in any case int fOfN = grid.TotalCol * grid.TotalRows; tempNodes.Clear(); foreach (Node n in listOfVisited) { if (n.expanded == false) { if ((n.heuristicCost + n.gOfN) < fOfN) { fOfN = (n.heuristicCost + n.gOfN); } } } // found a green node. break loop //tempNodes selects nodes with smallest F(n) that has not been expanded foreach (Node n in listOfVisited) { // if paths dont meet this will still find it if (n.heuristicCost == 0) { curretNode = n; ReturnPath(); breakloop = true; break; } if (((n.heuristicCost + n.gOfN) == fOfN) && (n.expanded == false)) { tempNodes.Enqueue(n); } } foreach (Node n in tempNodes) { //Console.Write("col {0}, row {1} ", n.currentCol, n.currentRow); //Console.WriteLine("h(n) {0}, g(n) {1}, f(n) {2}", n.heuristicCost, n.gOfN, n.heuristicCost + n.gOfN); //Console.ReadLine(); if (PossibleToMoveUp(n)) { MoveUp(n); } if (PossibleToMoveLeft(n)) { Moveleft(n); } if (PossibleToMoveDown(n)) { MoveDown(n); } if (PossibleToMoveRight(n)) { MoveRight(n); } n.expanded = true; } /*movement of target */ targetTempNodes.Clear(); fOfN = grid.TotalCol * grid.TotalRows; foreach (Node n in listOfVisitedTarget) { if (n.expanded == false) { if ((n.targetHeuristic + n.targetGOfN) < fOfN) { fOfN = (n.targetHeuristic + n.targetGOfN); } } } foreach (Node n in listOfVisitedTarget) { if (((n.targetHeuristic + n.targetGOfN) == fOfN) && (n.targetExpanded == false)) { targetTempNodes.Enqueue(n); } } foreach (Node n in targetTempNodes) { if (TargetPossibleToMoveUp(n)) { Node newNode = new Node(); newNode.currentCol = n.currentCol; newNode.currentRow = n.currentRow - 1; newNode.blockType = BlockValue(newNode.currentCol, newNode.currentRow); newNode.parentNode = n; newNode.targetHeuristic = GetHeuristicCostForTarget(newNode.currentCol, newNode.currentRow); newNode.targetGOfN = GetPath(newNode).Length;// use path as g(n) = number of steps newNode.getPathFromParent = Direction.Up; AddPathToGrid(newNode); listOfVisitedTarget.Add(newNode); newNode.targetVisited = true; } if (TargetPossibleToMoveLeft(n)) { Node newNode = new Node(); newNode.currentCol = n.currentCol - 1; newNode.currentRow = n.currentRow; newNode.blockType = BlockValue(newNode.currentCol, newNode.currentRow); newNode.parentNode = n; newNode.targetHeuristic = GetHeuristicCostForTarget(newNode.currentCol, newNode.currentRow); // gbfs & A* newNode.targetGOfN = GetPath(newNode).Length; // A* newNode.getPathFromParent = Direction.Left; AddPathToGrid(newNode); listOfVisitedTarget.Add(newNode); newNode.targetVisited = true; } if (TargetPossibleToMoveDown(n)) { Node newNode = new Node(); newNode.currentCol = n.currentCol; newNode.currentRow = n.currentRow + 1; newNode.blockType = BlockValue(newNode.currentCol, newNode.currentRow); newNode.parentNode = n; newNode.targetHeuristic = GetHeuristicCostForTarget(newNode.currentCol, newNode.currentRow); newNode.targetGOfN = GetPath(newNode).Length; newNode.getPathFromParent = Direction.Down; AddPathToGrid(newNode); listOfVisitedTarget.Add(newNode); newNode.targetVisited = true; } if (TargetPossibleToMoveRight(n)) { Node newNode = new Node(); newNode.currentCol = n.currentCol + 1; newNode.currentRow = n.currentRow; newNode.blockType = BlockValue(newNode.currentCol, newNode.currentRow); newNode.parentNode = n; newNode.targetHeuristic = GetHeuristicCostForTarget(newNode.currentCol, newNode.currentRow); newNode.targetGOfN = GetPath(newNode).Length; newNode.getPathFromParent = Direction.Right; AddPathToGrid(newNode); listOfVisitedTarget.Add(newNode); newNode.targetVisited = true; } n.targetExpanded = true; } // finds the first Node visiter from both side foreach (Node reverse in listOfVisitedTarget) { foreach (Node forward in listOfVisited) { if (reverse.currentCol == forward.currentCol && reverse.currentRow == forward.currentRow) { reverseNode = reverse; forwardNode = forward; breakloop = true; break; } } } draw.DrawGrid(gridList); Console.WriteLine("---------------"); } PathToMeetingPoint(forwardNode, reverseNode, gridList); }