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