Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/SelectionSchemeAnalyzer.cs @ 17434

Last change on this file since 17434 was 17434, checked in by bburlacu, 4 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 5.3 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 HEAL.Attic;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.EvolutionTracking
33{
34  [Item("SelectionSchemeAnalyzer", "An analyzer which gives information about the relative amount of individuals that get selected each generation")]
35  [StorableType("96FBBBE5-059C-4EA7-A633-0D2CBD7F58BE")]
36  public class SelectionSchemeAnalyzer : EvolutionTrackingAnalyzer
37  {
38    private const string StoreHistoryParameterName = "StoreHistory";
39
40    public IFixedValueParameter<BoolValue> StoreHistoryParameter {
41      get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
42    }
43
44    public SelectionSchemeAnalyzer() {
45      UpdateCounterParameter.ActualName = "SelectionSchemeAnalyzerUpdateCounter";
46      Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, new BoolValue(false)));
47    }
48
49    public bool StoreHistory { get { return StoreHistoryParameter.Value.Value; } }
50
51    [StorableConstructor]
52    protected SelectionSchemeAnalyzer(StorableConstructorFlag _) : base(_) { }
53
54    protected SelectionSchemeAnalyzer(SelectionSchemeAnalyzer original, Cloner cloner) : base(original, cloner) { }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SelectionSchemeAnalyzer(this, cloner);
58    }
59
60    public override IOperation Apply() {
61      int updateInterval = UpdateIntervalParameter.Value.Value;
62      IntValue updateCounter = UpdateCounterParameter.ActualValue;
63      // if counter does not yet exist then initialize it with update interval
64      // to make sure the solutions are analyzed on the first application of this operator
65      if (updateCounter == null) {
66        updateCounter = new IntValue(0);
67        UpdateCounterParameter.ActualValue = updateCounter;
68      }
69      updateCounter.Value++;
70      //analyze solutions only every 'updateInterval' times
71      if (updateCounter.Value != updateInterval) return base.Apply();
72      updateCounter.Value = 0;
73
74      if (PopulationGraph == null)
75        return base.Apply();
76      double selectionRatio = 1;
77
78      if (Generations.Value > 0) {
79        var previousRank = PopulationGraph.GetByRank(Generation.Value - 1);
80        selectionRatio = (double)previousRank.Count(x => x.OutDegree > 0) / PopulationSize.Value;
81      }
82
83      // the selection ratio represents the percentage of the old population that survived to reproduce
84      DataTable table;
85      if (!Results.ContainsKey("SelectionRatio")) {
86        table = new DataTable("Selection ratio");
87        Results.Add(new Result("SelectionRatio", table));
88        var row = new DataRow("Selection ratio") { VisualProperties = { StartIndexZero = true } };
89        row.Values.Add(selectionRatio);
90        table.Rows.Add(row);
91      } else {
92        table = (DataTable)Results["SelectionRatio"].Value;
93        table.Rows["Selection ratio"].Values.Add(selectionRatio);
94      }
95      DataTable contributionCountTable;
96      var contributions = PopulationGraph.GetByRank(Generation.Value - 1).OrderByDescending(x => x.Quality).Select(x => (double)x.OutDegree);
97      if (Results.ContainsKey("ContributionCount")) {
98        contributionCountTable = (DataTable)Results["ContributionCount"].Value;
99
100        var row = contributionCountTable.Rows["Contribution count"];
101        row.Values.Replace(contributions);
102      } else {
103        contributionCountTable = new DataTable("Contribution count");
104        Results.Add(new Result("ContributionCount", contributionCountTable));
105        var row = new DataRow("Contribution count") {
106          VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Columns }
107        };
108        row.Values.AddRange(contributions);
109        contributionCountTable.Rows.Add(row);
110      }
111
112      if (StoreHistory) {
113        DataTableHistory history;
114        if (Results.ContainsKey("ContributionCountHistory")) {
115          history = (DataTableHistory)Results["ContributionCountHistory"].Value;
116        } else {
117          history = new DataTableHistory();
118          Results.Add(new Result("ContributionCountHistory", history));
119        }
120        history.Add((DataTable)contributionCountTable.Clone());
121      }
122      return base.Apply();
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.