//Given a string decides which algorithm to run public Node search(String typeOfSearch) { Puzzle current = copy(); if (typeOfSearch == "BFS") { BFS bfs = new BFS(); return(bfs.search(current)); } else if (typeOfSearch == "DFS") { DFS dfs = new DFS(); return(dfs.search(current)); } else if (typeOfSearch == "DFSUndo") { DFSUndo dfsU = new DFSUndo(); return(dfsU.search(current)); } else if (typeOfSearch == "IDDFSUndo") { IDDFSUndo iDDFSUndo = new IDDFSUndo(); return(iDDFSUndo.search(current, 20)); } else if (typeOfSearch == "SimpleGreedy") { SimpleGreedy simpleGreedy = new SimpleGreedy(); return(simpleGreedy.search(current)); } else if (typeOfSearch == "UniqueFirstGreedy") { UniqueFirstGreedy greedy = new UniqueFirstGreedy(); return(greedy.solve(current, true)); } else if (typeOfSearch == "AStar") { AStar astar = new AStar(); return(astar.search(current, true)); } else if (typeOfSearch == "UniformCost") { UniformCost uCost = new UniformCost(); return(uCost.search(current)); } else { Debug.Log("Algortithm does not exist"); } return(null); }
//Greedy algoritm function //Returns node with solved puzzle if successfull and null if it does not private Node greedySearch() { while (priorityQueue.Count() != 0) { //Increases the number of visited nodes numNodes++; //Gets the first element of the priority queue Node current = priorityQueue.Dequeue(); //If it finds the solution returns true if (current.puzzle.isComplete()) { Debug.Log("Solved in " + numNodes + " nodes"); return(current); } //Get all the puzzle neighbors by applying all the operators to all the colors foreach (TileType tile in colors) { //Try to move down and if successfull add to queue Puzzle puzzleDown = current.puzzle.copy(); if (puzzleDown.moveDown(tile)) { Node node = new Node(puzzleDown, current, SimpleGreedy.calculatePuzzleScore(puzzleDown)); node.movedTile = tile; node.moveType = MoveType.Down; priorityQueue.Enqueue(node); } //Try to move up and if successfull add to queue Puzzle puzzleUp = current.puzzle.copy(); if (puzzleUp.moveUp(tile)) { Node node = new Node(puzzleUp, current, SimpleGreedy.calculatePuzzleScore(puzzleUp)); node.movedTile = tile; node.moveType = MoveType.Up; priorityQueue.Enqueue(node); } //Try to move left and if successfull add to queue Puzzle puzzleLeft = current.puzzle.copy(); if (puzzleLeft.moveLeft(tile)) { Node node = new Node(puzzleLeft, current, SimpleGreedy.calculatePuzzleScore(puzzleLeft)); node.movedTile = tile; node.moveType = MoveType.Left; priorityQueue.Enqueue(node); } //Try to move right and if successfull add to queue Puzzle puzzleRight = current.puzzle.copy(); if (puzzleRight.moveRight(tile)) { Node node = new Node(puzzleRight, current, SimpleGreedy.calculatePuzzleScore(puzzleRight)); node.movedTile = tile; node.moveType = MoveType.Right; priorityQueue.Enqueue(node); } } } return(null); }