public Basis Execute(Matrix matrix) { var basis = new Basis(); var row = matrix.GetRow(0); for (int i = 0; i < row.rows; i++) { if (row[i] == 0) { var v = new Vector(row.rows); v[i] = 1; basis.Add(v); } else { bool sign = row[i] > 0; for (int j = i + 1; j < row.rows; j++) { if (row[j] != 0 && row[j] > 0 ^ sign) { var lcm = MathExtensions.LCM(Math.Abs((int)row[i]), Math.Abs((int)row[j])); var v = new Vector(row.rows); v[i] = (lcm / Math.Abs((int)row[i])); v[j] = (lcm / Math.Abs((int)row[j])); basis.Add(v); } } } } return ExcecuteRound(matrix, basis, 1); }
public override Tree Build(Vector initialMarking=null) { PetriNet.BuildIncedentMatrix(); var tree = new Tree(); tree.Root = new TreeNode(initialMarking ?? PetriNet.InitialMarking); BuildFromNode(tree.Root); return tree; }
public static Vector operator *(int n, Vector v) { var nv = new Vector(v.rows); for (int i = 0; i < v.rows; i++) { nv[i] = v[i] * n; } return nv; }
public static Vector operator +(Vector a, Vector b) { if (a.rows != b.rows) throw new ArgumentException("Different vector sizes"); var nv = new Vector(a.rows); for (int i = 0; i < a.rows; i++) { nv[i] = a[i] + b[i]; } return nv; }
private Basis ExcecuteRound(Matrix matrix, Basis basis, int rowIndex) { var currentRow = matrix.GetRow(rowIndex); var currentRes = new Vector(basis.Count); for (int i = 0; i < basis.Count; i++) { currentRes[i] = (int)(basis[i].Transpose() * currentRow)[0, 0]; } var newBasis = Compose(basis, currentRes); if (rowIndex == matrix.rows-1) return newBasis; return ExcecuteRound(matrix, newBasis, rowIndex + 1); }
private Basis Compose(Basis basis, Vector items) { var newBasis = new Basis(); for (int i = 0; i < items.rows; i++) { if (items[i] == 0) newBasis.Add(basis[i]); else { bool sign = items[i] > 0; for (int j = i + 1; j < items.rows; j++) { if (items[j] != 0 && items[j] > 0 ^ sign) { var lcm = MathExtensions.LCM(Math.Abs((int)items[i]), Math.Abs((int)items[j])); newBasis.Add((lcm / Math.Abs((int)items[i])) * basis[i] + (lcm / Math.Abs((int)items[j])) * basis[j]); } } } } return newBasis; }
private void BuildFromNode(TreeNode node) { var tSet = GetAvailableTransitions(node); if (tSet != null && tSet.Count>0) { if (BackTrackChecking(node)!=BackTrackCheckingResult.Nothing) return; //building child nodes foreach (var t in tSet) { var newMarking = new Vector(PetriNet.PlacesCount); for (int p = 0; p < PetriNet.PlacesCount; p++) { newMarking[p] = node.Marking[p] + PetriNet.IncedentMatrix[p,t]; } node.AddChild(new TreeNode(newMarking),t); } foreach (var childNode in node.Child) { BuildFromNode(childNode); } } }
public abstract Tree Build(Vector initialMarking = null);
public Vector GetRow(int k) { Vector m = new Vector(cols); for (int i = 0; i < cols; i++) m[i] = mat[k, i]; return m; }