Skip to content
Snippets Groups Projects
Commit 44abdf4d authored by pjs's avatar pjs
Browse files

Added sptf.rb demo

parent 6c5300b7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env ruby #!/usr/bin/env ruby
require_relative '../lib/simplekit' 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 include Comparable
# rank customers by their processing times, smallest first.
def <=>(other) def <=>(other)
processing_time <=> other.processing_time processing_time <=> other.processing_time
end end
...@@ -41,6 +34,7 @@ class SPTF ...@@ -41,6 +34,7 @@ class SPTF
@numAvailableServers = @maxServers @numAvailableServers = @maxServers
@q = PriorityQueue.new @q = PriorityQueue.new
schedule(:arrival, 0.0) schedule(:arrival, 0.0)
schedule(:close_doors, @closeTime)
end end
...@@ -69,21 +63,24 @@ class SPTF ...@@ -69,21 +63,24 @@ class SPTF
# the desired closing time, halt the simulation. # the desired closing time, halt the simulation.
def endService def endService
@numAvailableServers += 1 @numAvailableServers += 1
if (@q.empty?) unless @q.empty?
halt if model_time >= @closeTime
else
schedule(:beginService, 0.0) schedule(:beginService, 0.0)
end end
end end
# Commence shutdown by denying the next :arrival
def close_doors
cancel :arrival
end
# Exponential random variate generator. # Exponential random variate generator.
# param: rate - The rate (= 1 / mean) of the distribution. # param: rate - The rate (= 1 / mean) of the distribution.
# returns: A realization of the specified distribution. # returns: A realization of the specified distribution.
def exponential(rate) def exponential(rate)
-Math.log(rand) / rate -Math.log(rand) / rate
end end
end end
# Instantiate an SPTF object with a particular parameterization and run it. # 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment