LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
afterEvent.m
1function [outspace, outrate, outprob, eventCache] = afterEvent(sn, ind, inspace, event, class, isSimulation, eventCache)
2% [OUTSPACE, OUTRATE, OUTPROB] = AFTEREVENT(QN, IND, INSPACE, EVENT, CLASS, ISSIMULATION, EVENTCACHE)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7% Event cache for faster simulation
8if isSimulation && nargin >= 7 && isobject(eventCache)
9 vector = [ind, event, class, inspace];
10 key = mat2str(vector);
11 if eventCache.isKey(key)
12 cachedResult = eventCache(key);
13 outprob = cachedResult{1};
14 outspace = cachedResult{2};
15 outrate = cachedResult{3};
16 if size(outspace,1) > 1
17 tot_rate = sum(outrate);
18 cum_rate = cumsum(outrate) / tot_rate;
19 firing_ctr = 1 + max([0,find( rand > cum_rate' )]); % select action
20 outspace = outspace(firing_ctr,:);
21 outrate = sum(outrate);
22 outprob = outprob(firing_ctr,:);
23 end
24 return
25 end
26else
27 key = NaN;
28 eventCache = [];
29end
30% Else continue to main body below
31M = sn.nstations;
32R = sn.nclasses;
33S = sn.nservers;
34phasessz = sn.phasessz;
35phaseshift = sn.phaseshift;
36pie = sn.pie;
37
38% ind: node index
39isf = sn.nodeToStateful(ind);
40
41if sn.isstation(ind)
42 ismkvmod = any(sn.procid(sn.nodeToStation(ind),:)==ProcessType.MAP | sn.procid(sn.nodeToStation(ind),:)==ProcessType.MMPP2);
43 ismkvmodclass = zeros(R,1);
44 for r=1:R
45 ismkvmodclass(r) = any(sn.procid(sn.nodeToStation(ind),r)==ProcessType.MAP | sn.procid(sn.nodeToStation(ind),r)==ProcessType.MMPP2);
46 end
47end
48
49lldscaling = sn.lldscaling;
50if isempty(lldscaling)
51 lldlimit = max(sum(sn.nclosedjobs),1);
52 lldscaling = ones(M,lldlimit);
53else
54 lldlimit = size(lldscaling,2);
55end
56
57cdscaling = sn.cdscaling;
58if isempty(cdscaling)
59 cdscaling = cell(M,1);
60 cdscaling{1} = @(ni) 1;
61 for i=2:M % faster this way
62 cdscaling{i} = cdscaling{1};
63 end
64end
65
66hasOnlyExp = false; % true if all service processes are exponential
67if sn.isstation(ind)
68 ist = sn.nodeToStation(ind);
69 K = phasessz(ist,:);
70 Ks = phaseshift(ist,:);
71 if max(K)==1
72 hasOnlyExp = true;
73 end
74 mu = sn.mu;
75 phi = sn.phi;
76 proc = sn.proc;
77 capacity = sn.cap;
78 classcap = sn.classcap;
79 if K(class) == 0 % if this class is not accepted at the resource
80 eventCache(key) = {outprob, outspace,outrate};
81 return
82 end
83 V = sum(sn.nvars(ind,:));
84 space_var = inspace(:,(end-V+1):end); % local state variables
85 space_srv = inspace(:,(end-sum(K)-V+1):(end-V)); % server state
86 space_buf = inspace(:,1:(end-sum(K)-V)); % buffer state
87elseif sn.isstateful(ind)
88 V = sum(sn.nvars(ind,:));
89 % in this case service is always immediate so sum(K)=1
90 space_var = inspace(:,(end-V+1):end); % local state variables
91 if sn.nodetype(ind) == NodeType.Transition
92 K = sn.nodeparam{ind}.firingphases;
93 nmodes = sn.nodeparam{ind}.nmodes;
94 Ks = [0,cumsum(K,2)];
95 space_buf = inspace(:,1:nmodes); % idle servers count put in buf
96 space_srv = inspace(:,(nmodes+1):(nmodes+sum(K))); % enabled servers' phases
97 else
98 space_buf = []; % buffer state
99 space_srv = inspace(:,(end-R-V+1):(end-V)); % server state
100 end
101else % stateless node
102 space_var = [];
103 space_srv = [];
104 space_buf = [];
105end
106
107if sn.isstation(ind)
108 [outspace, outrate, outprob, eventCache] = State.afterEventStation(sn, ind, inspace, event, class, isSimulation, eventCache, ...
109 M, R, S, phasessz, phaseshift, pie, isf, ismkvmod, ismkvmodclass, lldscaling, lldlimit, cdscaling, ...
110 hasOnlyExp, ist, K, Ks, mu, phi, proc, capacity, classcap, V, space_buf, space_srv, space_var, key);
111elseif sn.isstateful(ind)
112 switch sn.nodetype(ind)
113 case NodeType.Router
114 [outspace, outrate, outprob, eventCache] = State.afterEventRouter(sn, ind, event, class, isSimulation, eventCache, space_buf, space_srv, space_var, key);
115 case NodeType.Cache
116 [outspace, outrate, outprob, eventCache] = State.afterEventCache(sn, ind, event, class, isSimulation, eventCache, R, space_buf, space_srv, space_var, key);
117 case NodeType.Transition
118 [outspace, outrate, outprob, eventCache] = State.afterEventTransition(sn, ind, inspace, K, Ks, event, class, isSimulation, eventCache, R, space_buf, space_srv, space_var, key);
119 end % switch nodeType
120end
121end