LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
isValid.m
1function isValid = isValid(sn, n, s, options)
2% ISVALID Validate network state against capacity and scheduling constraints
3%
4% @brief Checks if a given state is valid according to network constraints
5% @param sn Network structure or Network object
6% @param n Number of jobs per class at the station
7% @param s Number of jobs per class that are currently running/being served
8% @param options Optional validation configuration parameters
9% @return isValid Boolean indicating if the state satisfies all constraints
10%
11% This function validates whether a proposed network state is feasible
12% given the network's capacity limitations, scheduling constraints, and
13% other structural requirements. It is essential for state space generation
14% and state-based analysis methods.
15%
16% Copyright (c) 2012-2026, Imperial College London
17% All rights reserved.
18
19isValid = true;
20
21if isa(sn,'Network')
22 sn = sn.getStruct();
23end
24
25if isempty(n) & ~isempty(s)
26 isValid = false;
27 return
28end
29
30if iscell(n) %then n is a cell array of states
31 ncell = n;
32 n = [];
33 for isf=1:length(ncell)
34 ist = sn.statefulToStation(isf);
35 ind = sn.statefulToNode(isf);
36 [~, n(ist,:), s(ist,:), ~] = State.toMarginal(sn, ind, ncell{isf});
37 end
38end
39
40R = sn.nclasses;
41K = zeros(1,R);
42for ist=1:sn.nstations
43 for r=1:R
44 K(r) = sn.phases(ist,r);
45 if sn.nodetype(sn.stationToNode(ist)) ~= NodeType.Place
46 if ~isempty(sn.proc) && ~isempty(sn.proc{ist}{r}) && any(any(isnan(sn.proc{ist}{r}{1}))) && n(ist,r)>0 % if disabled
47 isValid = false;
48 % line_error(mfilename,sprintf('Chain %d is initialized with an incorrect number of jobs: %f instead of %d.', nc, statejobs_chain, njobs_chain));
49 return
50 end
51 end
52 end
53 if any(n(ist,:)>sn.classcap(ist,:))
54 line_warning(mfilename,'Station %d is in a state with more jobs than its allowed capacity.\n');
55 isValid = false;
56 return
57 end
58end
59
60if nargin > 2 && ~isempty(s)
61 for ist=1:sn.nstations
62 if sn.nservers(ist)>0
63 % if more running jobs than servers
64 if sum(s(ist,:)) > sn.nservers(ist)
65 switch sn.sched(ist) % don't flag invalid if ps
66 case {SchedStrategy.FCFS,SchedStrategy.SIRO,SchedStrategy.LCFS,SchedStrategy.HOL}
67 isValid = false;
68 return
69 end
70 end
71 % if more running jobs than jobs at the node
72 if any(n<s)
73 isValid = false;
74 return
75 end
76 % non-idling condition
77 if sum(s(ist,:)) ~= min(sum(n(ist,:)), sn.nservers(ist))
78 % commented because in ps queues s are in service as well
79 % isValid = false;
80 %return
81 end
82 end
83 end
84end
85
86for nc=1:sn.nchains
87 njobs_chain = sum(sn.njobs(find(sn.chains(nc,:))));
88 if ~isinf(njobs_chain)
89 statejobs_chain = sum(sum(n(:,find(sn.chains(nc,:))),2),1);
90 %if ~options.force && abs(1-njobs_chain/statejobs_chain) > options.iter_tol
91 if abs(1-njobs_chain/statejobs_chain) > GlobalConstants.CoarseTol
92 isValid = false;
93 line_error(mfilename,sprintf('Chain %d is initialized with an incorrect number of jobs: %f instead of %d.', nc, statejobs_chain, njobs_chain));
94 return
95 end
96 %end
97 end
98end
99end