function petriNetPInvariantsGui % PETRINETPINVARIANTSGUI GUI for illustrating % token conservation (P-invariants). % % Place invariant condition: % y^T * A = 0 % % Token conservation: % y^T * Mk = y^T * M0 % % A P-invariant is a weighted group of places % whose total tokens never change. % samplePre = [1 0 0; 0 1 0; 0 0 1]; samplePost = [0 1 0; 0 0 1; 1 0 0]; sampleM0 = [1; 0; 0]; sampleMk = [0; 0; 1]; sampleY = [1; 1; 1]; fig = uifigure('Name', 'Petri Net P-Invariants', 'Position', [100 100 1220 740]); mainGrid = uigridlayout(fig, [2 2]); mainGrid.RowHeight = {'1x', 28}; mainGrid.ColumnWidth = {'1x', '1x'}; mainGrid.Padding = [10 10 10 10]; mainGrid.RowSpacing = 8; mainGrid.ColumnSpacing = 10; inputPanel = uipanel(mainGrid, 'Title', 'Inputs'); inputPanel.Layout.Row = 1; inputPanel.Layout.Column = 1; outputPanel = uipanel(mainGrid, 'Title', 'Results'); outputPanel.Layout.Row = 1; outputPanel.Layout.Column = 2; statusLabel = uilabel(mainGrid, 'Text', 'Ready.'); statusLabel.Layout.Row = 2; statusLabel.Layout.Column = [1 2]; inputGrid = uigridlayout(inputPanel, [8 2]); inputGrid.RowHeight = {22, '1x', 22, 28, 22, 28, 22, 30}; inputGrid.ColumnWidth = {'1x', '1x'}; inputGrid.Padding = [8 8 8 8]; inputGrid.RowSpacing = 8; inputGrid.ColumnSpacing = 8; uilabel(inputGrid, 'Text', 'Pre matrix W(p_i, t_j)'); preLabel = uilabel(inputGrid, 'Text', 'Post matrix W(t_j, p_i)'); preLabel.Layout.Row = 1; preLabel.Layout.Column = 2; preTable = uitable(inputGrid, ... 'Data', samplePre, ... 'ColumnEditable', true(1, size(samplePre, 2)), ... 'CellEditCallback', @refreshAll); preTable.Layout.Row = 2; preTable.Layout.Column = 1; postTable = uitable(inputGrid, ... 'Data', samplePost, ... 'ColumnEditable', true(1, size(samplePost, 2)), ... 'CellEditCallback', @refreshAll); postTable.Layout.Row = 2; postTable.Layout.Column = 2; uilabel(inputGrid, 'Text', 'Initial marking M0'); mkLabel = uilabel(inputGrid, 'Text', 'Target marking Mk'); mkLabel.Layout.Row = 3; mkLabel.Layout.Column = 2; m0Field = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleM0), ... 'ValueChangedFcn', @refreshAll); m0Field.Layout.Row = 4; m0Field.Layout.Column = 1; mkField = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleMk), ... 'ValueChangedFcn', @refreshAll); mkField.Layout.Row = 4; mkField.Layout.Column = 2; uilabel(inputGrid, 'Text', 'Invariant vector y'); yLabel = uilabel(inputGrid, 'Text', 'Example: [1 1 1]'); yLabel.Layout.Row = 5; yLabel.Layout.Column = 2; yField = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleY), ... 'ValueChangedFcn', @refreshAll); yField.Layout.Row = 6; yField.Layout.Column = 1; buttonGrid = uigridlayout(inputGrid, [1 2]); buttonGrid.Layout.Row = 8; buttonGrid.Layout.Column = [1 2]; buttonGrid.ColumnWidth = {'1x', '1x'}; uibutton(buttonGrid, 'Text', 'Check P-Invariant', 'ButtonPushedFcn', @refreshAll); uibutton(buttonGrid, 'Text', 'Reset Example', 'ButtonPushedFcn', @resetExample); outputGrid = uigridlayout(outputPanel, [6 1]); outputGrid.RowHeight = {80, '1x', 28, 60, 60, '1x'}; outputGrid.Padding = [8 8 8 8]; formulaText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', [ "Place invariant condition:" "y^T * A = 0" "Token conservation:" "y^T * Mk = y^T * M0" "A P-invariant is a weighted set of places whose total tokens never change." ]); formulaText.Layout.Row = 1; uilabel(outputGrid, 'Text', 'Incidence matrix A = Post - Pre'); incidenceTable = uitable(outputGrid, 'Data', samplePost - samplePre); incidenceTable.Layout.Row = 2; resultLabel = uilabel(outputGrid, 'Text', ''); resultLabel.Layout.Row = 3; conservationText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); conservationText.Layout.Row = 4; supportText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); supportText.Layout.Row = 5; basisText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); basisText.Layout.Row = 6; refreshAll(); function refreshAll(~, ~) try Pre = readMatrix(preTable.Data, 'Pre'); Post = readMatrix(postTable.Data, 'Post'); if ~isequal(size(Pre), size(Post)) error('Pre and Post matrices must have the same size.'); end requireNonnegativeIntegerMatrix(Pre, 'Pre'); requireNonnegativeIntegerMatrix(Post, 'Post'); [nPlaces, ~] = size(Pre); M0 = parseVector(m0Field.Value, nPlaces, 'M0'); Mk = parseVector(mkField.Value, nPlaces, 'Mk'); y = parseVector(yField.Value, nPlaces, 'y'); requireNonnegativeIntegerVector(M0, 'M0'); requireNonnegativeIntegerVector(Mk, 'Mk'); requireNonnegativeVector(y, 'y'); A = Post - Pre; incidenceTable.Data = A; invariantResidual = y.' * A; conservedInitial = y.' * M0; conservedTarget = y.' * Mk; conservationGap = conservedTarget - conservedInitial; isInvariant = norm(invariantResidual, inf) <= 1e-12; isConserved = abs(conservationGap) <= 1e-12; if isInvariant resultLabel.Text = 'y is a P-invariant: y^T * A = 0.'; else resultLabel.Text = 'y is not a P-invariant: y^T * A is nonzero.'; end conservationText.Value = [ "y^T * M0 = " + scalarToString(conservedInitial) "y^T * Mk = " + scalarToString(conservedTarget) "Difference = " + scalarToString(conservationGap) ]; if isInvariant && isConserved supportText.Value = [ "Token conservation holds for the chosen y." "Places with y(i) > 0 are in the support of the invariant." "That support is structurally bounded by the invariant." ]; elseif isInvariant supportText.Value = [ "y satisfies y^T * A = 0." "But y^T * Mk ~= y^T * M0 for the selected markings." "So the chosen markings do not conserve this invariant." ]; else supportText.Value = [ "The chosen y does not satisfy the place-invariant condition." "Try a nonnegative vector in null(A^T)." ]; end basis = computePInvariantBasis(A); if isempty(basis) basisText.Value = [ "No basis vector was extracted." "The null space may be trivial or numerically ill-conditioned." ]; else lines = strings(size(basis, 2) + 1, 1); lines(1) = "Candidate P-invariant basis vectors:"; for k = 1:size(basis, 2) lines(k + 1) = "v" + string(k) + " = " + vectorToString(basis(:, k)); end basisText.Value = lines; end statusLabel.Text = 'Ready.'; catch ME statusLabel.Text = "Error: " + string(ME.message); resultLabel.Text = ''; conservationText.Value = strings(0,1); supportText.Value = strings(0,1); basisText.Value = strings(0,1); end end function resetExample(~, ~) preTable.Data = samplePre; postTable.Data = samplePost; m0Field.Value = vectorToString(sampleM0); mkField.Value = vectorToString(sampleMk); yField.Value = vectorToString(sampleY); refreshAll(); end function A = readMatrix(data, name) if iscell(data) A = cell2mat(data); else A = data; end if isempty(A) error('%s matrix cannot be empty.', name); end if any(~isfinite(A), 'all') error('%s matrix must contain finite numbers.', name); end end function v = parseVector(textValue, expectedLength, name) if isstring(textValue) textValue = char(textValue); end textValue = strtrim(textValue); textValue = regexprep(textValue, '[\[\];,]', ' '); parts = split(string(textValue)); parts(parts == "") = []; if isempty(parts) error('Invalid %s.', name); end v = str2double(parts); if any(isnan(v)) error('Invalid %s.', name); end v = v(:); if numel(v) ~= expectedLength error('The %s vector must have %d elements.', name, expectedLength); end end function requireNonnegativeIntegerMatrix(A, name) tol = 1e-12; if any(A(:) < 0) error('%s must be nonnegative.', name); end if any(abs(A(:) - round(A(:))) > tol) error('%s must contain integers.', name); end end function requireNonnegativeIntegerVector(v, name) tol = 1e-12; if any(v(:) < 0) error('%s must be nonnegative.', name); end if any(abs(v(:) - round(v(:))) > tol) error('%s must contain integers.', name); end end function requireNonnegativeVector(v, name) if any(v(:) < 0) error('%s must be nonnegative.', name); end end function basis = computePInvariantBasis(A) % Compute a numerical basis for null(A^T). % Each column is a candidate P-invariant vector. basis = null(A.', 'r'); if isempty(basis) return; end % Normalize columns for display. for k = 1:size(basis, 2) col = basis(:, k); col = col ./ max(1, max(abs(col))); basis(:, k) = col; end end function s = vectorToString(v) v = v(:).'; if isempty(v) s = "[]"; return; end s = "[" + join(string(compose('%g', v)), " ") + "]"; end function s = scalarToString(x) s = string(compose('%g', x)); end end