[7128] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Analysis.FitnessLandscape.DataTables;
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Operators;
|
---|
| 8 | using HeuristicLab.Optimization;
|
---|
| 9 | using HeuristicLab.Parameters;
|
---|
| 10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 11 |
|
---|
| 12 | namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
|
---|
| 13 |
|
---|
| 14 | [Item("RepeatAggregator", "Aggregates analysis results from repeated runs on the same starting point.")]
|
---|
| 15 | [StorableClass]
|
---|
| 16 | public class RepeatAggregator : SingleSuccessorOperator {
|
---|
| 17 |
|
---|
| 18 | #region Parameter Properties
|
---|
| 19 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
| 20 | get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 21 | }
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | #region Construction & Cloning
|
---|
| 25 | [StorableConstructor]
|
---|
| 26 | protected RepeatAggregator(bool deserializing) : base(deserializing) { }
|
---|
| 27 | protected RepeatAggregator(RepeatAggregator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 28 | public RepeatAggregator() {
|
---|
| 29 | Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
|
---|
| 30 | }
|
---|
| 31 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 32 | return new RepeatAggregator(this, cloner);
|
---|
| 33 | }
|
---|
| 34 | #endregion
|
---|
| 35 |
|
---|
| 36 | public override IOperation Apply() {
|
---|
| 37 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
| 38 | var tables = ExtractTablesFromResults(results).ToList();
|
---|
| 39 | DataTable allValues = new DataTable("All AutoCorrelations");
|
---|
| 40 | DataRow avgRow = new DataRow("Average Auto Correlation");
|
---|
| 41 | DataRow stdRow = new DataRow("Std.Dev. of Auto Correlations");
|
---|
| 42 | DataRow countRow = new DataRow("n");
|
---|
| 43 | countRow.VisualProperties.SecondYAxis = true;
|
---|
| 44 | for (int i = 0; i<tables.Count; i++) {
|
---|
| 45 | DataRow row = tables[i].Rows.First();
|
---|
| 46 | allValues.Rows.Add(new DataRow(i.ToString(), "", row.Values));
|
---|
| 47 | while (avgRow.Values.Count < row.Values.Count) {
|
---|
| 48 | stdRow.Values.Add(0);
|
---|
| 49 | avgRow.Values.Add(0);
|
---|
| 50 | countRow.Values.Add(0);
|
---|
| 51 | }
|
---|
| 52 | for (int j = 0; j<row.Values.Count; j++) {
|
---|
| 53 | avgRow.Values[j] += row.Values[j];
|
---|
| 54 | countRow.Values[j]++;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | for (int i = 0; i<avgRow.Values.Count; i++) {
|
---|
| 58 | avgRow.Values[i] = avgRow.Values[i] / tables.Count;
|
---|
| 59 | }
|
---|
| 60 | foreach (var table in tables) {
|
---|
| 61 | DataRow row = table.Rows.First();
|
---|
| 62 | for (int j = 0; j<row.Values.Count; j++) {
|
---|
| 63 | stdRow.Values[j] += square(avgRow.Values[j] - row.Values[j]);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | for (int i = 0; i<avgRow.Values.Count; i++) {
|
---|
| 67 | stdRow.Values[i] = Math.Sqrt(stdRow.Values[i] / tables.Count);
|
---|
| 68 | }
|
---|
| 69 | results.RemoveAll(r => r.Value is IScope || r.Value is AutoCorrelationTable);
|
---|
| 70 | AutoCorrelationTable avgTable = new AutoCorrelationTable("Average repeated auto correlation");
|
---|
| 71 | AutoCorrelationTable stdTable = new AutoCorrelationTable("Std.Dev. of repeated auto correlation");
|
---|
| 72 | avgTable.Rows.Add(avgRow);
|
---|
| 73 | avgTable.Rows.Add(countRow);
|
---|
| 74 | stdTable.Rows.Add(stdRow);
|
---|
| 75 | results.Add(new Result("Average Auto Correlation", avgTable));
|
---|
| 76 | results.Add(new Result("All Auto Correlations", allValues));
|
---|
| 77 | results.Add(new Result("Std.Dev. Auto Correlations", stdTable));
|
---|
| 78 | return base.Apply();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public static IEnumerable<AutoCorrelationTable> ExtractTablesFromResults(ResultCollection results) {
|
---|
| 82 | foreach (IResult result in results) {
|
---|
| 83 | IScope scope = result.Value as IScope;
|
---|
| 84 | if (scope != null) {
|
---|
| 85 | foreach (Variable v in scope.Variables) {
|
---|
| 86 | AutoCorrelationTable table = v.Value as AutoCorrelationTable;
|
---|
| 87 | if (table != null) {
|
---|
| 88 | yield return table;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public static double square(double x) { return x * x; }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|