# How should I handle interrupting the current goal with goal-oriented action planning?

I'm reading about goal-oriented planning for AI, but there I'm having difficulty finding an implementation so that I can address some questions that I have. Specifically, I don't understand how to handle scenarios in which an agent is in the middle of a goal, but then another goal gets calculated as higher priority. For example, suppose I have the following goals/subgoals/actions

MineGold (goal)
|--> MoveToGoldMine (goal)
|    |--> MoveToPosition (action)
|--> SwingPickaxe (action)
|--> TakeGold (action)
|--> DeliverGold (goal)
|    |--> MoveToTownHall (goal)
|    |    |--> MoveToPosition (action)
|    |--> DropGold (action)

AvoidEnemy (goal)
|--> MoveToHidingSpot (goal)
|    |--> MoveToPosition (action)
|--> Wait (action)

AttackEnemmy (goal)
|--> MoveToEnemy (goal)
|    |--> MoveToPosition (action)


When the game starts, my agent calculates its goal priorities and its highest priority is the MineGold goal. So it pushes the list of subgoals and actions for MineGold onto its planning stack.

Suppose the agent moves to the gold mine, mines the gold, takes the gold, and then starts to move back to the town hall. While it's moving back to the town hall, an enemy soldier gets close enough such that the AvoidEnemy goal becomes higher priority than the MineGold goal. So now the agent pushes the AvoidEnemy subgoals and actions into its stack, ahead of the remaining DeliverGold subgoal.

While the agent is avoiding the soldier, an enemy assassin sneaks up on his team's commander. The commander calls for help so the AttackEnemy goal becomes the highest priority. So AttackEnemy is pushed onto the stack ahead of AvoidEnemy.

Eventually the assassin is killed, so the AttackEnemy goal is no longer valid and gets popped off the stack. Now the topmost goal is AvoidEnemy to avoid the enemy soldier. But if my agent just attacked the assassin, it would be weird for him to then start running away from the soldier (or maybe the soldier is already far away and I don't want to keep moving to a hiding spot), so I would rather him return to his original goal of DeliverGold.

Here are my questions:

1. Am I correct in assuming that I should be periodically recalculating the highest priority goal and switching to the highest priority goal if it's different than the existing goal?
2. Am I correct that when a new goal becomes higher priority than the current goal, then I should be pushing that new goal onto the stack ahead of the current goal, as opposed to overwriting the current goal with the new goal?
3. Assuming I'm correct that new goals are additive, how do I handle this situation where sometimes I don't want an agent to return to certain prior goals? I know that I could accomplish this through a set of "mutually exclusive" flags or something, but that seems like an inelegant way and that it would start to return me to using finite state machines instead of goal-oriented planning.

Am I correct in assuming that I should be periodically recalculating the highest priority goal and switching to the highest priority goal if it's different than the existing goal?

Yes. If you have an agent with multiple goals, you need to keep their priorities updated, and make sure the agent is on the highest priority one.

Am I correct that when a new goal becomes higher priority than the current goal, then I should be pushing that new goal onto the stack ahead of the current goal, as opposed to overwriting the current goal with the new goal?

This works. It does not have to be this way.

Assuming I'm correct that new goals are additive, how do I handle this situation where sometimes I don't want an agent to return to certain prior goals? I know that I could accomplish this through a set of "mutually exclusive" flags or something, but that seems like an inelegant way and that it would start to return me to using finite state machines instead of goal-oriented planning.

When you pop your goal, you check if it is still valid. You need to know when goals are valid. And don't bother with invalid goals. You don't need to update the priority of a goal if it is not valid, you just ignore the goal.

The system you describe is not what I would call goal-oriented. Perhaps I'm confusing terms here. Are you hard-coding plans?

For instance, I would expect that "DeliverGold" would be a goal, not "MineGold". I would expect the AI to be able to find a plan that involves a mine. But it does not have to involve a mine. Perhaps it can pick some gold from the ground if there was any.

And, no, you would not put it on a stack. If something else has higher priority, you drop the plan, and move to the new goal. If, eventually, the priority of the original goal is the higher again, then it makes a new plan. Perhaps by picking some gold from the ground that it had dropped because it was doing a plan for a different goal that required dropping the gold to pick something else.

So, if the goal is to deliver gold, the AI will find that the precondition is to have gold. So the agent can search all action that can give gold, then it will find that gold mines can give gold. Then it checks the preconditions of those actions. And it will find that it needs a pick tool. Then it searches all actions that can give a pick tool, and so on. Until it reaches an action that it can perform right away. BY doing this, the AI agent can create its own plan. This example describes backtracking, other search approaches also work.

That does not mean that your approach does not work. You just need to add checks for goals being valid. If the enemy is too far away, then don't make "AvoidEnemy" a valid goal.

You might also be interested in smart objects.

• Thank you for the answer. It's very helpful. When you asked "am I hard coding plans", this was also part of my confusion. I'm not trying to hard-code anything, but I may be doing something of a hybrid between goal-oriented planning and behavior trees. The reason for that is that I've been having trouble finding a thorough step-by-step explanation of goal-oriented planning, and the explanations that I have seen break each goal into a series of subgoals. Do you recommend that I break each subgoal of MineGold into individual goals and calculate the priority of each of those during updates? – Ben Rubin Jun 22 at 12:29
• @BenRubin no, don't break into goals with priorities. Perhaps my explanation helps. I want to point that the part about prediction other agents gets hard, goal-oriented AI by pretends it is the only active thing in the world. I also want to recommend the video Building the AI of F.E.A.R. with Goal Oriented Action Planning | AI 101. By the way, your solution reminds of the AI of the Sims, which is not goal-oriented, but still an interesting approach. Search for Needs-Based AI. – Theraot Jun 22 at 12:46
• @BenRubin Ah, by the way, in my linked explanation, I went with the assumption that the agent has only one goal. However, yes, you can have multiple goals, and you can a different system giving priorities to them. The linked video shows that. Now, for the goal the AI will make a plan, as long as the current goal does not change, and the plan remains valid, the AI will should keep at it. Giving priorities to the steps of the plan is not compatible with that. – Theraot Jun 22 at 12:56
• Thank you, I appreciate all your help. I'll take a look through those links. – Ben Rubin Jun 22 at 13:17