public override IOperation Apply() { int updateInterval = UpdateIntervalParameter.Value.Value; IntValue updateCounter = UpdateCounterParameter.ActualValue; if (updateCounter == null) { updateCounter = new IntValue(updateInterval); UpdateCounterParameter.ActualValue = updateCounter; } else { updateCounter.Value++; } if (updateCounter.Value == updateInterval) { updateCounter.Value = 0; bool max = MaximizationParameter.ActualValue.Value; ItemArray <T> solutions = SolutionParameter.ActualValue; double[] qualities = QualityParameter.ActualValue.Select(x => x.Value).ToArray(); T bestKnownSolution = BestKnownSolutionParameter.ActualValue; bool storeHistory = StoreHistoryParameter.Value.Value; // calculate index of current best solution int bestIndex = -1; if (!max) { bestIndex = qualities .Select((x, i) => new { Index = i, Value = x }) .OrderBy(x => x.Value) .First().Index; } else { bestIndex = qualities .Select((x, i) => new { Index = i, Value = x }) .OrderByDescending(x => x.Value) .First().Index; } // calculate allels of current best and (if available) best known solution Allele[] bestAlleles = CalculateAlleles(solutions[bestIndex]); Allele[] bestKnownAlleles = null; if (bestKnownSolution != null) { bestKnownAlleles = CalculateAlleles(bestKnownSolution); } // calculate allele frequencies var frequencies = solutions.SelectMany((s, index) => CalculateAlleles(s).Select(a => new { Allele = a, Quality = qualities[index] })). GroupBy(x => x.Allele.Id). Select(x => new AlleleFrequency(x.Key, x.Count() / ((double)solutions.Length), x.Average(a => a.Allele.Impact), x.Average(a => a.Quality), bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key), bestAlleles.Any(a => a.Id == x.Key))); // calculate dummy allele frequencies of alleles of best known solution which did not occur if (bestKnownAlleles != null) { var bestKnownFrequencies = bestKnownAlleles.Select(x => new AlleleFrequency(x.Id, 0, x.Impact, 0, true, false)).Except(frequencies, new AlleleFrequencyIdEqualityComparer()); frequencies = frequencies.Concat(bestKnownFrequencies); } // fetch results collection ResultCollection results; if (!ResultsParameter.ActualValue.ContainsKey(Name + " Results")) { results = new ResultCollection(); ResultsParameter.ActualValue.Add(new Result(Name + " Results", results)); } else { results = (ResultCollection)ResultsParameter.ActualValue[Name + " Results"].Value; } // store allele frequencies AlleleFrequencyCollection frequenciesCollection = new AlleleFrequencyCollection(frequencies); if (!results.ContainsKey("Allele Frequencies")) { results.Add(new Result("Allele Frequencies", frequenciesCollection)); } else { results["Allele Frequencies"].Value = frequenciesCollection; } // store allele frequencies history if (storeHistory) { if (!results.ContainsKey("Allele Frequencies History")) { AlleleFrequencyCollectionHistory history = new AlleleFrequencyCollectionHistory(); history.Add(frequenciesCollection); results.Add(new Result("Allele Frequencies History", history)); } else { ((AlleleFrequencyCollectionHistory)results["Allele Frequencies History"].Value).Add(frequenciesCollection); } } // store alleles data table DataTable allelesTable; if (!results.ContainsKey("Alleles")) { allelesTable = new DataTable("Alleles"); allelesTable.VisualProperties.XAxisTitle = "Iteration"; allelesTable.VisualProperties.YAxisTitle = "Number of Alleles"; allelesTable.VisualProperties.SecondYAxisTitle = "Number of Alleles"; allelesTable.Rows.Add(new DataRow("Unique Alleles")); allelesTable.Rows["Unique Alleles"].VisualProperties.StartIndexZero = true; allelesTable.Rows.Add(new DataRow("Unique Alleles of Best Known Solution", null)); allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true; allelesTable.Rows["Unique Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true; allelesTable.Rows.Add(new DataRow("Fixed Alleles", null)); allelesTable.Rows["Fixed Alleles"].VisualProperties.SecondYAxis = true; allelesTable.Rows["Fixed Alleles"].VisualProperties.StartIndexZero = true; allelesTable.Rows.Add(new DataRow("Fixed Alleles of Best Known Solution", null)); allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true; allelesTable.Rows["Fixed Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true; allelesTable.Rows.Add(new DataRow("Lost Alleles of Best Known Solution", null)); allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true; allelesTable.Rows["Lost Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true; results.Add(new Result("Alleles", allelesTable)); } else { allelesTable = (DataTable)results["Alleles"].Value; } int fixedAllelesCount = frequenciesCollection.Where(x => x.Frequency == 1).Count(); var relevantAlleles = frequenciesCollection.Where(x => x.ContainedInBestKnownSolution); int relevantAllelesCount = relevantAlleles.Count(); int fixedRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 1).Count(); int lostRelevantAllelesCount = relevantAlleles.Where(x => x.Frequency == 0).Count(); int uniqueRelevantAllelesCount = relevantAllelesCount - lostRelevantAllelesCount; allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesCollection.Count); allelesTable.Rows["Unique Alleles of Best Known Solution"].Values.Add(uniqueRelevantAllelesCount); allelesTable.Rows["Fixed Alleles"].Values.Add(fixedAllelesCount); allelesTable.Rows["Fixed Alleles of Best Known Solution"].Values.Add(fixedRelevantAllelesCount); allelesTable.Rows["Lost Alleles of Best Known Solution"].Values.Add(lostRelevantAllelesCount); // store alleles values if (!results.ContainsKey("Unique Alleles")) { results.Add(new Result("Unique Alleles", new DoubleValue(frequenciesCollection.Count))); } else { ((DoubleValue)results["Unique Alleles"].Value).Value = frequenciesCollection.Count; } if (!results.ContainsKey("Unique Alleles of Best Known Solution")) { results.Add(new Result("Unique Alleles of Best Known Solution", new DoubleValue(uniqueRelevantAllelesCount))); } else { ((DoubleValue)results["Unique Alleles of Best Known Solution"].Value).Value = uniqueRelevantAllelesCount; } if (!results.ContainsKey("Fixed Alleles")) { results.Add(new Result("Fixed Alleles", new DoubleValue(fixedAllelesCount))); } else { ((DoubleValue)results["Fixed Alleles"].Value).Value = fixedAllelesCount; } if (!results.ContainsKey("Fixed Alleles of Best Known Solution")) { results.Add(new Result("Fixed Alleles of Best Known Solution", new DoubleValue(fixedRelevantAllelesCount))); } else { ((DoubleValue)results["Fixed Alleles of Best Known Solution"].Value).Value = fixedRelevantAllelesCount; } if (!results.ContainsKey("Lost Alleles of Best Known Solution")) { results.Add(new Result("Lost Alleles of Best Known Solution", new DoubleValue(lostRelevantAllelesCount))); } else { ((DoubleValue)results["Lost Alleles of Best Known Solution"].Value).Value = lostRelevantAllelesCount; } // calculate contained alleles of best known solution and relative quality if (bestKnownAlleles != null) { double qualityRange = Math.Abs(qualities.Max() - qualities.Min()); var points = solutions.Select((s, index) => new Point2D <double>(CalculateAlleles(s).Intersect(bestKnownAlleles, new AlleleIdEqualityComparer()).Count(), Math.Abs(qualities[index] - qualities[bestIndex]) / qualityRange)); var avgContainedReleventAlleles = points.Select(x => x.X).Average(); var plot = new ScatterPlot("Contained Alleles of Best Known Solution and Relative Solution Qualtiy", null); plot.VisualProperties.XAxisTitle = "Contained Alleles of Best Known Solution"; plot.VisualProperties.YAxisTitle = "Relative Solution Quality"; plot.VisualProperties.XAxisMinimumAuto = false; plot.VisualProperties.XAxisMinimumFixedValue = 0.0; plot.VisualProperties.XAxisMaximumAuto = false; plot.VisualProperties.XAxisMaximumFixedValue = bestKnownAlleles.Length; plot.VisualProperties.YAxisMinimumAuto = false; plot.VisualProperties.YAxisMinimumFixedValue = 0.0; plot.VisualProperties.YAxisMaximumAuto = false; plot.VisualProperties.YAxisMaximumFixedValue = 1.0; var row = new ScatterPlotDataRow("Solutions of Current Generation", null, points); row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle; row.VisualProperties.PointSize = 5; plot.Rows.Add(row); if (!results.ContainsKey("Scatter Plot")) { results.Add(new Result("Scatter Plot", plot)); } else { results["Scatter Plot"].Value = plot; } if (storeHistory) { if (!results.ContainsKey("Scatter Plot History")) { results.Add(new Result("Scatter Plot History", new ScatterPlotHistory())); } ((ScatterPlotHistory)results["Scatter Plot History"].Value).Add(plot); } if (!allelesTable.Rows.ContainsKey("Average Contained Alleles of Best Known Solution")) { allelesTable.Rows.Add(new DataRow("Average Contained Alleles of Best Known Solution", null)); allelesTable.Rows["Average Contained Alleles of Best Known Solution"].VisualProperties.SecondYAxis = true; allelesTable.Rows["Average Contained Alleles of Best Known Solution"].VisualProperties.StartIndexZero = true; } allelesTable.Rows["Average Contained Alleles of Best Known Solution"].Values.Add(avgContainedReleventAlleles); if (!results.ContainsKey("Average Contained Alleles of Best Known Solution")) { results.Add(new Result("Average Contained Alleles of Best Known Solution", new DoubleValue(avgContainedReleventAlleles))); } else { ((DoubleValue)results["Average Contained Alleles of Best Known Solution"].Value).Value = avgContainedReleventAlleles; } } } return(base.Apply()); }