Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simplekit-ruby
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sanchez, Paul
simplekit-ruby
Commits
a581fda5
Commit
a581fda5
authored
6 years ago
by
pjs
Browse files
Options
Downloads
Patches
Plain Diff
Updated arg passing & demos
parent
62eeda72
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
demos/MyModelArgs.rb
+4
-4
4 additions, 4 deletions
demos/MyModelArgs.rb
demos/sptf.rb
+89
-0
89 additions, 0 deletions
demos/sptf.rb
lib/simplekit.rb
+3
-3
3 additions, 3 deletions
lib/simplekit.rb
with
96 additions
and
7 deletions
demos/MyModelArgs.rb
+
4
−
4
View file @
a581fda5
...
...
@@ -7,13 +7,13 @@ class MyModel
def
init
@x
=
1
schedule
(
:increment
,
rand
(
2
),
n:
1
,
c
:
97
)
schedule
(
:increment
,
rand
(
2
),
n:
1
,
c
har:
'z'
.
ord
)
end
def
increment
(
n
:,
c
:)
def
increment
(
n
:,
c
har
:)
@x
+=
n
schedule
(
:increment
,
2.0
*
rand
(
2
),
n:
@x
,
c
:
c
+
1
)
printf
"%f, %f, %c
\n
"
,
model_time
,
@x
,
c
schedule
(
:increment
,
2.0
*
rand
(
2
),
n:
@x
,
c
har:
char
-
1
)
printf
"%f, %f, %c
\n
"
,
model_time
,
@x
,
c
har
schedule
(
:halt
,
0.0
)
if
model_time
>
10
end
end
...
...
This diff is collapsed.
Click to expand it.
demos/sptf.rb
0 → 100644
+
89
−
0
View file @
a581fda5
#!/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
include
Comparable
def
<
=>
(
other
)
processing_time
<=>
other
.
processing_time
end
end
# Demonstration model of a shortest-processing-time-first queueing system.
# There are k servers and both the arrival and service processes could
# be anything.
class
SPTF
include
SimpleKit
# Constructor - initializes the model parameters.
# param: arrivalRate - The rate at which customers arrive to the system.
# param: serviceRate - The rate at which individual servers serve.
# param: maxServers - The total number of servers in the system.
# param: closeTime - The time the server would like to shut down.
def
initialize
(
arrivalRate
,
serviceRate
,
maxServers
,
closeTime
)
@arrivalRate
=
arrivalRate
@serviceRate
=
serviceRate
@maxServers
=
maxServers
@closeTime
=
closeTime
end
# Initialize the model state and schedule any necessary events.
def
init
@numAvailableServers
=
@maxServers
@q
=
PriorityQueue
.
new
schedule
(
:arrival
,
0.0
)
end
# An arrival event generates a customer and their associated service
# time, adds the customer to the queue, schedules the next arrival,
# and schedules a beginService event if a server is available.
def
arrival
@q
.
push
Customer
.
new
(
model_time
,
exponential
(
@serviceRate
))
schedule
(
:arrival
,
exponential
(
@arrivalRate
))
schedule
(
:beginService
,
0.0
)
if
(
@numAvailableServers
>
0
)
end
# Start service for the first customer in line, removing that
# customer from the queue and utilizing one of the available
# servers. An endService will be scheduled. Report the current
# time and how long this customer spent in line.
def
beginService
current_customer
=
@q
.
pop
@numAvailableServers
-=
1
schedule
(
:endService
,
current_customer
.
processing_time
)
printf
"%f,%f
\n
"
,
model_time
,
model_time
-
current_customer
.
arrival_time
end
# Frees up an available server, and schedules a beginService if
# anybody is waiting in line. If the line is empty and it's after
# the desired closing time, halt the simulation.
def
endService
@numAvailableServers
+=
1
if
(
@q
.
empty?
)
halt
if
model_time
>=
@closeTime
else
schedule
(
:beginService
,
0.0
)
end
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
This diff is collapsed.
Click to expand it.
lib/simplekit.rb
+
3
−
3
View file @
a581fda5
...
...
@@ -55,7 +55,7 @@ module SimpleKit
# at invocation time.
def
schedule
(
event
,
delay
,
**
args
)
raise
'Model scheduled event with negative delay.'
if
delay
<
0
@event_list
.
push
EventNotice
.
new
(
event
,
@model_time
,
delay
,
args
)
@event_list
.
push
EventNotice
.
new
(
event
,
@model_time
,
delay
,
**
args
)
end
# Start execution of a model. The simulation +model_time+ is initialized
...
...
@@ -71,7 +71,7 @@ module SimpleKit
if
current_event
.
args
.
empty?
@user_model
.
send
(
current_event
.
event
)
else
@user_model
.
send
(
current_event
.
event
,
current_event
.
args
)
@user_model
.
send
(
current_event
.
event
,
**
current_event
.
args
)
end
end
end
...
...
@@ -88,7 +88,7 @@ module SimpleKit
private
class
EventNotice
attr_reader
:event
,
:time
,
:time_stamp
,
:args
def
initialize
(
event
,
time
,
delay
,
**
args
)
def
initialize
(
event
,
time
,
delay
,
args
)
@event
=
event
@time_stamp
=
time
@time
=
time
+
delay
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment