} //End public IDS(puzzle puzzle) /***************METHODS***************/ public List <Node> doTreeSearch() { //Declare variables int curDepth = 0; Direction nextMove = Direction.NULL; //fringe = Insert(Make-Node(Initial-State[problem]), fringe) this.FirstFiveNodesExpanded.Add(copyState(new int[3, 3], this.originalBoardState)); this.TotalNumNodesExpanded += 1; while (!PuzzleBoard.isInGoalState()) { //if fringe is empty then return failure if (this.OrderedFringe.Count <= 0 && this.Fringe.Count <= 0 && TotalNumNodesExpanded > 1) { return(null); } //End if (curDepth >= maxDepth) //node = Remove-Front(fringe) this.Fringe.Clear(); this.Fringe = PuzzleBoard.getMovePositions(); determineNextMove(curDepth); if (this.OrderedFringe.Count > 0 && this.OrderedFringe[0].DepthWhenFound < curDepth - 1) { this.PuzzleBoard = new Puzzle(copyState(new int[3, 3], this.OrderedFringe[0].state)); this.Path = new List <Node>(this.OrderedFringe[0].Path); } //End if (this.OrderedFringe.Count > 0 && this.OrderedFringe[0].DepthWhenFound < curDepth) nextMove = this.PuzzleBoard.determineDirection(this.OrderedFringe[0].TileLocation); //nextMove = determineDirection(this.OrderedFringe[0].TileLocation); if (nextMove == Direction.NULL) { this.PuzzleBoard = new Puzzle(copyState(new int[3, 3], this.OrderedFringe[0].state)); this.PuzzleBoard.setState(this.PuzzleBoard.getReverseDirection(this.OrderedFringe[0].DirUsedToReachTile)); this.Path = new List <Node>(this.OrderedFringe[0].Path); nextMove = this.PuzzleBoard.determineDirection(this.OrderedFringe[0].TileLocation); } //End if (nextMove == Direction.NULL) this.PuzzleBoard.setState(nextMove); curDepth += 1; this.TotalNumNodesExpanded += 1; System.Console.WriteLine("Nodes expanded: " + this.TotalNumNodesExpanded); this.OrderedFringe.RemoveAt(0); if (this.FirstFiveNodesExpanded.Count < 5) { this.FirstFiveNodesExpanded.Add(copyState(new int[3, 3], this.PuzzleBoard.getPuzzleState())); } //End if (numNodesExpanded < 5) this.Path.Add(new Node(this.PuzzleBoard.getPreviousPosition(), this.PuzzleBoard.getTileID(this.PuzzleBoard.getPreviousPosition()), curDepth - 1, this.originalBoardState, nextMove)); //if Goal-Test(problem,State(node)) then return node if (this.PuzzleBoard.isInGoalState()) { break; } //End if (this.PuzzleBoard.isInGoalState()) //fringe = InsertAll(Expand(node, problem), fringe) if (this.TotalNumNodesExpanded >= 100000) { return(null); } //End if (this.TotalNumNodesExpanded >= 100000) } //End while (!PuzzleBoard.isInGoalState()) return(this.Path); } //End public List<Node> doTreeSearch()
} //End public IDS(puzzle puzzle) /***************METHODS***************/ public List <Node> doTreeSearch() { //Declare variables int maxDepth = 0; int curDepth = 0; Direction nextMove = Direction.NULL; //fringe = Insert(Make-Node(Initial-State[problem]), fringe) this.Fringe = PuzzleBoard.getMovePositions(); this.FirstFiveNodesExpanded.Add(copyState(new int[3, 3], this.originalBoardState)); maxDepth += 1; this.TotalNumNodesExpanded += 1; this.FirstFiveNodesExpanded.Add(copyState(new int[3, 3], this.originalBoardState)); this.TotalNumNodesExpanded += 1; while (!PuzzleBoard.isInGoalState()) { //if fringe is empty then return failure if ((curDepth >= maxDepth && this.OrderedFringe.Count <= 0) || (this.OrderedFringe.Count <= 0 && this.Fringe.Count <= 0)) { maxDepth += 1; curDepth = 0; resetPuzzleBoard(); this.Path.Clear(); this.Fringe.Clear(); this.Fringe = PuzzleBoard.getMovePositions(); this.TotalNumNodesExpanded += 1; if (this.FirstFiveNodesExpanded.Count < 5) { this.FirstFiveNodesExpanded.Add(copyState(new int[3, 3], this.originalBoardState)); } //End if (numNodesExpanded < 5) continue; } //End if (curDepth >= maxDepth) //node = Remove-Front(fringe) if (curDepth >= maxDepth) { if (curDepth > this.OrderedFringe[0].DepthWhenFound) { while (curDepth > this.OrderedFringe[0].DepthWhenFound) { curDepth -= 1; this.PuzzleBoard.setState(this.PuzzleBoard.getReverseDirection(this.Path.Last().DirUsedToReachTile)); this.Path.Remove(this.Path.Last()); } //End while (curDepth > this.OrderedFringe[0].DepthWhenFound) } //End if (curDepth > this.OrderedFringe[0].DepthWhenFound) } //End if (curDepth >= maxDepth) determineNextMove(curDepth); nextMove = this.PuzzleBoard.determineDirection(this.OrderedFringe[0].TileLocation); //nextMove = determineDirection(this.OrderedFringe[0].TileLocation); if (nextMove == Direction.NULL) { return(null); } //End if (nextMove == Direction.NULL) this.PuzzleBoard.setState(nextMove); curDepth += 1; this.TotalNumNodesExpanded += 1; this.OrderedFringe.RemoveAt(0); if (this.FirstFiveNodesExpanded.Count < 5) { this.FirstFiveNodesExpanded.Add(copyState(new int[3, 3], this.PuzzleBoard.getPuzzleState())); //this.FirstFiveNodesExpanded.Add(this.PuzzleBoard.getTileID(this.PuzzleBoard.getPreviousPosition())); } //End if (numNodesExpanded < 5) this.Path.Add(new Node(this.PuzzleBoard.getPreviousPosition(), this.PuzzleBoard.getTileID(this.PuzzleBoard.getPreviousPosition()), curDepth - 1, this.PuzzleBoard.getPuzzleState(), nextMove)); //if Goal-Test(problem,State(node)) then return node if (this.PuzzleBoard.isInGoalState()) { break; } //End if (this.PuzzleBoard.isInGoalState()) //fringe = InsertAll(Expand(node, problem), fringe) if (curDepth < maxDepth) { this.Fringe.Clear(); getTotalMovePositions(); //this.Fringe = PuzzleBoard.getMovePositions(); //this.TotalNumNodesExpanded += this.Fringe.Count; } //End if (curDepth < maxDepth) //387420489 if (this.TotalNumNodesExpanded >= 100000) { return(null); } //End if (this.TotalNumNodesExpanded >= 100000) } //End while (!PuzzleBoard.isInGoalState()) return(this.Path); } //End public List<Node> doTreeSearch()