LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
afterEventRouter.m
1function [outspace, outrate, outprob, eventCache] = afterEventRouter(sn, ind, event, class, isSimulation, eventCache, space_buf, space_srv, space_var, key)
2% [OUTSPACE, OUTRATE, OUTPROB, EVENTCACHE] = AFTEREVENTROUTER(SN, IND, EVENT, CLASS, ISSIMULATION, EVENTCACHE, SPACE_BUF, SPACE_SRV, SPACE_VAR, KEY)
3%
4% Handle router afterEvent logic
5
6% Copyright (c) 2012-2026, Imperial College London
7% All rights reserved.
8
9outspace = [];
10outrate = [];
11outprob = 1;
12
13switch event
14 case EventType.ARV
15 space_srv(:,class) = space_srv(:,class) + 1;
16 outspace = [space_srv, space_var]; % buf is empty
17 outrate = -1*ones(size(outspace,1)); % passive action, rate is unspecified
18 case EventType.DEP
19 if space_srv(class)>0
20 space_srv(:,class) = space_srv(:,class) - 1;
21 switch sn.routing(ind,class)
22 case RoutingStrategy.RROBIN
23 slot = sum(sn.nvars(ind,1:(sn.nclasses+class)));
24 outlinks = sn.nodeparam{ind}{class}.outlinks;
25 idx = find(space_var(slot) == outlinks);
26 if isempty(idx) || idx >= length(outlinks)
27 space_var(slot) = outlinks(1);
28 else
29 space_var(slot) = outlinks(idx+1);
30 end
31 case RoutingStrategy.WRROBIN
32 slot = sum(sn.nvars(ind,1:(sn.nclasses+class)));
33 % WRR cycles through weighted_outlinks (each outlink
34 % replicated by its weight). The state slot holds the
35 % POSITION in this list rather than a destination value,
36 % so that repeated outlinks advance correctly.
37 if isfield(sn.nodeparam{ind}{class}, 'weighted_outlinks') ...
38 && ~isempty(sn.nodeparam{ind}{class}.weighted_outlinks)
39 cycle_len = length(sn.nodeparam{ind}{class}.weighted_outlinks);
40 else
41 cycle_len = length(sn.nodeparam{ind}{class}.outlinks);
42 end
43 pos = space_var(slot);
44 if pos < 1 || pos > cycle_len
45 space_var(slot) = 1;
46 elseif pos >= cycle_len
47 space_var(slot) = 1;
48 else
49 space_var(slot) = pos + 1;
50 end
51 end
52 outspace = [space_srv, space_var]; % buf is empty
53 outrate = GlobalConstants.Immediate*ones(size(outspace,1)); % immediate action
54 end
55end
56
57if isSimulation
58 if nargin>=8 && isobject(eventCache)
59 eventCache(key) = {outprob, outspace,outrate};
60 end
61 if size(outspace,1) > 1
62 tot_rate = sum(outrate);
63 cum_rate = cumsum(outrate) / tot_rate;
64 firing_ctr = 1 + max([0,find( rand > cum_rate' )]); % select action
65 outspace = outspace(firing_ctr,:);
66 outrate = sum(outrate);
67 outprob = outprob(firing_ctr,:);
68 end
69end
70
71end