예제 #1
0
        private void RecursiveGraphBuilder(ActionGraph.Node lastNode)
        {
            foreach (Condition precondition in lastNode.Action.Preconditions)
            {
                foreach (Action action in usableActionPool)
                {
                    // if an action solves our current need add it to our graph
                    if (action.Effects.Contains(precondition))
                    {
                        var currentNode = new ActionGraph.Node(lastNode, action);

                        // recurse a level deeper if this new action has preconditions to solve
                        if (action.Preconditions != null)
                        {
                            RecursiveGraphBuilder(currentNode);
                        }
                    }
                }
            }
        }
예제 #2
0
        private Plan SolveGraphs()
        {
            var plan = new Plan();

            foreach (var graph in actionGraphs)
            {
                var nullNode = new ActionGraph.Node(null);
                nullNode.Children = graph.RootNodes;
                openNodes.Add(nullNode);
                ActionGraph.Node currentNode = null;
                int currentIndex             = 0;

                while (openNodes.Count != 0)
                {
                    // set current node to the lowest cost node
                    for (int i = 0; i < openNodes.Count; i++)
                    {
                        if (openNodes[i].Cost + openNodes[i].PathCost < currentNode.PathCost + currentNode.Cost)
                        {
                            currentNode  = openNodes[i];
                            currentIndex = i;
                        }
                    }

                    currentNode = openNodes[currentIndex];
                    openNodes.RemoveAt(currentIndex);
                    closedNodes.Add(currentNode);

                    // if we've found a solution we're done
                    if (currentNode.IsLeaf())
                    {
                        finalNode = currentNode;
                        break;
                    }

                    else // process the nodes
                    {
                        foreach (var child in currentNode.Children)
                        {
                            if (closedNodes.Contains(child))
                            {
                                continue;
                            }

                            if (openNodes.Contains(child))
                            {
                                if (child.Cost > currentNode.Cost)
                                {
                                    continue;
                                }
                                else
                                {
                                    openNodes.Add(child);
                                }
                            }
                        }
                    }
                }

                // append our found path onto the plan
                while (currentNode.IsRoot() == false)
                {
                    plan.Actions.Push(currentNode.Action);
                    currentNode = currentNode.Parent;
                }
            }

            return(plan);
        }