Exemplo n.º 1
0
        public static Node <State> TreeSearchMethod(IProblem <State> problem, IFringe <Node <State> > fringe, Enum method)
        {
            Func <Node <State>, int> calculatePriorityForBestFirstSearch = newNode =>
                                                                           problem.CountOfConflicts(newNode.StateOfNode);

            Func <Node <State>, int> calculatePriorityForAStar = newNode =>
                                                                 problem.CountOfConflicts(newNode.StateOfNode) + newNode.StepsForSolution;

            fringe.SetCompareMethod(ComparePriority);
            fringe.Add(new Node <State>(problem.InitialState, null)); ///tworzy root na stosie

            while (!fringe.IsEmpty)
            {
                var node = fringe.Pop();              //zdjecie ze stosu
                if (problem.IsGoal(node.StateOfNode)) //sprawdzenie zdjetego elementu ze stosu
                {
                    return(node);
                }

                foreach (var actualState in problem.Expand(node.StateOfNode))
                {
                    //foreach-a z możliwymy stanami, to tam sprawdzam czy dany stan z IListy
                    // już nie wystąpił, wywołując OnPathToRoot,
                    if (!node.OnPathToRoot(node.StateOfNode, actualState, problem.Compare))
                    //Wykonuje sie gdy nie ma znalezionego identycznego stanu
                    {
                        var nodeToAdd = new Node <State>(actualState, node);
                        nodeToAdd.Priority = CalculatePriorityMethod(method, calculatePriorityForBestFirstSearch,
                                                                     calculatePriorityForAStar, nodeToAdd);

                        fringe.Add(nodeToAdd);
                        CountOfSteps++;
                    }
                }
            }

            return(null);
        }