/// <summary>
        /// Moves the DFBnB algorithm forward one step.
        /// </summary>
        /// <returns>Returns the state the alorithm is in after the step, Searching or Ended.</returns>
        internal override State Step()
        {
            // stop condition
            if (openList.IsEmpty())
            {
                return(State.Ended);
            }
            // Check the next node in the queue and pop it
            var currentNode = openList.Pop();

            //do Prune if needed
            if (candidateGoalNode != null && currentNode.f < candidateGoalNode.g)
            {
                AlgPruned++;
                return(State.Searching);
            }
            //Expand what is not pruned
            Expended++;
            //store best candidate if we seeing it
            if (GoalCheckMethod.ValidGoal(currentNode))
            {
                if (candidateGoalNode == null || currentNode.g > candidateGoalNode.g)
                {
                    candidateGoalNode = currentNode;
#if DEBUG
                    Log.WriteLineIf("[AStarMax] Best Candidate:" + candidateGoalNode, TraceLevel.Verbose);
#endif
                }
            }

            foreach (var child in currentNode.Children)
            {
                if (!PrunningMethod.ShouldPrune(child))
                {
                    openList.Push(child);
                    Generated++;
                }
                else
                {
                    ExternalPruned++;
                }
            }

            return(State.Searching);
        }
        internal override State Step()
        {
#if DEBUG
            Log.WriteLineIf($"[StepStart] Open.Count:{OpenList.Count}, Exp:{Expended}, Gen:{Generated}, AlgPruned:{AlgPruned}, ExternalPruned:{ExternalPruned}", TraceLevel.Verbose);
#endif
            // stop condition
            if (openList.IsEmpty())
            {
#if DEBUG
                Log.WriteLineIf($"[StepEnd] Open.Count:{OpenList.Count}, Exp:{Expended}, Gen:{Generated}, AlgPruned:{AlgPruned}, ExternalPruned:{ExternalPruned} - return Ended-EmptyList", TraceLevel.Verbose);
#endif
                return(State.Ended);
            }
            // Check the next best node in OPEN and set it to current
            var currentNode = openList.Pop();

            if (Generated % LogSearchStatusEveryXGenerated == 0)
            {
#if DEBUG
                Log.WriteLineIf($"[AStarMax] OpenListSize:{openList.Count}", TraceLevel.Verbose);
#endif
            }

            //store best candidate if we seeing it
            if (GoalCheckMethod.ValidGoal(currentNode)) ///TODO: if this is a valid goal - dont try to expand id
            {
                if (candidateGoalNode == null || currentNode.g > candidateGoalNode.g)
                {
                    candidateGoalNode = currentNode;
#if DEBUG
                    Log.WriteLineIf("[ValidCandidate] Best Candidate:" + candidateGoalNode, TraceLevel.Verbose);
#endif
                }
            }

            if (candidateGoalNode != null && currentNode.f < candidateGoalNode.g)
            {
#if DEBUG
                Log.WriteLineIf($"[BestCandidate] Open.Count:{OpenList.Count}, Exp:{Expended}, Gen:{Generated}, AlgPruned:{AlgPruned}, ExternalPruned:{ExternalPruned} - return Ended-BestFound!!!", TraceLevel.Verbose);
#endif
                return(State.Ended);
            }
            //Expand current node
            Expended++;
#if DEBUG
            Log.WriteLineIf("[StepExpanding...] ", TraceLevel.Verbose);
#endif
            foreach (var child in currentNode.Children)
            {
#if DEBUG
                if (child.h > currentNode.h)
                {
                    Log.WriteLineIf($"[HEURISTIC NOT CONSISTENT] Parent: {currentNode.h}, Child:{child.GetBitsString()}", TraceLevel.Error);
                }
#endif
#if DEBUG
                Log.WriteLineIf($"[GenerateChild...] {child.GetBitsString()}", TraceLevel.Info);
#endif
                if (!PrunningMethod.ShouldPrune(child))
                {
#if DEBUG
                    Log.WriteLineIf("[Prune] false", TraceLevel.Verbose);
#endif
                    openList.Add(child);
                    Generated++;
                }
                else
                {
#if DEBUG
                    Log.WriteLineIf("[Prune] true", TraceLevel.Verbose);
#endif
                    ExternalPruned++;
                }
            }
#if DEBUG
            Log.WriteLineIf($"[AStarMax.StepEnd] Open.Count:{OpenList.Count}, Exp:{Expended}, Gen:{Generated}, AlgPruned:{AlgPruned}, ExternalPruned:{ExternalPruned} - return Searching", TraceLevel.Verbose);
#endif
            return(State.Searching);
        }
Exemple #3
0
        internal override State Step()
        {
#if DEBUG
            Log.WriteLineIf($"[StepStart] Head.G:{_head.g}, Head.H:{_head.h}", TraceLevel.Verbose);
#endif

            //Have we found the goal?
            if (GoalCheckMethod.ValidGoal(_head))
            {
                if (candidateGoalNode == null)
                {
#if DEBUG
                    Log.WriteLineIf("[GreedyMax.StepEnd GoalFound] GoalFound:" + _head.GetBitsString(), TraceLevel.Verbose);
#endif
                    candidateGoalNode = _head;
                    return(State.Ended);
                }
            }

            // stop condition
            if (_head.Children.Count == 0)
            {
                Log.WriteLineIf("[StepEnd] Head.Children.Count == 0 - return Ended-NoGoalFound", TraceLevel.Verbose);
                return(State.NoGoalFound);
            }

            //Expand head
            Expended++;
#if DEBUG
            Log.WriteLineIf("[StepExpanding...] ", TraceLevel.Verbose);
#endif
            int          best_h = -1;
            List <INode> sameHeuristicChilds = new List <INode>();
            foreach (var child in _head.Children)
            {
#if DEBUG
                Log.WriteLineIf($"[GenerateChild...] {child.GetBitsString()}", TraceLevel.Info);
#endif
                Generated++;
                if (child.h > best_h)
                {
                    best_h = child.h;
                    sameHeuristicChilds = new List <INode>();
                }
                if (child.h == best_h)
                {
                    sameHeuristicChilds.Add(child);
                }
            }
            if (best_h != 0)
            {
                _head = sameHeuristicChilds[rnd.Next(sameHeuristicChilds.Count)];
            }
            else
            {
                for (int i = 0; i < sameHeuristicChilds.Count; i++)
                {
                    if (GoalCheckMethod.ValidGoal(sameHeuristicChilds[i]))
                    {
                        _head = sameHeuristicChilds[i];
                        break;
                    }
                }
            }
#if DEBUG
            Log.WriteLineIf("[GreedyMax.StepEnd] - return Searching", TraceLevel.Verbose);
#endif
            return(State.Searching);
        }