Exemplo n.º 1
0
        public void AStarSearchLP()
        {
            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 AStarFirstSearch(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(63, searchMethod.InspectedNodes);
        }
Exemplo n.º 2
0
 private bool SituationAlreadyCreated(LloydPuzzleSituation situation)
 {
     foreach (LloydPuzzleSituation sit in m_CreatedSituations)
     {
         if (sit == situation)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public double GetHeuristicValue(OKSearchRoom.INode node, IHeuristicSearchProblem searchProblem, OKSearchRoom.ISearchMethod searchMethod)
        {
            double               heuristic     = 0.0;
            LloydPuzzleProblem   puzzleProblem = searchProblem as LloydPuzzleProblem;
            LloydPuzzleSituation sitDest       = puzzleProblem.Destination.Data as LloydPuzzleSituation;
            LloydPuzzleSituation sitNode       = (LloydPuzzleSituation)node.Data;
            int count = sitDest.Dimension * sitDest.Dimension;


            for (int i = 0; i < count; i++)
            {
                heuristic += sitDest.GetDistance(i, sitNode.IndexOf(sitDest[i]));
            }

            return(heuristic);
        }
Exemplo n.º 5
0
 public LloydPuzzleSituation(LloydPuzzleSituation Operand)
 {
     m_Situation = (int[])Operand.m_Situation.Clone();
     m_IndexHole = Operand.m_IndexHole;
 }
Exemplo n.º 6
0
 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);
     }
 }