public static void setMatrixValuesToListOfLabels(DMU.Math.Matrix matrix, List <Label> labels) { for (int i = 0; i < labels.Count; i++) { labels[i].Content = matrix.GetElement(i, 0); } }
public static bool isMatrixSymmetric(DMU.Math.Matrix matrix) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (!matrix.GetElement(i, j).Equals(matrix.GetElement(j, i))) { return(false); } } } return(true); }
public static double calculateEnergyAsynchronous(DMU.Math.Matrix weightsMatrix, DMU.Math.Matrix matrix) { double result = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { result += weightsMatrix.GetElement(i, j) * matrix.GetElement(i, 0) * matrix.GetElement(j, 0); } } result = -(result / 2); return(Math.Round(result, 3)); }
public static double calculateEnergySynchronous(DMU.Math.Matrix weightsMatrix, DMU.Math.Matrix matrix, DMU.Math.Matrix previousStepMatrix, DMU.Math.Matrix matrixI = null) { double result = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { result += weightsMatrix.GetElement(i, j) * matrix.GetElement(i, 0) * previousStepMatrix.GetElement(j, 0); } } result = -result; if (matrixI != null) { for (int i = 0; i < 3; i++) { result += matrixI.GetElement(i, 0) * (matrix.GetElement(i, 0) + previousStepMatrix.GetElement(i, 0)); } } return(Math.Round(result, 3)); }
public static string convertResultsToString(List <TestDataAndFields> resultsToConvert, DMU.Math.Matrix weightsMatrix, string title) { string matrixNumberFormatting = "F1"; string matrixColumnDelimiteFormatting = "\t"; string matrixRowDelimiterFormatting = "\n"; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(title); stringBuilder.AppendLine("Macierz wag (W):"); stringBuilder.Append(weightsMatrix.ToString(matrixNumberFormatting, matrixColumnDelimiteFormatting, matrixRowDelimiterFormatting)); foreach (TestDataAndFields testDataAndFields in resultsToConvert) { stringBuilder.AppendLine("----------------------------------------------------"); stringBuilder.AppendLine("--- Badanie nr. " + (testDataAndFields.index + 1) + " ---"); stringBuilder.AppendLine("----------------------------------------------------"); stringBuilder.AppendLine("Badany wektor:"); stringBuilder.AppendLine(DMU.Math.Matrix.Transpose(testDataAndFields.startingValue).ToString(matrixNumberFormatting, matrixColumnDelimiteFormatting, matrixRowDelimiterFormatting)); int i = 1; foreach (SingleIterationResult singleIterationResult in testDataAndFields.iterationHistory) { stringBuilder.AppendLine("Krok: " + i + "-------------------"); stringBuilder.AppendLine("Potencjał wejściowy (U):"); stringBuilder.AppendLine(DMU.Math.Matrix.Transpose(singleIterationResult.matrixResult).ToString(matrixNumberFormatting, matrixColumnDelimiteFormatting, "")); stringBuilder.AppendLine("Potencjał wyjściowy (V):"); stringBuilder.AppendLine(DMU.Math.Matrix.Transpose(singleIterationResult.matrixResultBiPolar).ToString(matrixNumberFormatting, matrixColumnDelimiteFormatting, matrixRowDelimiterFormatting)); stringBuilder.AppendLine("Energia(" + i + ")= " + singleIterationResult.energy); stringBuilder.AppendLine(); i++; } stringBuilder.Append("Wniosek: Punkt [" + DMU.Math.Matrix.Transpose(testDataAndFields.startingValue).ToString(matrixNumberFormatting, " ", "") + "] " + resultTypeToString(testDataAndFields.resultType)); switch (testDataAndFields.resultType) { case ResultType.GoesToPoint: { stringBuilder.Append(": [" + DMU.Math.Matrix.Transpose(testDataAndFields.currentValue).ToString(matrixNumberFormatting, " ", "") + "]"); } break; case ResultType.CreatesCycle: case ResultType.GoesToCycle: { stringBuilder.Append(": "); int index = 0; foreach (SingleIterationResult patternElement in testDataAndFields.foundPattern) { stringBuilder.Append("[" + DMU.Math.Matrix.Transpose(patternElement.matrixResultBiPolar).ToString(matrixNumberFormatting, " ", "") + "]"); if (index < (testDataAndFields.foundPattern.Count - 1)) { if (testDataAndFields.synchronousMode) { stringBuilder.Append(" <-> "); } else { stringBuilder.Append(" -> "); } } index++; } } break; } stringBuilder.AppendLine(); } return(stringBuilder.ToString()); }
public static void runAsynchronousAnalysisForTestDataListWithWeights(ref List <TestDataAndFields> listOfTestDataAndFields, ref DMU.Math.Matrix weightsMatrix, int[] asynchronousOrder, List <PointAnalysisResult> pointAnalysisResults = null) { for (int j = 0; j < listOfTestDataAndFields.Count; j++) { TestDataAndFields currentTestDataAndFields = listOfTestDataAndFields[j]; cleanResultDataStructures(ref currentTestDataAndFields); for (int i = 0; i < 8; i++) { bool finished = false; for (int k = 0; k < 3; k++) { DMU.Math.Matrix previousStepMatrix = currentTestDataAndFields.currentValue.Clone(); SingleIterationResult iterationResult = new SingleIterationResult(); currentTestDataAndFields.currentValue = DMU.Math.Matrix.Multiply(weightsMatrix, currentTestDataAndFields.currentValue); for (int l = 1; l <= 3; l++) { if (l != asynchronousOrder[k]) { currentTestDataAndFields.currentValue.SetElement(l - 1, 0, previousStepMatrix.GetElement(l - 1, 0)); } } iterationResult.matrixResult = currentTestDataAndFields.currentValue; currentTestDataAndFields.currentValue = currentTestDataAndFields.currentValue.ToBiPolar(); iterationResult.matrixResultBiPolar = currentTestDataAndFields.currentValue; iterationResult.energy = calculateEnergyAsynchronous(weightsMatrix, currentTestDataAndFields.currentValue); currentTestDataAndFields.iterationHistory.Add(iterationResult); if (i != 0 || (i == 0 && k == 2)) { DMU.Math.Matrix thirdToLastStepResult = i != 0 ? currentTestDataAndFields.iterationHistory[i * 3 + k - 3].matrixResultBiPolar : currentTestDataAndFields.startingValue; DMU.Math.Matrix secondToLastStepResult = currentTestDataAndFields.iterationHistory[i * 3 + k - 2].matrixResultBiPolar; DMU.Math.Matrix previousStepResult = currentTestDataAndFields.iterationHistory[i * 3 + k - 1].matrixResultBiPolar; if (currentTestDataAndFields.currentValue.Equals(previousStepResult) && currentTestDataAndFields.currentValue.Equals(secondToLastStepResult) && currentTestDataAndFields.currentValue.Equals(thirdToLastStepResult) ) { if (currentTestDataAndFields.currentValue.Equals(currentTestDataAndFields.startingValue)) { currentTestDataAndFields.resultType = ResultType.Static; } else { currentTestDataAndFields.resultType = ResultType.GoesToPoint; } LayoutGenerationHelpers.setMatrixValuesToListOfLabels(iterationResult.matrixResultBiPolar, currentTestDataAndFields.resultValueLabels); finished = true; break; } int upperTemplateBound = currentTestDataAndFields.iterationHistory.Count - 1; int lowerTemplateBound = upperTemplateBound - 1; int minimalTemplateBound = (int)Math.Ceiling((double)currentTestDataAndFields.iterationHistory.Count / 2.0); bool patternFound = false; List <SingleIterationResult> pattern = new List <SingleIterationResult>(); while (lowerTemplateBound >= minimalTemplateBound && !patternFound) { bool patternExists = true; int possiblePatternLength = upperTemplateBound - lowerTemplateBound + 1; for (int currentElementToCheckIndex = upperTemplateBound; currentElementToCheckIndex >= lowerTemplateBound; currentElementToCheckIndex--) { SingleIterationResult patternResult = currentTestDataAndFields.iterationHistory[currentElementToCheckIndex]; SingleIterationResult checkedResult = currentTestDataAndFields.iterationHistory[(currentElementToCheckIndex - possiblePatternLength)]; if (!patternResult.matrixResultBiPolar.Equals(checkedResult.matrixResultBiPolar) || patternResult.energy != checkedResult.energy ) { patternExists = false; break; } } patternFound = patternExists; lowerTemplateBound--; } if (patternFound) { for (int patternIndex = upperTemplateBound; patternIndex > lowerTemplateBound; patternIndex--) { pattern.Add(currentTestDataAndFields.iterationHistory[patternIndex]); } int lastUncheckedIndex = lowerTemplateBound - pattern.Count; bool startingValueIsPartOfThePattern = true; if (lastUncheckedIndex >= pattern.Count) { startingValueIsPartOfThePattern = false; } else { int patternIndex = pattern.Count - 1; for (int uncheckedValueIndex = lastUncheckedIndex; uncheckedValueIndex >= 0; uncheckedValueIndex--) { SingleIterationResult patternResult = pattern[patternIndex]; SingleIterationResult checkedResult = currentTestDataAndFields.iterationHistory[uncheckedValueIndex]; if (!patternResult.matrixResultBiPolar.Equals(checkedResult.matrixResultBiPolar) || patternResult.energy != checkedResult.energy ) { startingValueIsPartOfThePattern = false; break; } patternIndex--; } if (startingValueIsPartOfThePattern) { if (!pattern[patternIndex].matrixResultBiPolar.Equals(currentTestDataAndFields.startingValue)) { startingValueIsPartOfThePattern = false; } } } if (startingValueIsPartOfThePattern) { currentTestDataAndFields.resultType = ResultType.CreatesCycle; } else { currentTestDataAndFields.resultType = ResultType.GoesToCycle; } LayoutGenerationHelpers.setMatrixValuesToListOfLabels(iterationResult.matrixResultBiPolar, currentTestDataAndFields.resultValueLabels); finished = true; pattern.Reverse(); currentTestDataAndFields.foundPattern = pattern; break; } } if (i == 7 && k == 2) { LayoutGenerationHelpers.setMatrixValuesToListOfLabels(iterationResult.matrixResultBiPolar, currentTestDataAndFields.resultValueLabels); } } if (pointAnalysisResults != null) { fillPointAnalysisData(ref pointAnalysisResults, currentTestDataAndFields.resultType, j); } if (finished) { currentTestDataAndFields.resultLabel.Content = DataConverters.resultTypeToString(currentTestDataAndFields.resultType); break; } } } }
public static void runSynchronousAnalysisForTestDataListWithWeights(ref List <TestDataAndFields> listOfTestDataAndFields, ref DMU.Math.Matrix weightsMatrix, bool isWeightsMatrixSymetric = true, List <PointAnalysisResult> pointAnalysisResults = null) { for (int j = 0; j < listOfTestDataAndFields.Count; j++) { TestDataAndFields currentTestDataAndFields = listOfTestDataAndFields[j]; cleanResultDataStructures(ref currentTestDataAndFields); for (int i = 0; i < 8; i++) { DMU.Math.Matrix previousStepMatrix = currentTestDataAndFields.currentValue.Clone(); SingleIterationResult iterationResult = new SingleIterationResult(); currentTestDataAndFields.currentValue = DMU.Math.Matrix.Multiply(weightsMatrix, currentTestDataAndFields.currentValue); iterationResult.matrixResult = DMU.Math.Matrix.Add(currentTestDataAndFields.currentValue, matrixI); currentTestDataAndFields.currentValue = currentTestDataAndFields.currentValue.ToBiPolar(); iterationResult.matrixResultBiPolar = currentTestDataAndFields.currentValue; iterationResult.energy = calculateEnergySynchronous(weightsMatrix, currentTestDataAndFields.currentValue, previousStepMatrix, matrixI); currentTestDataAndFields.iterationHistory.Add(iterationResult); if (previousStepMatrix.Equals(iterationResult.matrixResultBiPolar)) { if (i == 0) { currentTestDataAndFields.resultType = ResultType.Static; } else { currentTestDataAndFields.resultType = ResultType.GoesToPoint; } LayoutGenerationHelpers.setMatrixValuesToListOfLabels(iterationResult.matrixResultBiPolar, currentTestDataAndFields.resultValueLabels); break; } if (isWeightsMatrixSymetric && i != 0) { double previousStepEnergy = currentTestDataAndFields.iterationHistory[i - 1].energy; if (previousStepEnergy == iterationResult.energy) { if (currentTestDataAndFields.startingValue.Equals(currentTestDataAndFields.currentValue)) { currentTestDataAndFields.resultType = ResultType.CreatesCycle; } else { currentTestDataAndFields.resultType = ResultType.GoesToCycle; } LayoutGenerationHelpers.setMatrixValuesToListOfLabels(iterationResult.matrixResultBiPolar, currentTestDataAndFields.resultValueLabels); if (currentTestDataAndFields.iterationHistory.Count != 1) { currentTestDataAndFields.foundPattern.Add(currentTestDataAndFields.iterationHistory[currentTestDataAndFields.iterationHistory.Count - 2]); } else { SingleIterationResult initialValue = new SingleIterationResult(); initialValue.matrixResultBiPolar = currentTestDataAndFields.startingValue; currentTestDataAndFields.foundPattern.Add(initialValue); } currentTestDataAndFields.foundPattern.Add(currentTestDataAndFields.iterationHistory[currentTestDataAndFields.iterationHistory.Count - 1]); break; } } if (i == 7) { LayoutGenerationHelpers.setMatrixValuesToListOfLabels(iterationResult.matrixResultBiPolar, currentTestDataAndFields.resultValueLabels); } currentTestDataAndFields.currentValue = iterationResult.matrixResultBiPolar; } if (pointAnalysisResults != null) { fillPointAnalysisData(ref pointAnalysisResults, currentTestDataAndFields.resultType, j); } currentTestDataAndFields.resultLabel.Content = DataConverters.resultTypeToString(currentTestDataAndFields.resultType); } }
public void displayInformation(TestDataAndFields testDataAndFields) { DMU.Math.Matrix currentValue = testDataAndFields.startingValue; string text = "Badanie " + (testDataAndFields.index + 1) + " Wektor: " + currentValue.ToString("F0"); this.Title = text; SourceInfo.Header = text; List <Rectangle> rectangles = new List <Rectangle>(); Label label = new Label(); { label.VerticalAlignment = VerticalAlignment.Center; label.HorizontalAlignment = HorizontalAlignment.Center; label.Content = DataConverters.resultTypeToString(testDataAndFields.resultType); } results.Children.Add(label); UniformGrid uniformGrid = new UniformGrid(); { uniformGrid.Rows = 1; for (int rectIndex = 0; rectIndex < testDataAndFields.iterationHistory.Count + 1; rectIndex++) { Rectangle rect = new Rectangle(); { rect.Width = 20; rect.Height = 20; rectangles.Add(rect); } uniformGrid.Children.Add(rect); } } results.Children.Add(uniformGrid); rectangles[0].Fill = brushes[biPolarToNumber(testDataAndFields.startingValue.ToArray())]; int i = 1; foreach (SingleIterationResult resultOfStep in testDataAndFields.iterationHistory) { GroupBox groupBox = new GroupBox(); { groupBox.Header = "Krok " + i; rectangles[i].Fill = brushes[biPolarToNumber(resultOfStep.matrixResultBiPolar.ToArray())]; Grid grid = new Grid(); { LayoutGenerationHelpers.addNMStarWideColumnsToGrid(4, 1, ref grid); LayoutGenerationHelpers.addNMStarHighRowsToGrid(3, 1, ref grid); LayoutGenerationHelpers.addNMStarHighRowsToGrid(1, 1, ref grid); label = new Label(); { Grid.SetColumn(label, 0); Grid.SetRow(label, 0); label.VerticalAlignment = VerticalAlignment.Center; label.HorizontalAlignment = HorizontalAlignment.Center; label.Content = "Potencjał wejściowy (U):"; } grid.Children.Add(label); Grid gridOutputs = new Grid(); { LayoutGenerationHelpers.addNMStarHighRowsToGrid(3, 1, ref gridOutputs); Grid.SetColumn(gridOutputs, 1); LayoutGenerationHelpers.addNLabelsToGridWithArrayAsContent(3, ref gridOutputs, resultOfStep.matrixResult.ToArray()); } grid.Children.Add(gridOutputs); label = new Label(); { Grid.SetColumn(label, 2); Grid.SetRow(label, 0); label.VerticalAlignment = VerticalAlignment.Center; label.HorizontalAlignment = HorizontalAlignment.Center; label.Content = "Potencjał wyjściowy (V):"; } grid.Children.Add(label); Grid gridOutputsBiPolar = new Grid(); { LayoutGenerationHelpers.addNMStarHighRowsToGrid(3, 1, ref gridOutputsBiPolar); Grid.SetColumn(gridOutputsBiPolar, 3); LayoutGenerationHelpers.addNLabelsToGridWithArrayAsContent(3, ref gridOutputsBiPolar, resultOfStep.matrixResultBiPolar.ToArray()); } grid.Children.Add(gridOutputsBiPolar); label = new Label(); { Grid.SetColumn(label, 0); Grid.SetRow(label, 1); Grid.SetColumnSpan(label, 4); label.VerticalAlignment = VerticalAlignment.Center; label.HorizontalAlignment = HorizontalAlignment.Center; label.Content = "Energia: " + resultOfStep.energy; } grid.Children.Add(label); } groupBox.Content = grid; } results.Children.Add(groupBox); i++; } }