代理行为(Agent Behaviour)

Agent Behaviour

Finite state machines(FSMs)

definition:

  1. The “machine” has a fixed finite set of possible states.
  2. It receives a sequence of inputs.
  3. Each state may have transition.

Programming

  1. Define legal states with a enum.
  2. A switch/case statement on the current state.
  3. For each state:
    Execute some behaviour,
    Check transition conditions and maybe change state.
public class GhostAI : MonoBehaviour { 
	 enum State {scatter, attack, scared, gohome} 
	 State state = State.scatter; 
 
	 void Update () { 
		 switch (state) { 
			 case State.scatter:
			 	moveToHomeCorner(); 
				 if (isAttackWave()) { 
				 state = State.attack; 
				 } else if (seePoweredPacMan()) { 
				 state = State.scared; 
				 } 
				 break;

The state pattern

Above enum/switch example suit for small FSMs.
The state pattern is a more readable/maintainable approach better for larger FSMs.
Basic idea: a state is defined by a class

public class ScatterState : State { 
	public void enter(Ghost ghost) { 
	ghost.reverseDirection(); 
	}
	
	public void execute(Ghost ghost) { 
		ghost.moveToHomeCorner(); 
		if (ghost.isAttackWave()) { 
		ghost.changeState(new AttackState()); 
		} else if (ghost.seePoweredPacMan()) { 
		ghost.changeState(new ScaredState()); 
		} 
	} 
	public void exit(Ghost ghost) {}
}
public class Ghost : MonoBehaviour { 
	public State current; 
	public void Start() { 
	current = new ScatterState(); 
	} 
	public void Update() { 
	current.execute(); 
	} 
	 public void changeState(State next) { 
	current.exit(Agent); 
	current = next; 
	current.enter(Agent); 
	} 
	public void reverseDirection() { ... } 
	public void moveToHomeCorner() { ... } 
	public bool isAttackWave() { return true; } 
	public bool seePoweredPacMan() { return true; } 
}

Extending FSMs

Hierarchical FSMs:
  • states can contain own state machines.
  • Reduces overall complexity of FSMs.
Pushdown Automata:
  • maintain a stack of current states we can push/pop.
  • Provides a history for the FSM of what it was doing, so it can go
    back to an old state that was previously interrupted.
Concurrent FSMs:
  • maintain a state tuple (状态元组).
  • Each part of the state is dealt with by a separate FSM.
  • Allows separation of different behaviours, e.g. moving and firing.

AI Architectures

Principles:

1. Modular AI Systems
  • small modular components
  • reduces duplication
  • can be flexibly recombined
  • reusable in different contexts
2. Hierarchical AI Systems
  • Hierarchical architectures lead to better scaleability
  • High level reasoner does strategy
  • Lower level reasoners fill in the details
  • Adding behaviour affects the complexity of only one reasoner, not the whole hierarchy
3. Persistant Decisions
  • Use a decision stack, e.g. push FSM state on when interrupted, pop
    when done.
4. Shared Knowledge
  • Multiple reasoners can exchange information on a blackboard
  • Allows coordination(协商) between agents or components
  • Store expensive knowledge
  • Store partial solutions
5. AI in the Word
  • Objects in the world can specify available actions/effects.
  • Locations can specify appropriate behaviour.
  • Events can specify appropriate responses.

Behaviour Tree

Unlike FSMs, behaviour trees are not reactive

  • Tree data structure
  • Leaf nodes are conditions or actions (no children)
  • Decoratorseg. Until fail, invert, interrupt, semaphore guard(1 child)
  • Composite nodes(合成节点) combine child behaviours(1+ children)

Return Status:

  • Evaluating a node returns success, failure or running.

Condition Nodes

  • Test some property of the game world
  • A completed test returns success or failure
  • An unfinished test returns running

Action Nodes

  • Change the state of the game world
  • Normally return running or success. Best to catch failure cases with a conditional

Selector Nodes

  • Evaluates each child in turn
  • If a child returns success or running it returns that value
  • Returns failure if all children do

Sequence Nodes

  • Evaluates each child in turn
  • If a child returns failure or running it returns that value
  • Returns success if all children do

Non-Determinism

  • ND-Selector node: choose between children in random order
  • ND-Sequence node: carry out children in random order

Extending Behaviour Trees

Concurrency(并行性)

  • Parallel selector: try all children concurrently and succeed when one has succeeds. Fail when all failed
  • Parallel sequence: try all children concurrently and succeed when all have succeeded. When one fails, request other children terminate, then fail

Contested Resources

Computing resources, e.g. too many NPCs requiring pathfinding
solutions will overload the processor
In-game resources, e.g. too many NPCs sitting at the same table
will look very odd
Multimedia resources, e.g., too many noises at the same time

Semaphores(信号灯)

A behaviour must ask a semaphore whether it can use the given resource, used to solve question about contested resources.

Blackboard architectures avoiding excessive parameterisation

tree = new Root(
	 new Service(0.5f, flipFoo,
		 new Selector(
			 new BlackboardCondition("foo", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART,
			 new Sequence(
				 new Action(() => Debug.Log("foo")),
				 new WaitUntilStopped()
			 	)
			 ),
			 new Sequence(
				 new Action(() => Debug.Log("bar")),
				 new WaitUntilStopped()
			 )
		 )
	 )
);
public void flipFoo() {
tree.Blackboard["foo"] = !tree.Blackboard.Get<bool>("foo");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值