LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
initDefault.m
1function initDefault(self, nodes)
2% INITDEFAULT(NODES)
3
4% open classes empty
5% closed classes initialized at ref station
6% running jobs are allocated in class id order until all
7% servers are busy
8
9%refreshStruct(self); % we force update of the model before we initialize
10
11sn = self.getStruct(false);
12
13R = sn.nclasses;
14N = sn.njobs';
15if nargin < 2
16 nodes = 1:self.getNumberOfNodes;
17end
18
19for ind=nodes
20 if sn.isstation(ind)
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)
27 n0(r) = N(r);
28 end
29 s0(r) = min(n0(r),s);
30 s = s - s0(r);
31 end
32 switch sn.nodetype(ind)
33 case NodeType.Cache
34 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)',s0(:)');
35 state_i = [state_i, 1:sn.nvars(ind,2*R+1)]; %#ok<AGROW>
36 case NodeType.Place
37 if sum(self.nodes{ind}.state)>0
38 % if the user pre-loaded manually some jobs, keep them
39 state_i = self.nodes{ind}.state;
40 else
41 state_i = zeros(1,self.getNumberOfClasses);
42 for r=1:sn.nclasses
43 if sn.refstat(r) == ist
44 state_i(r) = sn.njobs(r);
45 else
46 state_i(r) = 0;
47 end
48 end
49 end
50 otherwise
51 state_i = State.fromMarginalAndStarted(sn,ind,n0(:)',s0(:)');
52 if sn.isstation(ind)
53 for r=1:sn.nclasses
54 switch sn.procid(sn.nodeToStation(ind),r)
55 case ProcessType.MAP
56 %state_i = State.cartesian(state_i, [1:sn.phases(i,r)]');
57 state_i = State.cartesian(state_i, 1);
58 end
59 end
60 end
61 end
62 for r=1:sn.nclasses
63 switch sn.routing(ind,r)
64 case {RoutingStrategy.RROBIN, RoutingStrategy.WRROBIN}
65 % start from first connected queue
66 state_i = [state_i, find(sn.connmatrix(ind,:),1)];
67 end
68 end
69 if isempty(state_i)
70 line_error(mfilename,sprintf('Default initialization failed on station %d.',ind));
71 end
72 elseif sn.isstateful(ind) % not a station
73 switch sn.nodetype(ind)
74 case NodeType.Cache
75 state_i = [zeros(1,self.getNumberOfClasses), 1:sum(self.nodes{ind}.itemLevelCap)];
76 case NodeType.Router
77 state_i = zeros(1, self.getNumberOfClasses);
78 for r=1:sn.nclasses
79 switch sn.routing(ind,r)
80 case RoutingStrategy.RROBIN
81 % RR slot holds destination value; start at first connected queue
82 state_i = [state_i, find(sn.connmatrix(ind,:),1)]; %#ok<AGROW>
83 case RoutingStrategy.WRROBIN
84 % WRR slot holds POSITION in weighted_outlinks; start at 1
85 state_i = [state_i, 1]; %#ok<AGROW>
86 case RoutingStrategy.KCHOICES
87 % KCHOICES with memory: -1 marks "no prior pick".
88 if isfield(self.nodes{ind}.output.outputStrategy{1, r}, 'withMemory') ...
89 || (iscell(self.nodes{ind}.output.outputStrategy{1, r}) ...
90 && length(self.nodes{ind}.output.outputStrategy{1, r}) >= 3 ...
91 && length(self.nodes{ind}.output.outputStrategy{1, r}{3}) >= 2 ...
92 && self.nodes{ind}.output.outputStrategy{1, r}{3}{2})
93 state_i = [state_i, -1]; %#ok<AGROW>
94 end
95 end
96 end
97 case NodeType.Transition
98 % Differently from a server, the first nmodes states in a
99 % transitions count the servers that are not enabled and
100 % the last nodes count servers that just fired.
101 % This is required as the local state does not encode the
102 % buffer hence the enabling condition is not available.
103 state_i = sn.nodeparam{ind}.nmodeservers;
104 % For infinite servers, use large finite value for state (SSA needs finite states)
105 state_i(isinf(state_i)) = GlobalConstants.MaxInt();
106 % For non-Markovian distributions, firingphases is NaN - treat as 1 phase
107 firingphases = sn.nodeparam{ind}.firingphases;
108 firingphases(isnan(firingphases)) = 1;
109 state_i = [state_i, zeros(1, sum(firingphases)), zeros(size(state_i))]; %#ok<AGROW>
110 otherwise
111 state_i = [];
112 end
113 %line_error(mfilename,'Default initialization not available on stateful node %d.',i);
114 end
115
116 if sn.isstateful(ind) % not a station
117 if size(state_i,1)==1
118 self.nodes{ind}.setStateSpace(state_i);
119 self.nodes{ind}.setStatePrior(1);
120 self.nodes{ind}.setState(state_i);
121 elseif size(state_i,1)>1
122 prior_state_i = zeros(1,size(state_i,1)); prior_state_i(1) = 1;
123 self.nodes{ind}.setStateSpace(state_i);
124 self.nodes{ind}.setStatePrior(prior_state_i);
125 self.nodes{ind}.setState(state_i(1,:));
126 else
127 self.nodes{ind}.setStateSpace([]);
128 self.nodes{ind}.setStatePrior([]);
129 self.nodes{ind}.setState([]);
130 end
131 end
132end
133
134if self.isStateValid % problem with example_initState_2
135 self.hasState = true;
136else
137 line_error(mfilename,sprintf('Default initialization failed.'));
138end
139end
Definition mmt.m:124