Friday, February 19, 2016

Goal Oriented Action Planning

Goal Oriented Action Planning

Introduction
GOAP, or goal oriented action planning is a decision-making architecture for agents in game that allows them to plan a sequence of actions to satisfy a particular goal. It is based on a modification of the STRIPS language which is defined using goals and operators. It was originally implemented by Jeff Orkin for the game F.E.A.R. GOAP is known for the emergent behaviour that spawns from its simple components.

Why GOAP?
Most of the games use finite state machines for implementing AI. With complex AI, the states for FSM agents can become really complex which eventually get to a point where adding new behaviours to the AI is almost impossible and have too many side effects and gaps in the AI. The difficulty in managing the complexity of FSM’s was cited as the primary reason as to why GOAP was chosen for F.E.A.R.

GOAP can turn something like this:


  

Into this:



In GOAP, the actions are not connected to each other. This way, all the actions can be focussed individually making the code modular and easier to maintain.


Important Terms
To learn how GOAP works, first we need to define some important terms.

Ø States
States are used to define a single attribute about the game world surrounding the agent, and the attributes about the agent itself. The states are simply flags representing some information about the world or agent. For example, an agent can have states like LOW AMMO or HAS TARGET.

Ø Worldstates
Worldstates are collection of flags, where each flag represents a state. There are two different use cases for worldstates. First, it defines the current state a NPC is in. Secondly, the user can use it to define a target worldstate. So, the goals for the agents can be expressed as desired worldstates.

Ø Actions
An agent has to build a sequence of actions to reach its desired worldstate or its goal from its current worldstate. Actions have a precondition and post condition. Both the conditions are defined as worldstates. For example, to perform an action SHOOT, the agent current worldstate should define that the agent has a gun, ammo and a target. The post worldstate would result into the target taking damage. All the actions have a cost to them which are calculated dynamically.

Ø Goals
An agent has a list of goals which, as mentioned before, are defined as world states. The agent picks the most relevant goal dynamically from the list of goals according to its current world state, and then basically builds a plan to fulfill that goal.



GOAP Planner
The GOAP planner is the code that starts from the player current worldstate and tries to build a plan to the goal (desired worldstate). To do that, the planner goes through the actions, checks if the precondition for action is met and post condition is making the agent reach closer to its desired goa while keeping cost in mind. An A* Engine is the best way to implement the planner. A generic version of A* can be used for both pathing and planning for GOAP.


Code Implementation
To implement GOAP into the game, you would need to have these important classes or structure.

Ø WorldState structure – use to define current and target worldstates.




Ø Action Class – An abstract class to define different actions.



Ø Goal Class – Abstract class for all Goals.


Ø Goal Manager – Class to update goals, goal relevancies. Calls the planner to                 build a plan. 



Ø Goal Planner – Use the A* engine to build a plan to achieve most relevant goal.



Benefits of GOAP

With GOAP, large series of actions can be created without worrying about connecting them. Actions can easily be added and removed. The dynamic AI from GOAP behaves smarter than normal FSM. For complex character behaviour, GOAP is a great solution for having scalable, maintainable, and reusable decision making systems.