LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_nc_matching.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_SOLVERS_NC_SOLVER_NC_MATCHING_H
6#define LINE_SOLVERS_NC_SOLVER_NC_MATCHING_H
7
8/**
9 * SolverNC route for a Matching (SchedStrategy::FCFM) node: the exact analysis
10 * of an open model Source -> Matching -> Sink, the FCFM stochastic matching
11 * model of Comte (2022), through `qsys_match_fcfm`.
12 *
13 * Twins of MATLAB `sn_is_matching_model.m`, `sn_matching_refusal.m` and
14 * `solver_nc_matching_analyzer.m`, and of Java `Solver_nc_matching_analyzer`.
15 */
16
17#include <cmath>
18#include <cstddef>
19#include <limits>
20#include <string>
21#include <vector>
22
27#include "line/util/error.h"
28#include "line/util/matrix.h"
29
30namespace line {
31namespace nc {
32
33namespace matching_detail {
34
35/** 1-based index of the first Matching (FCFM) station, 0 when there is none. */
36template <class T>
38 for (std::size_t i = 1; i <= sn.stations.size(); ++i)
39 if (qn::matching::is_matching(sn, i)) return i;
40 return 0;
41}
42
43/** 1-based index of the Source station, 0 when there is none. */
44template <class T>
46 for (std::size_t i = 1; i <= sn.stations.size(); ++i) {
47 const std::size_t ind = sn.node_of_station(i);
48 if (ind != 0 && sn.nodes[ind - 1].nodetype == qn::NodeType::Source) return i;
49 }
50 return 0;
51}
52
53} // namespace matching_detail
54
55/** True when the model has a Matching (FCFM) station. */
56template <class T>
60
61/**
62 * "" when a model with a Matching node fits the exact product-form route,
63 * otherwise the reason it does not: open classes only with Poisson arrivals,
64 * the Matching node as the only station besides the Source, Source -> Matching
65 * -> Sink routing without class switching, no patience, and every visiting
66 * class compatible with some class.
67 */
68template <class T>
70 const std::size_t qist = matching_detail::matching_station(sn);
71 if (qist == 0) return "The model has no Matching node.";
72 const std::size_t K = sn.nclasses;
73 for (std::size_t r = 0; r < K; ++r)
74 if (!std::isinf(sn.classes[r].population)) return "A Matching node accepts open classes only.";
75 const std::size_t srcist = matching_detail::source_station(sn);
76 std::size_t nfcfm = 0;
77 bool others = false;
78 for (std::size_t i = 1; i <= sn.stations.size(); ++i) {
79 if (qn::matching::is_matching(sn, i)) ++nfcfm;
80 if (i != srcist && i != qist) others = true;
81 }
82 if (others || nfcfm > 1 || srcist == 0)
83 return "SolverNC solves a Matching node in isolation (Source -> Matching -> Sink); "
84 "other stations in the same model are not supported. Use SolverCTMC.";
85 if (sn.nchains != sn.nclasses) return "SolverNC does not support class switching around a Matching node.";
86 const std::vector<double> lam = qn::matching::source_rates(sn);
87 for (std::size_t r = 0; r < K; ++r)
88 if (lam[r] > 0 && sn.procid(srcist, r + 1) != qn::ProcessType::EXP)
89 return "The Matching node product form needs Poisson arrivals; class " + sn.classes[r].name +
90 " has a non-exponential arrival process. Use SolverCTMC.";
91 // sn.rt is indexed by stateful node; an exit to the Sink folds back onto the Source
92 const std::size_t qsf = sn.stateful_of_station(qist);
93 const std::size_t ssf = sn.stateful_of_station(srcist);
94 for (std::size_t r = 0; r < K; ++r) {
95 if (!(lam[r] > 0)) continue;
96 if (std::fabs(num_traits<T>::to_double(sn.rt((qsf - 1) * K + r, (ssf - 1) * K + r)) - 1.0) > 1e-12)
97 return "Class " + sn.classes[r].name +
98 " does not leave the Matching node to the Sink; SolverNC requires Source -> Matching -> "
99 "Sink routing.";
100 if (std::fabs(num_traits<T>::to_double(sn.rt((ssf - 1) * K + r, (qsf - 1) * K + r)) - 1.0) > 1e-12)
101 return "Class " + sn.classes[r].name +
102 " is not routed from the Source to the Matching node with probability 1.";
103 }
104 const qn::Station<T>& st = sn.stations[qist - 1];
105 for (std::size_t r = 0; r < st.impatience.size(); ++r)
107 return "Patience at a Matching node breaks the product form; use SolverCTMC (with setCap).";
108 const std::vector<std::vector<bool>> G = qn::matching::compatibility(sn, qist);
109 for (std::size_t r = 0; r < K; ++r) {
110 if (!(lam[r] > 0)) continue;
111 bool any = false;
112 for (std::size_t s = 0; s < K; ++s) any = any || G[r][s];
113 if (!any)
114 return "Class " + sn.classes[r].name +
115 " has no compatible class at the Matching node, so its jobs are never matched.";
116 }
117 return std::string();
118}
119
120/**
121 * Exact analysis of Source -> Matching -> Sink by `qsys_match_fcfm`. At the
122 * Matching station QN is the mean number waiting, TN the matched rate, RN QN
123 * over the accepted arrival rate, UN 0. When the node has no capacity and NCond
124 * fails, QN and RN are Inf, TN is NaN and lG is Inf, with a warning.
125 */
126template <class T>
128 const std::string why = sn_matching_refusal(sn);
129 if (!why.empty()) throw UnsupportedError(ErrorKind{"Structure"}, why);
130 const std::size_t qist = matching_detail::matching_station(sn);
131 const std::size_t srcist = matching_detail::source_station(sn);
132 const std::size_t M = sn.nstations, K = sn.nclasses;
133 const std::vector<double> lam = qn::matching::source_rates(sn);
134 const std::vector<std::vector<bool>> Gb = qn::matching::compatibility(sn, qist);
135 std::vector<std::vector<double>> G(K, std::vector<double>(K, 0.0));
136 for (std::size_t r = 0; r < K; ++r)
137 for (std::size_t s = 0; s < K; ++s) G[r][s] = Gb[r][s] ? 1.0 : 0.0;
138 const double cap = qist <= sn.cap.size() ? sn.cap[qist - 1] : std::numeric_limits<double>::infinity();
139 const qsys::MatchFcfmResult match = qsys::qsys_match_fcfm(lam, G, cap);
140
141 const T zero = num_traits<T>::from_int(0);
142 NcSolution<T> out;
143 mva::MvaSolution<T>& s = out.sol;
144 s.Q = Matrix<T>(M, K, zero);
145 s.U = Matrix<T>(M, K, zero);
146 s.R = Matrix<T>(M, K, zero);
147 s.Tp = Matrix<T>(M, K, zero);
148 s.X.assign(K, zero);
149 s.C.assign(K, zero);
150 s.method = "matching";
151 s.iter = 1;
152 for (std::size_t r = 0; r < K; ++r) s.Tp(srcist - 1, r) = num_traits<T>::from_double(lam[r]);
153 if (match.stable) {
154 for (std::size_t r = 0; r < K; ++r) {
155 s.Q(qist - 1, r) = num_traits<T>::from_double(match.Q[r]);
156 s.Tp(qist - 1, r) = num_traits<T>::from_double(match.X[r]);
157 s.R(qist - 1, r) = num_traits<T>::from_double(match.W[r]);
158 }
159 s.lG = std::log(match.Z);
160 } else {
161 if constexpr (num_traits<T>::is_exact) {
162 throw NumericError(ErrorKind{"Unstable"},
163 "The Matching node is unstable: the arrival rates violate NCond (lambda(I) < lambda(N(I)) "
164 "for every independent set I of classes). Set a capacity with setCap.");
165 } else {
166 const double inf = std::numeric_limits<double>::infinity();
167 const double nan = std::numeric_limits<double>::quiet_NaN();
168 for (std::size_t r = 0; r < K; ++r)
169 if (lam[r] > 0) {
170 s.Q(qist - 1, r) = num_traits<T>::from_double(inf);
171 s.R(qist - 1, r) = num_traits<T>::from_double(inf);
172 s.Tp(qist - 1, r) = num_traits<T>::from_double(nan);
173 }
174 s.lG = inf;
175 out.warning =
176 "The Matching node is unstable: the arrival rates violate NCond (lambda(I) < lambda(N(I)) for "
177 "every independent set I of classes). Set a capacity with setCap.";
178 }
179 }
180 for (std::size_t r = 0; r < K; ++r) {
181 s.X[r] = s.Tp(qist - 1, r);
182 s.C[r] = s.R(qist - 1, r);
183 }
184 out.actualmethod = "matching";
185 return out;
186}
187
188} // namespace nc
189} // namespace line
190
191#endif // LINE_SOLVERS_NC_SOLVER_NC_MATCHING_H
NumericError(const std::string &what)
Definition error.h:112
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
The exception types the port throws.
Dense matrix and non-owning view.
std::size_t source_station(const qn::NetworkStruct< T > &sn)
1-based index of the Source station, 0 when there is none.
std::size_t matching_station(const qn::NetworkStruct< T > &sn)
1-based index of the first Matching (FCFM) station, 0 when there is none.
std::string sn_matching_refusal(const qn::NetworkStruct< T > &sn)
"" when a model with a Matching node fits the exact product-form route, otherwise the reason it does ...
bool sn_is_matching_model(const qn::NetworkStruct< T > &sn)
True when the model has a Matching (FCFM) station.
NcSolution< T > solver_nc_matching_analyzer(const qn::NetworkStruct< T > &sn)
Exact analysis of Source -> Matching -> Sink by qsys_match_fcfm.
std::vector< double > source_rates(const NetworkStruct< T > &sn)
Arrival rate of each class at the Source (0 when the class has none).
bool is_matching(const NetworkStruct< T > &sn, std::size_t ist)
True when station ist (1-based) is a Matching (FCFM) station.
std::vector< std::vector< bool > > compatibility(const NetworkStruct< T > &sn, std::size_t ist)
Compatibility graph of Matching station ist (1-based), R x R.
MatchFcfmResult qsys_match_fcfm(const std::vector< double > &lam, const std::vector< std::vector< double > > &Gm, double Kcap=std::numeric_limits< double >::infinity())
Exact FCFM matching model.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Controls and result shape shared by the normalizing-constant analyzers.
A queueing network and its refreshed NetworkStruct.
Exact analysis of the first-come-first-matched (FCFM) stochastic matching model (Comte 2022).
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Class-level results, the [Q,U,R,T,C,X] of the MATLAB analyzers.
Definition mva_types.h:97
std::vector< T > X
Definition mva_types.h:99
double lG
log of the normalizing constant, the reference's lG.
Definition mva_types.h:119
std::vector< T > C
Definition mva_types.h:99
The [Q,U,R,T,C,X,lG] of the reference, plus the algorithm that ran.
Definition nc_types.h:134
mva::MvaSolution< T > sol
Definition nc_types.h:135
std::string warning
The reference's warning text, verbatim, empty when it did not warn.
Definition nc_types.h:154
std::string actualmethod
Definition nc_types.h:136
One station of the network.
std::vector< lang::ImpatienceType > impatience
Return value of qsys_match_fcfm.
std::vector< double > X
rate at which class-i items leave matched
bool stable
false when K = Inf and NCond fails
std::vector< double > Q
mean number of waiting items per class
std::vector< double > W
mean wait per accepted arrival (0 if none accepted)
double Z
normalizing constant