public Queue <GAction> Plan(List <GAction> actions, Dictionary <string, int> goal, WorldStates beliefStates) { List <GAction> usableActions = new List <GAction>(); foreach (GAction a in actions) { if (a.IsAchievable()) { usableActions.Add(a); } } List <Node> leaves = new List <Node>(); Node start = new Node(null, 0, GWorld.Instance.GetWorld().GetStates(), beliefStates.GetStates(), 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.cost < cheapest.cost) { cheapest = leaf; } } List <GAction> result = new List <GAction>(); Node n = cheapest; while (n != null) { if (n.action != null) { result.Insert(0, n.action); } n = n.parent; } Queue <GAction> queue = new Queue <GAction>(); foreach (GAction a in result) { queue.Enqueue(a); } Debug.Log("The action plan is : "); foreach (GAction a in queue) { Debug.Log($"Q: {a.actionName} "); } return(queue); }
public Queue <Action> plan(List <Action> actionList, Dictionary <string, int> goalDictionary, WorldStates agentPersonalStates) { //creates a list to store possible paths into List <Node> pathsThatReturnSuccessList = new List <Node>(); Node start = new Node(null, 0.0f, World.Instance.GetWorldStates().GetStates(), agentPersonalStates.GetStates(), null); //pass the first node through to start branching out the graph of plans from bool success = BuildGraph(start, pathsThatReturnSuccessList, actionList, goalDictionary); //if a plan wasn't found if (!success) { return(null); } //if all the plans found, find the one that's cheapest to execute //and use that Node cheapestPath = null; foreach (Node path in pathsThatReturnSuccessList) { //if didnt find a cheaper path cheapest path = path if (cheapestPath == null) { cheapestPath = path; } //if found a cheaper path cheapest = path else if (path._cost < cheapestPath._cost) { cheapestPath = path; } } //list to store our final path List <Action> resultingPlanList = new List <Action>(); //insert cheapest path inside of the resulting plan list - returns null when reach end of node while (cheapestPath != null) { if (cheapestPath._action != null) { resultingPlanList.Insert(0, cheapestPath._action); } cheapestPath = cheapestPath._parentNode; } //make a queue out of the actions in resulting plan list. Queue <Action> queue = new Queue <Action>(); foreach (Action a in resultingPlanList) { //puts action on end of queue queue.Enqueue(a); } //our final queue. return(queue); }