LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
infer_lqn_getobs.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_INFER_INFER_LQN_GETOBS_H
6#define LINE_API_INFER_INFER_LQN_GETOBS_H
7
8/**
9 * @file
10 * @ingroup api_infer
11 * Observation vector of a solved LQN, z = h(a).
12 *
13 * Templated port of matlab/src/api/infer/infer_lqn_getobs.m. The JAR carries
14 * the same selection inline inside jline/api/infer/InferLqn.java.
15 *
16 * This is the measurement half of the LQN parameter identification method of
17 * Zheng, Yang, Woodside, Litoiu, Iszlai, "Tracking Time-Varying Parameters in
18 * Software Systems with Extended Kalman Filters", CASCON 2005: given the
19 * per-element metric vectors of a solved model, it selects the entries the
20 * filter observes, in the order the specification lists them. It is the map
21 * whose sensitivity infer_lqn_jacobian differentiates and whose residual
22 * infer_lqn_ekf corrects on.
23 *
24 * The metric name is an enum here rather than MATLAB's case-insensitive
25 * string, so an unknown metric is a compile error instead of a runtime one;
26 * the only runtime rejection left is the one MATLAB also makes, an element
27 * name absent from the LQN. That rejection is an InputError and NOT a
28 * not-a-number or a zero: an observation silently read off the wrong element
29 * would be indistinguishable from a badly fitting model.
30 *
31 * ARITHMETIC: selection and copying only, no operation on the values at all,
32 * so the port is exact in every instantiation.
33 */
34
35#include <cstddef>
36#include <string>
37#include <vector>
38
40#include "line/num/number.h"
41#include "line/util/error.h"
42
43namespace line {
44namespace infer {
45
46/** The four per-element metrics of a solved LQN, as in getEnsembleAvg. */
47enum class LqnMetric { QLen, Util, RespT, Tput };
48
49/**
50 * Per-element metric vectors, each aligned with the element name list.
51 * Mirrors the struct MATLAB passes as METRICS.
52 */
53template <class T>
54struct LqnMetrics {
55 std::vector<T> QLen;
56 std::vector<T> Util;
57 std::vector<T> RespT;
58 std::vector<T> Tput;
59};
60
61/** One row of MATLAB's OBSSPEC struct array. */
62struct LqnObsSpec {
64 std::string name;
65};
66
67/**
68 * @brief Observation vector of a solved LQN, z = h(a).
69 *
70 * @param names element names, as in LayeredNetworkStruct.names
71 * @param metrics per-element metric vectors, indexed like names
72 * @param spec observations to extract, in order
73 * @return (numel(spec)) observation vector
74 */
75template <class T>
76std::vector<T> infer_lqn_getobs(const std::vector<std::string>& names,
77 const LqnMetrics<T>& metrics,
78 const std::vector<LqnObsSpec>& spec) {
79 std::vector<T> z;
80 z.reserve(spec.size());
81 for (std::size_t i = 0; i < spec.size(); ++i) {
82 const std::size_t idx = infer_lqn_findbyname(names, spec[i].name);
83 if (idx == INFER_LQN_NOT_FOUND)
84 throw InputError("infer_lqn_getobs: element '" + spec[i].name +
85 "' not found in the LQN");
86 const std::vector<T>* v = nullptr;
87 switch (spec[i].metric) {
88 case LqnMetric::QLen:
89 v = &metrics.QLen;
90 break;
91 case LqnMetric::Util:
92 v = &metrics.Util;
93 break;
95 v = &metrics.RespT;
96 break;
97 case LqnMetric::Tput:
98 v = &metrics.Tput;
99 break;
100 }
101 if (idx >= v->size())
102 throw InputError("infer_lqn_getobs: metric vector shorter than the element list");
103 z.push_back((*v)[idx]);
104 }
105 return z;
106}
107
108} // namespace infer
109} // namespace line
110
111#endif // LINE_API_INFER_INFER_LQN_GETOBS_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
First element of a named LQN container matching a name.
static const std::size_t INFER_LQN_NOT_FOUND
Returned when no element carries the requested name, MATLAB's [].
std::size_t infer_lqn_findbyname(const std::vector< std::string > &names, const std::string &name)
First element of a named LQN container matching a name.
LqnMetric
The four per-element metrics of a solved LQN, as in getEnsembleAvg.
std::vector< T > infer_lqn_getobs(const std::vector< std::string > &names, const LqnMetrics< T > &metrics, const std::vector< LqnObsSpec > &spec)
Observation vector of a solved LQN, z = h(a).
Number-type abstraction for the templated API port.
Per-element metric vectors, each aligned with the element name list.
One row of MATLAB's OBSSPEC struct array.