1function sn = sn_set_service_coc(sn, stationIdx, classIdx, rate, scv)
2% SN_SET_SERVICE_COC Update service rate preserving cell-of-cells format.
4% Works like sn_set_service with autoRefresh=
true, but writes mu, phi,
5% proc, pie
using cell-of-cells indexing (sn.mu{i}{k}) instead of 2D
6% cell indexing (sn.mu{i,k}). This avoids reshaping the cell arrays,
7% which would
break solvers that expect cell-of-cells format.
11% stationIdx - station index (1-based)
12% classIdx - class index (1-based)
13% rate - new service rate (positive scalar)
14% scv - squared coefficient of variation (default 1.0)
16% Copyright (c) 2012-2026, Imperial College London
18% This code
is released under the 3-Clause BSD License.
20if nargin < 5 || isempty(scv)
24% Update rates and scv matrices
25sn.rates(stationIdx, classIdx) = rate;
26sn.scv(stationIdx, classIdx) = scv;
28% Skip
if rate
is invalid
29if isnan(rate) || rate <= 0 || isinf(rate)
35% Determine process type and create MAP based on SCV
36if isnan(scv) || abs(scv - 1.0) < 1e-10
37 MAP = map_exponential(meanVal);
39 procType = ProcessType.EXP;
41 k = max(1, ceil(1.0 / scv));
42 MAP = map_erlang(meanVal, k);
44 procType = ProcessType.ERLANG;
46 MAP = map_hyperexp(meanVal, scv);
49 procType = ProcessType.HYPEREXP;
51 MAP = map_exponential(meanVal);
53 procType = ProcessType.EXP;
60% Update in cell-of-cells format
61sn.proc{stationIdx}{classIdx} = MAP;
62sn.procid(stationIdx, classIdx) = procType;
63sn.phases(stationIdx, classIdx) = nPhases;
64sn.phasessz(stationIdx, classIdx) = max(nPhases, 1);
66% Recompute phaseshift
for this station
68sn.phaseshift(stationIdx, 1) = 0;
70 cumSum = cumSum + sn.phasessz(stationIdx, c);
71 if c + 1 <= size(sn.phaseshift, 2)
72 sn.phaseshift(stationIdx, c + 1) = cumSum;
76% Update mu (rates from -diag(D0))
77sn.mu{stationIdx}{classIdx} = -diag(D0);
79% Update phi (completion probabilities)
80phiVec = zeros(nPhases, 1);
82 d1RowSum = sum(D1(i, :));
85 phiVec(i) = d1RowSum / d0Diag;
88sn.phi{stationIdx}{classIdx} = phiVec;
90% Update pie (initial phase distribution)
91sn.pie{stationIdx}{classIdx} = map_pie(MAP);