static int Main(string[] args) { StreamReader sr = new StreamReader(@"text.txt"); int[,] newGrid = Reader(sr); int[,] endGrid = Ft_expectedOutcome(); SearchAlgo ui = new SearchAlgo(); if (!ui.Isolvable(newGrid, dimentions)) { Console.WriteLine("Puzzle is Unsolvable"); Console.ReadLine(); return(0); } else { Console.WriteLine("Puzzle is Solvable"); ASNode puzzle = new ASNode(newGrid, endGrid); List <ASNode> solution = ui.AStar(puzzle); DisplayResults(solution); } Console.ReadLine(); return(1); }
public void MoveUp(int[,] initPuzzle, int i, int j) { if (i > 0) { int[,] pc = new int[dimention, dimention]; CopyPuzzle(pc, initPuzzle); int tmp = pc[i - 1, j]; pc[i - 1, j] = pc[i, j]; pc[i, j] = tmp; ASNode child = new ASNode(pc, goalGrid); //child.h = Heuristic(child); ; neighbours.Add(child); child.previous = this; } }
// Heuristic public static double ManhattonHeuristic(ASNode current) { double Manhatton = 0.0; Spot start; Spot end; for (int x = 0; x < current.dimention; x++) { for (int y = 0; y < current.dimention; y++) { if (current.puzzle[x, y] != current.goalGrid[x, y]) { start = SearchPos(current.puzzle[x, y], current.puzzle); end = SearchPos(current.puzzle[x, y], current.goalGrid); Manhatton += Ft_distance(start, end); } } } return(Manhatton); }
public List <ASNode> AStar(ASNode root) { List <ASNode> PathToSolution = new List <ASNode>(); List <ASNode> openSet = new List <ASNode>(); List <ASNode> closedSet = new List <ASNode>(); ASNode current = null; ASNode tmp = null; ASNode neighbour = null; double tmpG = 0.0; openSet.Add(root); while (openSet.Count > 0) { int winner = 0; for (int x = 0; x < openSet.Count; x++) { if (openSet[x].f < openSet[winner].f) { winner = x; } } current = openSet[winner]; if (current.GoalTest()) { tmp = current; PathToSolution.Add(tmp); while (tmp.previous != null) { PathToSolution.Add(tmp.previous); tmp = tmp.previous; } PathToSolution.Reverse(); break; //return (PathToSolution); } openSet.Remove(current); closedSet.Add(current); //Console.WriteLine("==========Current==============="); //current.PrintPuzzle(); current.ExpandNode(); //Console.WriteLine("===========neighbous======================"); //foreach (ASNode element in closedSet) //{ // element.PrintPuzzle(); //} //Console.WriteLine("================================="); //Console.ReadLine(); foreach (ASNode element in current.neighbours) { // Console.WriteLine("{0}", Isolvable(element.puzzle)); if (element.previous.previous != null) { //Optimization Avoid repeating same moves if (!Ft_same(element.puzzle, element.previous.previous.puzzle)) { neighbour = element; if (!closedSet.Contains(neighbour)) { tmpG = current.g + 1; if (openSet.Contains(neighbour)) { if (tmpG < neighbour.g) { neighbour.g = tmpG; } } else { neighbour.g = tmpG; openSet.Add(neighbour); } neighbour.h = ManhattonHeuristic(neighbour); neighbour.f = neighbour.g + neighbour.h; neighbour.previous = current; } } } else { neighbour = element; if (!closedSet.Contains(neighbour)) { tmpG = current.g + 1; if (openSet.Contains(neighbour)) { if (tmpG < neighbour.g) { neighbour.g = tmpG; } } else { neighbour.g = tmpG; openSet.Add(neighbour); } // neighbour.h = Heuristic(neighbour); neighbour.f = neighbour.g + neighbour.h; neighbour.previous = current; } } } } return(PathToSolution); }