LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
sampleSysAggr.m
1function sysStateAggr = sampleSysAggr(self, numEvents, markActivePassive)
2% SYSSTATEAGGR = SAMPLESYSAGGR(NUMEVENTS)
3
4if GlobalConstants.DummyMode
5 sysStateAggr = NaN;
6 return
7end
8
9if nargin<2 %~exist('numEvents','var')
10 numEvents = self.options.samples;
11end
12
13if nargin<3
14 markActivePassive = false;
15end
16
17sn = self.getStruct;
18numEvents = numEvents - 1; % we include the initialization as an event
19Q = getAvgQLenHandles(self);
20statStateAggr = cell(sn.nstations,1);
21
22% create a temp model
23modelCopy = self.model.copy;
24modelCopy.resetNetwork;
25
26% determine the nodes to logs
27isNodeClassLogged = false(modelCopy.getNumberOfNodes, modelCopy.getNumberOfClasses);
28for i= 1:modelCopy.getNumberOfStations
29 ind = self.model.getNodeIndex(modelCopy.getStationNames{i});
30 if sn.nodetype(ind) ~= NodeType.Source
31 for r=1:modelCopy.getNumberOfClasses
32 if ~Q{i,r}.disabled || nargin == 1
33 isNodeClassLogged(ind,r) = true;
34 else
35 isNodeClassLogged(node,r) = true;
36 end
37 end
38 end
39end
40
41% apply logging to the copied model
42Plinked = sn.rtorig;
43isNodeLogged = max(isNodeClassLogged,[],2);
44logpath = lineTempDir;
45modelCopy.linkAndLog(Plinked, isNodeLogged, logpath);
46
47% simulate the model copy and retrieve log data
48options = self.getOptions; options.samples = numEvents;
49options.method = 'jsim'; % Force jsim method to avoid recursion with 'replication'
50solverjmt = SolverJMT(modelCopy, options);
51solverjmt.maxEvents = numEvents*sn.nnodes*sn.nclasses;
52solverjmt.runAnalyzer(); % log data
53logData = SolverJMT.parseLogs(modelCopy, isNodeLogged, MetricType.toText(MetricType.QLen));
54
55% from here convert from nodes in logData to stations
56event = {};
57sn = modelCopy.getStruct;
58for ist= 1:sn.nstations
59 isf = sn.stationToStateful(ist);
60 ind = sn.stationToNode(ist);
61 t = [];
62 nir = cell(1,sn.nclasses);
63 event{isf,r} = {};
64 if sn.nodetype(ind) == NodeType.Source
65 nir{r} = [];
66 else
67 for r=1:sn.nclasses
68 if ~isempty(logData{isf,r})
69 [~,uniqTSi] = unique(logData{isf,r}.t);
70 if isNodeClassLogged(isf,r)
71 if ~isempty(logData{isf,r})
72 t = logData{isf,r}.t(uniqTSi);
73 event{isf,r} = logData{isf,r}.event;
74 %t = [t(2:end);t(end)];
75 nir{r} = logData{isf,r}.QLen(uniqTSi);
76 end
77 end
78 else
79 nir{r} = [];
80 event{isf,r} = {};
81 end
82 end
83 end
84 if isfinite(self.options.timespan(2))
85 stopAt = find(t>self.options.timespan(2),1,'first');
86 if ~isempty(stopAt) && stopAt>1
87 t = t(1:(stopAt-1));
88 for r=1:length(nir)
89 nir{r} = nir{r}(1:(stopAt-1));
90 end
91 end
92 end
93 statStateAggr{ist} = struct();
94 statStateAggr{ist}.handle = self.model.stations{ist};
95 statStateAggr{ist}.t = t;
96 statStateAggr{ist}.state = cell2mat(nir);
97 statStateAggr{ist}.event = {event{isf,:}};
98 statStateAggr{ist}.isaggregate = true;
99 %sysStateAggr.arv_job_id = logData{2}.arvID;
100 %sysStateAggr.dep_job_id = logData{2}.depID;
101end
102
103tranSysStateAggr = cell(1,1+sn.nstations);
104
105tranSysStateAggr{1} = []; % timestamps
106for i=1:sn.nstations % stations
107 if isempty(tranSysStateAggr{1})
108 tranSysStateAggr{1} = statStateAggr{i}.t;
109 else
110 tumax = min(max(tranSysStateAggr{1}),max(statStateAggr{i}.t));
111 tranSysStateAggr{1} = union(tranSysStateAggr{1}, statStateAggr{i}.t);
112 tranSysStateAggr{1} = tranSysStateAggr{1}(tranSysStateAggr{1}<=tumax);
113 tranSysStateAggr{1} = union(tranSysStateAggr{1}, statStateAggr{i}.t);
114 end
115end
116
117for i=1:sn.nstations % stations
118 ind = sn.stationToNode(i);
119 tranSysStateAggr{1+i} = [];
120 [~,uniqTSi] = unique(statStateAggr{i}.t);
121 if sn.nodetype(ind) ~= NodeType.Source
122 for j=1:sn.nclasses % classes
123 % we floor the interpolation as we hold the last state
124 if ~isempty(uniqTSi)
125 Qijt = interp1(statStateAggr{i}.t(uniqTSi), statStateAggr{i}.state(uniqTSi,j), tranSysStateAggr{1},'previous');
126 if isnan(Qijt(end))
127 Qijt(end)=Qijt(end-1);
128 end
129 tranSysStateAggr{1+i} = [tranSysStateAggr{1+i}, Qijt];
130 else
131 Qijt = NaN*ones(length(tranSysStateAggr{1}),1);
132 tranSysStateAggr{1+i} = [tranSysStateAggr{1+i}, Qijt];
133 end
134 end
135 else
136 tranSysStateAggr{1+i} = [Inf];
137 end
138end
139
140sysStateAggr = struct();
141sysStateAggr.handle = self.model.stations';
142sysStateAggr.t = tranSysStateAggr{1};
143sysStateAggr.state = {tranSysStateAggr{2:end}};
144
145% % % now put the events in the .event cell
146% eventSysStateAggr = cell(sn.nstations, sn.nclasses); % timestamps
147% for i=1:sn.nstations % stations
148% for r=1:sn.nclasses
149% if isempty(statStateAggr{i}.event)
150% eventSysStateAggr{i,r} = struct();
151% else
152% eventSysStateAggr{i,r} = struct();
153% for e=1:size(statStateAggr{i}.event{r},1)
154% if ~isempty(statStateAggr{i}.event{r}{e})
155% %statStateAggr{i}.event{r}{e}.t
156% %etpos = find(sysStateAggr.t == statStateAggr{i}.event{r}{e}.t);
157% %if ~isempty(etpos)
158% evtype = statStateAggr{i}.event{r}{e}.event;
159% evfield = EventType.toText(evtype);
160% if ~isfield(eventSysStateAggr{i,r},evfield)
161% eventSysStateAggr{i,r}.(EventType.toText(evtype)) = {};
162% end
163% eventSysStateAggr{i,r}.(EventType.toText(evtype)){end+1,1} = statStateAggr{i}.event{r}{e};
164% %end
165% end
166% end
167% %eventSysStateAggr{i,r} = statStateAggr{i}.event{r};
168% end
169% end
170% end
171% %sysStateAggr.t = [0; sysStateAggr.t(1:end-1)];
172% sysStateAggr.event = eventSysStateAggr;
173
174event = cellmerge(event);
175event = {event{cellisa(event,'Event')}}';
176event_t = cellfun(@(c) c.t, event);
177[~,I]=sort(event_t);
178sysStateAggr.event = {event{I}};
179sysStateAggr.event = sysStateAggr.event';
180sysStateAggr.isaggregate = true;
181%sysStateAggr.arv_job_id = logData{2}.arvID;
182%sysStateAggr.dep_job_id = logData{2}.depID;
183
184if markActivePassive
185 apevent = cell(1,length(sysStateAggr.t)-1);
186 for ti = 1:length(apevent)
187 apevent{ti} = struct('active',[],'passive',[]);
188 end
189 for e=1:length(sysStateAggr.event)
190 ti = find(sysStateAggr.event{e}.t == sysStateAggr.t);
191 if ~isempty(ti)
192 switch sysStateAggr.event{e}.event
193 case EventType.ARV
194 apevent{ti-1}.passive = sysStateAggr.event{e};
195 otherwise
196 apevent{ti-1}.active = sysStateAggr.event{e};
197 end
198 end
199 end
200 sysStateAggr.event = apevent';
201end
202end
Definition mmt.m:92