The Discrete Event Calculus for Games and Other (Soft) Real Time Systems Matthew Fuchs, PhD Paideia Computing Corp [email protected]

Copyright 2010 Paideia Computing Corp. Outline

• Movie • The target • DEC explained • DEC as a programming language • Implementation • Future possibilities

Copyright 2010 Paideia Computing Corp. “Give me a command”

• Video at http://www.paideiacomputing.com/videos/simonsays.mp4

Copyright 2010 Paideia Computing Corp. What I was looking for • Unified approach – Not mix of java, , etc., with protocols and events passing among them • Able to handle/model multi-modal input – Speech, movement, pointing, possession • Rich behavior, not just Finite State Machines – Rich behavior set, but not difficult to add new behaviors – Large variety of events arriving at any time • Can integrate with other AI systems or handle such tasks directly – Personality, planning, language • Multitasking – Handle multiple characters in a single environment • Sufficiently fast – “soft” real time – not aware of any explicitly temporal rule engines • Able to reason about programs

Copyright 2010 Paideia Computing Corp. Event Calculus

Copyright 2010 Paideia Computing Corp. What it’s been used for

• Robotic path planning • Linguistics • Story understanding • Abductive planning • Agent programming • Contract law • Workflow modeling • Mateas work in games • Etc.

Copyright 2010 Paideia Computing Corp. What I found: Rules with Time

• AI Formalism • Descended from • First proposed by Kowalski and Sergot, extended by Shanahan • Has an explicit time parameter – either continuous or discrete • “Commonsense” law of inertia • Handles the through Circumscription • Sorted (multiple token types)

Copyright 2010 Paideia Computing Corp. DEC

• What – Fluents – Events • When – Happens – HoldsAt • How – Initiates – Terminates – Trajectory – AntiTrajectory – Releases

Copyright 2010 Paideia Computing Corp. Fluents

• A function of sort fluent • Unique names

• f1(p1,…,pn) = f2(q1,…,qn) iff f1=f2 & p1=q1,…,pn=qn • HoldsAt(f,t) is 2 parameter predicate • HoldsAt(walks(John),0). • Either holds or doesn’t at t=0 • Normally only changes value through events (law of inertia)

Copyright 2010 Paideia Computing Corp. Events

• A function of sort event • Unique Names

– e(p1,…,pn) = e(q1,…,qn) iff p1=q1,…,pn=qn • Happens(e,t) is a 2 parameter predicate • Happens(notices(John,Tiger),4). • Events happen at particular moments in time and are only true at that moment (or happen again)

Copyright 2010 Paideia Computing Corp. Initiates/Terminates

• Initiates(e,f,t) – Happens(e,t) & ~HoldsAt(f,t) -> HoldsAt(f,t+1). • Terminates(e,f,t) – Happens(e,t) & HoldsAt(f,t) -> ~HoldsAt(f,t+1). • [t]Initiates(notices(John,Tiger),runs(John),t). • [t]Terminates(notices(John,Tiger),walks(John),t)

Copyright 2010 Paideia Computing Corp. Trajectory

• Trajectory(f1,t1,f1,t2)

• If f1 is initiated at t1 and holds at t1+t2, then f2 holds at t2 • Trajectory(walksFrom(John,X,Y,Phi),t1, position(John,X+t2*cos(Phi),Y+t2*sin(Phi),t2)

• Assumes you calculate the arguments to f2 at t2

• Depends on the math for f2

Copyright 2010 Paideia Computing Corp. AntiTrajectory

• AntiTrajectory(f1,t1,f2,t2)

• If f1 is terminated at t1 and doesn’t hold at t2, then f2 holds at t1+t2 • HoldsAt(Height(bird,h),t1)->AntiTrajectory(Flapping(bird),t1, 2 Height(bird,h – ½ G t2 )

• Assumes you calculate the arguments to f2 at t2

• Depends on the math for f2

Copyright 2010 Paideia Computing Corp. Released

• Releases(e,f,t). • A released fluent may take any logically allowed value • Allows fluents to function like ordinary logical variables – HoldsAt(sees(John,Tiger),t) -> HoldsAt(scared(John),t).

Copyright 2010 Paideia Computing Corp. Circumscription

• Handles frame problem – Some variables change between t and t + 1 – How to make sure nothing else does • Essentially create equivalence between a predicate and the rules that cause it

– 1-> Happens(e,t)

– 2 -> Happens(e,t)

– Happens(e,t) <-> 1 | 2

Copyright 2010 Paideia Computing Corp. Rest of syntax

• [] for universal quantificaton • {} for existential quantification • ! for negation • & for and, | for “or” • -> for implication • My additions: – walks(John)@time – notices(John,Tiger)!time – notices(John,Tiger)=>runs(John)@time – Notices(John,Tiger)/=>walks(John)@time

Copyright 2010 Paideia Computing Corp. Example

• John is walking • John is frightened of tigers • If John is frightened he – Stops walking – Finds a place to run to – Starts running

Copyright 2010 Paideia Computing Corp. Declarations sort mob sort gait mob John, Tiger gait Walking, Running structure Location(x:integer,y:integer,z:integer,theta:integer) externEvent SetGait(mob,gait) externEvent MovesTo(mob,location) fluent Position(mob,location) event Sees(mob,mob) predicate IsVisible(location,location) function MoveAway(location,location):location fluent Frightens(mob,mob) fluent Position(mob,location)

Copyright 2010 Paideia Computing Corp. Initial Conditions

Position(John, Location(0, 0, 0, 90)@0.

Position(Tiger, Location(0, 0, 8, -90)@0.

SetGait(John, Walking)@0.

Frightens(Tiger,John)@0.

Copyright 2010 Paideia Computing Corp. Some rules

[mob,location,time] MovesTo(mob,location)=>Position(mob,location),time).

[mob,location1,location2,time] Position(mob,location1)@time & !(location2 = location1) -> MovesTo(mob,location2) /=> Position(mob,location1)@time.

[mob1,mob2,location1,location2,time] Position(mob1,location1)@time & Position(mob2,location2)@time & IsVisible(location2,location1) -> Sees(mob1,mob2)!time.

Copyright 2010 Paideia Computing Corp. Last rules

[mob1,mob2,time] Frightens(mob1,mob2)@time & Sees(mob2,mob1)@time -> SetGait(mob2,Running)@time.

[mob1,mob2,location1,location2,time] Frightens(mob2,mob1)@time & Position(mob2,location2)@time & Position(mob1,location1)@time Sees(mob1,mob2)@time & location3 = MoveAway(location1,location2) -> MovesTo(mob1,location3)!time

Copyright 2010 Paideia Computing Corp. How it’s used

• Model checking • Completely static • Tic Tac Toe as an example – Rules to generate all games – Generates all games given some moves • SAT is slow • Perhaps Answer Set Programming?

Copyright 2010 Paideia Computing Corp. DEC as a programming language

• Time parameter maps to game/animation • “world” and “simulation” communicate via events – World events arrive at each tick – Rules are evaluated with new events – Some events are “external” and reflected back • Don’t need – Conscription – Many State Constraints (F(x) & F(y) => x = y) Copyright 2010 Paideia Computing Corp. Markov issues

• DEC rules are symmetric to time

– e!t -> e2!(t – 10)

– e2!t -> e!(t + 10)

• Future can affect past – running(John)@t1 -> {t2}t2 < t1 & scared(John)@t2.

• Programs run forward in time

Copyright 2010 Paideia Computing Corp. Implementation

• Convert into forward chaining – DEC rules don’t have Markov Property – Rules that don’t obey MP are tests/asserts • Might be used for reasoning later on – Execution but not reasoning only goes forwards • Apply rete for efficiency • Simplify by translating to CLIPS style system • Not included in current implementation – Released fluents (state constraints) – (Anti)Trajectory

Copyright 2010 Paideia Computing Corp. Rete algorithm

• Greek for “net” • Allows forward chaining systems to be fast • Rules of form (LHS -> RHS) – ((a & b) | (c & d)) -> g – ((e & b) | (c & d)) -> h • Break up LHS into variables and binary ops • Create a network from the results combining LHS overlaps • Facts (de)activateCopyright parts 2010 Paideia Computingof network Corp. Forward Chaining System • Active axioms of forms – -> Initiates(e,f,t) – -> Terminates(e,f,t)

– -> Trajectory(f1,t1,f2,t1)

– -> AntiTrajectory(f1,t1,f2,t2) • Implication means perform – ->Initiates(e,f,t) changes an explicit state • Other forms become tests – Move(rc1,rc2,mark1)!time & Move(rc3,rc4,mark2)!time -> rc1 = rc3 & rc2 = rc4 & mark1 = mark2.

Copyright 2010 Paideia Computing Corp. Primitives (Clips style)

• Fluents are atoms – (fluent, time, p1,…,pn) • Events are atoms – (event, time, p1,…,pn) • Add structures, lists – (name, id, type, ref count, p1,…,pn) • Primitive pattern matching

Copyright 2010 Paideia Computing Corp. Multistage architecture

• Each time step has four stages • Prepare new fluents • Create fluents • Garbage Collect • Generate new events

Copyright 2010 Paideia Computing Corp. Stage 0

(with events and fluents remaining from t-1) – Evaluate ->Initiates(e,f,t) • Leads to createFluent fact – Evaluate -> Terminates(e,f,t) • Leads to retractFluent fact – Test fluent contradictions • Initiate(e,f,t) & Terminate(e,f,t) are illegal

Copyright 2010 Paideia Computing Corp. Stage 1

• Actually create fluents

• Actually retract fluents

• Clean up events from round t - 1

Copyright 2010 Paideia Computing Corp. Stage 2

• Garbage collection

• Structures have references

• Language is all functional

• GC is through reference counting

Copyright 2010 Paideia Computing Corp. Stage 3

• Create all events

• Add all world events

• All rules of the forms: – -> Happens(e,t).

• Creates external events (for “outside world”)

Copyright 2010 Paideia Computing Corp. Test constraint violations

• X -> !Y • X -> *…+Y or X -> ,…-Y • *…+!Y • HoldsAt(f,t) where t > 0

Copyright 2010 Paideia Computing Corp. Rule by Rule

• DEC Syntax – Frightens(Tiger,John)@0 • Clips Syntax (defrule startup-1 (Time 0) (round 0) => (assert (createFluent Frightens 0 Tiger John)))

Copyright 2010 Paideia Computing Corp. HoldsAt as Constraint

• Frightens(Foobar,John)@1

• (defrule rule-2 (not (fluent Frightens ?_time0 Foobar John)) (round 4) => (assert (ConstraintViolation rule-2 "[Foobar,John]")))

Copyright 2010 Paideia Computing Corp. Happens on RHS

[mob1,mob2,location1,location2,time] Position(mob1,location1)@time & Position(mob2,location2)@time & IsVisible(location2,location1) -> Sees(mob1,mob2)!time.

(defrule rule-8 (fluent Position ?_time2 ?mob1 ?location1) (fluent Position ?_time3 ?mob2 ?location2) (IsVisible ?location2 ?location1 TRUE) (round 3) (Time ?time) => (assert (event Sees ?time ?mob1 ?mob2)))

Copyright 2010 Paideia Computing Corp. Initiates

[mob,location,time] MovesTo(mob,location)=>Position(mob,location)@time

(defrule rule-11 (event MovesTo ?_time0 ?mob ?location) (round 0) (Time ?time) => (assert (modifyRefs (++ ?*modifyIndex*) ?location 1)) (assert (createFluent Position ?time ?mob ?location)))

Copyright 2010 Paideia Computing Corp. Terminates

[mob,location1,location2,time]Position(mob,location1)@time & !(location2 = location1))-> MovesTo(mob,location2)/=>Position(mob,location1)@time)

(defrule rule-10 (event MovesTo ?_time0 ?mob ?location2) (fluent Position ?_time2 ?mob ?location1) (and (fluent Position ?_time3 ?mob ?location1) (not (test (= ?location2 ?location1)))) (round 0) (Time ?time) => (assert (modifyRefs (++ ?*modifyIndex*) ?location1 -1)) (assert (retractFluent Position ?time ?mob ?location1)))

Copyright 2010 Paideia Computing Corp. Predicates number7 = ViewAngle(number1,number2,number3,number4) & number8 = AngleDif(number7,number5) & number8 < number6)))-> LookingAt(number1,number2,number3,number4,number5,number6)

(defrule LookingAt-62 ?_need <- (need-LookingAt ?number1 ?number2 ?number3 ?number4 ?number5 ?number6 ?foo) (ViewAngle ?number1 ?number2 ?number3 ?number4 ?number7) (AngleDif ?number7 ?number5 ?number8) (test (< ?number8 ?number6) => (retract ?_need) (assert (LookingAt ?number1 ?number2 ?number3 ?number4 ?number5 ?number6 TRUE))) Copyright 2010 Paideia Computing Corp. Functions

[integer1,integer2]Add(integer1,integer2) = +(integer1,integer2)

(defrule Add-74 ?_need <- (need-Add ?integer1 ?integer2 ?foo) => (bind ?return (+ ?integer1 ?integer2)) (retract ?_need) (assert (Add ?integer1 ?integer2 ?return)))

Copyright 2010 Paideia Computing Corp. Behavior Speech Management Services

Speech Recognition output Actions Grammar/ results Lexicon

Speech Generation Recognition Engine (Nuance/Loquendo (Nuance/IBM/Sphinx) /Phonetic Arts) NPC voice

Game VoIP Layer Player voice events Player actions World Engine – Game/Learning NPC Actions

Copyright 2010 Paideia Computing Corp. Multiple versions

• Flash Media Server

• Multiverse for robot video

• HTTP version for Parrot

Copyright 2010 Paideia Computing Corp. Dancing Parrot

• Video at http://www.paideiacomputing.com/videos/parrot.mov

• Parrot front end by Oddcast ( www.oddcast.com )

Copyright 2010 Paideia Computing Corp. Fun other stuff

• Petri nets • Model checking – not just SAT • Better syntax – Pattern matching • Probabilistic processes – Adding to traversal – Implementing the other party

Copyright 2010 Paideia Computing Corp. Better Syntax

• f0@t-> (e1=>f1@t & e1/=>f2

& f3@t -> e2!t)

• ( 1 xor 2 xor 3)

Copyright 2010 Paideia Computing Corp. Probabilistic Processes

• Model opponents • e!t->f1@t | f2@t • Add probabilities

• e1!t:0.2 xor e2!t:0.6 xor e3!t:0.2 • 0.4:e!t->f1@t | f2@t

Copyright 2010 Paideia Computing Corp. Conclusion

• Works for animation/games • Can work for soft real time processes • Combines a rule engine with temporal processes • Currently translating to Drools

Copyright 2010 Paideia Computing Corp. Thank you!

[email protected]

Copyright 2010 Paideia Computing Corp.