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

Last change on this file since 13496 was 13496, checked in by bburlacu, 5 years ago

#1772: Update diversification operators (add extra evaluations to the evaluation counter, add the option for strict schema matching, add additional replacement ratio update rules.

File size: 6.1 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 ClearState() {
70      evaluatedSolutionsTracker = 0;
71      base.ClearState();
72    }
73
74    protected DiversificationStatisticsOperator(DiversificationStatisticsOperator original, Cloner cloner) : base(original, cloner) { }
75
76    public override IDeepCloneable Clone(Cloner cloner) { return new DiversificationStatisticsOperator(this, cloner); }
77
78    [StorableConstructor]
79    protected DiversificationStatisticsOperator(bool deserializing) : base(deserializing) { }
80
81    public override IOperation Apply() {
82      var results = ResultCollectionParameter.ActualValue;
83      DataTable table;
84      if (!results.ContainsKey("NumberOfChangedTrees")) {
85        table = new DataTable();
86        results.Add(new Result("NumberOfChangedTrees", table));
87        var row = new DataRow("Changed trees") { VisualProperties = { StartIndexZero = true } };
88        table.Rows.Add(row);
89      }
90      if (!results.ContainsKey("AverageSchemaLength")) {
91        table = new DataTable();
92        results.Add(new Result("AverageSchemaLength", table));
93        var row = new DataRow("Average schema length") { VisualProperties = { StartIndexZero = true } };
94        table.Rows.Add(row);
95      }
96      if (!results.ContainsKey("NumberOfSchemas")) {
97        table = new DataTable();
98        results.Add(new Result("NumberOfSchemas", table));
99        var row = new DataRow("Number of schemas") { VisualProperties = { StartIndexZero = true } };
100        table.Rows.Add(row);
101      }
102      if (!results.ContainsKey("EvaluatedSolutionsPerGeneration")) {
103        table = new DataTable();
104        results.Add(new Result("EvaluatedSolutionsPerGeneration", table));
105        var row = new DataRow("Evaluated solutions") { VisualProperties = { StartIndexZero = true } };
106        table.Rows.Add(row);
107        row.Values.Add(0);
108      }
109
110      lock (locker) {
111        // count the extra evaluations performed after diversification
112        EvaluatedSolutionsParameter.ActualValue.Value += NumberOfChangedTreesParameter.ActualValue.Value;
113      }
114
115      var evaluatedSolutions = EvaluatedSolutionsParameter.ActualValue.Value;
116      if (evaluatedSolutions > 0)
117        evaluatedSolutions -= evaluatedSolutionsTracker;
118      ((DataTable)results["NumberOfChangedTrees"].Value).Rows["Changed trees"].Values.Add(NumberOfChangedTreesParameter.ActualValue.Value);
119      ((DataTable)results["AverageSchemaLength"].Value).Rows["Average schema length"].Values.Add(AverageSchemaLengthParameter.ActualValue.Value);
120      ((DataTable)results["NumberOfSchemas"].Value).Rows["Number of schemas"].Values.Add(NumberOfSchemasParameter.ActualValue.Value);
121      ((DataTable)results["EvaluatedSolutionsPerGeneration"].Value).Rows["Evaluated solutions"].Values.Add(evaluatedSolutions);
122      evaluatedSolutionsTracker += evaluatedSolutions;
123
124      return base.Apply();
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.