1 / 18

Topic 4 State Machines

Topic 4 State Machines. Why State Machines? What is a State Machine? Finite State Automata Finite State Machines Using FSMs Use in Game AI Advantages and Disadvantages Hierarchical State Machines Reading : Champandard Chapters 38, 41 Links to Finite State Machines on website.

emil
Download Presentation

Topic 4 State Machines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic 4 State Machines Why State Machines? What is a State Machine? Finite State Automata Finite State Machines Using FSMs Use in Game AI Advantages and Disadvantages Hierarchical State Machines Reading: Champandard Chapters 38, 41 Links to Finite State Machines on website ICT219

  2. Why State Machines? • You may have noticed that the control code for the reactive animats we have experimented with in labs – both in FEAR and GunTactyx – is basically just a large (possibly nested) if-elseif-else structure • These work okay for simple behaviour, but can become very difficult to write, read and debug when the animat needs to be more complex • A potentially better model for reactive control is some kind of state machine – though as we will see, it is not perfectly suited to reactive control • State machines are widely used both in games and other kinds of intelligent software, so it is well worth studying

  3. What is a State Machine? • A state machine basically is a way of recognising or generating sequences of events in an orderly fashion • Some sequences are allowed and some are not eg telephone behaviour • Formally, we say that transitions between the states are triggered by a sequence of input symbols from a defined alphabet. • If a sequence of symbols is allowed by the mapping (state transition diagram) then the sequence is recognised, else it is rejected • Alternatively, we might arrange to drive the system to generate output symbols from the states and/or on the transitions

  4. c 3 1 Start state a b a 2 4 Final acceptance state c Finite State Automata • A FSA is a directed graph of nodes connected by arcs called transitions • FSA can be used to represent classes of state-sequences by the connectivity of the transitions. Imagine a sequence of symbols from the alphabet {a,b,c} make the transitions • We always begin with the start state (1 – bold circle) and allow the sequence to control the transitions between states. If we end up in final acceptance state (4 - double circle), the sequence is accepted as a member of the class, (outputs a Boolean T); else, it is rejected • This FSA would accept the sequence abccca, but not the sequence abcbba. So the state machine can enforce a grammar – a certain syntax for expressions in a formal language ICT219

  5. Finite State Automata • Formally, a FSA may be represented by as the quadruple FSA = {Σ, Q, δ ,F} where Σ is an input alphabet of symbols eg {a,b,c} Q is a set of states eg {1,2,3,4} δ is a transition function defined over Σ and Q eg state diagram F is a subset of Q called accepting (terminal) states eg {4} • One state, q0 , has a special meaning as the start state • δ is a function mapping the set of states Q and the input alphabet Σ onto the set of states: f(Q,Σ) → Q This describes the connections in the transition network, as diagrammed by the graph in our example • δ can also be represented as a table as shown on p.513 of the textbook ICT219

  6. 3 Start state 1 Fire Rocket Jump Look (down) 2 4 Rocket jump FSAs and Finite State Machines • Similarly, the following FSA could classify sequences of actions as belonging to a class such as “Rocket jump” • Finite State Machines are different. While a FSA may only produce one output if a sequence of transitions is recognised, a FSM may produce output symbols at each transition, or each state • Therefore, a FSM can be used to transduce a sequence of input symbols into a sequence of output symbols within a certain class • One type of FSM called a Mealy machine outputs symbols at each transition, and are thus suitable for sequential control of actions ICT219

  7. Mealy Machine • So a Mealy machine may be formalised as for FSA, but requires an alphabet of output symbols, and a function to generate them from transitions FSM = {Σ, Q, Z, δ ,λ} where Σ is an input alphabet of symbols eg {a,b,c} Q is a set of states eg {1,2,3,4} Z is an output alphabet of symbols eg {x,y,z} δ is a transition function defined over Σ and Q eg state diagram λ is a function to determine the output symbol Z based on transitions • There is a special start state but no special terminal state • The function λ computes an output symbol Z for each transition given the states Q and the input symbols Z ie f(Q,Σ) → Z ICT219

  8. 3 c/y b/z b/x 1 a/x 2 4 5 c/x c/z b/y output symbol input symbol Mealy Machine ICT219

  9. Moore Machine • A Moore machine has the same definition as a Mealy machine, but the function λ is simpler but less powerful FSM = {Σ, Q, Z, δ ,λ} where Σ is an input alphabet of symbols eg {a,b,c} Q is a set of states eg {1,2,3,4} Z is an output alphabet of symbols eg {x,y,z} δ is a transition function defined over Σ and Q eg state diagram λ is a function associating states Q with outputs Z • The function λ maps states Q directly onto output symbols Z f(Q) → Z ICT219

  10. z b b a a y x y c c output symbol b input symbol Moore Machine ICT219

  11. Using FSMs • There are other types of FSMs too, such as Augmented Transition Networks (ATNs). These are commonly used where efficient parsing of complex patterns of symbols must be recognised, such as in the natural language processing • Patterns of symbols such as abccca which belong to a class acceptable to a FSM are called regular expressions or regular languages. The FSM enforces the grammar of a given expression/language • Such state machines may be used to parse regular expressions making up an artificial language, such as Java or C. • They may also be used to parse sentences in a natural language, such as French. This could produce a syntactical parse tree, a breakdown of the linguistic components such as noun phrase, verb phrase, articles, etc. ICT219

  12. Use in Game AI • Although State Machines are very widely used in actual game code, they are rarely as pure as the FSA or FSM of theory. • You can use FSAs to recognise actions of anotherplayer eg observing enemy basic enemy acts, and recognising sequences as belonging to a strategy, or other high level description for decision-making • You can use FSMs to organise complex sequences of behaviour, by making the transitions be triggered by perceptual events in the world, while arranging for states to order procedures calling action functions

  13. Advantages/Disadvantages of FSMs Pros • Very simple, easy to visualise • Efficient implementation is possible • Theory is well-founded and well understood, algorithms off-the-shelf • Proven popular in computer games and for control of sequences, parsing of artificial and natural languages Cons • Large state machines can become complex to debug • Computationally limited to certain class of problems (regular exp) • The design of a FSM is fixed at design-time, not dynamic • Possible procedural control problems due to sequential chaos, loss of data, and flow of control (see Chapter 38) ICT219

  14. Designing a RCS using State Machine • Begin by listing your 1) inputs (perceptual tests – eg if(sight < 5.0)...) 2) states (eg avoid, wander, attack, flee, etc.) 3) outputs (eg hurl grenade, drop gun, eat food) • Draw a state transition diagram representing your machine • Actions are represented by procedures within the states that call basic output functions in the game eg run(), turn(), watch() etc. • Question: Where should state transitions be processed? Maybe things change while we’re inside a state procedure, so we should test for updates in there as well as ‘outside’? But this leads to redundancy and loss of performance • So, procedures within a state should be atomic – ie once a state has been entered, nothing may interrupt the flow of control for that action

  15. Designing A RCS using State Machine • Ideally, a reactive system has inputs processed in parallel. Can’t do this with a state machine, whose inputs are sequences of symbols - still rely on a sequence of tests to trigger transitions • There is the question of priorities – we need to ensure that high priority tests get done first, so that their transitions can get in first • There is also the problem of loss of data, since events are happening in real time; might get two important events yet we can only process one at a time • Possible solution – keep tests inside a simplified if-elseif-else structure. Have it generate symbols which are then enqueued onto a short queue • A queue is a First In, First Out (FIF0) data structure. At each perceive-act cycle, the next item in the queue is taken off for processing the transitions to arrive at a state.

  16. symbols move in this direction buffer length of 4 If (test1) enqueue(o) else if (test2) enqueue(a) else if (test3) enqueue(f) else enqueue (w) w 1. perceptual test enqueues an input symbol f o a 2. next symbol makes transition in state machine dequeue (next) process state transitions execute state procedure Designing A RCS using State Machine 3. state procedures generate actions

  17. a a b b zoom in zoom out Abstract Refined Hierarchical State Machines • But how can we deal with the problem that complex state machines can be just as difficult to write, debug and modify as other designs • One approach is the concept of hierarchical state machines • Consider the machine at more than one level, with the levels themselves related by transitions • Divide the complexity into parts and have a related set of states - or even a whole nested state machine – regarded from the top level as a single state ICT219

  18. Summary ICT219

More Related