-
Notifications
You must be signed in to change notification settings - Fork 30
/
MM1Queueing.cs
109 lines (95 loc) · 4.41 KB
/
MM1Queueing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#region License Information
/*
* This file is part of SimSharp which is licensed under the MIT license.
* See the LICENSE file in the project root for more information.
*/
#endregion
using System;
using System.Collections.Generic;
using static SimSharp.Distributions;
namespace SimSharp.Samples {
public class MM1Queueing {
private static readonly ExponentialTime OrderArrival = EXP(TimeSpan.FromMinutes(3.33));
private static readonly ExponentialTime ProcessingTime = EXP(TimeSpan.FromMinutes(2.5));
private IEnumerable<Event> Source(Simulation env, Resource server) {
while (true) {
yield return env.Timeout(OrderArrival);
env.Process(Order(env, server));
}
}
private IEnumerable<Event> Order(Simulation env, Resource server) {
using (var req = server.Request()) {
yield return req;
yield return env.Timeout(ProcessingTime);
}
}
private IEnumerable<Event> HandleWarmup(Simulation env, TimeSpan warmupTime, params IMonitor[] monitors) {
foreach (var mon in monitors) mon.Active = false;
yield return env.Timeout(warmupTime);
foreach (var mon in monitors) mon.Active = true;
}
public void Simulate(int repetitions = 5) {
var lambda = 1 / OrderArrival.Mean.TotalDays;
var mu = 1 / ProcessingTime.Mean.TotalDays;
var rho = lambda / mu;
var analyticWIP = rho / (1 - rho);
var analyticLeadtime = 1 / (mu - lambda);
var analyticWaitingtime = rho / (mu - lambda);
var env = new Simulation(randomSeed: 1, defaultStep: TimeSpan.FromDays(1));
var utilization = new TimeSeriesMonitor(env, name: "Utilization");
var wip = new TimeSeriesMonitor(env, name: "WIP", collect: true);
var leadtime = new SampleMonitor(name: "Lead time", collect: true);
var waitingtime = new SampleMonitor(name: "Waiting time", collect: true);
env.Log("Analytical results of this system:");
env.Log("Time\tUtilization.Mean\tWIP.Mean\tLeadtime.Mean\tWaitingTime.Mean");
env.Log("{4}\t{0}\t{1}\t{2}\t{3}", rho, analyticWIP, analyticLeadtime, analyticWaitingtime, double.PositiveInfinity);
env.Log("");
// example to create a running report of these measures every simulated week
//var report = Report.CreateBuilder(env)
// .Add("Utilization", utilization, Report.Measures.Mean | Report.Measures.StdDev)
// .Add("WIP", wip, Report.Measures.Min | Report.Measures.Mean | Report.Measures.Max)
// .Add("Leadtime", leadtime, Report.Measures.Min | Report.Measures.Mean | Report.Measures.Max)
// .Add("WaitingTime", waitingtime, Report.Measures.Min | Report.Measures.Mean | Report.Measures.Max)
// .SetOutput(env.Logger) // use a "new StreamWriter("report.csv")" to direct to a file
// .SetSeparator("\t")
// .SetPeriodicUpdate(TimeSpan.FromDays(7), withHeaders: true)
// .Build();
var summary = Report.CreateBuilder(env)
.Add("Utilization", utilization, Report.Measures.Mean)
.Add("WIP", wip, Report.Measures.Mean)
.Add("Leadtime", leadtime, Report.Measures.Mean)
.Add("WaitingTime", waitingtime, Report.Measures.Mean)
.SetOutput(env.Logger)
.SetSeparator("\t")
.SetFinalUpdate(withHeaders: false) // creates a summary of the means at the end
.SetTimeAPI(useDApi: true)
.Build();
env.Log("Simulated results of this system (" + repetitions + " repetitions):");
env.Log("");
summary.WriteHeader(); // write the header just once
for (var i = 0; i < repetitions; i++) {
env.Reset(i + 1); // reset environment
utilization.Reset(); // reset monitors
wip.Reset();
leadtime.Reset();
waitingtime.Reset();
var server = new Resource(env, capacity: 1) {
Utilization = utilization,
WIP = wip,
LeadTime = leadtime,
WaitingTime = waitingtime,
};
env.Process(Source(env, server));
env.Process(HandleWarmup(env, TimeSpan.FromDays(32), utilization, wip, leadtime, waitingtime));
env.Run(TimeSpan.FromDays(365));
}
env.Log("");
env.Log("Detailed results from the last run:");
env.Log("");
env.Log(utilization.Summarize());
env.Log(wip.Summarize(maxBins: 10, binWidth: 2));
env.Log(leadtime.Summarize(maxBins: 10, binWidth: 5 / 1440.0));
env.Log(waitingtime.Summarize(maxBins: 10, binWidth: 4 / 1440.0)); ;
}
}
}