-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation.R
95 lines (79 loc) · 2.86 KB
/
simulation.R
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
##### Simulation.R #####
## This file holds the necessary functions for the simulation.
# Libraries:
# circFbm
library("dvfBm")
# Best plots EU
library("ggplot2")
source("BeranWhittle.R") #' CetaFGN()
#' Build a fBm flow following the Kelly model.
#' First a cumulative FBM flow is built and then divided into FGN increments
#' that are easier to use in the SNC setting
#' @param rate constant arrival rate.
#' @param sample_length point in time until which the traffic
#' should be generated.
#' @param std_dev standard_deviation.
#' @return sample path of flow increments.
build_flow <- function(arrival_rate, hurst, sample_length, std_dev = 1.0) {
# Previously:
#' cumuflow <- rep(NA, sample_length)
#' flow <- rep(NA, sample_length)
#' fbm <- circFBM(n, hurst, plotfBm = FALSE)
#' for (t in 1:n) {
#' cumuflow[t] <- arrival_rate * t + std_dev * fbm[t]
#' if (t >= 2) {
#' flow[t] <- cumuflow[t] - cumuflow[t - 1]
#' } else {
#' flow[t] <- cumuflow[t]
#' }
#' }
#' return(flow)
# changed fbm to fbm * (sample_length ** hurst) as the package documentations
# says that fbm is otherwise only created in the interval
# 0, ..., (n - 1) / n
fbm <- circFBM(n = sample_length, H = hurst, plotfBm = FALSE) * (
sample_length ** hurst)
# work_around to avoid completely negative traffic
# while (mean(fbm) < 0) {
# fbm <- circFBM(n, hurst, plotfBm = FALSE)
# }
cumuflow <- arrival_rate * (1:sample_length) + std_dev * fbm
cumuflow_shift <- c(0, cumuflow[-length(cumuflow)])
flow_increments <- cumuflow - cumuflow_shift
return(flow_increments)
}
#' @example
# print(build_flow(arrival_rate = 1.0, hurst = 0.7, n = 2 ** 12,
# std_dev = 1.0))
#' Computes the empricial backlog distribution for a specific point in time
#' for FGN arrivals with mean arrival rate, Hurst parameter hurst and
#' standard deviation std_dev at a server with the given server rate
compute_distribution <- function(
arrival_rate, hurst, sample_length, time_n, server_rate, std_dev = 1.0,
iterations = 10 ** 3) {
backlogs <- rep(NA, iterations)
for (i in 1:iterations) {
flow <- build_flow(
arrival_rate = arrival_rate, hurst = hurst,
sample_length = sample_length, std_dev = std_dev)
b <- 0
for (k in 1:time_n) {
# Lindley's equation:
b <- max(b + flow[k] - server_rate, 0)
}
backlogs[i] <- b
.show_progress(i, iterations, prog_msg = "compute_distribution()")
}
return(backlogs)
}
#' @example
# print(compute_distribution(
# arrival_rate = 1.0, hurst = 0.7, sample_length = 10 ** 4,
# time_n = 10 ** 4, server_rate = 2.0, std_dev = 1.0,
# iterations = 10 ** 3))
.show_progress <- function(it, max_iterations, prog_msg = "progess") {
perc <- round(max_iterations / 10)
if (it %% perc == 0) {
print(paste0(prog_msg, ": ", it * 100 / max_iterations, "%"))
}
}