Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis.GenealogyAnalysis/Analyzers/SelectionSchemeAnalyzer.cs

Last change on this file was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.ProgramSynthesis {
32  [Item("SelectionSchemeAnalyzer", "An analyzer which gives information about the relative amount of individuals that get selected each generation")]
33  [StorableClass]
34  public class SelectionSchemeAnalyzer : EvolutionTrackingAnalyzer {
35    private const string StoreHistoryParameterName = "StoreHistory";
36
37    public IFixedValueParameter<BoolValue> StoreHistoryParameter {
38      get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
39    }
40
41    public SelectionSchemeAnalyzer() {
42      UpdateCounterParameter.ActualName = "SelectionSchemeAnalyzerUpdateCounter";
43      Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, new BoolValue(false)));
44    }
45
46    public bool StoreHistory { get { return StoreHistoryParameter.Value.Value; } }
47
48    [StorableConstructor]
49    protected SelectionSchemeAnalyzer(bool deserializing) : base(deserializing) { }
50
51    protected SelectionSchemeAnalyzer(SelectionSchemeAnalyzer original, Cloner cloner) : base(original, cloner) { }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new SelectionSchemeAnalyzer(this, cloner);
55    }
56
57    public override IOperation Apply() {
58      int updateInterval = UpdateIntervalParameter.Value.Value;
59      IntValue updateCounter = UpdateCounterParameter.ActualValue;
60      // if counter does not yet exist then initialize it with update interval
61      // to make sure the solutions are analyzed on the first application of this operator
62      if (updateCounter == null) {
63        updateCounter = new IntValue(0);
64        UpdateCounterParameter.ActualValue = updateCounter;
65      }
66      updateCounter.Value++;
67      //analyze solutions only every 'updateInterval' times
68      if (updateCounter.Value != updateInterval) return base.Apply();
69      updateCounter.Value = 0;
70
71      if (PopulationGraph == null)
72        return base.Apply();
73      double selectionRatio = 1;
74
75      if (Generations.Value > 0) {
76        var previousRank = PopulationGraph.GetByRank(Generation.Value - 1);
77        selectionRatio = (double)previousRank.Count(x => x.OutDegree > 0) / PopulationSize.Value;
78      }
79
80      // the selection ratio represents the percentage of the old population that survived to reproduce
81      DataTable table;
82      if (!Results.ContainsKey("SelectionRatio")) {
83        table = new DataTable("Selection ratio");
84        Results.Add(new Result("SelectionRatio", table));
85        var row = new DataRow("Selection ratio") { VisualProperties = { StartIndexZero = true } };
86        row.Values.Add(selectionRatio);
87        table.Rows.Add(row);
88      } else {
89        table = (DataTable)Results["SelectionRatio"].Value;
90        table.Rows["Selection ratio"].Values.Add(selectionRatio);
91      }
92      DataTable contributionCountTable;
93      var contributions = PopulationGraph.GetByRank(Generation.Value - 1).OrderByDescending(x => x.Quality).Select(x => (double)x.OutDegree);
94      if (Results.ContainsKey("ContributionCount")) {
95        contributionCountTable = (DataTable)Results["ContributionCount"].Value;
96
97        var row = contributionCountTable.Rows["Contribution count"];
98        row.Values.Replace(contributions);
99      } else {
100        contributionCountTable = new DataTable("Contribution count");
101        Results.Add(new Result("ContributionCount", contributionCountTable));
102        var row = new DataRow("Contribution count") {
103          VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns }
104        };
105        row.Values.AddRange(contributions);
106        contributionCountTable.Rows.Add(row);
107      }
108
109      if (StoreHistory) {
110        DataTableHistory history;
111        if (Results.ContainsKey("ContributionCountHistory")) {
112          history = (DataTableHistory)Results["ContributionCountHistory"].Value;
113        } else {
114          history = new DataTableHistory();
115          Results.Add(new Result("ContributionCountHistory", history));
116        }
117        history.Add((DataTable)contributionCountTable.Clone());
118      }
119      return base.Apply();
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.