Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/MinAverageMaxSymbolicExpressionNeastedTreeSyzeAnalyzer.cs @ 16722

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

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

File size: 4.0 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.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32using System.Collections.Generic;
33using System.Linq;
34using HeuristicLab.Optimization;
35using HEAL.Attic;
36
37namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
38  /// <summary>
39  /// An operator that tracks the min average and max length of symbolic expression trees.
40  /// </summary>
41  [Item("MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer", "An operator that tracks the min avgerage and max Neasted Tree Size of symbolic expression trees.")]
42  [StorableType("73ED3D0E-47F8-455D-B8C5-1220ACFEC5E1")]
43  public sealed class MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
44    private const string ResultsParameterName = "Results";
45    private const string MinMaxAvgNeastedTreeSizeResultName = "MinMaxAvgNeastedTreeSize";
46
47    #region parameter properties
48    public ValueLookupParameter<VariableCollection> ResultsParameter {
49      get { return (ValueLookupParameter<VariableCollection>)Parameters[ResultsParameterName]; }
50    }
51    #endregion
52
53    [StorableConstructor]
54    private MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer(StorableConstructorFlag deserializing) : base() { }
55    private MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer(MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer original, Cloner cloner)
56      : base(original, cloner) {
57      AfterDeserialization();
58    }
59    public MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer()
60      : base() {
61    }
62
63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() { }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new MinAverageMaxSymbolicExpressionNeastedTreeSizeAnalyzer(this, cloner);
68    }
69
70    public override IOperation Apply() {
71      var trees = SymbolicExpressionTreeParameter.ActualValue;
72      //var variablesNumber = new DoubleArray(trees.Length);
73      var variablesNumber = trees.Select(tree => tree.IterateNodesPostfix().Sum(n => n.GetLength())).ToArray();
74      var min = variablesNumber.Min();
75      var max = variablesNumber.Max();
76      var avg = variablesNumber.Average();
77
78      DataTable table;
79      if (ResultCollection.ContainsKey(MinMaxAvgNeastedTreeSizeResultName)) {
80        table = (DataTable)ResultCollection[MinMaxAvgNeastedTreeSizeResultName].Value;
81      } else {
82        table = new DataTable("Tree Variables Number");
83        ResultCollection.Add(new Result(MinMaxAvgNeastedTreeSizeResultName, table));
84        table.Rows.Add(new DataRow("Min") { VisualProperties = { StartIndexZero = true } });
85        table.Rows.Add(new DataRow("Max") { VisualProperties = { StartIndexZero = true } });
86        table.Rows.Add(new DataRow("Avg") { VisualProperties = { StartIndexZero = true } });
87      }
88      table.Rows["Min"].Values.Add(min);
89      table.Rows["Max"].Values.Add(max);
90      table.Rows["Avg"].Values.Add(avg);
91
92      return base.Apply();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.