public void BreadthFirstSearchLP() { int[] puzzle_start = { 2, 3, 8, 1, 6, 7, 5, 4, 0 }; LloydPuzzleSituation sitStart = new LloydPuzzleSituation(puzzle_start); int[] puzzle_ziel = { 1, 2, 3, 4, 5, 6, 7, 8, 0 }; LloydPuzzleSituation sitEnd = new LloydPuzzleSituation(puzzle_ziel); TreeNode nodeStart = new TreeNode(sitStart); TreeNode nodeEnd = new TreeNode(sitEnd); LloydPuzzleProblem problem = new LloydPuzzleProblem(nodeStart, nodeEnd, false); var searchMethod = new BreadthFirstSearch(problem); searchMethod.Run(); Assert.True(problem.FoundSolution); TreeNode currentNode = problem.Destination as TreeNode; int counter = 0; while (currentNode != null) { ((LloydPuzzleSituation)currentNode.Data).Show(); currentNode = currentNode.Parent; counter++; } Assert.Equal(15, counter); Assert.Equal(1001468, searchMethod.InspectedNodes); }
private bool SituationAlreadyCreated(LloydPuzzleSituation situation) { foreach (LloydPuzzleSituation sit in m_CreatedSituations) { if (sit == situation) { return(true); } } return(false); }
public INode[] GenerateChildren(INode node, int maxCount) { if (node == null) { throw new Exception("LLoydPuzzleProblem: The given node for GenerateChildren() is null."); } if (node is TreeNode == false) { throw new Exception("LLoydPuzzleProblem: The given node for GenerateChildren() has to be a tree node."); } TreeNode parentNode = node as TreeNode; List <INode> generatedNodes = new List <INode>(); LloydPuzzleSituation situation = (LloydPuzzleSituation)node.Data; if (situation.TestDirection(LloydPuzzleSituation.EDirection.Up)) { LloydPuzzleSituation sit = new LloydPuzzleSituation(situation); sit.DoMove(LloydPuzzleSituation.EDirection.Up); CheckOrAddCreatedSituation(parentNode, generatedNodes, sit); } if ((maxCount == 0 || (maxCount > 0 && generatedNodes.Count < maxCount)) && situation.TestDirection(LloydPuzzleSituation.EDirection.Down)) { LloydPuzzleSituation sit = new LloydPuzzleSituation(situation); sit.DoMove(LloydPuzzleSituation.EDirection.Down); CheckOrAddCreatedSituation(parentNode, generatedNodes, sit); } if ((maxCount == 0 || (maxCount > 0 && generatedNodes.Count < maxCount)) && situation.TestDirection(LloydPuzzleSituation.EDirection.Right)) { LloydPuzzleSituation sit = new LloydPuzzleSituation(situation); sit.DoMove(LloydPuzzleSituation.EDirection.Right); CheckOrAddCreatedSituation(parentNode, generatedNodes, sit); } if ((maxCount == 0 || (maxCount > 0 && generatedNodes.Count < maxCount)) && situation.TestDirection(LloydPuzzleSituation.EDirection.Left)) { LloydPuzzleSituation sit = new LloydPuzzleSituation(situation); sit.DoMove(LloydPuzzleSituation.EDirection.Left); CheckOrAddCreatedSituation(parentNode, generatedNodes, sit); } // Liste als Array zurückgeben INode[] result; result = new INode[generatedNodes.Count]; generatedNodes.CopyTo(result); return(result); }
private void CheckOrAddCreatedSituation(TreeNode parentNode, List <INode> generatedNodes, LloydPuzzleSituation sit) { if (m_KeepCreatedSituationsinMemory) { // Schaue nach, ob diese Situation schon einmal generiert wurde // Dies verhindert bei der Tiefensuche eine unendliche Suche. if (!SituationAlreadyCreated(sit)) { TreeNode childNode = new TreeNode(parentNode, sit); generatedNodes.Add(childNode); m_CreatedSituations.Add(sit); } } else { TreeNode childNode = new TreeNode(parentNode, sit); generatedNodes.Add(childNode); } }
public LloydPuzzleSituation(LloydPuzzleSituation Operand) { m_Situation = (int[])Operand.m_Situation.Clone(); m_IndexHole = Operand.m_IndexHole; }