function petriNetLinearAlgebraGui2 %PETRINETLINEARALGEBRAGUI GUI for Petri net linear % algebra tools. % % Incidence matrix: % A(i,j) = W(t_j, p_i) - W(p_i, t_j) % % Linear state equation: % Mk = M0 + A*x % % A reachable target state must admit a nonnegative % integer solution x >= 0. % 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]; fig = uifigure('Name', 'Petri Net Linear Algebra', 'Position', [100 100 1180 720]); 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', 'Output'); 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, [7 2]); inputGrid.RowHeight = {22, '1x', 22, 28, 22, 28, 30}; inputGrid.ColumnWidth = {'1x', '1x'}; inputGrid.RowSpacing = 8; inputGrid.ColumnSpacing = 8; inputGrid.Padding = [8 8 8 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', 'Format: [1 0 0] or 1,0,0'); hintLabel = uilabel(inputGrid, 'Text', 'Use spaces, commas, semicolons, or brackets'); hintLabel.Layout.Row = 5; hintLabel.Layout.Column = 2; buttonGrid = uigridlayout(inputGrid, [1 2]); buttonGrid.Layout.Row = 7; buttonGrid.Layout.Column = [1 2]; buttonGrid.ColumnWidth = {'1x', '1x'}; uibutton(buttonGrid, 'Text', 'Check State Equation', 'ButtonPushedFcn', @refreshAll); uibutton(buttonGrid, 'Text', 'Reset Example', 'ButtonPushedFcn', @resetExample); outputGrid = uigridlayout(outputPanel, [5 1]); outputGrid.RowHeight = {78, '1x', 28, 60, '1x'}; outputGrid.Padding = [8 8 8 8]; equationText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', [ "Incidence matrix:" "A(i,j) = W(t_j, p_i) - W(p_i, t_j)" "State equation:" "Mk = M0 + A * x" "Reachability test requires x to be a nonnegative integer vector." ]); equationText.Layout.Row = 1; uilabel(outputGrid, 'Text', 'Incidence matrix A'); incidenceTable = uitable(outputGrid, 'Data', samplePost - samplePre); incidenceTable.Layout.Row = 2; feasibilityLabel = uilabel(outputGrid, 'Text', ''); feasibilityLabel.Layout.Row = 3; xText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0, 1)); xText.Layout.Row = 4; detailText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0, 1)); detailText.Layout.Row = 5; 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, nTransitions] = size(Pre); M0 = parseVector(m0Field.Value, nPlaces, 'M0'); Mk = parseVector(mkField.Value, nPlaces, 'Mk'); requireNonnegativeIntegerVector(M0, 'M0'); requireNonnegativeIntegerVector(Mk, 'Mk'); A = Post - Pre; incidenceTable.Data = A; [isFeasible, x, solverMessage] = solveStateEquation(A, M0, Mk); if isFeasible feasibilityLabel.Text = 'Feasible: a nonnegative integer solution x exists.'; xText.Value = [ "x = " + vectorToString(x) "A*x = " + vectorToString(A * x) ]; else feasibilityLabel.Text = 'Not feasible: no nonnegative integer solution was found.'; xText.Value = [ "x = []" "No valid nonnegative integer solution." ]; end detailText.Value = [ "M0 = " + vectorToString(M0) "Mk = " + vectorToString(Mk) "Mk - M0 = " + vectorToString(Mk - M0) "State equation: Mk = M0 + A*x" "Solver status: " + solverMessage ]; statusLabel.Text = 'Ready.'; catch ME statusLabel.Text = "Error: " + string(ME.message); feasibilityLabel.Text = ''; xText.Value = strings(0, 1); detailText.Value = strings(0, 1); end end function resetExample(~, ~) preTable.Data = samplePre; postTable.Data = samplePost; m0Field.Value = vectorToString(sampleM0); mkField.Value = vectorToString(sampleMk); 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 [isFeasible, x, message] = solveStateEquation(A, M0, Mk) b = Mk - M0; nTransitions = size(A, 2); isFeasible = false; x = zeros(nTransitions, 1); message = "Unknown"; if exist('intlinprog', 'file') ~= 2 message = "intlinprog is not available."; return; end f = zeros(nTransitions, 1); intcon = 1:nTransitions; lb = zeros(nTransitions, 1); if exist('optimoptions', 'file') == 2 opts = optimoptions('intlinprog', 'Display', 'off'); [xSol, ~, exitflag] = intlinprog(f, intcon, [], [], A, b, lb, [], opts); else [xSol, ~, exitflag] = intlinprog(f, intcon, [], [], A, b, lb, []); end tol = 1e-9; if exitflag > 0 && ~isempty(xSol) xSol = round(xSol); if all(xSol >= 0) && norm(A * xSol - b, inf) <= tol isFeasible = true; x = xSol; message = "Feasible integer solution found."; return; end end message = "No nonnegative integer solution satisfies Mk = M0 + A*x."; end function s = vectorToString(v) v = v(:).'; if isempty(v) s = "[]"; return; end s = "[" + join(string(compose('%g', v)), " ") + "]"; end end