LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
sn_is_discrete_time.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_SN_SN_IS_DISCRETE_TIME_H
6#define LINE_API_SN_SN_IS_DISCRETE_TIME_H
7
8/**
9 * @file
10 * @ingroup api_sn
11 * Port of matlab/src/api/sn/sn_is_discrete_time.m.
12 *
13 * Decides whether every interarrival and service law of a model lives on the
14 * slot lattice {slotLength, 2*slotLength, ...}, which is what lets SolverMAM
15 * take the discrete-time (Q-MAM) path instead of the continuous one.
16 *
17 * The slot length is the LATTICE UNIT: a law keeps the meaning it has in every
18 * other solver, and is admitted when each of its samples is a positive multiple
19 * of d, which is the rule the LDES slotted engine enforces. Geometric (support
20 * {1,2,...}), DMAP and DiscreteUniform (integral bounds) step in whole time
21 * units, so they are on the lattice exactly when 1/d is an integer; Det(v) is on
22 * it when v/d is a positive integer. Immediate is deliberately not a lattice
23 * law: a zero interval is not a point of {d,2d,...}.
24 *
25 * Under timescale "auto" a model whose laws are all Det is NOT discrete-time,
26 * even when every value is a whole number of slots: D/D/c is a continuous-time
27 * model with a continuous-time answer, and only a Geometric, DiscreteUniform or
28 * DMAP says that the model was written per slot. An explicit "discrete" (or
29 * slotted=true) still admits it. The request itself is read through
30 * `solvers::slotted_request`, the resolver NC, MAM and LDES share.
31 *
32 * The test reads `procid`, `rates` and `scv` and NOT the fitted `proc`
33 * matrices: by the time a solver sees the struct a Geometric has already been
34 * converted to a CONTINUOUS MAP, so the lattice is no longer visible there. A
35 * DMAP is the exception, its (D0,D1) having MAP shape it survives verbatim.
36 * For the same reason this test must run BEFORE any phase-type conversion.
37 *
38 * ARITHMETIC: field, with one square root in the DiscreteUniform width. Every
39 * other test is a comparison, so the family instantiates under Rational apart
40 * from that branch, which needs a real square root.
41 */
42
43#include <cmath>
44#include <cstddef>
45#include <cstdio>
46#include <string>
47
50#include "line/num/number.h"
52#include "line/util/error.h"
53
54namespace line {
55namespace api {
56
57/** Which laws the model carries, and why it was refused when it was. */
59 bool has_lattice = false; ///< some law lives on the slot lattice
60 bool has_continuous = false; ///< some law does not
61 bool mixed = false; ///< both, so no single time scale fits
62 bool has_dmap = false; ///< a DMAP is present, which has no continuous reading
63 bool has_intrinsic = false; ///< a Geometric, DMAP or DiscreteUniform is present
64 std::string reason; ///< empty when the model is discrete-time
65};
66
67/** How the caller may override the automatic decision. */
69 /** "auto", "discrete" or "continuous". */
70 std::string timescale = "auto";
71 /** `options.config.slotted`: true asks for the discrete time scale. */
72 bool slotted = false;
73 /** Slot length in model time units: the lattice unit, never a rescale of a law. */
74 double slotlength = 1.0;
75};
76
77namespace detail {
78
79/**
80 * Integral bounds of a DiscreteUniform recovered from its mean and SCV, using
81 * var = ((hi-lo+1)^2-1)/12. Returns false when the SCV is not usable.
82 */
83inline bool duniform_bounds(double mean_slots, double scv, double* lo, double* hi) {
84 if (!std::isfinite(scv) || scv < 0) return false;
85 const double var_slots = scv * mean_slots * mean_slots;
86 const double width = std::sqrt(std::max(0.0, 12 * var_slots + 1)) - 1;
87 *lo = std::round(mean_slots - width / 2);
88 *hi = std::round(mean_slots + width / 2);
89 return true;
90}
91
92/** printf-style %g of a double, for the diagnostic messages. */
93inline std::string dt_fmt(double v) {
94 char buf[64];
95 std::snprintf(buf, sizeof(buf), "%g", v);
96 return buf;
97}
98
99/** Reason for a whole-time-unit law on a lattice that does not divide one unit. */
100inline std::string off_lattice_reason(const char* family, std::size_t ist, std::size_t r,
101 double slot_length) {
102 return std::string(family) + " at station " + std::to_string(ist + 1) + " class " +
103 std::to_string(r + 1) +
104 " steps in whole time units, which are not multiples of the slot length " +
105 dt_fmt(slot_length) + ". A slot length d admits it only when 1/d is an integer.";
106}
107
108} // namespace detail
109
110/**
111 * True when every law of `sn` is lattice-valued on `slot_length`.
112 *
113 * `slot_length` receives the slot in model time units and `info` the diagnosis.
114 * A DMAP mixed with continuous laws is an ERROR rather than a `false`: its
115 * (D0,D1) are probability matrices, so the continuous machinery would form
116 * inv(-D0) where the law needs inv(I-D0) and return a wrong number in silence.
117 */
118template <class T>
120 double* slot_length, DiscreteTimeInfo* info) {
121 using lang::ProcessType;
122
123 const solvers::SlottedRequest req =
124 solvers::slotted_request(options.timescale, options.slotted, options.slotlength);
125 const std::string& timescale = req.mode;
126 const double d = req.slot_length;
127
128 *slot_length = d;
129 *info = DiscreteTimeInfo();
130 if (timescale == "continuous") return false;
131
132 const double tol = 1e-12;
133 // Geometric, DiscreteUniform and DMAP step in whole time units: they are on
134 // the lattice {d,2d,...} exactly when one time unit is a whole number of slots.
135 const double unit_slots = 1.0 / d;
136 const bool unit_on_lattice =
137 std::round(unit_slots) >= 1 &&
138 std::abs(unit_slots - std::round(unit_slots)) <= tol * std::max(1.0, unit_slots);
139 for (std::size_t ist = 0; ist < sn.nstations; ++ist)
140 for (std::size_t r = 0; r < sn.nclasses; ++r) {
141 const ProcessType pt = sn.procid(ist + 1, r + 1);
142 if (pt == ProcessType::DISABLED || pt == ProcessType::NONE) continue;
143 if (sn.disabled[ist][r]) continue;
144 const double rate = num_traits<T>::to_double(sn.rates(ist, r));
145 if (!std::isfinite(rate) || rate <= 0) continue; // no interval law here
146 const double mean_time = 1.0 / rate;
147 const double mean_slots = mean_time / d;
148 const std::string where =
149 " at station " + std::to_string(ist + 1) + " class " + std::to_string(r + 1);
150
151 if (pt == ProcessType::GEOMETRIC) {
152 // mean 1/p time units; p in (0,1] is recovered exactly from the mean
153 info->has_lattice = true;
154 info->has_intrinsic = true;
155 if (mean_time < 1 - tol) {
156 info->has_continuous = true;
157 info->reason = "Geometric" + where + " has mean " + detail::dt_fmt(mean_time) +
158 ", below the minimum of its support {1,2,...}.";
159 } else if (!unit_on_lattice) {
160 info->has_continuous = true;
161 info->reason = detail::off_lattice_reason("Geometric", ist, r, d);
162 }
163 } else if (pt == ProcessType::DMAP) {
164 info->has_lattice = true;
165 info->has_dmap = true;
166 info->has_intrinsic = true;
167 if (!unit_on_lattice) {
168 info->has_continuous = true;
169 info->reason = detail::off_lattice_reason("DMAP", ist, r, d);
170 }
171 } else if (pt == ProcessType::DUNIFORM) {
172 info->has_lattice = true;
173 info->has_intrinsic = true;
174 double lo = 0, hi = 0;
175 const bool ok = detail::duniform_bounds(
176 mean_time, num_traits<T>::to_double(sn.scv(ist, r)), &lo, &hi);
177 if (!ok || lo < 1 - tol) {
178 info->has_continuous = true;
179 info->reason = "DiscreteUniform" + where + " spans [" + detail::dt_fmt(lo) +
180 "," + detail::dt_fmt(hi) +
181 "], which is not contained in {1,2,...}.";
182 } else if (!unit_on_lattice) {
183 info->has_continuous = true;
184 info->reason = detail::off_lattice_reason("DiscreteUniform", ist, r, d);
185 }
186 } else if (pt == ProcessType::DET) {
187 if (std::abs(mean_slots - std::round(mean_slots)) <= tol * std::max(1.0, mean_slots) &&
188 std::round(mean_slots) >= 1) {
189 info->has_lattice = true;
190 } else {
191 // a Det off the lattice is what makes the model continuous
192 info->has_continuous = true;
193 info->reason = "Det" + where + " is " + detail::dt_fmt(mean_time) +
194 ", which is not a positive multiple of the slot length " +
195 detail::dt_fmt(d) + ".";
196 }
197 } else {
198 info->has_continuous = true;
199 }
200 }
201
202 info->mixed = info->has_lattice && info->has_continuous;
203
204 if (timescale == "discrete") {
205 if (info->mixed)
206 throw InputError("sn_is_discrete_time: a discrete time scale was requested "
207 "(options.config.slotted or timescale='discrete') but the model "
208 "mixes lattice and non-lattice laws. " +
209 info->reason);
210 if (!info->has_lattice)
211 throw InputError("sn_is_discrete_time: a discrete time scale was requested "
212 "(options.config.slotted or timescale='discrete') but no "
213 "interarrival or service law is lattice-valued on a slot of " +
214 detail::dt_fmt(d) + ". " + info->reason);
215 return true;
216 }
217
218 // A model of Det laws alone is a continuous-time model (D/D/c) unless the
219 // caller says otherwise: auto needs a law that was written per slot.
220 const bool bool_out = info->has_lattice && !info->has_continuous && info->has_intrinsic;
221 if (!bool_out && info->has_dmap)
222 throw InputError("sn_is_discrete_time: the model mixes a DMAP with continuous-time laws. "
223 "A DMAP is only defined on a slotted time scale, so no solver can "
224 "interpret this model: " +
225 info->reason);
226 if (!bool_out && info->reason.empty() && info->mixed)
227 info->reason = "the model mixes lattice-valued and continuous laws";
228 return bool_out;
229}
230
231} // namespace api
232} // namespace line
233
234#endif // LINE_API_SN_SN_IS_DISCRETE_TIME_H
InputError(const std::string &what)
Definition error.h:105
A network plus its refreshed NetworkStruct.
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
bool sn_is_discrete_time(const qn::NetworkStruct< T > &sn, const DiscreteTimeOptions &options, double *slot_length, DiscreteTimeInfo *info)
True when every law of sn is lattice-valued on slot_length.
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
Definition lang_types.h:524
SlottedRequest slotted_request(std::string timescale, bool slotted, double slotlength)
Resolves timescale (empty reads as "auto"), slotted and slotlength as they come from options....
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
Port of matlab/src/solvers/line_slotted_request.m: the one reading of the discrete-time switch shared...
Which laws the model carries, and why it was refused when it was.
bool mixed
both, so no single time scale fits
bool has_dmap
a DMAP is present, which has no continuous reading
bool has_lattice
some law lives on the slot lattice
bool has_continuous
some law does not
std::string reason
empty when the model is discrete-time
bool has_intrinsic
a Geometric, DMAP or DiscreteUniform is present
How the caller may override the automatic decision.
bool slotted
options.config.slotted: true asks for the discrete time scale.
std::string timescale
"auto", "discrete" or "continuous".
double slotlength
Slot length in model time units: the lattice unit, never a rescale of a law.
The resolved time-scale request.
double slot_length
the lattice unit d in model time units
std::string mode
"auto", "discrete" or "continuous"