Пример #1
0
        private Node <TState, TAction> ChildNode(ISearchProblem <TState, TAction> problem, TAction action)
        {
            var nextState = problem.Result(State, action);

            return(new Node <TState, TAction>(
                       nextState,
                       action,
                       this,
                       problem.PathCost(
                           PathCost, State, action, nextState
                           )
                       ));
        }
Пример #2
0
        public void Step()
        {
            if (IsFinished)
            {
                return;
            }

            SearchNode <TState, TAction> node;

            do
            {
                if (Frontier.IsEmpty())
                {
                    IsFinished = true;
                    return;
                }
                node = Frontier.Pop();
            } while (_explored.ContainsKey(node.State));

            _explored[node.State] = node;
            CurrentState          = node.State;
            var actions = _problem.GetActions(node.State);

            foreach (var action in actions)
            {
                var childState = _problem.DoAction(node.State, action);
                var childCost  = node.PathCost + _problem.PathCost(node.State, action);
                var child      = new SearchNode <TState, TAction>(childState, node, action, childCost);

                if (_explored.ContainsKey(childState) || Frontier.ContainsState(childState))
                {
                    continue;
                }

                if (_problem.IsGoal(childState))
                {
                    CurrentState = childState;
                    IsFinished   = true;
                    IsSolved     = true;
                    // add goal to explored to allow this.getSolutionTo(goal)
                    _explored[childState] = child;
                }
                else
                {
                    Frontier.Push(child);
                }
            }
        }