Skip to content

Commit 68bb9df

Browse files
committed
Add Tutorial
1 parent 5d7a81b commit 68bb9df

9 files changed

Lines changed: 622 additions & 20 deletions

File tree

docs/make.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using Documenter
2+
using ResumableFunctions
23
using SimJulia
34

45
makedocs(
5-
modules = [SimJulia],
6-
clean = true,
76
format = :html,
8-
sitename = "SimJulia.jl",
7+
sitename = "SimJulia",
8+
authors = "Ben Lauwens",
99
pages = [
10-
"Overview" => "index.md",
11-
"Manual" => "manual.md",
10+
"Home" => "index.md",
11+
"Tutorial" => "tutorial.md",
12+
"Topical Guides" => "guides/index.md",
13+
"Examples" => ["Ross" => "examples/ross.md",],
14+
"API" => "api.md"
1215
]
1316
)
1417

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# API

docs/src/examples/ross.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Ross, Simulation 5th edition:
2+
3+
## A repair problem
4+
5+
### Source
6+
7+
Ross, Simulation 5th edition, Section 7.7, p. 124-126
8+
9+
### Description
10+
11+
A system needs $n$ working machines to be operational. To guard against machine breakdown, additional machines are kept available as spares. Whenever a machine breaks down it is immediately replaced by a spare and is itself sent to the repair facility, which consists of a single repairperson who repairs failed machines one at a time. Once a failed machine has been repaired it becomes available as a spare to be used when the need arises. All repair times are independent random variables having the common distribution function $G$. Each time a machine is put into use the amount of time it functions before breaking down is a random variable, independent of the past, having distribution function $F$.
12+
13+
The system is said to “crash” when a machine fails and no spares are available. Assuming that there are initially $n + s$ functional machines of which $n$ are put in use and $s$ are kept as spares, we are interested in simulating this system so as to approximate $E[T]$, where $T$ is the time at which the system crashes.
14+
15+
### Code
16+
17+
```jldoctest
18+
using Distributions
19+
using ResumableFunctions
20+
using SimJulia
21+
22+
const RUNS = 5
23+
const N = 10
24+
const S = 3
25+
const SEED = 150
26+
const LAMBDA = 100
27+
const MU = 1
28+
29+
srand(SEED)
30+
const F = Exponential(LAMBDA)
31+
const G = Exponential(MU)
32+
33+
@resumable function machine(sim::Simulation, repair_facility::Resource, spares::Store{Process})
34+
while true
35+
try
36+
@yield Timeout(sim, Inf)
37+
catch exc
38+
end
39+
@yield Timeout(sim, rand(F))
40+
get_spare = Get(spares)
41+
@yield get_spare | Timeout(sim, 0.0)
42+
state(get_spare) != SimJulia.idle ? interrupt(value(get_spare)) : throw(SimJulia.StopSimulation("No more spares!"))
43+
@yield Request(repair_facility)
44+
@yield Timeout(sim, rand(G))
45+
@yield Release(repair_facility)
46+
@yield Put(spares, active_process(sim))
47+
end
48+
end
49+
50+
@resumable function start_sim(sim::Simulation, repair_facility::Resource, spares::Store{Process})
51+
procs = Process[]
52+
for i=1:N
53+
push!(procs, @process machine(sim, repair_facility, spares))
54+
end
55+
@yield Timeout(sim, 0.0)
56+
for proc in procs
57+
interrupt(proc)
58+
end
59+
for i=1:S
60+
@yield Put(spares, @process machine(sim, repair_facility, spares))
61+
end
62+
end
63+
64+
function sim_repair()
65+
sim = Simulation()
66+
repair_facility = Resource(sim)
67+
spares = Store{Process}(sim)
68+
@process start_sim(sim, repair_facility, spares)
69+
msg = run(sim)
70+
stop_time = now(sim)
71+
println("At time $stop_time: $msg")
72+
stop_time
73+
end
74+
75+
results = Float64[]
76+
for i=1:RUNS
77+
push!(results, sim_repair())
78+
end
79+
println("Average crash time: ", sum(results)/RUNS)
80+
81+
# output
82+
83+
At time 5573.772841846017: No more spares!
84+
At time 1438.0294516073466: No more spares!
85+
At time 7077.413276961621: No more spares!
86+
At time 7286.490682742159: No more spares!
87+
At time 6820.788098062124: No more spares!
88+
Average crash time: 5639.298870243853
89+
```

docs/src/guides/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Topical Guides

docs/src/index.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
11
# Overview
22

3-
SimJulia is a discrete-event process-oriented simulation framework written in [Julia](http://julialang.org/) inspired by the Python library [SimPy](https://simpy.readthedocs.io/). Its preferred process dispatcher is based on semi-coroutines scheduling as implemented in [ResumableFunctions](git@github.com:BenLauwens/ResumableFunctions.jl.git). A `Process` in SimJulia is defined by a `@resumable function` yielding `Events`. SimJulia also provides three types of shared resources to model limited capacity congestion points: `Resources`, `Containers` and `Stores`. The API is modeled after the SimPy API but using some specific Julia semantics.
3+
SimJulia is a discrete-event process-oriented simulation framework written in [Julia](http://julialang.org/) inspired by the Python library [SimPy](https://simpy.readthedocs.io/). Its process dispatcher is based on semi-coroutines scheduling as implemented in [ResumableFunctions](https://github.com/BenLauwens/ResumableFunctions.jl.git). A `Process` in SimJulia is defined by a `@resumable function` yielding `Events`. SimJulia provides three types of shared resources to model limited capacity congestion points: `Resources`, `Containers` and `Stores`. The API is modeled after the SimPy API but some specific Julia semantics are used.
4+
5+
The documentation contains a tutorial, topical guides explaining key concepts, a number of examples and the API reference. The tutorial, the topical guides and some examples are borrowed from the SimPy to allow a direct comparison and an easy migration path for users. The differences between SimJulia and SimPy are clearly documented.
6+
7+
## Example
8+
9+
A short example simulating two clocks ticking in different time intervals looks like this:
10+
11+
```jldoctest
12+
julia> using ResumableFunctions
13+
14+
julia> using SimJulia
15+
16+
julia> @resumable function clock(sim::Simulation, name::String, tick::Float64)
17+
while true
18+
println(name, " ", now(sim))
19+
@yield Timeout(sim, tick)
20+
end
21+
end
22+
clock (generic function with 1 method)
23+
24+
julia> sim = Simulation()
25+
SimJulia.Simulation(0.0, DataStructures.PriorityQueue{SimJulia.BaseEvent,SimJulia.EventKey,Base.Order.ForwardOrdering}(), 0x0000000000000000, 0x0000000000000000, Nullable{SimJulia.AbstractProcess}())
26+
27+
julia> @process clock(sim, "fast", 0.5)
28+
SimJulia.Process 1
29+
30+
julia> @process clock(sim, "slow", 1.0)
31+
SimJulia.Process 3
32+
33+
julia> run(sim, 2)
34+
fast 0.0
35+
slow 0.0
36+
fast 0.5
37+
slow 1.0
38+
fast 1.0
39+
fast 1.5
40+
```
41+
42+
## Installation
43+
44+
SimJulia is a registered package and can be installed by running:
45+
```julia
46+
Pkg.add("SimJulia")
47+
```
48+
49+
## Authors
50+
51+
* Ben Lauwens, Royal Military Academy, Brussels, Belgium.
52+
53+
## License
54+
55+
SimJulia is licensed under the MIT "Expat" License.

docs/src/manual.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)