440 likes | 532 Vues
Character Artificial Intelligence CE00875-3. Finite State Automata Lecture 3. 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.
E N D
Character Artificial IntelligenceCE00875-3 Finite State Automata Lecture 3
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
Why State Machines? • The control code for reactive animats 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
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
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
3 Start state 1 Fire Rocket Jump Look (down) 2 4 Rocket jump FSA 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
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 ief(Q,Σ) → Z
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
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
z b b a a y x y c c output symbol b input symbol Moore Machine
Example: Vending Machine • State: description of the internal settings of the machine, e.g. how much money has been depositied and not spent • Finite states: 0, 25, 50, 75, 100, • Rules: determine how inputs can change state
25 25 50 25 001 010 25 ret ret soda 75 soda 0 011 000 ret 100 25 ret 100 100 Example: Vending Machine Inputs 25 = 00 100 = 01 soda = 10 ret = 11
Example: Vending Machine state input new state S2 S1 S0 I0 I1 S2 S1 S0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 0 1 1 1 0 0 0 1 1 0 0 state input new state S2 S1 S0 I0 I1 S2 S1 S0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 0 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 0 0
Example: Vending Machine state input new state S2 S1 S0 I0 I1 S2 S1 S0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 1 0 0 1 0 0 1 0 0 0 X 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 0 1 1 state input new state S2 S1 S0 I0 I1 S2 S1 S0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 1 1 0 0 0 0 1 0 0 1 0 0 0 1 X X X 1 1 0 0 0
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 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
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
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.
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 3. state procedures generate actions Designing A RCS using State Machine
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
Emotive States; Emotive Bot • Emotions and Computers • A Model of Emotions • Emotional Expression for Communication • Affective Model for Bots • Sensations, Emotions, Moods and Feelings • Portraying Emotions in Games • An Extensible Affective System • Processing Sensations & Emotions • The Affective System Put Together
Emotions and Computers • The idea of computers with emotions may seem strange because i) our culture separates emotions and logic and ii) associates computers with logic • Yet the truth is that models of emotions have been built from the earliest days of AI and “affective computing” is by now a well-established subfield • There could be many advantages in making machines cable of having emotions, or at least recognising them in humans and reacting properly • In nouvelle game AI, we are interested in making emotional animats for the same reason as story-tellers – because this will enhance the realism of non-player characters, make them more entertaining and help to more fully engage players with them • A lot of interesting philosophical questions flow from the idea of emotional computers
recruits energy and directs destructive force toward an obstacle releases hormones, tensions muscles and prepares for fight or flight FEAR | ANGER withholds energy, forces withdrawal to safety and produces aversive signal for learning recruits energy and produces reinforcing signal for learning JOY | SORROW causes rejection of the target object or state of affairs/produces aversive signal for learning tolerates target object or state of affairs/produces reinforcing signal for learning ACCEPTANCE | DISGUST flags unexpected event/ directs attention to percepts/ raises heart rate allows preparation for expected event/ tests model of the world ANTICIPATION | SUPRISE A Model of Emotions • Plutchick (1980) described eight primary emotions organised into pairs of states. In this model, it is impossible to experience the two states of a pair at once. The overall emotional status is registered by degrees of arousal of one of each pair • Each state has a degree of intensity and a target object or state of affairs to which it refers • Each state has behavioural implications which betray its “purpose” in helping the organism survive
A Model of Emotions • The Plutchik model allows states to combine to produce compound “flavours” of emotion. Eg sorrow + suprise = disappointment; fear + acceptance = submission, etc. • The model presupposes perceptual, learning and cognitive systems, but says little about them • Since emotions are closely tied to sensors, effectors and bodily interaction in the world, it follows that embodied AI might be useful at providing these as a more complete system than traditional AI models, which had to work on disembodied computers • Generally speaking, affective models of this kind try to mimic the different basic functions of emotions: regulation of behaviour, learning and communication • Plutchik’s model is an example of how emotional states might signal survival-related classes of stimuli and how this could regulate action
Emotional Expression for Communication • Emotion has an important role in dealing with others • Being able to read the emotional state of another human would have powerful survival implications, and hence be selected for in human evolution • Being able to influence the emotional state of another human would also be very useful • Humans signal their emotional states by universal expressions, and we can read these in other humans • It could be useful to have computersor robots do this: - vision systems could read human facial expressions and classify them as emotions - graphical or robot faces could have their features set into universal expressions to signal emotions
Emotional Expression for Communication • Emotions can also be expressed in (and read from) gestures • Over a longer time frame, emotions can be expressed in (and read from) behaviours • Emotions may also be communicated via language: - choice of words may be influenced by emotional state - pitch, tone, speed and loudness of voice carries affective information • This means that an emotional system needs to interact with other behavioural control mechanisms and influence them according to the current emotional state of the system • So research on these areas often overlaps with the study of realistic body animation, including facial expression, mannerisms, natural language generation and speech synthesis
Sensations • Emotions in embodied creatures begin with sensations. In this model, a sensation will be defined as an immediate reaction to a creature’s current situation • They might include surprise, anticipation, disgust, attraction, pain and pleasure • Sensations are visceral “gut reactions” – they happen almost instantaneously, and do not last very long. They may arise from two different sources: • Sensations from Perceptions – The body detects a certain class of stimuli from the environment, which causes an immediate short term reaction eg sensation of surprise if a player or NPC appears suddenly • Sensations from Cognition – A mental state may also cause a sensation. For example, the thought of blood might cause a sensation of disgust
Emotions • Higher-level and longer-lasting affective trends are called emotions in this model. An emotion is a lasting characteristic of a persons affective state • Emotions might include fear, anger, joy and sorrow • Emotions change slowly compared to sensations. Emotions could be triggered by a single sensation, or by multiple sensations • Champardard calls basic dimensions (such as fear, anger etc) primary emotions. Each may have various degrees of intensity, which Champandard calls secondary emotions (?) • Eg the primary emotion of fear might have a low intensity (apprehension) or a high intensity (terror) • As with Plutchik’s model, some might be mutually antagonistic eg joy and sorrow. These are called complementary emotions.
Moods • Now a creature’s mood will be defined as its complete set of emotions at a particular time • This will be represented as a set of primary emotions, each with its own intensity • A positive mood might include acceptance, satisfaction (moderate amount of joy) and awe (high level of surprise) • Moods thus include complex emotions, which are subsets of primary emotions
Feelings • Affective states can refer to an object or event (even one remembered in the past or anticipated in the future) • In Champadard’s model, these are called feelings • Feelings are persistent associations between an emotion and a (class of) object • Examples are cruelty (to dogs), love (of a brother), hatred (of pirates) • Feelings do not depend on the current state of the creature, but may refer to past or present (or even hypothetical) states of affairs
Portraying Emotions in Games • How could an observer see the effects of emotional states in an animat which had them? • Many games (certainly FEAR/Quake2) have little, if any provision for controlling facial expressions well enough to signal emotions that way (The action is usually too fast to see the faces close up anyway) • However, recall that emotions can also be communicated by actions and language • Existing actions can be triggered or changed by affective state. Eg, terror might cause the animat to wail, or depression might cause it walk more slowly than usual • A lack of action might also signify something: eg failing to respond to attack might be a sign of submission. Higher level decisions, even at the strategic level, might also reveal emotions in some circumstances
Portraying Emotions in Games • Existing gestures such as Flipoff() may be used to signify (anger) • Champandard describes a general-purpose interface to the enumerated action functions of the Quake 2 enginevoid Gesture ( const GestureType g ); • Senses indirectly play a role in the portrayal of emotions, because they are closely linked to action – especially in reactive models • A good example is anticipation, which has the effect in real creatures of heightening sensory awareness. The thresholds at which stimuli signifying possible danger are detected are lower than usual in this state • Chat communications can also be used to make language signify emotions eg Say() or SayTeam() functions void Say ( const string& text, const GroupType g = everybody );
Portraying Emotions in Games • Some affective behaviour scenarios: - Joy: dance or jump - Fear: faster but less accurate turns than usual; run faster - Extreme fear: stop thinking and freeze for a while - Disgust: turn away from reference stimulus; say “ugh” - Anticipation: swing gun wildly at any sound; slower walking than usual • Extra touches for very emotive characters - Insult enemies during battle; perform taunting gestures - Run away screaming when the enemy is powerful - Keep a history of conflict, and target enemy based on grudges
An Extensible Affective System • Champandard designs an affective system for FEAR animats that - is based on the model described so far (and the Plutchick psychoevolutionary model) - uses a FEAR finite state automata/machines module - works with other behavioural control architectures (since emotions are only part of a functioning cognitive system) and can be enhanced • Simplified: only models four primary emotions: fear/anger, surprise/anticipation. These are complementary states, so one should observe no more than two at any time • Simplified: each emotion is represented as a binary value: either fully activated or inactive (no secondary emotional levels here) • Simplified: emotional states merely raise or lower the effectiveness of existing reactive behaviours
FEAR’s FSA/FSM Module • The FEAR platform allows both Finite State Automata (recognisers, acceptors, parsers) and Finite State Machines (transducers) to be easily and efficiently implemented • FSA – inputs are a sequence of symbols (eg from an vector of characters) which can trigger the state transitions. The output can be any kind of data, but usually sets a categorical variable (for classification) or a Boolean variable (for acceptance): bool Accept ( const vector<Symbol>& sequence ); SymbolRecognise( const vector<Symbol>& sequence ); • FSM – one output for each input, so you need to either step a single symbol in and out, or else run an entire set of symbols and get a matching output set: Symbol Tick ( const Symbol in ); void Simulate(const vector<Symbol>& in vector<Symbol>& out );
Fear/surprise explosion fs unexpected sound item picked up Fear/anticipation fa Anger/surprise as expected damage Anger/anticipation aa Processing Sensations • Sensations will be simply perceived classes of objects or events in the environment, such as unexpected sounds, blood splats or explosions (cognitive sensations are not addressed in this prototype) • A simple FSA, used as a recogniser, will accept perceptions or sequences of perceptions and move into a state representing the current sensation, at each Think cycle
fear/anticipation fear/surprise fs fa aa as aa fa fs as fa fs as anger/anticipation aa anger/surprise Processing Emotions • The mood of the system may be in any of four states, each consisting of a pair of (non-complementary) emotions. This enforces the constraint that complementary emotions may not be expressed at the same time • The moods are fully interconnected, so sensations from the sensation FSA may move the system from any mood to any other • Each state will output an associated change in the parameters used • to control accuracy and power of actions; and the delay and precision of perceptual functions...
Emotional Output surprise anticipation fear • Accuracy is the randomness or error of an action: 1.0 means the commanded action is performed perfectly; 0.0 means the action is randomised • Power is used to scale the magnitude of the action: 1.0 means the actions have their maximal value 0.0 means the action is frozen • Delay alters perceptions. A delay of n means n seconds pass before a stimuli is recognised by perceptual functions • Precision alters the location of objects in space by perceptual functions. A value of 1 means the position is exact; while 0 means that a large random number is added to the distance and angle anger
emotion FSM sensation FSA cycle of perceptions pattern of parameters affecting all actions and perceptions sensation Affective System Put Together • At each Think cycle a (possibly new) sensation may arise in the sensation FSA if it reaches an end state • New sensations are passed asynchronously to the emotion FSM, which may change state to a new mood • Each mood outputs its characteristic parameters which in turn affect both actions and perceptions
Evaluation of Affective System • This design is (partly) implemented in the Moody animat • The alterations to action and perceptual parameters do make a difference to the animat’s behaviour, but it is subtle and you need observe carefully to see the effects • It would be possible to make the emotional system output more dramatic behaviour - easier to see, but possibly inappropriate at times • Errors in perception and changes in speed of response are realistic emotional effects and do lead to game events which wouldn’t happen otherwise • System is simple to adjust (though not as simple as Ch suggests) • Transitions are all boolean – states do not rise and fall in degree, so no “secondary emotions” are possible. Also the emotions do not change • Emotional states have no “hysteresis”, meaning they can change as fast as sensations – not true to the model
Summary • The recognition and production of emotions in computers is possible, and there are many reasons to wish to do so • In nature, emotions regular behaviour, assist learning and allow communication to other organisms • Plutchick’s model of emotion specifies 8 primary states organised into mutually antagonistic pairs. Each state has behavioural, learning and communication implications • Embodied animats in games are a good test bed for emotions, because they can play out these implications in the game world, and should be more entertaining as a result • But the portrayal of emotions in games can be problematic, because the opportunities for facial expression, gestures etc. could be limited • Champandard defines sensations, emotions, moods and feelings technically, as part of an application of Plutchick’s model to the design of a FEAR animat (Moody) • This model uses a FSA to transform perceptions into sensations • The sensations drive a FSM model of emotional states, each of which modifies the efficiency and speed of perceptions and actions in the Moody animat