Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Evolvability/EvolvabilityAnalyzer.cs @ 17514

Last change on this file since 17514 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

File size: 8.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using HEAL.Attic;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Operators;
10using HeuristicLab.Optimization;
11using HeuristicLab.Optimization.Operators;
12using HeuristicLab.Parameters;
13
14namespace HeuristicLab.Analysis.FitnessLandscape.Evolvability {
15
16  [Item("Evolvability Analyzer", "Compares two sets of scopes containing the original population and the evolved population generating several evolvability measures.")]
17  [StorableType("6B05CC43-161A-45C6-9B08-A4CC2235A96B")]
18  public class EvolvabilityAnalyzer : AlgorithmOperator, IAnalyzer {
19
20    public bool EnabledByDefault
21    {
22      get { return false; }
23    }
24
25    #region Parameters
26    public LookupParameter<DoubleValue> QualityParameter
27    {
28      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
29    }
30    public LookupParameter<IntValue> SampleSizeParameter
31    {
32      get { return (LookupParameter<IntValue>)Parameters["SampleSize"]; }
33    }
34    public LookupParameter<HeatMap> EvolvabilityPlotParameter
35    {
36      get { return (LookupParameter<HeatMap>)Parameters["EvolvabilityPlot"]; }
37    }
38    public LookupParameter<VariableCollection> ResultsParameter
39    {
40      get { return (LookupParameter<VariableCollection>)Parameters["Results"]; }
41    }
42    public LookupParameter<ScatterPlot> FitnessCloudParameter
43    {
44      get { return (LookupParameter<ScatterPlot>)Parameters["FitnessCloud"]; }
45    }
46    public LookupParameter<DataTable> EvolvabilityPortraitParameter
47    {
48      get { return (LookupParameter<DataTable>)Parameters["EvolvabilityPortrait"]; }
49    }
50    public ValueLookupParameter<IntValue> NrOfBinsParameter
51    {
52      get { return (ValueLookupParameter<IntValue>)Parameters["NrOfBins"]; }
53    }
54    public ScopeParameter ScopeParameter
55    {
56      get { return (ScopeParameter)Parameters["Scope"]; }
57    }
58    #endregion
59
60    #region Constructors
61    public EvolvabilityAnalyzer() {
62      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The qualities of evolved and unevolved solution candidates"));
63      Parameters.Add(new LookupParameter<IntValue>("SampleSize", "The number of samples or population size for evolvability analysis"));
64      Parameters.Add(new LookupParameter<HeatMap>("EvolvabilityPlot", "Collected evolvability values"));
65      Parameters.Add(new LookupParameter<VariableCollection>("Results", "The collection of all results of this algorithm"));
66      Parameters.Add(new LookupParameter<ScatterPlot>("FitnessCloud", "A fitness cloud of the original vs. evolved sample."));
67      Parameters.Add(new LookupParameter<DataTable>("EvolvabilityPortrait", "Evolvability Portrait as described in Smith et. al (2002). Evol Comput 10:1–34"));
68      Parameters.Add(new ValueLookupParameter<IntValue>("NrOfBins", "Nr of bins for evolvability portrait analysis", new IntValue(25)));
69      Parameters.Add(new ScopeParameter("Scope", "The current scope"));
70
71      var resultsCollector = new ResultsCollector();
72      resultsCollector.CollectedValues.Add(new LookupParameter<HeatMap>(EvolvabilityPlotParameter.Name));
73      resultsCollector.CollectedValues.Add(new LookupParameter<ScatterPlot>(FitnessCloudParameter.Name));
74      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(EvolvabilityPortraitParameter.Name));
75
76      OperatorGraph.InitialOperator = resultsCollector;
77      resultsCollector.Successor = null;
78    }
79
80    [StorableConstructor]
81    protected EvolvabilityAnalyzer(StorableConstructorFlag _) : base(_) { }
82
83    protected EvolvabilityAnalyzer(EvolvabilityAnalyzer original, Cloner cloner)
84      : base(original, cloner) {
85    }
86    #endregion
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new EvolvabilityAnalyzer(this, cloner);
90    }
91
92    public override IOperation Apply() {
93      var points = GenerateQualityPairs(ScopeParameter.ActualValue);
94      GetOrCreateEvolvabilityPlot().Add(points);
95      GetOrCreateScatterPlot().Add(points);
96      CreateEvolvabilityPortrait(ScopeParameter.ActualValue);
97      return base.Apply();
98    }
99
100    private IEnumerable<PointF> GenerateQualityPairs(IScope globalScope) {
101      foreach (IScope sampleScope in globalScope.SubScopes) {
102        double originalQuality = ((DoubleValue)sampleScope.Variables[QualityParameter.ActualName].Value).Value;
103        foreach (IScope evolvedScope in sampleScope.SubScopes) {
104          double evolvedQuality = ((DoubleValue)evolvedScope.Variables[QualityParameter.ActualName].Value).Value;
105          yield return new PointF((float)originalQuality, (float)evolvedQuality);
106        }
107      }
108    }
109
110    private HeatMap GetOrCreateEvolvabilityPlot() {
111      HeatMap evolvabilityPlot = EvolvabilityPlotParameter.ActualValue as HeatMap;
112      if (evolvabilityPlot == null) {
113        evolvabilityPlot = new HeatMap();
114        evolvabilityPlot.Name = "Evolvability";
115        EvolvabilityPlotParameter.ActualValue = evolvabilityPlot;
116      }
117      return evolvabilityPlot;
118    }
119
120    private ScatterPlot GetOrCreateScatterPlot() {
121      ScatterPlot scatterPlot = FitnessCloudParameter.ActualValue as ScatterPlot;
122      if (scatterPlot == null) {
123        scatterPlot = new ScatterPlot();
124        scatterPlot.Name = "Fitness Cloud";
125        scatterPlot.XAxisName = "Parent Fitness";
126        scatterPlot.YAxisName = "Offspring Fitness";
127        FitnessCloudParameter.ActualValue = scatterPlot;
128      }
129      return scatterPlot;
130    }
131
132    private DataTable GetOrCreateEvolvabilityPortrait() {
133      DataTable evolvabilityPortrait = EvolvabilityPortraitParameter.ActualValue as DataTable;
134      if (evolvabilityPortrait == null) {
135        evolvabilityPortrait = new DataTable("Evolvability Portrait");
136        evolvabilityPortrait.Rows.Add(new DataRow("E_a", "Probability of non-deleterious mutation"));
137        evolvabilityPortrait.Rows.Add(new DataRow("E_b", "Average expected offspring fitness"));
138        evolvabilityPortrait.Rows.Add(new DataRow("E_c", "Top offspring fitness"));
139        evolvabilityPortrait.Rows.Add(new DataRow("E_d", "Bottom offspring fitness"));
140        EvolvabilityPortraitParameter.ActualValue = evolvabilityPortrait;
141      }
142      return evolvabilityPortrait;
143    }
144
145    private double GetQuality(IScope scope) {
146      return ((DoubleValue)scope.Variables[QualityParameter.ActualName].Value).Value;
147    }
148
149    private void CreateEvolvabilityPortrait(IScope globalScope) {
150      DataTable table = GetOrCreateEvolvabilityPortrait();
151      var e_a = table.Rows["E_a"].Values;
152      var e_b = table.Rows["E_b"].Values;
153      var e_c = table.Rows["E_c"].Values;
154      var e_d = table.Rows["E_d"].Values;
155      var qualities = globalScope.SubScopes.Select(s => GetQuality(s));
156      double min = qualities.Min();
157      double max = qualities.Max();
158      double range = max - min;
159      double stepSize = range / NrOfBinsParameter.ActualValue.Value;
160      double threshold = min + stepSize;
161
162      int n_not_worse = 0;
163      int n = 0;
164      double totalOffspringQuality = 0;
165      double best = double.MinValue;
166      double worst = double.MaxValue;
167      foreach (IScope sampleScope in globalScope.SubScopes) {
168        double originalQuality = GetQuality(sampleScope);
169        foreach (IScope evolvedScope in sampleScope.SubScopes) {
170          double evolvedQuality = GetQuality(evolvedScope);
171          if (evolvedQuality >= originalQuality)
172            n_not_worse++;
173          n++;
174          totalOffspringQuality += evolvedQuality;
175          best = Math.Max(best, evolvedQuality);
176          worst = Math.Min(worst, evolvedQuality);
177        }
178        if (originalQuality > threshold) {
179          threshold += stepSize;
180          e_a.Add(1.0 * n_not_worse / n);
181          e_b.Add(totalOffspringQuality / n);
182          e_c.Add(best);
183          e_d.Add(worst);
184          n_not_worse = 0;
185          n = 0;
186          totalOffspringQuality = 0;
187          best = double.MinValue;
188          worst = double.MaxValue;
189        }
190      }
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.