Finite State Machines

Similar documents
Principles of Computer Game Design and Implementation. Lecture 23

State-Driven Agent Design

Lotfi Zadeh (professor at UC Berkeley) wrote his original paper on fuzzy set theory. In various occasions, this is what he said

State-Driven Agent Design

Lecture 3 Finite State Machines

In examining performance Interested in several things Exact times if computable Bounded times if exact not computable Can be measured

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

Engineering program development. Edited by Péter Vass

Pacman. you want to see how the maze was created, open the file named unity_pacman_create_maze.

Lab 4 Due April 18 th

6.111 Lecture # 8. Topics for Today: (as time permits)

Principles of Computer Game Design and Implementation. Revision Lecture

CS 354R: Computer Game Technology

Chapter 4 Defining Classes I

Critters. Critter #2 Attack.ROAR Attack.POUNCE Attack.SCRATCH. Critter #1

Structured Analysis and Structured Design

MODEL BASED TEST DESIGN AT UNITY

CS 134 Programming Exercise 9:

Artificial Intelligence

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Programming Exercise

LOOPS. Repetition using the while statement

Welcome to SENG 480B / CSC 485A / CSC 586A Self-Adaptive and Self-Managing Systems

SOFTWARE DESIGN COSC 4353 / Dr. Raj Singh

VBA Collections A Group of Similar Objects that Share Common Properties, Methods and

CS 405G: Introduction to Database Systems. Database Design II

Decisions in Java IF Statements

Building a Complex Application: Customer Tracker. Table of Contents. Description. Design Features Demonstrated in this Application

Object-Oriented Analysis and Design Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology-Kharagpur

A Simple Example. The Synchronous Language Esterel. A First Try: An FSM. The Esterel Version. The Esterel Version. The Esterel Version

Operating in a Mixed-language Environment Using HDL, C/C++, SystemC and SystemVerilog

Chapter 4: Control structures. Repetition

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Major Assignment: Pacman Game

Finding and Fixing Bugs

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

SOME TYPES AND USES OF DATA MODELS

logical operators and else-if statements

Lecture 2 Finite Automata

Chapter 4: Control structures

COMP12111 Fundamentals of Computer Engineering Paul Nutter Vasilis Pavlidis Comments

1 Process Coordination

Empath. A Modeling Language for Living Beings. White Paper. Jeremy Posner, Nalini Kartha, Sampada Sonalkar, William Mee

Elliotte Rusty Harold August From XML to Flat Buffers: Markup in the Twenty-teens

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Agent Design Example Problems State Spaces. Searching: Intro. CPSC 322 Search 1. Textbook Searching: Intro CPSC 322 Search 1, Slide 1

THE OBJECT-ORIENTED DESIGN PROCESS AND DESIGN AXIOMS (CH -9)

The Stack, Free Store, and Global Namespace

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

The Esterel Language. The Esterel Version. Basic Ideas of Esterel

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Introduction to Scratch

Art 486: Introduction to Interactive Media.

A Game Map Complexity Measure Based on Hamming Distance Yan Li, Pan Su, and Wenliang Li

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

This paper was presented at DVCon-Europe in November It received the conference Best Paper award based on audience voting.

Developing with VMware vcenter Orchestrator. vrealize Orchestrator 5.5.1

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

CS112 Lecture: Defining Instantiable Classes

Recalibrating the Touch Screen

Input part 3: Implementing Interaction Techniques

In-Class Exercises. ETH Zurich. ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven:

Software Service Engineering

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

COMP219: Artificial Intelligence. Lecture 14: Knowledge Representation

CS 378: Computer Game Technology

CS123. Programming Your Personal Robot. Part 2: Event Driven Behavior

The viewer works in Chrome, Edge, Firefox, and Safari. The web address for the workshop is

Reactive and Hybrid Agents. Based on An Introduction to MultiAgent Systems and slides by Michael Wooldridge

XNA Network Protocol Specification, Draft 2

ENGR 102 Engineering Lab I - Computation

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Comparing and Contrasting different Approaches of Code Generator(Enum,Map-Like,If-else,Graph)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

EVENTS AND SIGNALS. Figure 1: Events. kinds of events Signal Event

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

DRAWING ENVIRONMENT DIAGRAMS

Programming Game Al by Example

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

(Refer Slide Time: 1:27)

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Lab 6 Debugging. Objective. Introduction. Prelab

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING

Chapter Goals. Contents LOOPS

Program Modeling Concepts:

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Critter #1 Attack.ROAR random winner #2 wins #1 wins Attack.POUNCE #1 wins random winner #2 wins Attack.SCRATCH #2 wins #1 wins random winner

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

2D Arrays. Lecture 25

Today (1) N64 Controller (Project Checkpoint#1) Today (2) Administrative Info (1)

SQL Maestro and the ELT Paradigm Shift

Specification of Model Behavior

COMP 110 Programming Exercise: Simulation of the Game of Craps

MASTERS THESIS SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR THE DEGREE OF MASTER OF MECHANICAL ENGINEERING

Digital Design and Computer Architecture J. Spjut & M. Spencer

The Bizarre Truth! Automating the Automation. Complicated & Confusing taxonomy of Model Based Testing approach A CONFORMIQ WHITEPAPER

Graph and the World of Brains

(Refer Slide Time: 1:40)

Pac Man Game Programming Language. Reference Manual. Chun Kang Chen (cc3260) Hui Hsiang Kuo (hk2604) Shuwei Cao (sc3331) Wenxin Zhu (wz2203)

SFWR ENG 3S03: Software Testing

Transcription:

Finite State Machines

Finite State Machines (FSMs) An abstract machine that can exist in one of several different and predefined states Defines a set of conditions that determine when the state should change State defines the behaviors to be executed

Finite State Machines (FSMs) Long history of involvement in game AI Ghosts in Pac Man are FSMs Ghosts can roam freely, chase player, evade player The transition between these behaviors are determined by the player s actions Even today, FSMs are still very commonly used Easy to understand, implement, debug Lightweight and efficient

Basic State Machine Model A generic FSM diagram: S nodes are states, t arrows are transitions Each FSM typically models a set of behaviors for a character or group of characters

Basic State Machine Model 4 possible states {Si, S1, S2, S3} Transition functions {t1,t2, t3,t4, t5} Initial state Si, remains in this state until t1 provides a stimulus to switch to S1

Pac Man Ghost FSM 3 possible states: Roam, Chase, Evade Transitions include conditions for remaining in a same state

Pac Man Ghost FSM

Finite State Machine Design Previous sample implementation may not be the most efficient design Efficient design Encourage re-usability Two main components in design 1. Data structures used to store the data associated with the game AI entity 2. Functions for operating transition between states

Finite State Machine Design Structures and Classes Typical to store all game AI-related data in a structure or class You can also store the current AI state (for FSM) class AIEntity { public: int type; int state; int row; int column; int health; int strength; int intelligence; int magic; };

Finite State Machine Design Use some global constants to define the states (which are in integers) #define kroam 1 #define kevade 2 #define kattack 3 #define khide 4

Finite State Machine Design Transition functions Add additional functions that determine how the AI entity should behave class AIEntity { }; public: int type; int state; int row; int column; int health; int strength; int intelligence; int magic; Boolean playerinrange(); int checkhealth();

Finite State Machine Design In your main game loop, constant checking should be done to determine when to change states if ((checkhealth()<kpoorhealth) && (playerinrange()==false)) state=khide; else if (checkhealth()<kpoorhealth) state=kevade; else if (playerinrange()) else state=kattack; state=kroam;

Finite State Machine Design How would the original FSM diagram looked like for this example behavior? if ((checkhealth()<kpoorhealth) && (playerinrange()==false)) state=khide; else if (checkhealth()<kpoorhealth) state=kevade; else if (playerinrange()) else state=kattack; state=kroam;

Example: Ant FSM Description of Ant AI First, the ants will move randomly in their environment in an attempt to locate a piece of food. Once an ant finds a piece of food, it will return to its home position. When it arrives home, it will drop its food and then start a new search for water rather than food. The thirsty ants will roam randomly in search of water. Once an ant finds water, it will resume its search for more food. Returning food to the home position also will result in a new ant emerging from the home position. The ant population will continue to grow so long as more food is returned to the home position. Of course, the ants will encounter obstacles along the way. In addition to the randomly placed food will be randomly placed poison. Naturally, the poison has a fatal effect on the ants. Can you summarize the behavior in an FSM?

Example: Ant FSM

AI Design-to-Implementation Storyboarding Character Design AI Design (FSM) AI Implementation FSMs can model behaviors very easily, but much more work needs to be done earlier for character design Can we use one generic FSM to model the behavior of a few types of AI characters?

Re-using one generic FSM? No, if your AI characters require different set of states and transitions Yes, if they have the same set of states and transitions, but different condition variables E.g. A stronger and matured ant and a junior ant might have the same behaviors (states, transitions) but different requirements for finding food/water, or different resistance levels towards poison.

Limitations of a single FSM A single FSM can have limitations in expressing some behaviors. A common source of difficulty is alarm behaviors, or behaviors that require the AI character to do something urgently at any given state, and to resume back the state later Can you think of a scenario where this might happen?

Example: Cleaning Robot AI Single FSM operates with no problems However, the robot can run low on power and requires recharging Alarm mechanism: Interrupting the normal behavior to respond to something else important

Example: Cleaning Robot AI Adding Get Power state to accommodate the alarm mechanism is not difficult but number of states increase about x2 What if we have a Hide alarm that is another alarm behavior?

Example: Cleaning Robot AI So, rather than combining all the logic into a single FSM, we can separate into several FSMs, arranged in a hierarchy Higher levels of hierarchy can respond to alarm behaviors

Hierarchical State Machine Higher level: Operates the alarm behavior (to get power) Lower level (within the Clean Up mother state): Operates the cleaning up behavior

Hierarchical State Machine H* state: History state that indicates which sub-state (in lower level) should be entered (initially) or resumed (if just returned from the higher level)

Hierarchical State Machine It is possible to implement both FSMs separately, but a lot of switching between the two is required implementation inefficiency Nested hierarchy: We are in more than one state at a time (on different levels), just keep track of multiple levels of states

Cross Hierarchical Transition If the robot has no objects to collect (of found nothing useful), makes sense to go back to charging bay rather than wasting power Transition from lower level state to a higher level state Lower level FSM is reset, with no history record

Weaknesses of FSMs 1. Rigid modeling of behaviors Each character can only have one state at a time straightforward but limited 2. Complex transition conditions can affect efficiency if many conditions need to be checked 3. Behaviors are deterministic Designer pre-determines the actions and behaviors of the character, unlikely for it to respond out side what it was designed to

Addressing them 1. Rigid modeling of behaviors Modeling multiple states in a nested hierarchy might help to construct more complex behaviors 2. Complex transition conditions can affect efficiency Use decision trees in the transitions 3. Behaviors are deterministic Apply fuzzy logic to the transitions