Beispiel #1
0
 public Node( 
     string state,
     Node parent,
     MoveToAction action,
     int stepCost,
     int estimate
     )
 {
     m_state = state;
     m_parent = parent;
     m_action = action;
     m_pathCost = parent.PathCost + stepCost;
     m_estimateCost = estimate;
 }
        public virtual IEnumerable<MoveToAction> Search( Problem problem )
        {
            List<Node> frontier = new List<Node>();
            HashSet<Node> explored = new HashSet<Node>();

            Node root = new Node( problem.InitialState );
            frontier.Add( root );
            while( frontier.Count > 0 ) {
                Node nodeToExpand = frontier.First();
                frontier.Remove( nodeToExpand );

                //Console.WriteLine( nodeToExpand.State );

                var newNodes = ExpandNode(
                    nodeToExpand,
                    explored,
                    problem
                );

                foreach( Node fn in newNodes ) {
                    var test = fn.Equals( newNodes.First() );
                    if( IsGoalState( fn.State, problem.GoalTest ) ) {

                        return ActionsFromNodes( fn.GetPathFromRoot() );
                    }

                    frontier.Add( fn );
                }

                // This could be costly.  Find better alternative later
                frontier = frontier
                    .OrderByDescending( n => n.PathCost + n.EstimateCost )
                    .Distinct()
                    .ToList();
            }

            return new List<MoveToAction>();
        }
        private IEnumerable<Node> ExpandNode( 
			Node node, 
			HashSet<Node> explored, 
			Problem problem 
		)
        {
            explored.Add( node );

            var childNodes = new List<Node>();
            var actionsFunction = problem.ActionsFunction;
            var resultFunction = problem.ResultFunction;
            var stepCostFunction = problem.StepCostFunction;

            foreach( var action in actionsFunction.GetActions( node.State ) ) {
                string successorState = resultFunction.Result(
                    node.State,
                    action
                );

                int stepCost = stepCostFunction.Cost(
                    node.State,
                    action,
                    successorState
                );
                int estimateCost = m_heuristic.Calculate( successorState );
                var child = new Node(
                    successorState,
                    node,
                    action,
                    stepCost,
                    estimateCost
                );
                if( !explored.Contains( child ) ) {
                    childNodes.Add( child );
                }
            }

            return childNodes;
        }
Beispiel #4
0
        public bool Equals( Node n )
        {
            if( ( object )n == null ) {
                return false;
            }

            return m_state == n.State;
        }