Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/Aggregators/EvolvabilityAggregator.cs @ 10349

Last change on this file since 10349 was 7202, checked in by epitzer, 13 years ago

#1696: Add static item image according to #1651

File size: 5.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Optimization;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Data;
10using HeuristicLab.Common.Resources;
11
12namespace HeuristicLab.Analysis.FitnessLandscape {
13
14  [Item("Evolvability Aggregator", "Aggregates evolvability values into an evolvability portrait.")]
15  [StorableClass]
16  public class EvolvabilityAggregator : NamedItem, IAggregator {
17
18    #region Fields
19    [Storable]
20    private List<double> E_a;
21    [Storable]
22    private List<double> E_b;
23    [Storable]
24    private List<double> E_c;
25    [Storable]
26    private List<double> E_d;
27    [Storable]
28    private List<double> baseQualities;
29    [Storable]
30    private List<double> expectedAbsoluteQualityDeltas;
31    [Storable]
32    private List<double> expectedRelativeQualityDeltas;
33    #endregion   
34
35    #region Properties
36    public static new Image StaticItemImage { get { return VSImageLibrary.Database; } }
37    public override bool CanChangeName { get { return false; } }
38    #endregion
39
40    #region Construction and Cloning
41    [StorableConstructor]
42    protected EvolvabilityAggregator(bool deserializing) : base(deserializing) { }
43    protected EvolvabilityAggregator(EvolvabilityAggregator original, Cloner cloner)
44      : base(original, cloner) {
45      E_a = original.E_a.ToList();
46      E_b = original.E_b.ToList();
47      E_c = original.E_c.ToList();
48      E_d = original.E_d.ToList();
49      baseQualities = original.baseQualities.ToList();
50      expectedAbsoluteQualityDeltas = original.expectedAbsoluteQualityDeltas.ToList();
51      expectedRelativeQualityDeltas = original.expectedRelativeQualityDeltas.ToList();
52    }
53    public EvolvabilityAggregator() {
54      name = ItemName;
55      description = ItemDescription;
56      E_a = new List<double>();
57      E_b = new List<double>();
58      E_c = new List<double>();
59      E_d = new List<double>();
60      baseQualities = new List<double>();
61      expectedAbsoluteQualityDeltas = new List<double>();
62      expectedRelativeQualityDeltas = new List<double>();
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new EvolvabilityAggregator(this, cloner);
66    }
67    #endregion
68
69    #region IAggregator Members
70    public void MaybeAddResult(IResult result) {
71      if (result.DataType == typeof(DoubleValue)) {
72        double value = ((DoubleValue)result.Value).Value;
73        switch (result.Name) {
74          case "E_a": E_a.Add(value); break;
75          case "E_b": E_b.Add(value); break;
76          case "E_c": E_c.Add(value); break;
77          case "E_d": E_d.Add(value); break;
78          case "Base Quality": baseQualities.Add(value); break;
79          case "Expected Absolute Quality Delta": expectedAbsoluteQualityDeltas.Add(value); break;
80          case "Expected Relative Quality Delta": expectedRelativeQualityDeltas.Add(value); break;
81        }
82      }
83    }
84
85    public void Reset() {
86      E_a.Clear();
87      E_b.Clear();
88      E_c.Clear();
89      E_d.Clear();
90      baseQualities.Clear();
91      expectedAbsoluteQualityDeltas.Clear();
92      expectedRelativeQualityDeltas.Clear();
93    }
94
95    private class PortraitEntry {
96      public readonly double A, B, C, D, Q, AbsQDelta, RelQDelta;
97      public PortraitEntry(double a, double b, double c, double d, double q, double absQDelta, double relQDelta) {       
98        A = a;
99        B = b;
100        C = c;
101        D = d;
102        Q = q;
103        AbsQDelta = absQDelta;
104        RelQDelta = relQDelta;
105      }           
106    }
107
108    public IResult CreateResult() {
109      var portrait = new DataTable("Evolvability Portrait");
110      var E_a_row = new DataRow("E_a", "probability of non-deleterious mutation")
111                      {VisualProperties = {SecondYAxis = true}};
112      var E_b_row = new DataRow("E_b", "average expected offspring fitness");
113      var E_c_row = new DataRow("E_c", "90th percentile offspring fitness");
114      var E_d_row = new DataRow("E_d", "10th percentile offspring fitness");
115      var baseQualityRow = new DataRow("Q", "Base quality");
116      var absQDelta = new DataRow("abs Q delta", "expected absolute quality delta");
117      var relQDelta = new DataRow("rel Q delta", "expected relatve quality delta")
118                        {VisualProperties = {SecondYAxis = true}};
119      var entries = E_a.Select((t, i) =>
120        new PortraitEntry(t, E_b[i], E_c[i], E_d[i],
121          baseQualities[i], expectedAbsoluteQualityDeltas[i], expectedRelativeQualityDeltas[i]))
122          .ToList();
123      Action<List<PortraitEntry>> addBinAverages = b => {
124        if (b.Count > 0) {
125          E_a_row.Values.Add(b.Average(e => e.A));
126          E_b_row.Values.Add(b.Average(e => e.B));
127          E_c_row.Values.Add(b.Average(e => e.C));
128          E_d_row.Values.Add(b.Average(e => e.D));
129          baseQualityRow.Values.Add(b.Average(e => e.Q));
130          absQDelta.Values.Add(b.Average(e => e.AbsQDelta));
131          relQDelta.Values.Add(b.Average(e => e.RelQDelta));
132        }
133      };
134      var a = new DistributionAnalyzer(baseQualities);
135      addBinAverages(entries.Where(e => e.Q <= a[0.1]).ToList());
136      for (double t = 0.1; t < 1; t += 0.1)
137        addBinAverages(entries.Where(e => a[t] < e.Q && e.Q <= a[t+0.1]).ToList());
138      portrait.Rows.Add(E_a_row);
139      portrait.Rows.Add(E_b_row);
140      portrait.Rows.Add(E_c_row);
141      portrait.Rows.Add(E_d_row);
142      portrait.Rows.Add(baseQualityRow);
143      portrait.Rows.Add(absQDelta);
144      portrait.Rows.Add(relQDelta);
145      return new Result("Evolvability Portrait", portrait);
146    }   
147    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.