Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer.cs @ 17300

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

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

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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("MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer", "An operator that tracks the min avgerage and max VariablesNumber of symbolic expression trees.")]
42  [StorableType("3D76565D-B334-463A-8D17-F43A75CBD892")]
43  public sealed class MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
44    private const string ResultsParameterName = "Results";
45    private const string MinMaxAvgVariablesNumberResultName = "MinMaxAvgTreeVariablesNumber";
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 MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer(StorableConstructorFlag deserializing) : base() { }
55    private MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer(MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer original, Cloner cloner)
56      : base(original, cloner) {
57      AfterDeserialization();
58    }
59    public MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer()
60      : base() {
61    }
62
63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() { }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer(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().OfType<IVariableTreeNode>().Count()).ToArray();
74      var min = variablesNumber.Min();
75      var max = variablesNumber.Max();
76      var avg = variablesNumber.Average();
77
78      DataTable table;
79      if (ResultCollection.ContainsKey(MinMaxAvgVariablesNumberResultName)) {
80        table = (DataTable)ResultCollection[MinMaxAvgVariablesNumberResultName].Value;
81      } else {
82        table = new DataTable("Tree Variables Number");
83        ResultCollection.Add(new Result(MinMaxAvgVariablesNumberResultName, 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.