Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/MinAverageMaxSymbolicExpressionTreeComplexcityAnalyzer.cs @ 16899

Last change on this file since 16899 was 16722, checked in by msemenki, 6 years ago

#2988: Add first version of GP for Evolvment models of models.

File size: 4.3 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using System.Collections.Generic;
34using System.Linq;
35using HeuristicLab.Optimization;
36using HEAL.Attic;
37
38namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
39  /// <summary>
40  /// An operator that tracks the min average and max length of symbolic expression trees.
41  /// </summary>
42  [Item("MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer", "An operator that tracks the min avgerage and max Complexity of symbolic expression trees.")]
43  [StorableType("DB872B51-047A-4655-A34E-E9DA11D5860A")]
44  public sealed class MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
45    private const string ResultsParameterName = "Results";
46    private const string MinMaxAvgComplexityResultName = "MinMaxAvgTreeComplexity";
47
48    #region parameter properties
49    public ValueLookupParameter<VariableCollection> ResultsParameter {
50      get { return (ValueLookupParameter<VariableCollection>)Parameters[ResultsParameterName]; }
51    }
52    #endregion
53
54    [StorableConstructor]
55    private MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer(StorableConstructorFlag deserializing) : base() { }
56    private MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer(MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer original, Cloner cloner)
57      : base(original, cloner) {
58      AfterDeserialization();
59    }
60    public MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer()
61      : base() {
62    }
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() { }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new MinAverageMaxSymbolicExpressionTreeComplexityAnalyzer(this, cloner);
69    }
70
71    public override IOperation Apply() {
72      var trees = SymbolicExpressionTreeParameter.ActualValue;
73      var complexities = trees.Select(SymbolicDataAnalysisModelComplexityCalculator.CalculateComplexity).ToArray();
74
75      var min = complexities.Min();
76      var max = complexities.Max();
77      var avg = complexities.Average();
78
79      //double min = complexities[0], max = complexities[0], avg = 0;
80
81      //foreach (var c in complexities) {
82      //  if (min > c) {
83      //    min = c;
84      //  }
85      //  if (max < c) {
86      //    max = c;
87      //  }
88      //  avg += c;
89      //}
90      //avg /= complexities.Length;
91
92      DataTable table;
93      if (ResultCollection.ContainsKey(MinMaxAvgComplexityResultName)) {
94        table = (DataTable)ResultCollection[MinMaxAvgComplexityResultName].Value;
95      } else {
96        table = new DataTable("Tree Complexity");
97        ResultCollection.Add(new Result(MinMaxAvgComplexityResultName, table));
98        table.Rows.Add(new DataRow("Min") { VisualProperties = { StartIndexZero = true } });
99        table.Rows.Add(new DataRow("Max") { VisualProperties = { StartIndexZero = true } });
100        table.Rows.Add(new DataRow("Avg") { VisualProperties = { StartIndexZero = true } });
101      }
102      table.Rows["Min"].Values.Add(min);
103      table.Rows["Max"].Values.Add(max);
104      table.Rows["Avg"].Values.Add(avg);
105
106      return base.Apply();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.