#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Problems.DataAnalysis.Symbolic; using System.Collections.Generic; using System.Linq; using HeuristicLab.Optimization; using HEAL.Attic; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { /// /// An operator that tracks the min average and max length of symbolic expression trees. /// [Item("MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer", "An operator that tracks the min avgerage and max VariablesNumber of symbolic expression trees.")] [StorableType("3D76565D-B334-463A-8D17-F43A75CBD892")] public sealed class MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer { private const string ResultsParameterName = "Results"; private const string MinMaxAvgVariablesNumberResultName = "MinMaxAvgTreeVariablesNumber"; #region parameter properties public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters[ResultsParameterName]; } } #endregion [StorableConstructor] private MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer(StorableConstructorFlag deserializing) : base() { } private MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer(MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer original, Cloner cloner) : base(original, cloner) { AfterDeserialization(); } public MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer() : base() { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } public override IDeepCloneable Clone(Cloner cloner) { return new MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer(this, cloner); } public override IOperation Apply() { var trees = SymbolicExpressionTreeParameter.ActualValue; //var variablesNumber = new DoubleArray(trees.Length); var variablesNumber = trees.Select(tree => tree.IterateNodesPostfix().OfType().Count()).ToArray(); var min = variablesNumber.Min(); var max = variablesNumber.Max(); var avg = variablesNumber.Average(); DataTable table; if (ResultCollection.ContainsKey(MinMaxAvgVariablesNumberResultName)) { table = (DataTable)ResultCollection[MinMaxAvgVariablesNumberResultName].Value; } else { table = new DataTable("Tree Variables Number"); ResultCollection.Add(new Result(MinMaxAvgVariablesNumberResultName, table)); table.Rows.Add(new DataRow("Min") { VisualProperties = { StartIndexZero = true } }); table.Rows.Add(new DataRow("Max") { VisualProperties = { StartIndexZero = true } }); table.Rows.Add(new DataRow("Avg") { VisualProperties = { StartIndexZero = true } }); } table.Rows["Min"].Values.Add(min); table.Rows["Max"].Values.Add(max); table.Rows["Avg"].Values.Add(avg); return base.Apply(); } } }