diff --git a/demos/sptf.rb b/demos/sptf.rb index e19ad0d210095399ddcda041fdc4601ca254d129..34b0f0f1b02a15900fd66c4c600469c48f773526 100644 --- a/demos/sptf.rb +++ b/demos/sptf.rb @@ -1,18 +1,11 @@ #!/usr/bin/env ruby require_relative '../lib/simplekit' -require_relative '../lib/priority_queue' - -# Customer class to store arrival times and processing times. -class Customer - attr_reader :arrival_time, :processing_time - - def initialize(arrival, processing) - @arrival_time = arrival - @processing_time = processing - end +# Create a Customer Struct that will store arrival times and processing times. +Customer = Struct.new(:arrival_time, :processing_time) do include Comparable + # rank customers by their processing times, smallest first. def <=>(other) processing_time <=> other.processing_time end @@ -41,6 +34,7 @@ class SPTF @numAvailableServers = @maxServers @q = PriorityQueue.new schedule(:arrival, 0.0) + schedule(:close_doors, @closeTime) end @@ -69,21 +63,24 @@ class SPTF # the desired closing time, halt the simulation. def endService @numAvailableServers += 1 - if (@q.empty?) - halt if model_time >= @closeTime - else + unless @q.empty? schedule(:beginService, 0.0) end end + # Commence shutdown by denying the next :arrival + def close_doors + cancel :arrival + end + # Exponential random variate generator. # param: rate - The rate (= 1 / mean) of the distribution. # returns: A realization of the specified distribution. def exponential(rate) -Math.log(rand) / rate end - end # Instantiate an SPTF object with a particular parameterization and run it. -SPTF.new(4.5, 1.0, 5, 100.0).run +srand(9876543) # set seed for repeatability +SPTF.new(6, 1.0, 5, 20.0).run