Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simulation Engine (Simulator)‏. Outline Review of discrete time and discrete event simulation Standard simulator for DEVS atomic model Standard simulator.

Similar presentations


Presentation on theme: "Simulation Engine (Simulator)‏. Outline Review of discrete time and discrete event simulation Standard simulator for DEVS atomic model Standard simulator."— Presentation transcript:

1 Simulation Engine (Simulator)‏

2 Outline Review of discrete time and discrete event simulation Standard simulator for DEVS atomic model Standard simulator for DEVS coupled model Real time simulation A heap-based simulator to improve performance

3 Review of Discrete Time Model Current state Current input Next stateCurrent output 0000 0111 1000 1111 Question: specify δ(q,x) and λ(q,x) for the table above δ(q,x) = x λ(q,x) = x Here δ is called the state transition function and is the more abstract concept for the first three columns of the table. λ is called the output function.

4 Discrete Time Model A sequent of state, q(0), q(1), q(2),… is called a state trajectory. Having an arbitrary initial state q(0), subsequent states in the sequence are determined by q(t+1) = δ(q(t), x(t)). Similarly, a corresponding output trajectory is given by y(t) = λ(q(t), x(t))‏ Time 0123456789 Input trajectory 1010101010 State trajectory 01010101010 Output trajectory 1010101010

5 Discrete Time Simulation The following algorithm is an example of a simulator for a discrete time model: T i =0, T f =9 the starting and ending times, here 0 and 9 X(0) = 1, …, x(9) = 0 the input trajectory q(0) = 0 the initial state t=T i While (t<=T f ){ y(t) = λ(q(t), x(t))‏ q(t+1) = δ(q(t), x(t))‏ t = t+1 } Question: The computation complexity of this algorithm

6 Review of an event-based Approach of cellular automata Concentrate on the interesting events only, namely births and deaths, as well as the changes in the neighborhood. To do that, we need a means to determine when interesting things happen. Events can be caused by the environment, such as the changes of the sum of alive neighbors. The occurrence of such external events are not under the control of the model component itself. On the other side, the component may schedule events to occur. Those are called internal events. Given a particular state, e.g., a particular fitness of the cell, a time advance is specified as the time it takes until the next internal event occurs, supposing that no external event happens in the meantime.

7 Event Scheduling Event scheduling is a basic approach in discrete event simulation. The event scheduling utilizes a event list, which stores a list of events that are ordered by increasing scheduling times. The event with earliest scheduled time is removed from the list and the clock is advanced to the time of this imminent event. The routine associated with the imminent event is executed. A tie-breaking procedure is employed if there is more than one such imminent events. Execution of the event routine may cause new events to be added in the proper place on the list. Also, existing events may be rescheduled or even canceled. The next cycle now begins with the clock advance to the earliest scheduled time.

8 Event Scheduling Event: Generate_Job nr-waiting = nr-waiting +1 schedule a Generate_Job in inter-gen-time if nr-waiting = 1 then schedule a Process_Job in service-time Event: Process_Job nr-waiting = nr-waiting -1 if nr-waiting >0 then schedule a Process_Job in service-time Break-Ties by: Process_Job then Generate_Job Gen Queue Server

9 Event list Scheduling

10 DEVS Atomic Model (S, e)‏ x yX(5.5) = 1.3, X(6.7) = 5.5 Initialize()‏ tN = sigma; While (not terminated){ getNextEvent(); currentTime = eventTime; if(currentTime <sigma) // this is the external event call external transition function, which will set a new sigma if(currentTime == sigma) // this is the internal event call output function call internal transition function, which will set a new sigma tN = sigma; } External input exampleevent queue

11 Simulator for DEVS Atomic Model The DEVS simulator drives the simulation What is the main design idea? –The idea is that the simulator should call the deltext, deltint, deltcon, out functions in the right place at the right time. How to do it? Assuming we are time t….. Who trigger the model to arrive at this point? –Some kind of event (external or internal)‏ What shall we check first? –if t==tN? What shall we do next? –Execute output function and state transition functions Next? How to decide when will be next t? –Update tL and tN Initialization?

12 Atomic Model Simulator Every atomic model has a simulator assigned to it which keeps track of the time of the last event, tL and the time of the next event, tN. Initially, the state of the model is initialized as specified by the modeler to a desired initial state, s init. The event times, tL and tN are set to 0 and ta(s init ), respectively. If there are no external events, the clock time, t is advanced to tN, the output is generated and the internal transition function of the model is executed. The simulator then updates the event times as shown, and processing continues to the next cycle. If an external event is injected to the model at some time, t ext (no earlier than the current clock and no later than tN), the clock is advanced to t ext. If t ext == tN the output is generated. Then the input is processed by the confluent or external event transition function, depending on whether t ext coincides with tN or not. genDevs. simulation. atomicSimula tor tL =: t tN =: t + ta(s)‏ When receive m if m!= null and t < tN, s :=  ext (s,t-tN,m)‏ if m!= null and t == tN, s :=  con (s,t-tN,m)‏ if m= null and t == tN, s =:  int (s)‏ s =: s init tL =: 0 tN =: ta(s init )‏ m timeline (abstract or logica)‏ inject at time t tN tL Legend: m = message s = state t = clock time tL = time of last event tN = time of next event If t == tN generate output (s)‏

13 Atomic Model Simulator public void computeInputOutput(double t){ if(equalTN(t)){ output = myModel.Out(); else output = new message(); // empty message } public void wrapDeltfunc(double t, Message x){ if (x.isEmpty() && !equalTN(t)) { return; } else if((!x.isEmpty()) && equalTN(t)) { double e = t - tL; myModel.deltcon(e,x); } else if(equalTN(t)) { myModel.deltint(); } else if(!x.isEmpty()) { double e = t - tL; myModel.deltext(e,x); } tL = t; tN = tL + myModel.ta(); }

14 DEVS Simulator for Coupled Model There is a global timer The idea is that the simulator (called coordinator) should first find the components with smallest next event time. The components with the smallest tN (call imminents) will execute their output functions, and then send their outputs to other components based on the couplings Right after that, the imminents need to execute the internal transition functions. Meanwhile, the components that receive inputs will execute the external transition functions. These functions change the sigma of those components. The next cycle begins

15 coordinator simulator Component tN tN. tL After each transition tN = t + ta(), tL = t simulator Component tN tN. tL simulator Component tN tN. tL Coupled Model 1 nextTN 2. outTN 3 getOut 4 sendOut 5 applyDelt Basic DEVS Simulation Protocol Each simulator reacts to the incoming message as follows: If it is imminent and its input message is empty, then it invokes its model’s inernal transition function If it is imminent and its input message is not empty, it invokes its model’s confluence transition function If is not imminent and its input message is not empty, it invokes its model’s external transition function If is not imminent and its input message is empty then nothing happens. Coupled Model Coordinator: 1. Coordinator sends nextTN to request tN from each of the simulators. 1.All the simulators reply with their tNs in the outTN message to the coordinator 1.Coordinator sends to each simulator a getOut message containing the global tN (the minimum of the tNs)‏ 1. Each simulator checks if it is imminent (its tN = global tN) and if so, returns the output of its model in a message to the coordinator in a sendOut message. 1. Coordinator uses the coupling specification to distribute the outputs as accumulated messages back to the simulators in an applyDelt message to the simulators – for those simulators not receiving any input, the messages sent are empty. For a coupled model with atomic model components, a coordinator is assigned to it and coupledSimulators are assigned to its components. In the basic DEVS Simulation Protocol, the coordinator is responsible for stepping simulators through the cycle of activities shown.

16 coordinator Atomic Model Atoimc3 coupledSimulator simulators.tellAll("initialize“)‏ simulators.AskAll(“nextTN”)‏ simulators.tellAll("computeInputOutput“)‏ simulators.tellAll("sendMessages")‏ simulators.tellAll("DeltFunc“)‏ message coupledSimulator Atomic Model Atomic Model simulators.tellAll("initialize“) --- at start of simulation simulators.AskAll(“nextTN”)‏ simulators.tellAll("computeInputOutput“)‏ simulators.tellAll("sendMessages")‏ simulators.tellAll("DeltFunc“)‏ DEVS cycle DEVS Simulation Protocol as Implemented in DEVSJAVA coupling genDevs. simulation. coordinator genDevs. simulation. coupledSimul ator

17 Standard Coordinator simulators.tellAll(“initiazlize”)‏ while (iter < iterNum){ simulators.AskAll(“nextTN”)‏ tN = compareAndFindTN(); simulators.tellAll("computeOut“,tN)‏ simulators.tellAll("sendOut")‏ simulators.tellAll("ApplyDelt“,tN)‏ iter++ }

18 Sequence Diagram of Initialization

19 Illustrating The DEVS Simulation Protocol within DEVS itself gpt The DEVS Simulation Protocol can be illustrated within DEVSJAVA itself as follows: The coupled model, simTrip, represents the simulation of coupled model gpt. We define simulator and sCoordinator subclasses of atomic. The components of simTrip are: 3 instances of the simulator class, one each for g, p, and t 1 instance of the sCoordinator class. SimpArc simulato r SimpArc gpt SimpArc sCoordina tor SimpArc simTrip

20 Simulate hierarchical coupled model in fast mode Coordinator CoupledCoordinatorCoupledSimulator4 Atomic4 CoupledSimulator3 Atomic3 simulators.tellAll("initialize“)‏ simulators.AskAll(“nextTN”)‏ simulators.tellAll("computeInputOutput“)‏ simulators.tellAll("sendMessages")‏ simulators.tellAll("DeltFunc“)‏ putMessage CoupledSimulator1CoupledSimulator2 Atomic1Atomic2 putMessage putMyMessage Coupled1 sendDownMessage

21 DEVS Simulation Protocol Classes Atomic Simulator coordinator coupledSimulator 1:n coupledCoordinator 1:n Stand alone atomic models are assigned to AtomicSimulators Coupled models are assigned to coordinators Atomic models as components within coupled models are assigned to coupledSimulators Coupled models as components within coupled models are assigned to coupledCoordinators genDevs. simulation. coordinator genDevs. simulation. coupledSimul ator

22 Real-Time Simulator/Executor When first entering the state, s, the simulator starts a thread to sleep for a time ta(s) If an external event is injected to the model at some time, t ext (no earlier than the current clock and no later than tN), the sleeping thread is interrupted and If t ext == tN the output is generated. Then the input is processed by the confluent or external event transition function, depending on whether t ext coincides with tN or not. genDevs. simulation.realTi me atomicRTSimula tor tL =: t tN =: t + ta(s)‏ When receive m if m!= null and t < tN, s :=  ext (s,t-tN,m)‏ if m!= null and t == tN, s :=  con (s,t-tN,m)‏ if m= null and t == tN, s =:  int (s)‏ s =: s init tL =: 0 tN =: ta(s init )‏ m timeline inject at time t tN tL Legend: m = message s = state t = clock time tL = time of last event tN = time of next event If t == tN generate output (s)‏ sleep for ta(s)‏ Execution in real time of an atomic model is similar to its simulation in logical or abstract time. The difference is that rather than being controlled by an events list within the coordinator, each simulator governs itself according to the host processor system clock. Thus passage of time in the model is identical to that of a wall clock. This difference is seen in the following alteration of the simulator: real Real time execution of DEVS models is facilitated by the separation of model from simulator which allows the simulator to be “changed out” by one that adheres to a real clock. The ease of migration from simulation to execution enables a designprocess called model- continuity discussed later.

23 Simulate coupled model in real time mode -- Centralized way RTCentraCoord CoupledCoordinatorCoupledSimulator4 Atomic4 CoupledSimulator3 Atomic3 putMessage simulators.tellAll("initialize“)‏ simulators.AskAll(“nextTN”)‏ myThread.sleep( nextTN – currentTime)‏ simulators.tellAll("computeInputOutput“)‏ simulators.tellAll("sendMessages")‏ simulators.tellAll("DeltFunc“)‏ CoupledSimulator1CoupledSimulator2 Atomic1Atomic2 putMessage putMyMessage Coupled1 sendDownMessage

24 Performance of the standard coordinator This standard simulation protocol follows closely with the semantic of DEVS models. Thus it is easy to be understood and implemented. However, this protocol tends to result in slow simulation speed for models that have a large number of components. This is because in every simulation cycle, all the simulators, no matter they are imminent or not, have to go through the simulation steps as described above. For the models that have only a few imminents, there exists a lot of unnecessary computation. Furthermore, this protocol uses a compare and select approach to find the smallest tNs among all components in every simulation cycle. This means n comparison (assuming there are n components in the model) is needed in every simulation cycle. For models with a large number of components but small number of imminents, the search process to find the smallest tN can be improved by using a heap. simulators.AskAll(“nextTN”)‏ tN = compareAndFindTN(); simulators.tellAll("computeOut“,tN)‏ simulators.tellAll("sendOut")‏ simulators.tellAll("ApplyDelt“,tN)‏

25 A heap-based Coordinator In every simulation cycle, the heap coordinator fist gets the smallest tN and the imminents from the heap. With the smallest tN and imminents in hand, the coordinator sends out the computeOut and sendOut messages to imminents. The sendOut message will trigger imminents to put their output messages to their destination simulators, which are called influences in the context of this paper. The influences, like the imminents, need to execute their state transition functions. Thus before executing imminents.tellAll("ApplyDelt“,tN), the coordinator adds those influences into imminents by executing imminents = imminents.addAll(influencees). At the end of the iteration, the coordinator asks all imminents to update their newest tNs in the heap to prepare for the next simulation iteration. Generally speaking, the update process where each active simulator removes and inserts its tN in the heap has computation complexity O(n*log2N) (n is the number of imminents in the simulation cycle). For large-scale cellular models with small number of imminents (n<<N), this heap coordinator has computation complexity at the magnitude of log2N, thus resulting in considerable performance improvement. tN = Heap.getMin()‏ imminents = Heap.getImms()‏ imminents.tellAll("computeOut“,tN)‏ imminents.tellAll("sendOut")‏ imminents = imminents.addAll(influencees)‏ imminents.tellAll("ApplyDelt“,tN)‏ imminents.tellAll(“updateHeap”)‏

26 Demonstration of the Heap-based Coordinator Standard coordinator for the forest fire model Heap-based coordinator for the forest fire model

27 Question? How to improve the simulation performance (speed) of the sheep-grass ecological simulation? What are the factors that cause the simulation slow? –A grass cell does not need to send out message if all its neighbors are grass already –The standard coordinator is slow when dealing with a large number of cells How to improve them?

28 Next Class Send me your homework #3 (including the pictures) by next Tuesday (firm date)‏ Demonstration of your homework #3 in the beginning of next class Email me a half page description about your project (including the topic, and the extension that you plan to add based on your homework) by next Wednesday.


Download ppt "Simulation Engine (Simulator)‏. Outline Review of discrete time and discrete event simulation Standard simulator for DEVS atomic model Standard simulator."

Similar presentations


Ads by Google