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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Analysis;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using System.Linq;
|
---|
34 | using HeuristicLab.Optimization;
|
---|
35 | using HEAL.Attic;
|
---|
36 |
|
---|
37 | namespace 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 | }
|
---|