home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / tests / sim2 < prev    next >
Encoding:
Text File  |  1987-12-30  |  2.2 KB  |  94 lines

  1. "
  2.      Simple Minded simulation from Chapter 6 of book
  3.  
  4.      IceCream Store -
  5.           single event queue
  6.           multiple group size
  7.           discrete probability on number of scoops selected
  8. "
  9. Class Main
  10. [
  11.      main      | i |
  12.           i <- IceCreamStore new.
  13.           [i time < 25] whileTrue: [ i proceed ].
  14.           i reportProfits
  15. ]
  16.  
  17. Class Simulation
  18. | currentTime nextEvent nextEventTime |
  19. [
  20.      new
  21.           currentTime <- 0
  22. |
  23.      time
  24.           ^ currentTime
  25. |
  26.      addEvent: event at: eventTime
  27.           nextEvent <- event.
  28.           nextEventTime <- eventTime
  29. |
  30.      proceed
  31.           currentTime <- nextEventTime.
  32.           self processEvent: nextEvent
  33. ]
  34.  
  35. Class IceCreamStore :Simulation
  36. | profit rand scoopDistribution |
  37. [
  38.      new
  39.           profit <- 0.
  40.           rand <- Random new.
  41.           (scoopDistribution <- DiscreteProbability new)
  42.                defineWeights: #(65 25 10).
  43.           self scheduleArrival
  44. |
  45.      scheduleArrival
  46.           self addEvent: Customer new
  47.                at: (self time + (rand randInteger: 5))
  48. |
  49.      processEvent: event
  50.           ('customer received at ', self time printString) print.
  51.           profit <- profit + ((self scoopsFor: event groupSize)  * 0.17 ).
  52.           self scheduleArrival
  53. |    
  54.      scoopsFor: group         | number |
  55.           number <- 0.
  56.           group timesRepeat:
  57.                [number <- number + scoopDistribution next].
  58.           ('group of ', group printString, ' have ', number
  59.           printString, ' scoops ') print.
  60.           ^ number
  61.  
  62. |
  63.      reportProfits
  64.           ('profits are ', profit printString) print
  65. ]
  66.  
  67. Class Customer
  68. | groupSize |
  69. [
  70.      new
  71.           groupSize <- (Random new "randomize" ) randInteger: 8
  72. |
  73.      groupSize
  74.           ^ groupSize
  75. ]
  76.  
  77. Class DiscreteProbability
  78. | weights rand max |
  79. [
  80.      defineWeights: anArray
  81.           weights <- anArray.
  82.           (rand <- Random new) "randomize".
  83.           max <- anArray inject: 0 into: [:x :y | x + y]
  84. |
  85.      next | index value |
  86.           value <- rand randInteger: max.
  87.           index <- 1.
  88.           [value > (weights at: index)]
  89.                whileTrue: [value <- value - (weights at: index).
  90.                          index <- index + 1].
  91.           ^ index
  92. ]
  93.  
  94.