LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1_setup.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_QSYS_QSYS_MG1_SETUP_H
6#define LINE_API_QSYS_QSYS_MG1_SETUP_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * M/G/1 with setup times: the server switches off when empty and needs a setup
12 * time U before serving the job that finds it off.
13 *
14 * Port of matlab/src/api/qsys/qsys_mg1_setup.m. By the decomposition of
15 * Fuhrmann and Cooper, the waiting time is the M/G/1 waiting time plus the
16 * equilibrium excess of the setup period,
17 *
18 * Wq = lambda E[S^2] / (2 (1 - rho)) + (2 E[U] + lambda E[U^2]) / (2 (1 + lambda E[U])).
19 *
20 * ARITHMETIC: field. Only the squared coefficient of variation appears, so the
21 * computation is exact for T = Rational.
22 */
23
24#include <string>
25
28#include "line/num/number.h"
29#include "line/util/error.h"
30
31namespace line {
32namespace qsys {
33
34/** MATLAB's [W, Wq, U, Q, res] outputs. */
35template <class T>
37 T W; ///< mean sojourn time
38 T Wq; ///< mean waiting time
39 T U; ///< utilization rho
40 T Q; ///< mean number in system lambda W
41 QsysResT<T> res; ///< meanWaitingTime ... probSetup, analyzer
42};
43
44/**
45 * @brief M/G/1 with setup times, setup given by its first two moments.
46 *
47 * @param lambda arrival rate
48 * @param mu service rate
49 * @param cs coefficient of variation of the service time
50 * @param EU mean setup time
51 * @param EU2 second moment of the setup time
52 */
53template <class T>
54Mg1SetupResult<T> qsys_mg1_setup(const T& lambda, const T& mu, const T& cs, const T& EU, const T& EU2) {
55 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
56 const T two = num_traits<T>::from_int(2);
57 if (lambda <= zero || mu <= zero || cs < zero)
58 throw InputError(ErrorKind("Argument"),
59 "qsys_mg1_setup: lambda and mu must be positive scalars and cs non-negative");
60 const T EUsq = T(EU * EU);
61 const T tol = T(num_traits<T>::from_double(1e-12) * (EUsq > one ? EUsq : one));
62 if (EU < zero || EU2 < T(EUsq - tol))
63 throw InputError(ErrorKind("Argument"),
64 "qsys_mg1_setup: setup moments must satisfy EU >= 0 and EU2 >= EU^2");
65 const T rho = T(lambda / mu);
66 if (rho >= one) resdetail::throw_unstable("qsys_mg1_setup", "rho", num_traits<T>::to_double(rho));
67 const T ES2 = T((one + cs * cs) / (mu * mu));
69 r.Wq = T(lambda * ES2 / (two * (one - rho)) + (two * EU + lambda * EU2) / (two * (one + lambda * EU)));
70 r.W = T(r.Wq + one / mu);
71 r.U = rho;
72 r.Q = T(lambda * r.W);
73 r.res.set("meanWaitingTime", r.Wq);
74 r.res.set("meanSojournTime", r.W);
75 r.res.set("meanQueueLength", r.Q);
76 r.res.set("meanQueueLengthQ", T(lambda * r.Wq));
77 r.res.set("utilization", rho);
78 r.res.set("probIdle", T((one - rho) / (one + lambda * EU)));
79 r.res.set("probSetup", T((one - rho) * lambda * EU / (one + lambda * EU)));
80 r.res.analyzer = "exact:M/G/1-setup";
81 return r;
82}
83
84/**
85 * @brief M/G/1 with setup times, setup given as a Distribution (MATLAB passes
86 * it in place of EU and reads its mean and variance).
87 */
88template <class T>
89Mg1SetupResult<T> qsys_mg1_setup(const T& lambda, const T& mu, const T& cs, const lang::Distrib<T>& U) {
90 const T EU = lang::dist_moment(U, 1);
91 return qsys_mg1_setup(lambda, mu, cs, EU, T(lang::dist_moment(U, 2)));
92}
93
94} // namespace qsys
95} // namespace line
96
97#endif // LINE_API_QSYS_QSYS_MG1_SETUP_H
InputError(const std::string &what)
Definition error.h:105
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
T dist_moment(const Distrib< T > &d, unsigned k)
The k-th raw moment.
void throw_unstable(const char *fn, const char *what, double v)
A LINE:input:Unstable refusal "fn: unstable system: <what> = %g >= 1".
Definition qsys_res.h:213
Mg1SetupResult< T > qsys_mg1_setup(const T &lambda, const T &mu, const T &cs, const T &EU, const T &EU2)
M/G/1 with setup times, setup given by its first two moments.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:52
Number-type abstraction for the templated API port.
The res struct the exact queueing-system functions return, and the numeric helpers those functions sh...
ErrorKind(std::string n)
Definition error.h:39
MATLAB's [W, Wq, U, Q, res] outputs.
QsysResT< T > res
meanWaitingTime ... probSetup, analyzer
T Q
mean number in system lambda W
MATLAB's res struct: named measures in insertion order, plus res.analyzer.
Definition qsys_res.h:57
std::string analyzer
Definition qsys_res.h:59
void set(const std::string &name, const T &v)
Sets (or overwrites) a scalar field.
Definition qsys_res.h:62