1function [SSq,SSh,sn] = reachableSpaceGenerator(sn,options)
2% [SSQ,SSH,QN] = REACHABLESPACEGENERATOR(QN,OPTIONS)
4% This differs from spaceGenerator as it
is restricted to states reachable
5% from the initial state.
7% Copyright (c) 2012-2026, Imperial College London
10% By
default the jobs are all initialized in the first valid state
12%nstations = sn.nstations;
13nstateful = sn.nstateful;
14%init_nserver = sn.nservers; % restore Inf at delay
nodes
27 node_a{act} = sync{act}.active{1}.node;
28 node_p{act} = sync{act}.passive{1}.node;
29 class_a{act} = sync{act}.active{1}.class;
30 class_p{act} = sync{act}.passive{1}.class;
31 event_a{act} = sync{act}.active{1}.event;
32 event_p{act} = sync{act}.passive{1}.event;
37space = cell(1,nstateful);
39 space{i} = sn.state{i};
41SSh = ones(1,nstateful); % initial state
45maxstatesz = zeros(1,nstateful);
49% Dfilt{a} = sparse([]);
53 %Q = ctmc_makeinfgen(Q);
54 SSq = []; % initial state
58 SSq(i,colctr:(colctr+size(space{j},2)-1)) = space{j}(SSh(i,j),:);
59 colctr = colctr+size(space{j},2);
63 %
if (size(Dfilt{a},1) < size(SSq,1)) || (size(Dfilt{a},2) < size(SSq,1))
64 % Dfilt{a}(size(SSq,1),size(SSq,1)) = 0;
71 stateCell = stack{end};
72 state = cell2mat(stateCell);
73 ih = stack_index(end);
76 statelen = cellfun(@length, stateCell);
77 newStateCell = cell(1,A);
80 enabled_sync = {}; % row
is action label, col1=rate, col2=
new state
81 %enabled_new_state = {};
86 update_cond_a =
true; %((node_a{act} == last_node_a || node_a{act} == last_node_p));
87 newStateCell{act} = stateCell;
88 if update_cond_a || isempty(outprob_a{act})
89 isf = sn.nodeToStateful(node_a{act});
90 [newStateCell{act}{sn.nodeToStateful(node_a{act})}, rate_a{act}, outprob_a{act}] = State.afterEvent(sn, node_a{act}, stateCell{isf}, event_a{act}, class_a{act}, isSimulation);
91 if size(newStateCell{act}{sn.nodeToStateful(node_a{act})},1) == 0
96 if isempty(newStateCell{act}{sn.nodeToStateful(node_a{act})}) || isempty(rate_a{act}) % state not found
100 for ia=1:size(newStateCell{act}{sn.nodeToStateful(node_a{act})},1) %
for all possible
new states of the active agent
101 if isempty(newStateCell{act}{sn.nodeToStateful(node_a{act})})
105 if newStateCell{act}{sn.nodeToStateful(node_a{act})}(ia,:) == -1 % hash not found
108 %update_cond_p = ((node_p{act} == last_node_a || node_p{act} == last_node_p)) || isempty(outprob_p{act});
109 update_cond =
true; %update_cond_a || update_cond_p;
111 if node_p{act} ~= local
112 if node_p{act} == node_a{act} %self-loop
114 % since self-loop sare multiple instantaneous
115 % state transitions, we save the intermediate
116 % states in the sn.space structure
118 nci = newStateCell{act}{sn.nodeToStateful(node_a{act})};
120 if length(nci(j,:)) > size(space{i},2)
121 space{i} = [zeros(size(space{i},1),length(nci(j,:))-size(space{i},2)),space{i}];
122 elseif length(nci(j,:)) < size(space{i},2)
123 nci = [zeros(1, size(space{i},2)-length(nci(j,:))),nci(j,:)];
125 if matchrow(space{i},nci(j,:))==-1
126 space{i}(end+1,:) = nci(j,:);
129 [newStateCell{act}{sn.nodeToStateful(node_p{act})}, ~, outprob_p{act}] = State.afterEvent(sn, node_p{act}, newStateCell{act}{sn.nodeToStateful(node_a{act})}, event_p{act}, class_p{act}, isSimulation);
130 outprob_a{act} = outprob_p{act};
132 else % departure from active
134 [newStateCell{act}{sn.nodeToStateful(node_p{act})}, ~, outprob_p{act}] = State.afterEvent(sn, node_p{act}, stateCell{sn.nodeToStateful(node_p{act})}, event_p{act}, class_p{act}, isSimulation);
137 if ~isempty(newStateCell{act}{sn.nodeToStateful(node_p{act})})
138 if sn.isstatedep(node_a{act},3)
139 prob_sync_p{act} = sync{act}.passive{1}.prob(stateCell, newStateCell{act}); %state-dependent
141 prob_sync_p{act} = sync{act}.passive{1}.prob;
144 prob_sync_p{act} = 0;
147 if ~isempty(newStateCell{act}{sn.nodeToStateful(node_a{act})})
148 if node_p{act} == local
149 prob_sync_p{act} = 1; %outprob_a{act}; % was 1
151 if ~isnan(rate_a{act})
152 if all(~cellfun(@isempty,newStateCell{act}))
153 if event_a{act} == EventType.DEP
154 node_a_sf{act} = sn.nodeToStateful(node_a{act});
155 node_p_sf{act} = sn.nodeToStateful(node_p{act});
157 % simulate also self-loops as we need to log them
158 if node_p{act} < local && ~csmask(class_a{act}, class_p{act}) && sn.nodetype(node_p{act})~=NodeType.Source && (rate_a{act}(ia) * prob_sync_p{act} >0)
159 line_error(mfilename,sprintf(
'Error: state-dependent routing at node %d (%s) violates the class switching mask (node %d -> node %d, class %d -> class %d).', node_a{act}, sn.nodenames{node_a{act}}, node_a{act}, node_p{act}, class_a{act}, class_p{act}));
161 enabled_rates(ctr) = rate_a{act}(ia) * prob_sync_p{act};
162 enabled_sync{ctr} = act;
163 ctr = ctr + 1; % keep
171 for firing_ctr=1:length(enabled_rates)
172 firing_rate = enabled_rates(firing_ctr);
173 last_node_a = node_a{enabled_sync{firing_ctr}};
174 last_node_p = node_p{enabled_sync{firing_ctr}};
175 act = enabled_sync{firing_ctr};
176 netstates = newStateCell{act};
177 if enabled_rates(firing_ctr)>0 && ~any(cellfun(@isempty,netstates))
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 nvec = cellfun(@(c) size(c,1)-1, netstates);
185 for i=1:size(netstates,2)
187 firingprob = firingprob*outprob_a{enabled_sync{firing_ctr}}(1+n(i));
190 firingprob = firingprob*outprob_p{enabled_sync{firing_ctr}}(1+n(i));
192 maxstatesz(i) = max(maxstatesz(i),length(netstates{i}(1+n(i),:)));
193 newstate = [newstate,zeros(1,maxstatesz(i)-length(netstates{i}(1+n(i),:))),netstates{i}(1+n(i),:)];
194 newstatec{i} = [zeros(1,maxstatesz(i)-length(netstates{i}(1+n(i),:))),netstates{i}(1+n(i),:)];
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 hashednewstate = zeros(1,nstateful);
200 if length(newstatec{i}) > size(space{i},2)
201 hashednewstate(i) = matchrow([zeros(size(space{i},1),length(newstatec{i})-size(space{i},2)),space{i}], newstatec{i});
203 hashednewstate(i) = matchrow(space{i}, newstatec{i});
206 jh = matchrow(SSh,hashednewstate);
207 if jh == -1 || (length(newstate)~=length(state) || any(newstate~=state))
208 % store and update jh index
211 if hashednewstate(i) == -1
213 if length(nci) > size(space{i},2)
214 space{i} = [zeros(size(space{i},1),length(nci)-size(space{i},2)),space{i}];
215 elseif length(nci) < size(space{i},2)
216 nci = [zeros(1, size(space{i},2)-length(nci)),nci];
218 space{i}(end+1,:) = nci;
219 hashednewstate(i) = size(space{i},1);
222 SSh = [SSh; hashednewstate];
224 stack{end+1} = newstatec;
225 stack_index(end+1) = jh;
228 %
for some reason the Q generation
is buggy so we
230 %
if (size(Q,1)< ih) || (size(Q,2)< jh)
231 % Q(ih,jh) = firing_rate;
233 % Dfilt{act}(ih,jh) = 0;
235 % Dfilt{act}(ih,jh) = firing_rate;
237 % Q(ih,jh) = Q(ih,jh) + firing_rate;
239 %
if (size(Dfilt{act},1) < ih) || (size(Dfilt{act},2) < jh)
240 % Dfilt{act}(ih,jh) = 0;
243 % Dfilt{act}(ih,jh) = Dfilt{act}(ih,jh) + firing_rate;
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%