Model-Based Design of Game AI

Similar documents
Information System Design IT60105

The Discrete EVent System specification (DEVS) formalism

TRANSITION CONFLICTS DETECTION IN BINARY MODULAR STATECHART DIAGRAMS 1. Grzegorz Łabiak

CS 4120 Lecture 3 Automating lexical analysis 29 August 2011 Lecturer: Andrew Myers. 1 DFAs

The STATEMATE Semantics of Statecharts. Presentation by: John Finn October 5, by David Harel

Theoretical Foundations of the UML Lecture 18: Statecharts Semantics (1)

Embedded Systems 2. REVIEW: Actor models. A system is a function that accepts an input signal and yields an output signal.

The WHILE Hierarchy of Program Schemes is Infinite

New Constructions of Sonar Sequences

Opus: University of Bath Online Publication Store

Expansion formula using properties of dot product (analogous to FOIL in algebra): u v 2 u v u v u u 2u v v v u 2 2u v v 2

Plan for Today and Beginning Next week (Lexical Analysis)

7. Queueing Systems. 8. Petri nets vs. State Automata

UML Model Refactoring

Discrete Dynamics Finite State Machines גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

State modeling. Marlon Dumas. Institute of Computer Science

Structuring Unreliable Radio Networks

SPECIFICATION MODELS. Chapter 3. Overview. Introducing Hierarchy. StateCharts

Description of the ED library Basic Atoms

Lecture 05: High-Level Design with SysML. An Introduction to SysML. Where are we? What is a model? The Unified Modeling Language (UML)

A framework for the timing analysis of dynamic branch predictors

Refinement formal design with sequence diagrams

This is a repository copy of Attributed Graph Transformation via Rule Schemata : Church-Rosser Theorem.

Simulation of Discrete Event Systems

Concentration of magnetic transitions in dilute magnetic materials

Polynomial Degree and Finite Differences

SUFFIX TREE. SYNONYMS Compact suffix trie

Model checking the basic modalities of CTL with Description Logic

Logical Agent & Propositional Logic

The Role of the Graphing Calculator in the Qualitative Analysis of Function

On Equilibria of Distributed Message-Passing Games

Johns Hopkins Math Tournament Proof Round: Automata

Chaos and Dynamical Systems

Embedded Systems 5. Synchronous Composition. Lee/Seshia Section 6.2

Branching Bisimilarity with Explicit Divergence

1 Caveats of Parallel Algorithms

Mathematical Ideas Modelling data, power variation, straightening data with logarithms, residual plots

TESTING is one of the most important parts of the

2nd Exam. First Name: Second Name: Matriculation Number: Degree Programme (please mark): CS Bachelor CS Master CS Lehramt SSE Master Other:

A test sequence selection method for statecharts

Vertical Implementation

Section 8.5. z(t) = be ix(t). (8.5.1) Figure A pendulum. ż = ibẋe ix (8.5.2) (8.5.3) = ( bẋ 2 cos(x) bẍ sin(x)) + i( bẋ 2 sin(x) + bẍ cos(x)).

Termination Problem of the APO Algorithm

CAP Plan, Activity, and Intent Recognition

Non-preemptive multiprocessor scheduling of strict periodic systems with precedence constraints

Tarek S. Ellaithy 1, Alexander Prexl 1, Nicolas Daynac 2. (1) DEA Egypt Branches, (2) Eliis SAS, France. Introduction

Discrete Event Systems Exam

Methodology for Computer Science Research Lecture 4: Mathematical Modeling

Gravitational Model of the Three Elements Theory: Mathematical Explanations

Reversibility of Turing Machine Computations

Design Parameter Sensitivity Analysis of High-Speed Motorized Spindle Systems Considering High-Speed Effects

Cell-based Model For GIS Generalization

Robot Position from Wheel Odometry

Embedded Systems Development

Structuring Unreliable Radio Networks

Let us first give some intuitive idea about a state of a system and state transitions before describing finite automata.

Synthesizing State-Based Object Systems from LSC Specifications

Distributed Timed Automata with Independently Evolving Clocks

The Mean Version One way to write the One True Regression Line is: Equation 1 - The One True Line

Optimal Routing in Chord

EE 144/244: Fundamental Algorithms for System Modeling, Analysis, and Optimization Fall 2016

Automata Theory (2A) Young Won Lim 5/31/18

Actions and Activities

Attributed Graph Transformation via Rule Schemata: Church-Rosser Theorem

Superluminal Hidden Communication as the Underlying Mechanism for Quantum Correlations: Constraining Models

CS188: Artificial Intelligence, Fall 2009 Written 2: MDPs, RL, and Probability

M = (Q,Σ,δ,q 0,F) Σ is the alphabet over which the transitions are defined.

MAD. Models & Algorithms for Distributed systems -- 2/5 -- download slides at

Technical Report: SRI-CSL July Computer Science Laboratory, SRI International, Menlo Park. also. Technical Report: TR-98-09

Travel Grouping of Evaporating Polydisperse Droplets in Oscillating Flow- Theoretical Analysis

Exploring the relationship between a fluid container s geometry and when it will balance on edge

DESIGN SUPPORT FOR MOTION CONTROL SYSTEMS Application to the Philips Fast Component Mounter

The Cellular Automata Formalism and its Relationship to DEVS

Available online at Energy Procedia 100 (2009) (2008) GHGT-9

#A50 INTEGERS 14 (2014) ON RATS SEQUENCES IN GENERAL BASES

Distributed control of "ball on beam" system

Embedded Systems. Embedded Systems

Comments on A Time Delay Controller for Systems with Uncertain Dynamics

Time and Schedulability Analysis of Stateflow Models

Automata, Logic and Games: Theory and Application

Interface Automata with Complex Actions - Extended Version

Interacting Vehicles: Rules of the Game

An Implementation of the Contract Net Protocol Based on Marginal Cost Calculations

Semantics of S.S.M. (Safe State Machine)

Representation theory of SU(2), density operators, purification Michael Walter, University of Amsterdam

Object-Oriented Analysis and Design

1D spirals: is multi stability essential?

Bridging the Semantic Gap Between Heterogeneous Modeling Formalisms and FMI

Automatic Synthesis of Distributed Protocols

4.4 Solving Problems Using

The efficiency of identifying timed automata and the power of clocks

Theory of Computation. Theory of Computation

CS188: Artificial Intelligence, Fall 2009 Written 2: MDPs, RL, and Probability

RATIONAL EXPECTATIONS AND THE COURNOT-THEOCHARIS PROBLEM

SENSE: Abstraction-Based Synthesis of Networked Control Systems

Design Variable Concepts 19 Mar 09 Lab 7 Lecture Notes

Pseudo-automata for generalized regular expressions

Improved cellular models with parallel Cell-DEVS

An object-oriented design process. Weather system description. Layered architecture. Process stages. System context and models of use

Static Program Analysis using Abstract Interpretation

Transcription:

Model-Based Design of Game AI Alexandre Denault, Jörg Kienzle and Hans Vangheluwe School of Computer Science, McGill University Montréal, Canada, H3A 2A7 email: {adenau,joerg,hv}@cs.mcgill.ca KEYORDS Modern Computer Games, Model Compilers, Rhapsody Statecharts, Game AI. ABSTRACT The complexity of modern computer games has increased drastically over the last decades. The need for sophisticated game AI, in particular for Non-Player Characters (NPCs) grows with the demand for realistic games. riting meaningful, consistent, modular, re-useale and efficient AI code is not straightforward. In this article, we suggest to model rather than to code game AI. A variant of Rhapsody Statecharts is proposed as an appropriate modelling formalism. The Tank ars game y Electronic Arts (EA) is introduced to demonstrate our approach in a concrete setting. By modelling a simple AI, it is demonstrated how the modularity of the Rhapsody Statecharts formalism leads quite naturally to layered modelling of game AI. Finally, our Statechart compiler is used to synthesize efficient C++ code for use in the Tank ars main game loop. STATECHARTS Statecharts were introduced y David Harel in 1987 [Har87] as a formalism for visual modelling of the ehaviour of reactive systems. A full definition of the STATEMATE semantics of Statecharts was only pulished in 1996 [HN96]. More recently, with the introduction of UML 2.0, the Rhapsody semantics as descried in [HK04] is more tuned to the modelling of software systems. In this article, we will use a su-set of the Rhapsody Statecharts semantics. At the heart of the Statechart formalism is the notion of discrete states and the transition etween. Statecharts are a discrete-event formalism which means it takes a timed sequence of discrete events as inputs and produces a timed sequence of discrete as output. Internally, the system transitions etween discrete states due to either external or internal events. This leads to a piecewise constant state trajectory inside the system as illustrated in Figure 1. In Figure 2, a simple model is shown with two states start and end. The small arrow pointing to start denotes that state as the default initial state. If the system is in state start and it receives event, Figure 1: Discrete-Event In/State/Out Trajectories Figure 2: Statechart Basic Transition and condition guard evaluates to true, the transition to state end is taken and the side-effect action is executed. Additionally, entry/exit actions are executed whenever a state is entered/exited. All of the parts of event[guard]/action are optional. The special event after( t) indicates that a transition will e taken autonomously after t time units (unless interrupted earlier). Statecharts add hierarchy to the aove asic notion of state automata. Figure 3 shows a composite state s1 with several nested states. Initially, the system will start in nested state s11 as at the top level, s1 is the default state and within s1, s11 is the default state. To understand the nesting, when in a state such as s11, upon arrival of an event such as f, an outgoing transition is looked which is triggered y event f. This lookup is performed traversing all nested states, from the inside outwards. The first matching transition is taken. This approach keeps the semantics deterministic despite the seemingly conflicting f trigger on transitions to oth s13 and s2. hen in state s12, there is

no conflict and event f will take the system to state s2. hen in state s2, event e will take the system to state s1. As the latter is a composite state, the system will transition (after executing s1 s entry action) to the s11, the default state of s1. In addition to hierarchy, Statecharts add orthogonal components and roadcast communication to state automata. In our implementation these features will not e used. The most interesting feature of Rhapsody statecharts is that it allows for a comined description of structure and ehaviour of ojects. This is achieved y adding Statechart ehaviour descriptions to UML Class Diagrams as shown in Figure reffig:classdiagram. The ehaviour of individual ojects (class instances) is descried y the class statechart. For conceptual clarity we require that methods in a class will only have local effects. They can only change the oject s attriutes. All external effects must e modelled in the Statechart. This allows for a clean separation of externally visile, reactive, timed ehaviour from internal (computation) details. Ojects communicate y means of a GEN action which sends an event to a target oject as shown in Figure reffig:classdiagram (myc2->gen(set(size=2))). Events can e handef process(eventqueue): while EventQueue not empty: evt = EventQueue.pop() if CurrentState reacts to evt: t = transition reacting to evt whose guard evaluates to True compute states that will e exited and entered as a result of taking t next = last state to e entered perform exit actions, trigger and enter actions set CurrentState = next def processall(self): for oj,evtq in self.ojectqueues.items(): o.process(evtq) Figure 3: Hierarchy in Statecharts Figure 5: Processing Concurrent Ojects deled asynchronously or synchronously (in which case they are similar to remote method calls). e will mostly use asynchronous message passing. To support concurrency etween ojects, our Statechart compiler will give each oject an event queue. All oject queues will e processed fairly as shown in the pseudo-code in Figure reffig:alg. MODELLING GAME AI Tank ars Figure 4: Modelling Structure and Behaviour In 2005, Electronic Arts announced the EA Tank ars competition 1, in which computer science students compete against each other y writing artificial intelligence (AI) components that control the movements of a tank. In Tank ars, two tanks, oth controlled y an AI, fight a one-on-one attle in a 100 y 100 meter world. Each tank has a set of environment sensors, that sense information aout the tank s remaining life points and fuel, its position, the direction in which it is facing, it s front radar information (what ojects walls or enemy are located within 40 meters in front of the tank), if a tank is hit and from where the shot was fired, and if the tank is standing on top of a fuel or health station. In addition, a tank has a rotating turret with a direction and a second, more powerful radar, mounted on the turret, that detects ostacles at distances up to 60 meters (see Figure 6). The Tank ars simulation is time-sliced (as opposed to discrete-event). Every time slice, the AI component of a tank is given the current state of the world as seen y the tank sensors. The AI then has to decide whether to change the speed of the tank, whether to turn, whether to turn the turret, whether to fire and how far, and whether to refuel or repair, if possile. Each turn lasts 50 milliseconds if the AI does not make a decision when the time limit elapsed, the tank will not move during this time slice. 1 www.info.ea.com/company/company tw.php

R m e te rs v ] i d t h 2 0 m e r s e t T u a n g r r e t R e 6 0 m a d a e t r e r s 3m 5 m e t e r s Figure 6: Tank Input F r o n t R a d a r R a n g e 4 0 m e t e r s Time-slicing vs. Continuous Time As mentioned aove, the simulation in Tank ars is uilt on a time-sliced architecture. Every 50ms, the new state of the environment is sent to the AI component. Statecharts on the other hand are purely event-ased. At the modeling level, as well as when the model is simulated, time is continuous, i.e. infinite time precision is availale. There is no time-slicing: a transition that is laeled with a time delay such as after(t) means that the transition should fire exactly after the time interval t has elapsed, t eing a real numer. Continuous time is most general, and is most appropriate at this level of astraction for several reasons: Modeling freedom: The modeler is not unnecessarily constrained or encumered with implementation issues, ut can focus on the logic of the model. Symolic analysis: Using timed logic it is possile to analyze the model to prove properties. Simulation: Simulation can e done with infinite accuracy (accuracy of real numers on a computer) in simulation environments such as SMV (reference). Reuse: Continuous time is the most general formalism, and can therefore e used in any simulation environment. hen a model is used in a specific environment, actual code has to e synthesized, i.e. the continuous time model has to e mapped to the time model used in the target simulation. In games that are event-ased such a mapping is straightforward. This is however not the case for Tank ars, in which an approximation has to take place: the synthesized code can execute at most once every time-slice. Fortunately, if the time slice is small enough compared to the dynamics of the system to e modeled (such as the motion of a tank), the approximation is acceptale and the resulting simulation close to equivant to a continuous time simulation. id th 2 0 F r o n t R a d a r [ 2 0 r a y s o i d s e t R a y D a t a ( d ) o o l e a n r a y i t h E n e m y ( ) o o l e a n e n e m y F r o n t ( ) o o l e a n e n e m y R i g h t ( ) o o l e a n e n e m y L e f t ( ) o o l e a n w a l l F r o n t ( ) o o l e a n w a l l R i g h t ( ) o o l e a n w a l l L e f t ( ) [ r a y N o E n e m y N o F r o n t R a d a r B e h a v i o r i t h E n e m y ]/ e n e m y F o u n d F r o n t a l l E n e m y S i g h t e d [ \ r a y i t h E n e m y ]/ e n e m y L o s t F r o n t [ r a y i t h a l l ]/ w a l l F o u n d F r o n t a l l S i g h t e d [ \ r a y i t h a l l ]/ w a l l L o s t F r o n t Figure 8: Input Event Generation Bridging the Time-Sliced Event-Driven Gap In order to use event-ased reasoning in a time sliced environment, a ridge etween the two worlds has to e uilt. In a previous section, we descried the semantics of Rhapsody Statecharts where each oject s ehavior is descried in a separate Statechart. e exploit the modularization capaility offered y oject-orientation and define ojects that encapsulate the perceived state of the world. One oject is defined for each sensor. At every time slice, the Tank ars framework calls the C++ function static voidai (const TankAIInput in, TankAIInstructions & out) of the AI oject. The in parameter contains a struct that descries the state of all environment sensors. The function proceeds y storing the new sensor states in the appropriate ojects (see Figure 7). The mapping from time-sliced to event-ased simulation is done at the level of the sensor ojects. If a significant change occurred in the environment, then the sensor should generate a corresponding event. hat kind of changes are significant and should therefore e signalled with an event depends entirely on the AI. A simple AI might only react to coarse grained environment changes, whereas a more complex AI might want to react to each slightest change. The idea is illustrated in this paragraph using the FrontRadar oject. The class definition is given in the left hand side of Figure 8. The actual radar information otained at each time slice is very accurate. In fact, each radar sends out 20 rays, which reflect when they hit an ostacle. During each turn, the reflection data of every ray is made availale to the AI, and the AI stores the complete ray data in the front radar oject y calling setraydata() as shown in Figure 7. Important events for a simple AI are the sighting of an enemy or a wall, or the fact that an enemy or wall is no longer on the radar. The creation of these events is illustrated in the FrontRadarBehavior Statechart in Figure 8. henever event handling takes place (we chose in our implementation to process events at every time slice), the rayithenemy condition is evaluated y calling the corresponding function provided y the

: T a n k a r s : A I : F r o n t R a d a r : g r o u n d S e n s o r : p o s i t i o n S e n s o r A I ( ) s e t R a y D a t a ( d ) s e t G r o u n d ( t ) s e t P o s i t i o n ( p ) class, and if the condition evaluates to true, then the transition fires and a corresponding event is generated. The events from the sensors are then roadcast to other statecharts at higher levels of astraction. Sophisticated AIs might have analyzer ojects, for instance ojects that keep track of the wall configuration in the world, or ojects that track the enemy. These ojects react to sensor events and update their state. In case an important situation is detected, for instance tankentereddeadend, an appropriate event is created and roadcast. At the topmost level of astraction is the oject that defines the high-level strategy of the tank. This oject might define different modes or priorities, for instance following the enemy, or looking for fuel. Modes changes or other high-level command events such as fleefromenemy are sent on to coordinator and planner ojects that take care of the detailed execution of these high-level commands. Finally, actuator ojects update their state when receiving low-level events such as advancefullspeed. After all events have een processed, the state in the actuator ojects is copied into the out struct of the AI function and returned to the Tank ars simulation. The event propagation through the different levels of astraction is illustrated in Figure 9. MODELLING TANK ARS In the sequel, we show a few small parts of our simple Tank ars AI model. All Statecharts are modelled our AToM 3 visual modelling environment [dlva04]. Note that default states are not indicated y a small arrow ut are rather denoted in green. Figure 10 shows the Statechart for the main tank ehaviour. The tank is highly conservative and toggles etween Searching and resting (Stopped) mode. This strategy conserves fuel and turns out to pay off. The autonomous ehaviour gets interrupted when a wall is encountered in which case the tank turns. Figure 11 shows wall detection at work. hile in SearchingForall mode, the default state is allunknown. From any state in SearchingForall, a parametrized UPDATE triggers a transition to an appropriate state reflecting the tank s knowledge aout the proximity to a wall. Figure 12 demonstrates how the different fuel levels are Figure 7: Converting Time Sliced Execution to Events Seeking Stopped AFTER(750) AFTER(500) Searching VIS_ALL_FRONT VIS_ALL_UNKNON Figure 10: Conservative Tank Behaviour SearchingForall UPDATE [isallunknown] allunknown UPDATE [isallleft] allright allleft UPDATE [isallright] allfront Figure 11: all Detection Turning UPDATE [isallfront] dealth with y means of different modes. Similarly, Figure 13 shows how overal tank health is monitored. It also shows how the tank is ultimately destroyed when health ecomes negative. It will e clear from the aove that judicious use of state nesting comined with concurrent ojects allows for concise and easy to understand models. e have compiled the aove models into C++ code with UPDATE [fuellevel <= 75.0 UPDATE [fuellevel <= 50.0 UPDATE [fuellevel <= 25.0 Fuel100 Fuel75 Fuel50 Fuel25 Figure 12: Fuel Level Monitoring

S G P A M y I G O T i m e ž s l i c e d I n p u t C o r r d i n a t o r / e n s o r O j e c t s n a l e r O j e c t s A I B e h a v i o r A c t u a t o r O j e c t s y z P l a n n e r O j e c t s T i m e ž s l i c e d u t p u t F r o n t R a d a r E n e m T r a c k e r M o v e m e n t P l a n n e r T a n k E n g i n e F u e l M o n i t o r T a n k A u n s t e m S S y T u r r e t o r l d a p p e r T a n k ž T u r r e t C o o r d i n a t o r T u r r e t R o t a t o r MonitoringHealth Destroyed UPDATE [health = [100, 75 Health100 Health75 Health50 Health25 UPDATE [ health <= 0 ] UPDATE [health = [75,50[ UPDATE [health = [50,25[ Figure 13: Tank Health Monitoring Figure 9: Event Propagation UPDATE [ health = [25,0[ Figure 14: all Encounter Execution Trace our Statechart compiler. After inserting this code into the Tank ars game (in the AI function), realistic ehaviour is oserved as shown in Figure 14. The figure shows a trace of a scenario where a tank encounters a wall, initiates turning until the wall is no longer in the line of sight, and finally continues on its way. ACKNOLEDGEMENTS Huining Feng uilt the DChart visual modelling environment, simulator and compiler [Fen04]. David Meunier re-used the visual modelling environment and uilt the first prototype of our Rhapsody Statecharts compiler (generating Python code). Both of these efforts formed the asis for the work descried in this paper. Jörg Kienzle and Hans Vangheluwe greatfully acknowledge partial support for this work through their National Sciences and Engineering Research Council of Canada (NSERC) Discovery Grant. REFERENCES [dlva04] Juan de Lara, Hans Vangheluwe, and Manuel Alfonseca. Meta-modelling and graph grammars for multiparadigm modelling in AToM 3. Software and Systems Modeling (SoSyM), 3(3):194 209, August 2004. DOI: 10.1007/s10270-003-0047-5. [Fen04] [Har87] Thomas Huining Feng. DCharts, a formalism for modeling and simulation ased design of reactive software systems. M.Sc. dissertation, School of Computer Science, McGill University, Feruary 2004. David Harel. Statecharts: A visual formalism for complex systems. Science of Computer Programming, 8:231 274, 1987. [HK04] David Harel and Hillel Kugler. The rhapsody semantics of statecharts (or, on the executale core of the uml). LNCS, 3147:325 354, 2004. [HN96] David Harel and Amnon Naamad. The statemate semantics of statecharts. ACM Transactions on Software Engineering and Methodology, 5(4):293 333, Octoer 1996.