1function initDefault(self,
nodes)
5% closed
classes initialized at ref station
6% running jobs are allocated in
class id order until all
9%refreshStruct(self); % we force update of the model before we initialize
11sn = self.getStruct(
false);
16 nodes = 1:self.getNumberOfNodes;
21 ist = sn.nodeToStation(ind);
22 n0 = zeros(1,length(N)); % number of jobs in the initial state
23 s0 = zeros(1,length(N)); % number of active servers in the initial state
24 s = sn.nservers(ist); % allocate
25 for r=find(isfinite(N))' %
for all closed
classes
26 if sn.nodeToStation(ind) == sn.refstat(r)
32 switch sn.nodetype(ind)
34 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)
',s0(:)');
35 % Cache state width = totalCacheCapacity + (per-item retrieval bitmap).
36 % Initialize the cache region with items 1..totalCacheCapacity and the
37 % retrieval-system region (one column per item) with zeros (nothing
38 % being retrieved). The bitmap
is omitted when no retrieval system.
39 if isfield(sn.nodeparam{ind},
'totalCacheCapacity')
40 tcc = sn.nodeparam{ind}.totalCacheCapacity;
42 tcc = sn.nvars(ind,2*R+1);
45 if isfield(sn.nodeparam{ind},
'retrievalSystemCapacity') ...
46 && sn.nodeparam{ind}.retrievalSystemCapacity > 0
47 rbw = sn.nodeparam{ind}.nitems;
49 state_i = [state_i, 1:tcc, zeros(1,rbw)]; %#ok<AGROW>
51 if sum(self.nodes{ind}.state)>0
52 %
if the user pre-loaded manually some jobs, keep them
53 state_i = self.nodes{ind}.state;
55 state_i = zeros(1,self.getNumberOfClasses);
57 if sn.refstat(r) == ist
58 state_i(r) = sn.njobs(r);
65 if sn.isstation(ind) && sn.sched(ist) == SchedStrategy.PAS
66 % Pass-and-swap stations: the full ordered state space (all
67 % placements of the marginal)
is built independently of the
68 % initial state,
while the user-specified placement (
if any)
69 %
is kept as the initial state. A closed PAS station holding
70 % more than one job under a non-empty swapping graph has a
71 % reducible generator (one recurrent component per placement
72 % order), so the placement
is a required model input: error
73 % rather than fabricate a
default ordering.
74 userState = self.nodes{ind}.getState();
75 hasUser = ~isempty(userState) && any(userState(:) > 0);
77 if numel(sn.nodeparam) >= ind && isstruct(sn.nodeparam{ind}) ...
78 && isfield(sn.nodeparam{ind},
'swapGraph')
79 sg = sn.nodeparam{ind}.swapGraph;
81 hasSwap = ~isempty(sg) && any(sg(:) ~= 0);
82 isClosed = any(isfinite(N));
85 present = userState(userState > 0);
86 for r = 1:R, nUser(r) = sum(present == r); end
87 sU = zeros(1, R); ss = sn.nservers(ist);
88 for r = 1:R, sU(r) = min(nUser(r), ss); ss = ss - sU(r); end
89 space_i = State.fromMarginalAndStarted(sn, ind, nUser, sU);
92 cols = min(numel(userState), W);
93 urow(1:cols) = userState(1:cols);
94 % keep the user placement as the initial state (row 1)
95 state_i = unique([urow; space_i],
'rows',
'stable');
96 elseif hasSwap && isClosed && sum(n0) > 1
97 line_error(mfilename, sprintf([
'A closed pass-and-swap station with a non-empty swapping ' ...
98 'graph requires an explicit initial job placement (station %d). Call setState on the ' ...
99 'station with the ordered class list (oldest first) before solving.'], ind));
101 state_i = State.fromMarginalAndStarted(sn, ind, n0(:)
', s0(:)');
104 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)
',s0(:)');
107 switch sn.procid(sn.nodeToStation(ind),r)
109 %state_i = State.cartesian(state_i, [1:sn.phases(i,r)]
');
110 state_i = State.cartesian(state_i, 1);
117 switch sn.routing(ind,r)
118 case {RoutingStrategy.RROBIN, RoutingStrategy.WRROBIN}
119 % start from first connected queue
120 state_i = [state_i, find(sn.connmatrix(ind,:),1)];
124 line_error(mfilename,sprintf('Default initialization failed on station %d.
',ind));
126 elseif sn.isstateful(ind) % not a station
127 switch sn.nodetype(ind)
129 % [class counts | cache contents (items 1..tcc) | per-item retrieval
130 % bitmap (zeros, nothing being retrieved)]. The bitmap (one column per
131 % item) is omitted when no retrieval system is configured.
132 tcc = self.nodes{ind}.totalCacheCapacity;
133 if self.nodes{ind}.retrievalSystemCapacity > 0
134 rbw = self.nodes{ind}.items.nitems;
138 state_i = [zeros(1,self.getNumberOfClasses), 1:tcc, zeros(1,rbw)];
140 state_i = zeros(1, self.getNumberOfClasses);
142 switch sn.routing(ind,r)
143 case RoutingStrategy.RROBIN
144 % RR slot holds destination value; start at first connected queue
145 state_i = [state_i, find(sn.connmatrix(ind,:),1)]; %#ok<AGROW>
146 case RoutingStrategy.WRROBIN
147 % WRR slot holds POSITION in weighted_outlinks; start at 1
148 state_i = [state_i, 1]; %#ok<AGROW>
149 case RoutingStrategy.KCHOICES
150 % KCHOICES with memory: -1 marks "no prior pick".
151 if isfield(self.nodes{ind}.output.outputStrategy{1, r}, 'withMemory
') ...
152 || (iscell(self.nodes{ind}.output.outputStrategy{1, r}) ...
153 && length(self.nodes{ind}.output.outputStrategy{1, r}) >= 3 ...
154 && length(self.nodes{ind}.output.outputStrategy{1, r}{3}) >= 2 ...
155 && self.nodes{ind}.output.outputStrategy{1, r}{3}{2})
156 state_i = [state_i, -1]; %#ok<AGROW>
160 case NodeType.Transition
161 % Differently from a server, the first nmodes states in a
162 % transitions count the servers that are not enabled and
163 % the last nodes count servers that just fired.
164 % This is required as the local state does not encode the
165 % buffer hence the enabling condition is not available.
166 state_i = sn.nodeparam{ind}.nmodeservers;
167 % For infinite servers, use large finite value for state (SSA needs finite states)
168 state_i(isinf(state_i)) = GlobalConstants.MaxInt();
169 % For non-Markovian distributions, firingphases is NaN - treat as 1 phase
170 firingphases = sn.nodeparam{ind}.firingphases;
171 firingphases(isnan(firingphases)) = 1;
172 state_i = [state_i, zeros(1, sum(firingphases)), zeros(size(state_i))]; %#ok<AGROW>
176 %line_error(mfilename,'Default initialization not available on stateful node %d.
',i);
179 if sn.isstateful(ind) % not a station
180 if size(state_i,1)==1
181 self.nodes{ind}.setStateSpace(state_i);
182 self.nodes{ind}.setStatePrior(1);
183 self.nodes{ind}.setState(state_i);
184 elseif size(state_i,1)>1
185 prior_state_i = zeros(1,size(state_i,1)); prior_state_i(1) = 1;
186 self.nodes{ind}.setStateSpace(state_i);
187 self.nodes{ind}.setStatePrior(prior_state_i);
188 self.nodes{ind}.setState(state_i(1,:));
190 self.nodes{ind}.setStateSpace([]);
191 self.nodes{ind}.setStatePrior([]);
192 self.nodes{ind}.setState([]);
197if self.isStateValid % problem with example_initState_2
198 self.hasState = true;
200 line_error(mfilename,sprintf('Default initialization failed.
'));