ICT2191 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.

Slides:



Advertisements
Similar presentations
ICT2191 Topic 11 Genetic Algorithm Animat Design of an animat for defensive tactics Representation of Action Sequences Genetic Operators Genetic Algorithm.
Advertisements

Finite-State Machines with No Output Ying Lu
Finite State Automata. A very simple and intuitive formalism suitable for certain tasks A bit like a flow chart, but can be used for both recognition.
Augmented Transition Networks
4b Lexical analysis Finite Automata
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Interaction Modeling for Testing We would generate the test cases based on our understanding of the interactions that may happen. The source is, again,
SM2220 – Class 10 Finite Automata. SM2220 – Class 10 Finite Automata Computation theory Formal language.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
System Design and Analysis
Lecture 6 & 7 System Models.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Topics Automata Theory Grammars and Languages Complexities
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
Grammars, Languages and Finite-state automata Languages are described by grammars We need an algorithm that takes as input grammar sentence And gives a.
Lecture 10 Topics: Sequential circuits Basic concepts Clocks
02/06/05 “Investigating a Finite–State Machine Notation for Discrete–Event Systems” Nikolay Stoimenov.
Introduction to Systems Analysis and Design Trisha Cummings.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 8: Modelling Interactions and Behaviour.
1 COMP541 State Machines Montek Singh Feb 8, 2012.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Lecture 03: Theory of Automata:08 Finite Automata.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
1 111 Computability, etc. Midterm review. Turing machines. Finite state machines. Push down automata. Homework: FSA, PDA, TM problems (can work in teams)
Lecture 07: Formal Methods in SE Finite Automata Lecture # 07 Qaisar Javaid Assistant Professor.
Lecture 1 Computation and Languages CS311 Fall 2012.
1 Computability Five lectures. Slides available from my web page There is some formality, but it is gentle,
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Computabilty Computability Finite State Machine. Regular Languages. Homework: Finish Craps. Next Week: On your own: videos +
Character Artificial Intelligence CE Finite State Automata Finite State Automata Lecture 3.
2. Regular Expressions and Automata 2007 년 3 월 31 일 인공지능 연구실 이경택 Text: Speech and Language Processing Page.33 ~ 56.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Artificial Intelligence for Games Finite State Machines
CS1Q Computer Systems Lecture 11 Simon Gay. Lecture 11CS1Q Computer Systems - Simon Gay 2 The D FlipFlop The RS flipflop stores one bit of information.
School of Computer Science, The University of Adelaide© The University of Adelaide, Control Data Flow Graphs An experiment using Design/CPN Sue Tyerman.
SM2220 – Class 06 Finite Automata. SM2220 – Class 06 Topic in theoretical computing. A subset of computation machines. Closely related to formal language.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Dr Nick Mitchell (Room CM 224)
Finite State Machines (FSM) OR Finite State Automation (FSA) - are models of the behaviors of a system or a complex object, with a limited number of defined.
Digital System Design using VHDL
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 1 Regular Languages Some slides are in courtesy.
Concepts and Realization of a Diagram Editor Generator Based on Hypergraph Transformation Author: Mark Minas Presenter: Song Gu.
Lecture 16: Modeling Computation Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output.
1 COMP541 Finite State Machines - 1 Montek Singh Sep 22, 2014.
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
1 Course Overview Why this course “formal languages and automata theory?” What do computers really do? What are the practical benefits/application of formal.
Finite State Machines Logical and Artificial Intelligence in Games Lecture 3a.
The Church-Turing Thesis Chapter Are We Done? FSM  PDA  Turing machine Is this the end of the line? There are still problems we cannot solve:
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
1 COMP541 Sequential Logic – 2: Finite State Machines Montek Singh Feb 29, 2016.
The Game Development Process: Artificial Intelligence.
Theory of Computation Automata Theory Dr. Ayman Srour.
Finite Automata.
Finite State Machines Dr K R Bond 2009
Lexical analysis Finite Automata
Unified Modeling Language
Graph Coverage for Specifications CS 4501 / 6501 Software Testing
Lecture 2 Introduction to Programming
CO Games Development 2 Week 19 Extensions to Finite State Machines
Theory of Computation Languages.
4b Lexical analysis Finite Automata
Graph Coverage for Specifications CS 4501 / 6501 Software Testing
Dynamic Modeling Lecture # 37.
CSCI1600: Embedded and Real Time Software
4b Lexical analysis Finite Automata
Basic Concepts of Algorithm
EGR 2131 Unit 12 Synchronous Sequential Circuits
What is it? The term "Automata" is derived from the Greek word "αὐτόματα" which means "self-acting". An automaton (Automata in plural) is an abstract self-propelled.
Presentation transcript:

ICT2191 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

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

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

ICT2194 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 c a b c a Final acceptance state Start state

ICT2195 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, q 0, 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

ICT2196 FSAs and Finite State Machines Similarly, the following FSA could classify sequences of actions as belonging to a class such as “Rocket jump” Fire Rocket Jump Look (down) Rocket jump Start state 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

ICT2197 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

ICT2198 Mealy Machine b/z b/x c/z a/x 1 2 b/y c/x c/y input symbol output symbol

ICT2199 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

ICT21910 Moore Machine b b c a x b c a y y z input symbol output symbol

ICT21911 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.

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 another player 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

ICT21913 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)

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

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.

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

ICT21917 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 a b zoom in zoom out a b Abstract Refined

ICT21918 Summary