public IntMatrix SimulateToMatrix(int iEndTime) { IntMatrix im = new IntMatrix(pnd.GroupedPlaces.Count, iEndTime + 1); try { if (resultDataFromSimulation != null) { for (int idx = 0; idx < iEndTime; idx++) { int k = idx; if (k < resultDataFromSimulation.Count) { List <int> row = resultDataFromSimulation[k]; for (int i = 0; i < row.Count; i++) { im[i, idx] = (int)row[i]; } } } } } catch (Exception ex) { } return(im); }
public static IntMatrix operator *(IntMatrix lhs, IntMatrix rhs) { if (lhs.Dimensions.Width != rhs.Dimensions.Height) { throw new ArgumentException("Invalid parameters dimensions."); } else { IntMatrix m = new IntMatrix(lhs.Dimensions.Height, rhs.Dimensions.Width); for (int i = 0; i < m.Dimensions.Height; i++) { for (int j = 0; j < m.Dimensions.Width; j++) { int iValue = 0; for (int u = 0; u < lhs.Dimensions.Width; u++) { iValue += (int)lhs[i, u] * (int)rhs[u, j]; } m[i, j] = iValue; } } return(m); } }
public new IntMatrix Transpose() { IntMatrix im = new IntMatrix(this.Dimensions.Width, this.Dimensions.Height); for (int i = 0; i < this.Dimensions.Height; i++) { for (int j = 0; j < this.Dimensions.Width; j++) { im[j, i] = this[i, j]; } } return(im); }
public void Fire() { PetriNetDocument pnd = ((PetriNetEditor)this.Parent).Document; IntMatrix imF = pnd.F; IntMatrix imS = pnd.S; // Get all tokens in parents foreach (Place p in this.PlaceParents) { p.Tokens -= imF[pnd.Transitions.IndexOf(this), pnd.GroupedPlaces.IndexOf(p)]; } // And put them in childs foreach (Place p in this.PlaceChilds) { p.Tokens += imS[pnd.GroupedPlaces.IndexOf(p), pnd.Transitions.IndexOf(this)]; } }
public static IntMatrix operator +(IntMatrix lhs, IntMatrix rhs) { if (lhs.Dimensions != rhs.Dimensions) { throw new ArgumentException("Invalid parameters dimensions."); } else { IntMatrix m = new IntMatrix(lhs.Dimensions.Height, lhs.Dimensions.Width); for (int i = 0; i < m.Dimensions.Height; i++) { for (int j = 0; j < m.Dimensions.Width; j++) { m[i, j] = (int)lhs[i, j] + (int)rhs[i, j]; } } return(m); } }
public bool CanFire() { PetriNetDocument pnd = ((PetriNetEditor)this.Parent).Document; IntMatrix imF = pnd.F; int iCountMetConditions = 0; foreach (Place p in this.PlaceParents) { if (p.Tokens >= imF[pnd.Transitions.IndexOf(this), pnd.GroupedPlaces.IndexOf(p)]) { iCountMetConditions++; } } if (iCountMetConditions == this.PlaceParents.Count && this.PlaceParents.Count != 0) { return(true); } return(false); }
public Oscillogram(PetriNetDocument pnd, IntMatrix im) { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); this.UpdateStyles(); this.im = im; this.pnd = pnd; this.iGraphHeight = this.pnd.ResponseOptions.GraphHeight; this.iGraphDistance = this.pnd.ResponseOptions.GraphDistance; this.iSectionWidth = this.pnd.ResponseOptions.SectionWidth; this.iNumbersPerSection = this.pnd.ResponseOptions.NumbersPerSection; this.szSelection = pnd.ResponseOptions.Selection; this.szRange = new Size(0, pnd.ResponseOptions.EndTime); this.Height = iTopMargin + iGraphHeight * this.pnd.ResponseOptions.Places.Length + iGraphDistance * (this.pnd.ResponseOptions.Places.Length - 1) + iBottomMargin; this.Width = iLeftMargin + iSectionWidth * (this.szRange.Height - this.szRange.Width) / (this.iNumbersPerSection) + iRightMargin; this.alGroupedPlaces = pnd.GroupedPlaces; this.aResponsePlaces = pnd.ResponseOptions.Places; }
private void Simulate() { #region if (this.pnd.PetriNetType == PetriNetType.TimeInvariant) if (this.pnd.PetriNetType == PetriNetType.TimeInvariant) { // TODO : Deselect all selected controls in pnd SaveDataForCharting(0); while (true) { this.Step(); SaveDataForCharting(this.pnd.StepCounter); // Find is there any transition that can fire if (this.pnd.FireableTransitions.Count != 0 || this.ignoreLackOfFireableTransition) { Thread.Sleep(this.sleepBetweenStep); // 1000 by default.. Application.DoEvents(); } else { break; } } this.bRunning = false; // Raise SimulationFinished event if (this.SimulationFinished != null) { this.SimulationFinished(this, new EventArgs()); } } #endregion #region else if (this.pnd.PetriNetType == PetriNetType.PTimed) else if (this.pnd.PetriNetType == PetriNetType.PTimed) { // TODO : Deselect all selected controls in pnd // Initialize IntMatrix imk = new IntMatrix(this.pnd.GroupedPlaces.Count, 1); IntMatrix imF = this.pnd.F; IntMatrix Ft = this.pnd.F.Transpose(); IntMatrix Wt = this.pnd.W.Transpose(); ArrayList alInputPlaces = this.pnd.InputPlaces; ArrayList alOperationPlaces = this.pnd.OperationPlaces; ArrayList alResourcePlaces = this.pnd.ResourcePlaces; ArrayList alControlPlaces = this.pnd.ControlPlaces; ArrayList alOutputPlaces = this.pnd.OutputPlaces; ArrayList alGroupedPlaces = this.pnd.GroupedPlaces; ArrayList alTransitions = this.pnd.Transitions; IntMatrix Sv = this.pnd.Sv; IntMatrix Sr = this.pnd.Sr; IntMatrix Sd = this.pnd.Sd; IntMatrix Sy = this.pnd.Sy; IntMatrix Tv0 = this.pnd.Tv0; IntMatrix Tr0 = this.pnd.Tr0; IntMatrix Td0 = this.pnd.Td0; IntMatrix Ty0 = this.pnd.Ty0; IntMatrix[] Tv = new IntMatrix[1]; Tv[0] = new IntMatrix(Tv0.Dimensions.Height, Tv0.Dimensions.Width); IntMatrix[] Tr = new IntMatrix[1]; Tr[0] = new IntMatrix(Tr0.Dimensions.Height, Tr0.Dimensions.Width); IntMatrix[] Td = new IntMatrix[1]; Td[0] = new IntMatrix(Td0.Dimensions.Height, Td0.Dimensions.Width); IntMatrix[] Ty = new IntMatrix[1]; Ty[0] = new IntMatrix(Ty0.Dimensions.Height, Ty0.Dimensions.Width); // Initial condition for (int i = 0; i < alGroupedPlaces.Count; i++) { if (ht.Count != 0) { imk[i, 0] = (int)ht[alGroupedPlaces[i]]; } else { imk[i, 0] = ((Place)alGroupedPlaces[i]).Tokens; } } // Get mks IntMatrix imks = imk; // Get index of first control place int iIndexOfFirstControlPlace = -1; if (alControlPlaces.Count != 0) { iIndexOfFirstControlPlace = alGroupedPlaces.IndexOf(alControlPlaces[0]); } #region Set inputs // Set inputs Hashtable htInputTokens = new Hashtable(); foreach (PlaceInput pi in pnd.InputPlaces) { if (pi.InputType == InputType.Periodic) { ArrayList alInputTimes = new ArrayList(); int iTime = 0; while (iTime < this.pnd.EndTime / this.pnd.Td) { iTime += pi.Interval; alInputTimes.Add(iTime); } htInputTokens.Add(pi, alInputTimes); } else if (pi.InputType == InputType.Stohastic) { ArrayList alInputTimes = new ArrayList(); Random r = new Random(Environment.TickCount); int iTime = 0; while (iTime < this.pnd.EndTime / this.pnd.Td) { int i = r.Next(1, pi.RandomInterval); iTime += i; alInputTimes.Add(iTime); } htInputTokens.Add(pi, alInputTimes); } } #endregion SaveDataForCharting(0); // Simulate for (int k = 1; k <= this.pnd.EndTime / this.pnd.Td; k++) { foreach (Transition t in alTransitions) { t.RefreshMT(); } Thread.Sleep(this.pnd.Td); Application.DoEvents(); #region Set control vector // Set control vector bool isPhytonExecutedOK = script.ScriptSingleStep(); Tv0 = this.pnd.Tv0; // Refresh duration information.. if (alControlPlaces.Count != 0) { if (isPhytonExecutedOK) { for (int i = 0; i < alControlPlaces.Count; i++) { imk[iIndexOfFirstControlPlace + i, 0] = ((Place)alControlPlaces[i]).Tokens; } } else { // Set all control places tokens to 1 if they don't have parents for (int i = 0; i < alControlPlaces.Count; i++) { if (((Place)alControlPlaces[i]).Parents.Count == 0) { imk[iIndexOfFirstControlPlace + i, 0] = 1; } } } this.pnd.StepCounter++; foreach (Rule r in this.pnd.Rules) { int[] iaTokensVector = new int[alGroupedPlaces.Count]; for (int i = 0; i < iaTokensVector.Length; i++) { iaTokensVector[i] = (int)imk[i, 0]; } char[] ca = r.ControlVector(alGroupedPlaces, alControlPlaces, iaTokensVector); if (ca != null) { for (int i = 0; i < ca.Length; i++) { if (ca[i] != 'x') { if (((Place)this.pnd.ControlPlaces[i]).Parents.Count == 0) { imk[iIndexOfFirstControlPlace + i, 0] = int.Parse(ca[i].ToString()); } } } } } } #endregion #region Find transitions that can fire // Find transitions that can fire IntMatrix imT = new IntMatrix(alTransitions.Count, 1); for (int i = 0; i < alTransitions.Count; i++) { Transition t = (Transition)alTransitions[i]; ArrayList alPlaceParents = t.PlaceParents; int iCountMetConditions = 0; for (int j = 0; j < alPlaceParents.Count; j++) { Place p = (Place)alPlaceParents[j]; if ((int)imk[alGroupedPlaces.IndexOf(p), 0] >= imF[i, alGroupedPlaces.IndexOf(p)]) { iCountMetConditions++; } } if (iCountMetConditions == alPlaceParents.Count && alPlaceParents.Count != 0) { imT[i, 0] = 1; } } #endregion // Calculate mks+1 imks = imks + Wt * imT; #region Determine maximum tokens in one place // Determine maximum tokens in one place int iMaxTokens = 0; IntMatrix imOpsAndRes = new IntMatrix(this.pnd.OperationPlaces.Count + this.pnd.ResourcePlaces.Count, 1); for (int z = 0; z < this.pnd.OperationPlaces.Count; z++) { imOpsAndRes[z, 0] = imks[this.pnd.InputPlaces.Count + z, 0]; } for (int z = 0; z < this.pnd.ResourcePlaces.Count; z++) { imOpsAndRes[this.pnd.OperationPlaces.Count + z, 0] = imks[this.pnd.InputPlaces.Count + this.pnd.OperationPlaces.Count + z, 0]; } iMaxTokens = imOpsAndRes.Max(); #endregion #region Adjust sizes of Tx matrixes // Adjust sizes of Tx matrixes if (iMaxTokens > Tv.Length) { IntMatrix[] Tvx = new IntMatrix[iMaxTokens]; for (int i = 0; i < iMaxTokens; i++) { Tvx[i] = new IntMatrix(Tv0.Dimensions.Height, Tv0.Dimensions.Width); } Tv.CopyTo(Tvx, 0); Tv = Tvx; IntMatrix[] Trx = new IntMatrix[iMaxTokens]; for (int i = 0; i < iMaxTokens; i++) { Trx[i] = new IntMatrix(Tr0.Dimensions.Height, Tr0.Dimensions.Width); } Tr.CopyTo(Trx, 0); Tr = Trx; IntMatrix[] Tdx = new IntMatrix[iMaxTokens]; for (int i = 0; i < iMaxTokens; i++) { Tdx[i] = new IntMatrix(Td0.Dimensions.Height, Td0.Dimensions.Width); } Td.CopyTo(Tdx, 0); Td = Tdx; IntMatrix[] Tyx = new IntMatrix[iMaxTokens]; for (int i = 0; i < iMaxTokens; i++) { Tyx[i] = new IntMatrix(Ty0.Dimensions.Height, Ty0.Dimensions.Width); } Ty.CopyTo(Tyx, 0); Ty = Tyx; } #endregion if (this.pnd.Editor.PauseBeforeFiring == true && k != 1) { if (imT.Max() == 1) { if (this.SimulationProcessFinished != null) { this.SimulationProcessFinished(this, EventArgs.Empty); } } } // Calculate Tx_temp matrixes IntMatrix Tv_temp = IntMatrix.MulTim(Tv0, imT); IntMatrix Tr_temp = IntMatrix.MulTim(Tr0, imT); IntMatrix Td_temp = IntMatrix.MulTim(Td0, imT); IntMatrix Ty_temp = IntMatrix.MulTim(Ty0, imT); // Adjust all Connections foreach (Connection cn in this.pnd.ConnectionsAll) { cn.TokenPositions = new ArrayList(Tv.Length); for (int h = 0; h < Tv.Length; h++) { cn.TokenPositions.Add(0); } } try { #region Adjust Tv matrix // Put new values in table for (int m = 0; m < Tv0.Dimensions.Height; m++) { for (int l = 0; l < Tv0.Dimensions.Width; l++) { if (Tv_temp[m, l] > 0) { // Find first available position in Tv matrix int iAvailable = -1; for (int z = 0; z < Tv.Length; z++) { if (Tv[z][m, l] == 0) { iAvailable = z; break; } } if (iAvailable == -1) { throw new ArgumentOutOfRangeException("No free layer. Max tokens number in one place has been exceeded."); } Tv[iAvailable][m, l] = Tv_temp[m, l]; Tv_temp[m, l] = 0; ((PlaceOperation)alOperationPlaces[m]).MaxFillAngle = Tv[iAvailable][m, l]; ((PlaceOperation)alOperationPlaces[m]).FillAngle = Tv[iAvailable][m, l]; } } } // Decrement all values by 1 for (int ctn = 0; ctn < Tv.Length; ctn++) { for (int m = 0; m < Tv0.Dimensions.Height; m++) { for (int l = 0; l < Tv0.Dimensions.Width; l++) { if (Tv[ctn][m, l] > 0) { if (Tv[ctn][m, l] == 1) { imk[m + alInputPlaces.Count, 0] = imk[m + alInputPlaces.Count, 0] + Sv[m, l]; } Tv[ctn][m, l] = Tv[ctn][m, l] - 1; if (Tv[ctn][m, l] < ((PlaceOperation)alOperationPlaces[m]).FillAngle || ((PlaceOperation)alOperationPlaces[m]).FillAngle == 0) { ((PlaceOperation)alOperationPlaces[m]).FillAngle = Tv[ctn][m, l]; } if (this.pnd.TokenGameAnimation == true) { // Get connection Connection cn = Connection.GetConnectionBetweenControls((ConnectableControl)alTransitions[l], (ConnectableControl)alOperationPlaces[m]); int i = (int)((((PlaceOperation)alOperationPlaces[m]).MaxFillAngle - Tv[ctn][m, l]) * 100f / (float)((PlaceOperation)alOperationPlaces[m]).MaxFillAngle); if (i < 100) { cn.TokenPositions[ctn] = i; } else { cn.TokenPositions[ctn] = 0; } } } } } } #endregion #region Adjust Tr matrix // Put new values in table for (int m = 0; m < Tr0.Dimensions.Height; m++) { for (int l = 0; l < Tr0.Dimensions.Width; l++) { if (Tr_temp[m, l] > 0) { // Find first available position in Tr matrix int iAvailable = -1; for (int z = 0; z < Tr.Length; z++) { if (Tr[z][m, l] == 0) { iAvailable = z; break; } } if (iAvailable == -1) { throw new ArgumentOutOfRangeException("No free layer. Max tokens number in one place has been exceeded."); } Tr[iAvailable][m, l] = Tr_temp[m, l]; Tr_temp[m, l] = 0; ((PlaceResource)alResourcePlaces[m]).MaxFillAngle = Tr[iAvailable][m, l]; ((PlaceResource)alResourcePlaces[m]).FillAngle = Tr[iAvailable][m, l]; } } } // Decrement all values by 1 for (int ctn = 0; ctn < Tr.Length; ctn++) { for (int m = 0; m < Tr0.Dimensions.Height; m++) { for (int l = 0; l < Tr0.Dimensions.Width; l++) { if (Tr[ctn][m, l] > 0) { if (Tr[ctn][m, l] == 1) { imk[m + alInputPlaces.Count + alOperationPlaces.Count, 0] = imk[m + alInputPlaces.Count + alOperationPlaces.Count, 0] + Sr[m, l]; } Tr[ctn][m, l] = Tr[ctn][m, l] - 1; if (Tr[ctn][m, l] < ((PlaceResource)alResourcePlaces[m]).FillAngle || ((PlaceResource)alResourcePlaces[m]).FillAngle == 0) { ((PlaceResource)alResourcePlaces[m]).FillAngle = Tr[ctn][m, l]; } if (this.pnd.TokenGameAnimation == true) { // Get connection Connection cn = Connection.GetConnectionBetweenControls((ConnectableControl)alTransitions[l], (ConnectableControl)alResourcePlaces[m]); int i = (int)((((PlaceResource)alResourcePlaces[m]).MaxFillAngle - Tr[ctn][m, l]) * 100f / (float)((PlaceResource)alResourcePlaces[m]).MaxFillAngle); if (i < 100) { cn.TokenPositions[ctn] = i; } else { cn.TokenPositions[ctn] = 0; } } } } } } #endregion #region Adjust Td matrix for (int ctn = 0; ctn < iMaxTokens; ctn++) { for (int m = 0; m < Td0.Dimensions.Height; m++) { for (int l = 0; l < Td0.Dimensions.Width; l++) { if (Td_temp[m, l] > 0) { if (Td[ctn][m, l] > 0) { Td[ctn + 1][m, l] = Td_temp[m, l]; Td_temp[m, l] = 0; } else { Td[ctn][m, l] = Td_temp[m, l]; Td_temp[m, l] = 0; } } if (Td[ctn][m, l] == 1) { imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count, 0] = imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count, 0] + Sd[m, l]; Td[ctn][m, l] = Td[ctn][m, l] - 1; } } } } #endregion #region Adjust Ty matrix for (int ctn = 0; ctn < iMaxTokens; ctn++) { for (int m = 0; m < Ty0.Dimensions.Height; m++) { for (int l = 0; l < Ty0.Dimensions.Width; l++) { if (Ty_temp[m, l] > 0) { if (Ty[ctn][m, l] > 0) { Ty[ctn + 1][m, l] = Ty_temp[m, l]; Ty_temp[m, l] = 0; } else { Ty[ctn][m, l] = Ty_temp[m, l]; Ty_temp[m, l] = 0; } } if (Ty[ctn][m, l] > 0) { imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count + alControlPlaces.Count, 0] = imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count + alControlPlaces.Count, 0] + Sy[m, l]; Ty[ctn][m, l] = Ty[ctn][m, l] - 1; } } } } #endregion } catch (ArgumentOutOfRangeException e) { MessageBox.Show(e.ToString()); break; } // Calculate mk+1 imk = imk - Ft * imT; #region Set Inputs // Set Inputs if (pnd.InputPlaces.Count != 0) { for (int i = 0; i < pnd.InputPlaces.Count; i++) { PlaceInput pi = (PlaceInput)pnd.InputPlaces[i]; if (pi.InputType != InputType.Fixed) { if (((ArrayList)htInputTokens[pi]).Contains(k)) { imk[i, 0] += 1; imks[i, 0] += 1; } } } } #endregion this.pnd.Editor.RefreshMT(); // Raise SimulationStepFinished event if (this.SimulationStepFinished != null) { this.SimulationStepFinished(this, new EventArgs()); } for (int j = 0; j < this.pnd.Places.Count; j++) { ((Place)this.pnd.GroupedPlaces[j]).Tokens = imk[j, 0]; } SaveDataForCharting(this.pnd.StepCounter + 1); } this.bRunning = false; // Raise SimulationFinished event if (this.SimulationFinished != null) { this.SimulationFinished(this, new EventArgs()); } } #endregion }