telematics 2 performance evaluation
play

Telematics 2 & Performance Evaluation Chapter 6 A Simple - PDF document

Telematics 2 & Performance Evaluation Chapter 6 A Simple Example (Acknowledgement: These slides have been prepared by Prof. Dr. Holger Karl) Telematics 2 / Performance Evaluation (WS 17/18): 06 A Simple Example 1 Goal of this chapter


  1. Telematics 2 & Performance Evaluation Chapter 6 A Simple Example (Acknowledgement: These slides have been prepared by Prof. Dr. Holger Karl) Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 1 Goal of this chapter q In this chapter, we will develop our first simulation program q It simulates a single queue, generates customers, and computes statistics q You should learn a few things about implementation concepts for discrete event simulation q And maybe a few things about C++ J q The entire source code is available on the course webpage (under Course Handouts) q Please do look at it! q A first look at how to interpret results of simulation programs Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 2

  2. Overview q Model and its state q Implementing a queue q Generating random numbers q Program structure q Interpreting results Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 3 Model q Single server q Single queue, first-in-first-out discipline q Input parameters q Pattern of customer arrivals: § Time between customers is exponentially distributed § Mean λ is a parameter for the program q Pattern of service times: § Time needed to serve a customer is exponentially distributed § Mean μ is a parameter for the program q So-called M/M/1 system q Metrics: Server utilization, average number in queue, average waiting time q Stop simulation after 10000 customers have been processed Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 4

  3. Necessary state q Current simulation time q Server state: idle or busy q Time of next arrival q Time of next departure q A queue data structure to represent the queue q Queue is a first-in-first-out data structure – how to implement? q Queue entries contain required processing time and arrival time of the task (for statistics) Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 5 Variables to compute metrics q Average waiting time q Every queue entry contains time of arrival q When task exits queue, use this value to update total waiting time , later divide that by number of customers q Average queue length q Recall the representation as area under the curve of number of tasks currently in queue q Represent total area under this curve q Average server utilization q Similar to average queue length, yet the curve only takes on values of 0 and 1, representing idle or busy q Represent total area under this curve as well Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 6

  4. Open questions q How to obtain randomness in a simulation program? q How to implement a queue? q How to organize the overall program structure? Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 7 Overview q Model and its state q Implementing a queue q Generating random numbers q Program structure q Interpreting results Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 8

  5. Implementing a queue q A queue as abstract data type q Contains data of a certain type (at least, here) q Has a push operation that adds data to the end q Has a pop operation that removes one data element from the front and returns it (error if applied to empty queue) q Useful: length operation that returns number of elements q Number of elements should be unbounded q Many simple implementations possible q Here: single-linked list q Make it reusable by making the data type a parameter of the class q Write a � template class � Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 9 Templates in C++ q Template: define a type that takes one (or more) types as parameter q E.g. define a � queue � type (a class) that contains the general logic of a queue, but is not specialized towards a certain type to be contained within the queue q When creating objects of such a general, parameterized class, the parameters have to be filled with concrete types, e.g., a queue of integers, or a queue of floats, or … q Looks difficult, is difficult J q Well, not really – just a little bit awkward to write or read q For debugging use a recent compiler, e.g., clang! Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 10

  6. Templates in C++ q Important point: the entire implementation of the class has to be included in the .hpp file (not just the declarations) q Reason: Templates in C++ are realized more or less via smart preprocessing q Compiler generates textual instances of code for every usage of the template class, these instances are then compiled q Complete source code necessary to do this q Look at example in SIMQueue.hpp q SIMQueue class acts as a wrapper, containing pointers to the next elements (not needed outside) q queue is the actual implementation Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 11 Overview q Model and its state q Implementing a queue q Generating random numbers q Generators for random sequences q Seeds for generators q From random sequences to random distributions q How to use and reuse generators q Program structure q Interpreting results Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 12

  7. Generating random numbers q How to simulate the random process of arriving customers, needing random service times? q Sources of true randomness exist, but are awkward to handle q E.g., number of particles radiating per second from radio-active material q Slow! q Additional disadvantage: reproducing experiments would be difficult ➡ Deterministically generate pseudo-random numbers to represent service times, times between arrivals, etc. Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 13 Generating random numbers q One (bad) possibility: Do it from scratch q Many algorithms for random number generators (RNGs) exist q Simple example: Linear congruential generators x n = ( ax n − 1 + b ) mod c where x n is the current random number q Example parameters: x n = 5 x n -1 + 1 mod 16 q Initial value x 1 = 5 q Resulting sequence of � random numbers � : 10, 3, 0, 1, 6, 15, 12, 13, 2, 11, 8, 9, 14, 7, 4, 5 (and here we are back again!), 10, 3, … q Division by 16 gives numbers between 0 and 1, to be interpreted as random numbers Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 14

  8. Generating random numbers q Typical properties of random number generators: a state and an initial state (seed) q In example: State is the current x i , initial value is x 0 q Modulus defines maximum number of different values, also defines required amount of space to hold state ■ A modulus of 16 needs 4 bits to represent state, but only 16 different numbers are possible (at best) -> not particularly random q Larger state (often) gives � better � pseudo-random properties q Designing fast & good random number generators is difficult q Extensively treated in the literature, but not part of this course q Testing for randomness also difficult Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 15 Generator objects for random numbers q Usually: use a library, e.g., C++ standard library (libc++) q Other programming environments usually provide similar mechanisms q Only for C++11! q Concept for random numbers in C++11 (and many others): Separate generation of random number streams from distributions Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 16

  9. Generator objects for random numbers q Different classes that generate random streams, according to different algorithms q E.g. additive congruential generator: std::linear_congruential_engine< uint32_t, LC_A, LC_B, LC_M> gen(seed); q gen is an object that provides a sequence of random numbers q Accessed via the operator() interface (operator overloading of � () � ) q All these classes return numbers that are uniformly distributed over the range of all possible numbers that fit into the given space q i.e., 0 .. 2 space -1 q May have many problems, i.e. correlation between subsequent samples, may only be good for some seeds and parameters… Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 17 More modern alternative: MersenneTwister q Much larger internal state - 624 32bit words(!) q Based on 624 linear feedback shift registers q Rather fast due to SHIFT and XOR operations q Rerandomizes internal state every 624 random values (called twist) q Leads to extremely long period: 2 19937 – 1 q May output same value in two consecutive steps q Passes MANY randomness tests q Still sensitive to randomness of seeds q Either: q Generate seed by (cryptographic) hash function q Throw away the first >10000 random numbers q De-facto standard in many programs, e.g. std::mt19937_64 Telematics 2 / Performance Evaluation (WS 17/18): 06 – A Simple Example 18

Recommend


More recommend