LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
sum_closing.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_SUM_SUM_CLOSING_H
6#define LINE_API_SUM_SUM_CLOSING_H
7
8/**
9 * @file
10 * @ingroup api_sum
11 * Closing method for open and mixed non-product-form queueing networks,
12 * solved with the summation method.
13 *
14 * Templated port of matlab/src/api/sum/sum_closing.m, cross-checked against
15 * jar/src/main/java/jline/api/sum/Sum_closing.java.
16 *
17 * The external world of the open classes is replaced by one extra -/G/1
18 * station with demand 1/(Ropen lambda0_r) for open class r, service SCV equal
19 * to that class's interarrival SCV, and unit visit ratio. The resulting closed
20 * network is solved by sum_closed with a large closing population Kclosed for
21 * the open classes (5000 by default, the value recommended for the summation
22 * method). Closed classes pass through untouched, which is what makes the
23 * method applicable to mixed networks. The open-class throughput approaches
24 * lambda0 from below as Kclosed grows.
25 *
26 * Reference: G. Bolch, S. Greiner, H. de Meer, K.S. Trivedi, Queueing Networks
27 * and Markov Chains, 2nd ed., Wiley, 2006, Sec. 10.1.5.
28 *
29 * ARITHMETIC: it is a wrapper around sum_closed, whose bisection stops on a
30 * tolerance, so it carries the same has_transcendental gate. The closing
31 * itself is exact: it only builds one extra row of demands.
32 *
33 * MATLAB marks the open classes by N(r) = Inf; here the class is open exactly
34 * when lambda0(r) > 0, and the N entry of an open class is ignored (it is
35 * overwritten by Kclosed), so no infinite population ever has to be
36 * represented in the number type.
37 */
38
39#include <cstddef>
40#include <vector>
41
43#include "line/num/number.h"
44#include "line/util/error.h"
45#include "line/util/matrix.h"
46
47namespace line {
48namespace sum {
49
50/** Mirrors the [XN, QN, UN, RN, TN, it] return list of the MATLAB function. */
51template <class T>
53 std::vector<T> XN; ///< (R) class throughputs
54 Matrix<T> QN; ///< (M x R) queue lengths at the original stations
55 Matrix<T> UN; ///< (M x R) utilizations at the original stations
56 Matrix<T> RN; ///< (M x R) residence times at the original stations
57 std::vector<T> TN; ///< (R) mean response time in the original network
58 std::size_t it = 0;
59};
60
61/** Closing controls; Kclosed is the population given to the open classes. */
63 long Kclosed = 5000;
65};
66
67/**
68 * @brief Closing method for open and mixed non-product-form queueing
69 * networks, solved with the summation method.
70 *
71 * @param lambda0 (R) external arrival rates, zero for a closed class
72 * @param scva (R) interarrival-time SCVs of the open classes, 1 if Poisson
73 * @param L (M x R) service demands, open-class visits per external arrival
74 * @param mi (M) servers per station
75 * @param scv (M x R) service-time SCVs
76 * @param N (R) populations of the closed classes; entries of open classes are ignored
77 * @param Z (R) think times
78 * @param options tolerances, iteration caps and the closing method
79 */
80template <class T>
81SumClosingResult<T> sum_closing(const std::vector<T>& lambda0, const std::vector<T>& scva,
82 const Matrix<T>& L, const std::vector<Servers>& mi,
83 const Matrix<T>& scv, const std::vector<long>& N,
84 const std::vector<T>& Z,
85 const ClosingOptions& options = ClosingOptions()) {
87 "sum_closing requires transcendental arithmetic: it drives sum_closed, whose "
88 "bisection stops on a tolerance");
89 const std::size_t M = L.rows(), R = L.cols();
90 if (lambda0.size() != R || scva.size() != R || N.size() != R || Z.size() != R)
91 throw InputError("sum_closing: an input disagrees with L on the class count");
92 if (mi.size() != M) throw InputError("sum_closing: L and mi disagree on the station count");
93 if (scv.rows() != M || scv.cols() != R) throw InputError("sum_closing: scv has the wrong shape");
94 if (options.Kclosed <= 0) throw InputError("sum_closing: the closing population must be positive");
95
96 const T zero = num_traits<T>::from_int(0);
97 const T one = num_traits<T>::from_int(1);
98
99 std::vector<std::size_t> open;
100 for (std::size_t r = 0; r < R; ++r)
101 if (lambda0[r] > zero) open.push_back(r);
102 if (open.empty())
103 throw InputError("sum_closing: no open class, use sum_closed for closed networks");
104 const long Ropen = static_cast<long>(open.size());
105
106 // augment with the closing -/G/1 station, visited by the open classes only
107 Matrix<T> Laug(M + 1, R, zero);
108 Matrix<T> scvaug(M + 1, R, one);
109 for (std::size_t i = 0; i < M; ++i)
110 for (std::size_t r = 0; r < R; ++r) {
111 Laug(i, r) = L(i, r);
112 scvaug(i, r) = scv(i, r);
113 }
114 std::vector<long> Naug = N;
115 for (std::size_t r : open) {
116 Laug(M, r) = one / (num_traits<T>::from_int(Ropen) * lambda0[r]);
117 scvaug(M, r) = scva[r];
118 Naug[r] = options.Kclosed;
119 }
120 std::vector<Servers> miaug = mi;
121 miaug.push_back(Servers::of(1));
122
123 const SumClosedResult<T> in = sum_closed(Laug, Naug, Z, miaug, scvaug, options.sum);
124
126 out.XN = in.XN;
127 out.it = in.it;
128 out.QN = Matrix<T>(M, R, zero);
129 out.UN = Matrix<T>(M, R, zero);
130 out.RN = Matrix<T>(M, R, zero);
131 for (std::size_t i = 0; i < M; ++i)
132 for (std::size_t r = 0; r < R; ++r) {
133 out.QN(i, r) = in.QN(i, r);
134 out.UN(i, r) = in.UN(i, r);
135 out.RN(i, r) = in.RN(i, r);
136 }
137 out.TN.assign(R, zero);
138 for (std::size_t r = 0; r < R; ++r) {
139 if (out.XN[r] > zero) {
140 T q = zero;
141 for (std::size_t i = 0; i < M; ++i) q += out.QN(i, r);
142 out.TN[r] = q / out.XN[r];
143 }
144 }
145 return out;
146}
147
148} // namespace sum
149} // namespace line
150
151#endif // LINE_API_SUM_SUM_CLOSING_H
InputError(const std::string &what)
Definition error.h:39
T sum() const
Definition matrix.h:117
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
The exception types the port throws.
Dense matrix and non-owning view.
SumClosedResult< T > sum_closed(const Matrix< T > &L, const std::vector< long > &N, const std::vector< T > &Z, const std::vector< Servers > &mi, const Matrix< T > &scv, const SumOptions &options=SumOptions())
Summation method (SUM) and its extension (ESUM) for closed queueing networks, including non-product-f...
Definition sum_closed.h:196
SumClosingResult< T > sum_closing(const std::vector< T > &lambda0, const std::vector< T > &scva, const Matrix< T > &L, const std::vector< Servers > &mi, const Matrix< T > &scv, const std::vector< long > &N, const std::vector< T > &Z, const ClosingOptions &options=ClosingOptions())
Closing method for open and mixed non-product-form queueing networks, solved with the summation metho...
Definition sum_closing.h:81
Number-type abstraction for the templated API port.
Closing controls; Kclosed is the population given to the open classes.
Definition sum_closing.h:62
static Servers of(long m)
Definition sum_closed.h:68
Mirrors the [XN, QN, UN, RN, it] return list of the MATLAB function.
Definition sum_closed.h:84
Matrix< T > QN
(M x R) mean queue lengths
Definition sum_closed.h:86
Matrix< T > UN
(M x R) utilizations, per server at queueing stations
Definition sum_closed.h:87
std::size_t it
iterations of the outer loop
Definition sum_closed.h:89
Matrix< T > RN
(M x R) residence times, QN/XN
Definition sum_closed.h:88
std::vector< T > XN
(R) class throughputs
Definition sum_closed.h:85
Mirrors the [XN, QN, UN, RN, TN, it] return list of the MATLAB function.
Definition sum_closing.h:52
std::vector< T > XN
(R) class throughputs
Definition sum_closing.h:53
Matrix< T > QN
(M x R) queue lengths at the original stations
Definition sum_closing.h:54
Matrix< T > UN
(M x R) utilizations at the original stations
Definition sum_closing.h:55
std::vector< T > TN
(R) mean response time in the original network
Definition sum_closing.h:57
Matrix< T > RN
(M x R) residence times at the original stations
Definition sum_closing.h:56
Convergence controls, mirroring the trailing (tol, maxiter) arguments.
Definition sum_closed.h:93
Summation method (SUM) and its extension (ESUM) for closed queueing networks, including non-product-f...