5#ifndef LINE_API_SN_SN_IS_DISCRETE_TIME_H
6#define LINE_API_SN_SN_IS_DISCRETE_TIME_H
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);
93inline std::string dt_fmt(
double v) {
95 std::snprintf(buf,
sizeof(buf),
"%g", v);
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.";
125 const std::string& timescale = req.
mode;
130 if (timescale ==
"continuous")
return false;
132 const double tol = 1e-12;
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;
145 if (!std::isfinite(rate) || rate <= 0)
continue;
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);
151 if (pt == ProcessType::GEOMETRIC) {
155 if (mean_time < 1 - tol) {
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) {
161 info->
reason = detail::off_lattice_reason(
"Geometric", ist, r, d);
163 }
else if (pt == ProcessType::DMAP) {
167 if (!unit_on_lattice) {
169 info->
reason = detail::off_lattice_reason(
"DMAP", ist, r, d);
171 }
else if (pt == ProcessType::DUNIFORM) {
174 double lo = 0, hi = 0;
175 const bool ok = detail::duniform_bounds(
177 if (!ok || lo < 1 - tol) {
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) {
184 info->
reason = detail::off_lattice_reason(
"DiscreteUniform", ist, r, d);
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) {
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) +
".";
204 if (timescale ==
"discrete") {
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. " +
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);
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: " +
226 if (!bool_out && info->
reason.empty() && info->
mixed)
227 info->
reason =
"the model mixes lattice-valued and continuous laws";
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.
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.
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"