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([]);
52 % Cooperative wall-clock budget checkpoint (options.timeout): a partial
53 % reachable space would give silently wrong results, so abort
the solve.
54 if lineTimeoutExceeded(options)
55 line_error(mfilename,
'Reachable state space generation exceeded the wall-clock time budget (options.timeout=%gs).', options.timeout);
58 %Q = ctmc_makeinfgen(Q);
59 SSq = []; % initial state
63 SSq(i,colctr:(colctr+size(space{j},2)-1)) = space{j}(SSh(i,j),:);
64 colctr = colctr+size(space{j},2);
68 %
if (size(Dfilt{a},1) < size(SSq,1)) || (size(Dfilt{a},2) < size(SSq,1))
69 % Dfilt{a}(size(SSq,1),size(SSq,1)) = 0;
76 stateCell = stack{end};
77 state = cell2mat(stateCell);
78 ih = stack_index(end);
81 statelen = cellfun(@length, stateCell);
82 newStateCell = cell(1,A);
85 enabled_sync = {}; % row
is action label, col1=rate, col2=
new state
86 %enabled_new_state = {};
91 update_cond_a =
true; %((node_a{act} == last_node_a || node_a{act} == last_node_p));
92 newStateCell{act} = stateCell;
93 if update_cond_a || isempty(outprob_a{act})
94 isf = sn.nodeToStateful(node_a{act});
95 [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);
96 if size(newStateCell{act}{sn.nodeToStateful(node_a{act})},1) == 0
101 if isempty(newStateCell{act}{sn.nodeToStateful(node_a{act})}) || isempty(rate_a{act}) % state not found
105 for ia=1:size(newStateCell{act}{sn.nodeToStateful(node_a{act})},1) %
for all possible
new states of
the active agent
106 if isempty(newStateCell{act}{sn.nodeToStateful(node_a{act})})
110 if newStateCell{act}{sn.nodeToStateful(node_a{act})}(ia,:) == -1 % hash not found
113 %update_cond_p = ((node_p{act} == last_node_a || node_p{act} == last_node_p)) || isempty(outprob_p{act});
114 update_cond =
true; %update_cond_a || update_cond_p;
116 if node_p{act} ~= local
117 if node_p{act} == node_a{act} %self-loop
119 % since self-loop sare multiple instantaneous
120 % state transitions, we save
the intermediate
121 % states in
the sn.space structure
123 nci = newStateCell{act}{sn.nodeToStateful(node_a{act})};
125 if length(nci(j,:)) > size(space{i},2)
126 space{i} = [zeros(size(space{i},1),length(nci(j,:))-size(space{i},2)),space{i}];
127 elseif length(nci(j,:)) < size(space{i},2)
128 nci = [zeros(1, size(space{i},2)-length(nci(j,:))),nci(j,:)];
130 if matchrow(space{i},nci(j,:))==-1
131 space{i}(end+1,:) = nci(j,:);
134 [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);
135 outprob_a{act} = outprob_p{act};
137 else % departure from active
139 [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);
142 if ~isempty(newStateCell{act}{sn.nodeToStateful(node_p{act})})
143 if sn.isstatedep(node_a{act},3)
144 prob_sync_p{act} = sync{act}.passive{1}.prob(stateCell, newStateCell{act}); %state-dependent
146 prob_sync_p{act} = sync{act}.passive{1}.prob;
149 prob_sync_p{act} = 0;
152 if ~isempty(newStateCell{act}{sn.nodeToStateful(node_a{act})})
153 if node_p{act} == local
154 prob_sync_p{act} = 1; %outprob_a{act}; % was 1
156 if ~isnan(rate_a{act})
157 if all(~cellfun(@isempty,newStateCell{act}))
158 if event_a{act} == EventType.DEP
159 node_a_sf{act} = sn.nodeToStateful(node_a{act});
160 node_p_sf{act} = sn.nodeToStateful(node_p{act});
162 % simulate also self-loops as we need to log them
163 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)
164 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}));
166 enabled_rates(ctr) = rate_a{act}(ia) * prob_sync_p{act};
167 enabled_sync{ctr} = act;
168 ctr = ctr + 1; % keep
176 for firing_ctr=1:length(enabled_rates)
177 firing_rate = enabled_rates(firing_ctr);
178 last_node_a = node_a{enabled_sync{firing_ctr}};
179 last_node_p = node_p{enabled_sync{firing_ctr}};
180 act = enabled_sync{firing_ctr};
181 netstates = newStateCell{act};
182 if enabled_rates(firing_ctr)>0 && ~any(cellfun(@isempty,netstates))
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 nvec = cellfun(@(c) size(c,1)-1, netstates);
190 for i=1:size(netstates,2)
192 firingprob = firingprob*outprob_a{enabled_sync{firing_ctr}}(1+n(i));
195 firingprob = firingprob*outprob_p{enabled_sync{firing_ctr}}(1+n(i));
197 maxstatesz(i) = max(maxstatesz(i),length(netstates{i}(1+n(i),:)));
198 newstate = [newstate,zeros(1,maxstatesz(i)-length(netstates{i}(1+n(i),:))),netstates{i}(1+n(i),:)];
199 newstatec{i} = [zeros(1,maxstatesz(i)-length(netstates{i}(1+n(i),:))),netstates{i}(1+n(i),:)];
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 hashednewstate = zeros(1,nstateful);
205 if length(newstatec{i}) > size(space{i},2)
206 hashednewstate(i) = matchrow([zeros(size(space{i},1),length(newstatec{i})-size(space{i},2)),space{i}], newstatec{i});
208 hashednewstate(i) = matchrow(space{i}, newstatec{i});
211 jh = matchrow(SSh,hashednewstate);
212 if jh == -1 || (length(newstate)~=length(state) || any(newstate~=state))
213 % store and update jh index
216 if hashednewstate(i) == -1
218 if length(nci) > size(space{i},2)
219 space{i} = [zeros(size(space{i},1),length(nci)-size(space{i},2)),space{i}];
220 elseif length(nci) < size(space{i},2)
221 nci = [zeros(1, size(space{i},2)-length(nci)),nci];
223 space{i}(end+1,:) = nci;
224 hashednewstate(i) = size(space{i},1);
227 SSh = [SSh; hashednewstate];
229 stack{end+1} = newstatec;
230 stack_index(end+1) = jh;
233 %
for some reason
the Q generation
is buggy so we
235 %
if (size(Q,1)< ih) || (size(Q,2)< jh)
236 % Q(ih,jh) = firing_rate;
238 % Dfilt{act}(ih,jh) = 0;
240 % Dfilt{act}(ih,jh) = firing_rate;
242 % Q(ih,jh) = Q(ih,jh) + firing_rate;
244 %
if (size(Dfilt{act},1) < ih) || (size(Dfilt{act},2) < jh)
245 % Dfilt{act}(ih,jh) = 0;
248 % Dfilt{act}(ih,jh) = Dfilt{act}(ih,jh) + firing_rate;
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % explore SPN global synchronizations: a Transition
's ENABLE moves tokens
257 % into the mode's servers and its FIRE completes them, applying
the PRE
258 % consumption and POST production on
the places. These live in sn.gsync and
259 % not in sn.sync, so without
this block a net whose dynamics are entirely
260 % Petri-net firings never leaves its initial marking.
261 if isfield(sn,
'gsync') && ~isempty(sn.gsync)
262 for g=1:length(sn.gsync)
263 gind = sn.gsync{g}.active{1}.node;
264 isf_t = sn.nodeToStateful(gind);
265 [outglspace, outrate, outprob] = State.afterGlobalEvent(sn, gind, stateCell, sn.gsync{g},
false);
269 for io=1:length(outrate)
274 if ~isempty(outprob) && io <= length(outprob)
275 prob_io = outprob(io);
280 if size(outglspace{isf_t},1) < io
284 gStates{isf_t} = outglspace{isf_t}(io,:);
285 if sn.gsync{g}.active{1}.event == EventType.FIRE
287 if i ~= isf_t && ~isequal(stateCell{i}, outglspace{i})
288 gStates{i} = outglspace{i};
293 % A firing that consumed tokens its input place did not hold
is
294 % not enabled and must never be emitted. afterGlobalEvent gates
295 %
the completion on
the place
's enabling degree for exactly this
296 % reason, so this is an invariant guard, not the fix. The lattice
297 % generator has an implicit backstop -- an unrepresentable marking
298 % is absent from sn.space, matchrow misses and solver_ctmc drops
299 % the arc -- but enumerating reachable states has none, so a
300 % regression in the FIRE handler would silently mint the state
301 % and surface only as a mean token count above the population.
302 % Failing here names the event instead.
303 if i_hasNegativePlace(sn, gStates)
304 line_error(mfilename, sprintf(['%s at %s produced a negative place:
' ...
305 'a firing completed without its input place holding
the tokens
' ...
306 'to pay
for it. The FIRE handler in afterGlobalEvent should have
' ...
307 'gated
this completion on
the enabling degree.
'], ...
308 EventType.toText(sn.gsync{g}.active{1}.event), sn.nodenames{gind}));
311 newstatec = cell(1,nstateful);
313 maxstatesz(i) = max(maxstatesz(i),length(gStates{i}));
314 newstatec{i} = [zeros(1,maxstatesz(i)-length(gStates{i})),gStates{i}];
316 hashednewstate = zeros(1,nstateful);
318 if length(newstatec{i}) > size(space{i},2)
319 hashednewstate(i) = matchrow([zeros(size(space{i},1),length(newstatec{i})-size(space{i},2)),space{i}], newstatec{i});
321 hashednewstate(i) = matchrow(space{i}, newstatec{i});
324 jh = matchrow(SSh,hashednewstate);
327 if hashednewstate(i) == -1
329 if length(nci) > size(space{i},2)
330 space{i} = [zeros(size(space{i},1),length(nci)-size(space{i},2)),space{i}];
331 elseif length(nci) < size(space{i},2)
332 nci = [zeros(1, size(space{i},2)-length(nci)),nci];
334 space{i}(end+1,:) = nci;
335 hashednewstate(i) = size(space{i},1);
338 SSh = [SSh; hashednewstate]; %#ok<AGROW>
339 stack{end+1} = newstatec; %#ok<AGROW>
340 stack_index(end+1) = size(SSh,1); %#ok<AGROW>
345 % explore fork firing synchronizations (FJ-augmented structs only):
346 % each firing consumes the parent held at the Fork and emits one
347 % sibling per branch atomically (multi-node successor states)
348 if isfield(sn,'fjsync
') && ~isempty(sn.fjsync)
349 for k=1:length(sn.fjsync)
350 [fjStates, ~, fjprob] = State.afterFJEvent(sn, sn.fjsync{k}, stateCell, false);
351 for io=1:length(fjStates)
353 newstatec = cell(1,nstateful);
355 maxstatesz(i) = max(maxstatesz(i),length(fjStates{io}{i}));
356 newstatec{i} = [zeros(1,maxstatesz(i)-length(fjStates{io}{i})),fjStates{io}{i}];
358 hashednewstate = zeros(1,nstateful);
360 if length(newstatec{i}) > size(space{i},2)
361 hashednewstate(i) = matchrow([zeros(size(space{i},1),length(newstatec{i})-size(space{i},2)),space{i}], newstatec{i});
363 hashednewstate(i) = matchrow(space{i}, newstatec{i});
366 jh = matchrow(SSh,hashednewstate);
369 if hashednewstate(i) == -1
371 if length(nci) > size(space{i},2)
372 space{i} = [zeros(size(space{i},1),length(nci)-size(space{i},2)),space{i}];
373 elseif length(nci) < size(space{i},2)
374 nci = [zeros(1, size(space{i},2)-length(nci)),nci];
376 space{i}(end+1,:) = nci;
377 hashednewstate(i) = size(space{i},1);
380 SSh = [SSh; hashednewstate]; %#ok<AGROW>
381 stack{end+1} = newstatec; %#ok<AGROW>
382 stack_index(end+1) = size(SSh,1); %#ok<AGROW>
392function neg = i_hasNegativePlace(sn, stateCell)
393% True when any place in the successor holds a negative number of tokens, which
394% means the firing that produced it consumed tokens the place did not have.
397 if ~sn.isstateful(ind) || sn.nodetype(ind) ~= NodeType.Place
400 isf = sn.nodeToStateful(ind);
401 if isf > numel(stateCell) || isempty(stateCell{isf})
404 if any(stateCell{isf} < 0)