//재귀를 사용해서 찾는다. private void Search(Node_ root, char data, char leftData, char rightData) { //도착 노드가 null이면 종료하고 돌아간다. if (root == null) { return; } //위치를 찾으면 else if (root.data == data) { //'.'이 아닌 경우에 한해서 좌, 우 노드 생성 후 데이터 삽입 if (leftData != '.') { root.left = new Node_(leftData); } if (rightData != '.') { root.right = new Node_(rightData); } } //아직 못찾았고, 탐색할 노드들이 남아있으면 else { Search(root.left, data, leftData, rightData); Search(root.right, data, leftData, rightData); } }
public Node_(Node_ parent, float runningCost, List <Condition> state, GoapAction action) { this.parent = parent; this.runningCost = runningCost; this.state = state; this.action = action; }
public void Inorder(Node_ root) { if (root.left != null) { Inorder(root.left); } Console.Write(root.data); if (root.right != null) { Inorder(root.right); } }
public void Preorder(Node_ root) { Console.Write(root.data); if (root.left != null) { Preorder(root.left); } if (root.right != null) { Preorder(root.right); } }
public void Postorder(Node_ root) { if (root.left != null) { Postorder(root.left); } if (root.right != null) { Postorder(root.right); } Console.Write(root.data); }
/** * Returns true if at least one solution was found. * The possible paths are stored in the leaves list. Each leaf has a * 'runningCost' value where the lowest cost will be the best action * sequence. */ private bool BuildGraph(Node_ parent, List <Node_> leaves, HashSet <GoapAction> usableActions, List <Goal> goals) { bool foundOne = false; foreach (GoapAction action in usableActions) { //if the parent's state (world state) has the conditions for this action's preconditions, we can use it here if (PreconditionsInWorldState(action, action._preconditions, parent.state)) { //parent.state is the world's state in this instance List <Condition> currentState = PopulateState(parent.state, action._effects); Node_ node = new Node_(parent, parent.runningCost + action.cost, currentState, action); if (GoalInState(goals, currentState)) { //if the goal aligns with the state -- //I think this is the constantly changing thing to see if each step satisfies the last leaves.Add(node); foundOne = true; } else { HashSet <GoapAction> subset = actionSubset(usableActions, action); //taking all actions and the current action in this iteration . The current action needs to be removed because it didn't match the goal bool found = BuildGraph(node, leaves, subset, goals); //this time we're redoing this whole thing while taking the useless action out of the equation, but why? What makes this action useless? //(The goal isn't found in this one?) if (found) { foundOne = true; } } } else { Debug.Log(action.ToString() + " Preconditions aren't in the world state"); GoapAgent.prettyPrint(action); } } return(foundOne); }
public void Add(char data, char leftData, char rightData) { if (root == null) { if (data != '.') { root = new Node_(data); } if (leftData != '.') { root.left = new Node_(leftData); } if (rightData != '.') { root.right = new Node_(rightData); } } //최초 상태가 아니면 어디에 들어가야하는지 찾는다. else { Search(root, data, leftData, rightData); } }
//i// /** * Plan what sequence of actions can fulfill the goal. * Returns null if a plan could not be found, or a list of the actions * that must be performed, in order, to fulfill the goal. */ public Queue <GoapAction> Plan(GameObject agent, HashSet <GoapAction> availableActions, List <Condition> worldState, List <Goal> goal) { // reset the actions so we can start fresh with them foreach (GoapAction a in availableActions) { a.doReset(); } // check what actions can run using their checkProceduralPrecondition HashSet <GoapAction> usableActions = new HashSet <GoapAction>(); foreach (GoapAction a in availableActions) { if (a.checkProceduralPrecondition(agent)) { usableActions.Add(a); } else { Debug.Log(a.ToString() + " 's precondition check failed"); GoapAgent.prettyPrint(a); } } // we now have all actions that can run, stored in usableActions // build up the tree and record the leaf nodes that provide a solution to the goal. List <Node_> leaves = new List <Node_>(); //build graph Node_ start = new Node_(null, 0, worldState, null); bool success = BuildGraph(start, leaves, usableActions, goal); if (!success) { //Debug.log("No Plan"); return(null); } Node_ cheapest = null; foreach (Node_ leaf in leaves) { if (cheapest == null) { cheapest = leaf; } else { if (leaf.runningCost < cheapest.runningCost) { cheapest = leaf; } } } //get its node and work back through the parents List <GoapAction> result = new List <GoapAction>(); Node_ n = cheapest; while (n != null) { if (n.action != null) { result.Insert(0, n.action); } n = n.parent; } //we now have this action list in correct order Queue <GoapAction> queue = new Queue <GoapAction>(); foreach (GoapAction a in result) { queue.Enqueue(a); } //hooray we have a plan! return(queue); }