CS302 Topic: Simulation and Priority Queues The banking - - PowerPoint PPT Presentation

cs302 topic simulation and priority queues
SMART_READER_LITE
LIVE PREVIEW

CS302 Topic: Simulation and Priority Queues The banking - - PowerPoint PPT Presentation

CS302 Topic: Simulation and Priority Queues The banking simulation, Part I Tuesday, Oct. 4, 2005 Announcements Lab 4 (Stock Reports); due this Friday, Oct. 7 Dont procrastinate!! I like work: it fascinates me. I like work:


slide-1
SLIDE 1

CS302 Topic: Simulation and Priority Queues

The banking simulation, Part I

Tuesday, Oct. 4, 2005

slide-2
SLIDE 2

Announcements

  • Lab 4 (Stock Reports); due this Friday, Oct. 7
  • Don’t procrastinate!!

I like work: it fascinates me. I like work: it fascinates me. I can sit and look at it for hours. I can sit and look at it for hours.

  • - J.K. Jerome (Three Men in a Boat, 1889)

J.K. Jerome (Three Men in a Boat, 1889)

  • Midterm :
  • 2 weeks from today (Tues., Oct. 18)
  • Includes material covered in class and on labs
  • Open book, open notes
  • No electronic devices
slide-3
SLIDE 3

Recall: What are properties of Priority Queue / Heap?

What are properties of heap?

  • Structure property:
  • Complete binary tree. In other words, tree is completely filled, with

possible exception of last level, which is filled from left to right

  • Heap-Order property (for a min heap):
  • For every internal node v other than the root,
  • key(v) ≥ key(parent(v))

Heap

slide-4
SLIDE 4

Recall: What are Priority Queue / Heap Runtimes?

Time complexities:

  • insert:
  • O(log n), worst case
  • O(1), on average
  • Can be shown that, on average, don’t have to percolate

new element all the way up

  • On average, percolation terminates early
  • On average, 2.607 comparisons are required to perform

an insert

  • deleteMin:
  • O(log n)
  • buildHeap:
  • O(n)

Heap

slide-5
SLIDE 5

Recall: What are possible implementations of Priority Queues?

  • Unsorted linear list

5 2 3 1 4

  • Sorted linear list

2 3 4 5 1

  • Red-black tree
  • Heap

W hich did w e favor? W hy? Heaps Better runtime characteristics

slide-6
SLIDE 6

A C+ + Implementation of Priority Queues

NOTE: Here, we are giving an example of implementing priority queues using red-black trees In Lab 6: You’ll implement Priority Queues using Heaps – THIS IS THE PREFERRED WAY NOTE: Different implementations of Priority Queues can have different interfaces

  • We’ll describe one possible interface today
  • In Lab 6:

Your interface might be slightly different!

slide-7
SLIDE 7

A Public Interface for a Priority Queue

class pQueue { public: pQueue(); ~pQueue(); void insert(int key, Jval val); int minKey(); Jval minVal(); void deleteMin(); int size(); bool empty(); };

What operations should we provide?

  • Required (for any priority queue):
  • Insert()
  • DeleteMin()
  • Optional (depends on interface design):
  • minKey()
  • minVal()
  • size()
  • empty()
slide-8
SLIDE 8

Example Program using pQueues

#include <iostream> #include "pQueue.h" #include "Fields.h" #include <string> using namespace std; main() { pQueue p; Fields f; string *s; int key; while (f.get_line() >= 0) { // we need to use a pointer to a string because a Jval cannot store // a string (it can store a char * but it cannot store a string) s = new string(f.get_current_line()); sscanf(s->c_str(), "%d", &key); p.insert(key, new_jval_v(s)); } while (!p.Empty()) { s = (string *) p.minVal().v; cout << *s; p.deleteMin(); } }

What does this program do? Sorts each line of standard input based on its first field (which we assume is integer)

sortem.cpp

slide-9
SLIDE 9

Priority Queue interface using red-black trees

Hold elements of priority queue in red-black tree Maintain size of queue What should the data variables be for this priority queue interface?

class pQueue { public: ... protected rbTree <int> tree; int queueSize; };

slide-10
SLIDE 10

Priority Queue interface using red-black trees (con’t.)

  • What should constructor do?

pQueue::pQueue() { queuesize = 0; }

  • Since tree is statically allocated, it is automatically

created and destroyed. So, no need for memory allocation in constructor.

  • What should destructor do?

pQueue::~pQueue() { }

slide-11
SLIDE 11

Priority Queue interface using red-black trees (con’t.)

How do we implement insert()?

void pQueue::insert(int key, Jval val) { tree.insert(key, val); queueSize++; }

slide-12
SLIDE 12

Priority Queue interface using red-black trees (con’t.)

How do we implement minKey()?

int pQueue::minKey() { if (queueSize == 0) { fprintf(stderr, "pQueue::Minkey() called on an empty pQueue\n"); exit(1); } tree.first(); return tree.getKey(); }

slide-13
SLIDE 13

Priority Queue interface using red-black trees (con’t.)

How do we implement minVal()?

Jval pQueue::minVal() { if (queueSize == 0) { fprintf(stderr, "pQueue::Minval() called on an empty pQueue\n"); exit(1); } tree.first(); return tree.getVal(); }

slide-14
SLIDE 14

Priority Queue interface using red-black trees (con’t.)

How do we implement deleteMin()?

void pQueue::deleteMin() { if (queueSize == 0) { fprintf(stderr, "pQueue::deleteMin() called on an empty pQueue\n"); exit(1); } tree.first(); tree.deleteNode(); queueSize--; }

slide-15
SLIDE 15

Priority Queue interface using red-black trees (con’t.)

How do we implement size()?

int pQueue::size() { return queueSize; }

How do we implement empty()?

bool pQueue::empty() { return (queueSize == 0); }

slide-16
SLIDE 16

Recall: Applications of Priority Queues?

  • Example Applications:
  • Unix/ Linux job scheduling
  • Processes given a priority
  • Time allocated to process is based on priority of job
  • Priority of jobs in printer queue
  • Sorting
  • Standby passengers at airport
  • Auctions
  • Stock market
  • Event-driven simulations
  • VLSI design (channel routing, pin layout)
  • Artificial intelligence search algorithms
slide-17
SLIDE 17

Discrete Event Simulations

Here, we’re interested in procesess and systems that change at discrete instants What are examples of discrete events?

  • Pushing an elevator button
  • Starting of a motor
  • Stopping of a motor
  • Arrival of a customer
  • Departure of a customer
  • Turning on a light

These events are discrete events, because there is an instant of time at which each occurs

slide-18
SLIDE 18

Are these discrete events?

  • Train departing station
  • Yes
  • Train moving from point A to point B
  • No
  • Train arriving at point B
  • Yes
  • Note:
  • We can model an event of a train moving from point

A to point B as two separate events (i.e., the departure and the arrival)

  • If we associate a time value with each discrete event,

then we can model the duration of activities

slide-19
SLIDE 19

Discrete Event Simulators aren’t good for continuous time

For example, not good for simulating motion of planetary bodies Better approach?

  • Differential equations
slide-20
SLIDE 20

Note on Queuing Theory

  • Queuing Theory: a branch of mathematics that deals with

computing (probabilistically) delays in lines at servicing points

  • Answer depends on:
  • How frequently users arrive
  • How long it takes to process a user once their turn arrives
  • Both of above typically represented as probability

distribution functions

  • Finding solutions:
  • Simple cases:

Can compute answer analytically

  • More complex cases (e.g., with k operators): Solve using

discrete event simulation

slide-21
SLIDE 21

The Banking Simulation

We have a bank:

  • Customers arrive and wait in a line until one of k

tellers is available

  • Customer arrival governed by probability distribution
  • Service time governed by a probability distribution

We want to know:

  • How long (on average) does a customer have to

wait?

  • How long (on average) is the line?
  • What is the optimal number of tellers?

Enough so that customers don’t have to wait too long Not so many that we waste our money paying tellers

slide-22
SLIDE 22

The Simulation Process

  • Simulation consists of processing events.
  • Here, events are:
  • Customer arriving
  • Customer departing
  • Use probability distributions to generate:
  • Input stream consisting of ordered pairs of arrival time and

service time, sorted by arrival time

  • Then, we run the simulation for a while and collect data on:
  • How the number of tellers impacts how long people wait
  • How long tellers are idle
  • From data, we can determine optimal number of tellers
slide-23
SLIDE 23

Example of simulation process

Our system: 2 tellers, 4 customers. Simulator generates these events:

New Person: 0 enters the bank at 1.5. Transaction time: 5.7 New Person: 1 enters the bank at 2.8. Transaction time: 1.9 New Person: 2 enters the bank at 3.3. Transaction time: 8.7 New Person: 3 enters the bank at 9.2. Transaction time: 2.7

based on “customer arrival” probability distribution based on “service time” probability distribution

slide-24
SLIDE 24

Simulation process, con’t.

  • Time 1.5: Person 0 enters bank
  • Both tellers are free teller 1 starts working on person 0’s transaction.

(Transaction takes 5.7 minutes, so it’s done at 1.5+ 5.7 = 7.2)

  • Time 2.8: Person 1 enters bank
  • Teller 2 is free teller 2 starts working on person 1’s transaction.

(Transaction takes 1.9 minutes, so it’s done at 2.8+ 1.9 = 4.7)

  • Time 3.3: Person 2 enters bank
  • Both tellers are busy, so person 2 waits.
  • Time 4.7: Teller 2 is free
  • Teller 2 starts working on person 2’s transaction. (Transaction takes

8.7 minutes, so it will be done at 4.7+ 8.7 = 13.4)

  • Time 7.2: Teller 1 is free.
  • Time 9.2: Person 3 enters bank.
  • Teller 1 is free, teller 1 starts working on person 3’s transaction

(Transaction takes 2.7 minutes, so it will be done at 9.2+ 2.7 = 11.9)

  • Time 11.9: Teller 1 is free
  • Time 13.4: Teller 2 is free. No more people generated by simulation, so

we’re done.

New Person: 0 enters the bank at 1.5. Transaction time: 5.7 New Person: 1 enters the bank at 2.8. Transaction time: 1.9 New Person: 2 enters the bank at 3.3. Transaction time: 8.7 New Person: 3 enters the bank at 9.2. Transaction time: 2.7

slide-25
SLIDE 25

Analyzing data: Waiting time?

  • Time 1.5: Person 0 enters bank
  • Both tellers are free teller 1 starts working on person 0’s transaction.

(Transaction takes 5.7 minutes, so it’s done at 1.5+ 5.7 = 7.2)

  • Time 2.8: Person 1 enters bank
  • Teller 2 is free teller 2 starts working on person 1’s transaction.

(Transaction takes 1.9 minutes, so it’s done at 2.8+ 1.9 = 4.7)

  • Time 3.3: Person 2 enters bank
  • Both tellers are busy, so person 2 waits.
  • Time 4.7: Teller 2 is free
  • Teller 2 starts working on person 2’s transaction. (Transaction takes

8.7 minutes, so it will be done at 4.7+ 8.7 = 13.4)

  • Time 7.2: Teller 1 is free.
  • Time 9.2: Person 3 enters bank.
  • Teller 1 is free, teller 1 starts working on person 3’s transaction

(Transaction takes 2.7 minutes, so it will be done at 9.2+ 2.7 = 11.9)

  • Time 11.9: Teller 1 is free
  • Time 13.4: Teller 2 is free. No more people generated by simulation, so

we’re done.

Person 0: Person 2: 1.4 Person 1: Person 3:

Average = 0.35

slide-26
SLIDE 26

Analyzing data: Teller idle time? (Let’s say that bank “closes” at 12.0)

  • Time 1.5: Person 0 enters bank
  • Both tellers are free teller 1 starts working on person 0’s transaction.

(Transaction takes 5.7 minutes, so it’s done at 1.5+ 5.7 = 7.2)

  • Time 2.8: Person 1 enters bank
  • Teller 2 is free teller 2 starts working on person 1’s transaction.

(Transaction takes 1.9 minutes, so it’s done at 2.8+ 1.9 = 4.7)

  • Time 3.3: Person 2 enters bank
  • Both tellers are busy, so person 2 waits.
  • Time 4.7: Teller 2 is free
  • Teller 2 starts working on person 2’s transaction. (Transaction takes

8.7 minutes, so it will be done at 4.7+ 8.7 = 13.4)

  • Time 7.2: Teller 1 is free.
  • Time 9.2: Person 3 enters bank.
  • Teller 1 is free, teller 1 starts working on person 3’s transaction

(Transaction takes 2.7 minutes, so it will be done at 9.2+ 2.7 = 11.9)

  • Time 11.9: Teller 1 is free
  • Time 13.4: Teller 2 is free. No more people generated by simulation, so

we’re done.

Teller 1: 1.5 + 2 + 0.1 = 3.6 2.8 + 0 = 2.8 Teller 2:

Average = 3.2

slide-27
SLIDE 27

Suppose we change # tellers to 3. How does this affect waiting time?

Person 2 would get helped immediately

  • Time 1.5: Person 0 enters bank
  • Both tellers are free teller 1 starts working on person 0’s transaction.

(Transaction takes 5.7 minutes, so it’s done at 1.5+ 5.7 = 7.2)

  • Time 2.8: Person 1 enters bank
  • Teller 2 is free teller 2 starts working on person 1’s transaction.

(Transaction takes 1.9 minutes, so it’s done at 2.8+ 1.9 = 4.7)

  • Time 3.3: Person 2 enters bank
  • Both tellers are busy, so person 2 waits.
  • Time 4.7: Teller 2 is free
  • Teller 2 starts working on person 2’s transaction. (Transaction takes

8.7 minutes, so it will be done at 4.7+ 8.7 = 13.4)

  • Time 7.2: Teller 1 is free.
  • Time 9.2: Person 3 enters bank.
  • Teller 1 is free, teller 1 starts working on person 3’s transaction

(Transaction takes 2.7 minutes, so it will be done at 9.2+ 2.7 = 11.9)

  • Time 11.9: Teller 1 is free
  • Time 13.4: Teller 2 is free. No more people generated by simulation, so

we’re done.

Average waiting time drops to 0

slide-28
SLIDE 28

Suppose we change # tellers to 3. How does this affect idle time?

  • Time 1.5: Person 0 enters bank
  • Both tellers are free teller 1 starts working on person 0’s transaction.

(Transaction takes 5.7 minutes, so it’s done at 1.5+ 5.7 = 7.2)

  • Time 2.8: Person 1 enters bank
  • Teller 2 is free teller 2 starts working on person 1’s transaction.

(Transaction takes 1.9 minutes, so it’s done at 2.8+ 1.9 = 4.7)

  • Time 3.3: Person 2 enters bank
  • Both tellers are busy, so person 2 waits.
  • Time 4.7: Teller 2 is free
  • Teller 2 starts working on person 2’s transaction. (Transaction takes

8.7 minutes, so it will be done at 4.7+ 8.7 = 13.4)

  • Time 7.2: Teller 1 is free.
  • Time 9.2: Person 3 enters bank.
  • Teller 1 is free, teller 1 starts working on person 3’s transaction

(Transaction takes 2.7 minutes, so it will be done at 9.2+ 2.7 = 11.9)

  • Time 11.9: Teller 1 is free
  • Time 13.4: Teller 2 is free. No more people generated by simulation, so

we’re done.

Goes up to 5.83 (from 3.2)

slide-29
SLIDE 29

Here’s the general process for discrete event simulation loop

“State”: all variables that describe a system

  • Initialize (State)
  • Time ← 0
  • Initialize (futureEventSet)
  • While (futureEventSet not empty) do:
  • Event ← next event in futureEventSet (i.e., deleteMin)
  • Time ← Time (Event)
  • State ← function of old state and event
  • Update(futureEventSet)

Generic Simulation Program: Generic Simulation Program:

slide-30
SLIDE 30

Event Generation

Tricky part of simulation:

  • Choosing how to generate random events

Example: Say we want transaction times to average 10 minutes.

  • Here are 2 sequences that average 10:
  • 8 12 10 11 9 7 13
  • 0 0 0 0 0 0 0 0 0 100
  • But, these have very different characteristics

we need to define the distribution we’re interested in

slide-31
SLIDE 31

Distributions: Uniform

Distribution: Defines characterization of random numbers in more specific ways than just an average value Uniform distribution:

Mean m For our purposes, we’ll have random numbers 0 ≤ r ≤ 2m, with every value between 0 and 2m equally likely

slide-32
SLIDE 32

Uniform distributions in C+ + (pseudo-random number generators)

srand48(i): takes integer i and uses it as a seed to the random number generator (just do this once) drand48(): returns a double uniformly distributed in range [ 0,1)

  • drand is a random number generator that

follows a uniform distribution with a mean of 0.5

What do you do if you want a mean of m? Multiply the result of drand48 by 2m.

slide-33
SLIDE 33

Note on how pseudo-random number generators work

drand48() generates a sequence of 48-bit integers Xi according to following: srand48(seedval) sets the high order 32-bits of Xi to the argument seedval Note: these numbers aren’t really random.

  • Sequence repeats
  • Numbers are predictable

1

( )mod , for 0x5DEECE66D 0xB

n n

X aX c m n a c

+ =

+ ≥ = =

slide-34
SLIDE 34

Example of generating random numbers from uniform distribution

/* generate 20 random numbers using a uniform distribution with a mean of “mean” */ srand48(737); // provide an initial "seed" to random generator for (i = 0; i < 20; i++) { d = drand48()*mean*2; printf("%d \t %lf \n", i, d); }

slide-35
SLIDE 35

Uniform Distribution in Banking Example

Service time: represented by uniform distribution

slide-36
SLIDE 36

Distributions: Exponential

In our banking simulation: arrival time is represented by histogram distribution

slide-37
SLIDE 37

That’s all, folks.

Questions?

slide-38
SLIDE 38

Recall: How do we map “tree” structure to heap array?

2 5 9 7 2 6 12 11 6 1 2 3 4 5 6 7 8 9

2 5 6 9 7 2 6 12 11 1 2 3 4 5 6 7 8 9 10