TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... ·...

28
STATECHARTS MODELLING OF TANK TANK TANK TANK WARS WARS WARS WARS School of Computer Science McGill University, Montréal, Canada COMP 763 / Silvia Mur Blanch [Project Presentation]

Transcript of TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... ·...

Page 1: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

STATECHARTS

MODELLING OF

TANKTANKTANKTANK WARSWARSWARSWARSTANKTANKTANKTANK WARSWARSWARSWARS

School of Computer ScienceMcGill University, Montréal, Canada

COMP 763 / Silvia Mur Blanch

[Project Presentation]

Page 2: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

2

� Recap

� The static part: the controller

� Designing the tank components’ statecharts

� Recreating the simulation environment

Overview

Statecharts modelling of Tank Wars

� Recreating the simulation environment

� Summary

Page 3: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recap3

� EA TankWars: AI programming contest, C++ environment

� AI specification should be done at a higher lever of abstraction: modelled instead of coded (reusability)

� Since models define reactions to game events, event-based formalisms seem to be the most appropriate

Statecharts modelling of Tank Wars

� Formalisms:

� State / event based

� Autonomous / reactive behaviour

� Notion of time

� Modularity

� Availability of simulators / code generators

� Time-sliced Vs. Event-based

Page 4: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

4

� Recap

� The static part: the controller

� Designing the tank components’ statecharts

� Recreating the simulation environment

Overview

Statecharts modelling of Tank Wars

� Recreating the simulation environment

� Summary

Page 5: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

The static part: the controller5

� One big statechart (digital watch) Vs. many small statecharts

� Defining each component in different statecharts means event broadcasting is not possible

Statecharts modelling of Tank Wars

� Necessity to use controller: it will “contain” all the tank’s components and link them

Page 6: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

The static part: the controller6

� Defining each component of the tank with a statechart,e.g.:

FuelTank

Statecharts modelling of Tank Wars

start

COMPOSITE STATEinit

Page 7: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

The static part: the controller7

� Event “start” to begin the simulation of the statecharts,(pass controller as parameter) e.g.:

print “fuelTank start”

ctl=[PARAMS]

� Defining an instance of each component / statechart of

Statecharts modelling of Tank Wars

� Defining an instance of each component / statechart ofthe tank, e.g.:

self.fuelTank = FuelTank.FuelTank()

self.fuelTank.initModel()self.fuelTank.event(“start”, self)

� Adding variables to the tank’s components, e.g.:self.fuelTank.fuelLevel = MAX_TANK_FUEL

Page 8: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

The static part: the controller8

� Tank’s components generate events in other components(statecharts) by using “narrow cast”, e.g.:

� FuelTank

Ctl.fuelTank.fuelLevel < 100

Statecharts modelling of Tank Wars

� Action:

ctl.fuelTank.fuelLow = True

ctl.pilotStrategy.event("fuelLow")

fuelLevelOk fuelLow

Ctl.fuelTank.fuelLevel < 100

Page 9: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

� Recap

� The static part: the controller

� Designing the tank components’ statecharts

� Recreating the simulation environment

9

Overview

� Recreating the simulation environment

� Summary

Statecharts modelling of Tank Wars

Page 10: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts10

� Announcements (sensors)

Statecharts modelling of Tank Wars

Page 11: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts11

� FuelTank (sensors)

Statecharts modelling of Tank Wars

Page 12: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts12

� InRangeDetector (analyzers)

Statecharts modelling of Tank Wars

Page 13: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts13

� EnemyTracker (memorizers)

Statecharts modelling of Tank Wars

Page 14: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts14

� ObstacleMap (memorizers)

Statecharts modelling of Tank Wars

Page 15: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts15

� PilotStrategy (strategic deciders)

Statecharts modelling of Tank Wars

Page 16: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts16

� TurretTankMovementCoordinator (coordinators)

Statecharts modelling of Tank Wars

Page 17: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts17

� MotorControl (actuator)

Statecharts modelling of Tank Wars

Page 18: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Designing the tank components’ statecharts18

� TurretControl (actuator)

Statecharts modelling of Tank Wars

Page 19: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

19

� Recap

� The static part: the controller

� Designing the tank components’ statecharts

� Recreating the simulation environment

Overview

Statecharts modelling of Tank Wars

� Recreating the simulation environment

� Summary

Page 20: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recreating the simulation environment20

� Time-sliced to event-based: a lot of work!

� Implement a very simple simulation environment insteadof using EA Tank Wars environment

� Use Python instead of C++

Statecharts modelling of Tank Wars

� Use Python instead of C++

� Translate EA Tank Wars header files into Python

� ai

� coord

� visor

Page 21: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recreating the simulation environment21

� Using Tkinter to draw world map and tank

Statecharts modelling of Tank Wars

Page 22: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recreating the simulation environment22

� World map defined using numbers in a plain text file

Statecharts modelling of Tank Wars

Page 23: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recreating the simulation environment23

� Moving the tank (translation)

def translatePoint(point, dx, dy):

x = point.x + dx

y = point.y + dy

return Coord2D(x, y)

� Turning the tank and the turret (rotation)

Statecharts modelling of Tank Wars

� Turning the tank and the turret (rotation)

def rotatePoint(point, origin, angle):

x = origin.x + ((point.x - origin.x) *

cos(angle) - (point.y - origin.y) * sin(angle))

y = origin.y + ((point.x - origin.x) *

sin(angle) + (point.y - origin.y) * cos(angle))

return Coord2D(x, y)

Page 24: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recreating the simulation environment24

� The simulation is also described with a statechart

root = Tk()

root.withdraw()

window = newWindow(root)

fr = FileReader.FileReader("input2.txt")

fr.loadFile()

Statecharts modelling of Tank Wars

fr.loadFile()

dr = Drawer.Drawer(root, window, fr.lines, 40, 40)

ctl = TankController.TankController(dr)

sim = simulation.simulation()

sim.initModel()

sim.event("start", ctl)

root.mainloop()

Page 25: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Recreating the simulation environment25

� Simulation example:

Statecharts modelling of Tank Wars

Page 26: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

26

� Recap

� The static part: the controller

� Designing the tank components’ statecharts

� Recreating the simulation environment

Overview

Statecharts modelling of Tank Wars

� Recreating the simulation environment

� Summary

Page 27: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

Summary27

� Succeeded:

� Model tank’s AI at a higher level of abstraction by using event-based formalism: statecharts

� Necessity to use controller class to link tank’s components and allow communication between them: “narrow cast”

Summary

Statecharts modelling of Tank Wars

allow communication between them: “narrow cast”

� Implementing a very simple environment for simulation instead of using EA Tank Wars

� Describing simulation with a statechart too

� Not succeeded (yet):

� Perceiving environment through the sensors (feeding the tank with input: obstacles, enemies, etc.)

Page 28: TankWars Presentation 2 - McGill Universitymsdl.cs.mcgill.ca/people/hv/teaching/MSBDesign... · 2008. 9. 9. · [Project Presentation ] 2 Recap The static part: the controller Designing

28

Thank you!

Statecharts modelling of Tank Wars