1 / 19

CS1001 Lecture 18

CS1001 Lecture 18. Simulation Section 7.2, 7.3 Using 1-D arrays in Project. Simulation. Study the dynamic behavior of a real world process using a model Two types of models:

Download Presentation

CS1001 Lecture 18

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1001 Lecture 18 • Simulation • Section 7.2, 7.3 • Using 1-D arrays in Project

  2. Simulation • Study the dynamic behavior of a real world process using a model • Two types of models: • deterministic -- modeling a process with a set of equations (e.g., from text: electrical circuits, post office stamp machine) • stochastic -- modeling a process that involves randomness (e.g., arrival of people in grocery stores)

  3. PO Stamp Machine • Customer input money and stamp wanted • Deterministic -- users have a set of input and associated known output • Stamp machine • ask for more money, if more is needed • give stamps and change

  4. PO Stamp Machine Breaking the problem down Stamp Machine Get User Order Process Order Output Stamp Needed Money Input Calculate Change Ask for $ Give Stamp Give Change

  5. BEGIN FINISH $ enough ? Ask for $ Get Input Program Design (Partial) Compute Cost Compute Need No yes Compute Change Process Additional $ yes No More Stamps ?

  6. Get Input Get User Input PRINT *, “Insert $20, $10, $5, or $1” READ *, Bill Item = 0 DO IF (Item .EQ. 9) EXIT PRINT *, “Enter item from menu:” PRINT *, “1 for 50 33-cent stamps ($16.50)” PRINT *, “2 for 25 33-cent stamps ($8.25)” PRINT *, “3 for 10 33-cent stamps ($3.30)” PRINT *, “4 for 2 32-cent stamps ($0.66)” PRINT *, “9 when finished” READ *, Item Cost = LookUp(Item) Compute Cost

  7. FUNCTION LookUp(Var) • INTEGER, INTENT(IN) :: Var • IF (Var == 1) THEN • LookUp = 16.50 • ELSE IF (Var == 2) THEN • LookUp = 8.25 • ELSE IF (Var == 3) THEN • LookUp = 3.30 • etc. Compute Cost

  8. $ enough ? Ask for $ Compute Need No yes Compute Change Process Additional $ • IF ((Cost <= Bill) .AND. (Item == 9)) THEN • CALL Change(Bill, Cost, Dollar, Quarter, Dime, Nickle) • ELSE IF (Cost > Bill) THEN • Need = Cost - Bill • More = Paper(Need) • PRINT *, “Insert $”, More • :

  9. FUNCTION Paper(Need) • INTEGER :: Paper • INTEGER, INTENT(IN) :: Need • IF (Need .GE. 5 .AND. Need .LE. 100) THEN • Paper = 1 • ELSE IF (Need .GT. 100 .AND. Need .LE. 500) THEN • Paper = 5 • ELSE IF (Need .GT. 500 .AND. Need .LE. 1000) THEN • Paper = 10 • : • : • END FUNCTION Paper

  10. SUBROUTINE Change(Bill, Cost, Dollar, Quarter, Dime, Nickle) • INTEGER, INTENT(IN) :: Bill, Cost • INTEGER, INTENT(OUT) :: Dollar, Quarter, Dime, Nickle • INTEGER :: Diff • Diff = Bill - Cost • IF (Diff .GT. 0) THEN • Dollar = Diff / 100 • Diff = MOD(Diff, 100) • Quarter = Diff / 25 • Diff = MOD(Diff, 25) • : • : • END SUBROUTINE Change

  11. Project Introduction Grocery Simulation Get parameters Run Simulation Output Statistics Simulation Clock Generate Customers Checkout Customers Gather Statistics Stochastic processes because customer arrival and customer checkout times are random

  12. Get Parameters Output Statistics BEGIN FINISH Finish Clock ? Program Design - Simple New customers Customers to Queues Calculate statistics Process Checkout lines Gather statistics No yes

  13. Grocery Store Program PROGRAM Grocery ! Header Information ! Declarations PRINT *, “…” ! Get Parameters READ *, … ! Initialization DO Clock= 1, Nclock ! Run Simulation New_customer Add_to_Queue (…) Check_out Gather_Stat END DO Compute_Stat ! Compute statistics Print *, … ! Output statistics END PROGRAM Grocery

  14. Checkout station • Same data type structure, at most 15 of them • Instead of checkout1, checkout2, … checkout15, represent them and their properties by arrays e.g., checkout(1), checkout(2),…checkout(15) represent checkout queue 1,2, …15 e.g., number of customers in each checkout station and idle time for each station INTEGER, DIMENSION(15) : Ncustomers, Idle_time Ncustomers(I) is the number of customers in queue I Idle_time(I) is the Idle_time accumulated for queue I

  15. Queue Data: Ncustomers • Given nstations as user input for the number of check out stations ==> number of queues needed • initialization: DO I=1,nstations Ncustomers(I) = 0 Idle_time(I) = 0 END DO

  16. Ncustomers(1) Ncustomers(2) : 5645387564 • Finding the line with least number of customers: shortestline=1 DO I=2, nstations IF (Ncustomers(I) .LT. Ncustomers(shortestline)) & shortestline=I END DO Initially: Shortest line is first line shortest I = 2 Ncustomers(2) .LT. Ncustomers(1) 1 I = 2 Ncustomers(3) .LT. Ncustomers(1)  3 I = 2 Ncustomers(4) .LT. Ncustomers(3) I = 2 Ncustomers(5) .LT. Ncustomers(3)  5 I = 2 Ncustomers(6) .LT. Ncustomers(5) I = 2 Ncustomers(7) .LT. Ncustomers(5) I = 2 Ncustomers(8) .LT. Ncustomers(5) I = 2 Ncustomers(9) .LT. Ncustomers(5) I = 2 Ncustomers(10) .LT. Ncustomers(5)

  17. Add_to_Queue SUBROUTINE Add_to_queue ! Find shortest line shortestline=1 DO I=2, nstations IF (Ncustomers(I) .LT. Ncustomers(shortestline)) & shortestline=I END DO ! customer to a checkout Call AddTo (shortestline) ! Other book keeping activities Ncustomers(shortestline) = Ncustomers(shortestline)+1 END SUBROUTINE Add_to_queue Customers to Queues AddTo (I) -- a subroutine that will add a customer to checkout I

  18. Checkout Customers SUBROUTINE Check_out DO I = 1, nstations ! If no customer, then checkout queue is idle IF (Ncustomers(I) .EQ. 0) THEN Idle_time(I) = Idle_time(I)+1 ELSE IF (checkout_time(I) .EQ. Time) THEN ! Check to see whether Call POP_queue (I) Ncustomers(I) = Ncustomers(I) - 1 END IF END DO END SUBROUTINE Check_out POP_queue(I) -- a subroutine that will remove a customer from checkout I

  19. Checkout_time - initial ideas • Checkout time may be represented by an array • Checkout_time(15) • Checkout_time(I) is the time when a customer in checkout line I is finished • Checkout_time • Use -1 as value to indicate that no checkout_time is set • At the beginning - initialized each element to -1 • If there is no customer in checkout line I, then checkout_time(I) = -1. • At clock time k, if a customer is checked out and there is another customer in line, then ...

More Related