Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/DiversificationStatisticsOperator.cs @ 13693

Last change on this file since 13693 was 13527, checked in by bburlacu, 9 years ago

#1772: Fixed the way operator improvement is calculated so it also works when a diversification strategy is applied (introducing multiple intermediate vertices between parent and child). Minor code refactoring in the diversification operators. Fixed very small typo in the BeforeManipulatorOperator.

File size: 6.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 HeuristicLab.Analysis;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  [Item("SchemaCleanupOperator", "Operator which removes the schemas from the global scope after they have been evaluated.")]
33  [StorableClass]
34  public class DiversificationStatisticsOperator : SingleSuccessorOperator {
35    private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees";
36    private const string NumberOfSchemasParameterName = "NumberOfSchemas";
37    private const string AverageSchemaLengthParameterName = "AverageSchemaLength";
38    private const string ResultCollectionParameterName = "Results";
39    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
40
41    private readonly object locker = new object();
42
43    public ILookupParameter<IntValue> NumberOfChangedTreesParameter {
44      get { return (ILookupParameter<IntValue>)Parameters[NumberOfChangedTreesParameterName]; }
45    }
46    public ILookupParameter<IntValue> NumberOfSchemasParameter {
47      get { return (ILookupParameter<IntValue>)Parameters[NumberOfSchemasParameterName]; }
48    }
49    public ILookupParameter<DoubleValue> AverageSchemaLengthParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters[AverageSchemaLengthParameterName]; }
51    }
52    public ILookupParameter<ResultCollection> ResultCollectionParameter {
53      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
54    }
55    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
56      get { return (ILookupParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
57    }
58
59    private int evaluatedSolutionsTracker;
60
61    public DiversificationStatisticsOperator() {
62      Parameters.Add(new LookupParameter<IntValue>(NumberOfChangedTreesParameterName));
63      Parameters.Add(new LookupParameter<IntValue>(NumberOfSchemasParameterName));
64      Parameters.Add(new LookupParameter<DoubleValue>(AverageSchemaLengthParameterName));
65      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName));
66      Parameters.Add(new LookupParameter<IntValue>(EvaluatedSolutionsParameterName));
67    }
68
69    public override void InitializeState() {
70      base.InitializeState();
71      evaluatedSolutionsTracker = 0;
72    }
73
74    public override void ClearState() {
75      base.ClearState();
76      evaluatedSolutionsTracker = 0;
77    }
78
79    protected DiversificationStatisticsOperator(DiversificationStatisticsOperator original, Cloner cloner) : base(original, cloner) { }
80
81    public override IDeepCloneable Clone(Cloner cloner) { return new DiversificationStatisticsOperator(this, cloner); }
82
83    [StorableConstructor]
84    protected DiversificationStatisticsOperator(bool deserializing) : base(deserializing) { }
85
86    public override IOperation Apply() {
87      var results = ResultCollectionParameter.ActualValue;
88      DataTable table;
89      if (!results.ContainsKey("NumberOfChangedTrees")) {
90        table = new DataTable();
91        results.Add(new Result("NumberOfChangedTrees", table));
92        var row = new DataRow("Changed trees") { VisualProperties = { StartIndexZero = true } };
93        table.Rows.Add(row);
94      }
95      if (!results.ContainsKey("AverageSchemaLength")) {
96        table = new DataTable();
97        results.Add(new Result("AverageSchemaLength", table));
98        var row = new DataRow("Average schema length") { VisualProperties = { StartIndexZero = true } };
99        table.Rows.Add(row);
100      }
101      if (!results.ContainsKey("NumberOfSchemas")) {
102        table = new DataTable();
103        results.Add(new Result("NumberOfSchemas", table));
104        var row = new DataRow("Number of schemas") { VisualProperties = { StartIndexZero = true } };
105        table.Rows.Add(row);
106      }
107      if (!results.ContainsKey("EvaluatedSolutionsPerGeneration")) {
108        table = new DataTable();
109        results.Add(new Result("EvaluatedSolutionsPerGeneration", table));
110        var row = new DataRow("Evaluated solutions") { VisualProperties = { StartIndexZero = true } };
111        table.Rows.Add(row);
112        row.Values.Add(0);
113      }
114
115      lock (locker) {
116        // count the extra evaluations performed after diversification
117        EvaluatedSolutionsParameter.ActualValue.Value += NumberOfChangedTreesParameter.ActualValue.Value;
118      }
119
120      var evaluatedSolutions = EvaluatedSolutionsParameter.ActualValue.Value;
121      if (evaluatedSolutions > 0)
122        evaluatedSolutions -= evaluatedSolutionsTracker;
123      ((DataTable)results["NumberOfChangedTrees"].Value).Rows["Changed trees"].Values.Add(NumberOfChangedTreesParameter.ActualValue.Value);
124      ((DataTable)results["AverageSchemaLength"].Value).Rows["Average schema length"].Values.Add(AverageSchemaLengthParameter.ActualValue.Value);
125      ((DataTable)results["NumberOfSchemas"].Value).Rows["Number of schemas"].Values.Add(NumberOfSchemasParameter.ActualValue.Value);
126      ((DataTable)results["EvaluatedSolutionsPerGeneration"].Value).Rows["Evaluated solutions"].Values.Add(evaluatedSolutions);
127      evaluatedSolutionsTracker += evaluatedSolutions;
128
129      return base.Apply();
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.