[11221] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 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 |
|
---|
[11224] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11221] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Analysis;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[11224] | 28 | using HeuristicLab.Data;
|
---|
[11221] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.Operators;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
| 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
|
---|
| 36 | [StorableClass]
|
---|
| 37 | [Item("SymbolicDataAnalysisInternalDiversityAnalyzer", "An analyzer that determines the average diversity of symbolic expression trees.")]
|
---|
| 38 | public class SymbolicDataAnalysisInternalDiversityAnalyzer : SingleSuccessorOperator, ISymbolicDataAnalysisAnalyzer {
|
---|
| 39 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
[11224] | 40 | private const string QualityParameterName = "Quality";
|
---|
| 41 | private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions";
|
---|
| 42 |
|
---|
[11221] | 43 | private const string ResultCollectionParameterName = "Results";
|
---|
| 44 | private readonly BottomUpSimilarityCalculator busCalculator;
|
---|
| 45 |
|
---|
| 46 | public SymbolicDataAnalysisInternalDiversityAnalyzer() {
|
---|
| 47 | busCalculator = new BottomUpSimilarityCalculator();
|
---|
| 48 |
|
---|
| 49 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
|
---|
[11224] | 50 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName));
|
---|
[11221] | 51 | Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName));
|
---|
[11224] | 52 | Parameters.Add(new FixedValueParameter<PercentValue>(PercentageOfBestSolutionsParameterName));
|
---|
[11221] | 53 | }
|
---|
| 54 |
|
---|
| 55 | protected SymbolicDataAnalysisInternalDiversityAnalyzer(SymbolicDataAnalysisInternalDiversityAnalyzer original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 60 | return new SymbolicDataAnalysisInternalDiversityAnalyzer(this, cloner);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[11224] | 63 | #region parameter properties
|
---|
[11221] | 64 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 65 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 66 | }
|
---|
[11224] | 67 |
|
---|
| 68 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 69 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[11221] | 72 | public ILookupParameter<ResultCollection> ResultCollectionParameter {
|
---|
| 73 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[11224] | 76 | public IFixedValueParameter<PercentValue> PercentageOfBestSolutionsParameter {
|
---|
| 77 | get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageOfBestSolutionsParameterName]; }
|
---|
| 78 | }
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
| 81 | public double PercentageOfBestSolutions {
|
---|
| 82 | get { return PercentageOfBestSolutionsParameter.Value.Value; }
|
---|
| 83 | set { PercentageOfBestSolutionsParameter.Value.Value = value; }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[11221] | 86 | public bool EnabledByDefault { get { return true; } }
|
---|
| 87 |
|
---|
| 88 | public override IOperation Apply() {
|
---|
| 89 |
|
---|
| 90 | var results = ResultCollectionParameter.ActualValue;
|
---|
| 91 |
|
---|
| 92 | DataTable table;
|
---|
| 93 | if (!results.ContainsKey("Avg. Internal Diversity")) {
|
---|
| 94 | table = new DataTable("Internal Diversity") { VisualProperties = { YAxisTitle = "Internal Diversity" } };
|
---|
[11224] | 95 | var row = new DataRow("Avg. Internal Diversity") { VisualProperties = { StartIndexZero = true } };
|
---|
| 96 | table.Rows.Add(row);
|
---|
[11221] | 97 | results.Add(new Result("Avg. Internal Diversity", table));
|
---|
| 98 | } else {
|
---|
| 99 | table = (DataTable)results["Avg. Internal Diversity"].Value;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[11224] | 102 | var trees = SymbolicExpressionTreeParameter.ActualValue.ToArray();
|
---|
| 103 | var qualities = QualityParameter.ActualValue.ToArray();
|
---|
[11221] | 104 |
|
---|
[11224] | 105 | Array.Sort(qualities, trees, new ReverseComparer());
|
---|
| 106 | int n = (int)Math.Floor(trees.Length * PercentageOfBestSolutions);
|
---|
| 107 |
|
---|
| 108 | double avgInternalDiversity = trees.Take(n).Average(tree => CalculateInternalDiversity(tree));
|
---|
| 109 |
|
---|
| 110 | table.Rows["Avg. Internal Diversity"].Values.Add(avgInternalDiversity);
|
---|
[11221] | 111 | return base.Apply();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private double CalculateInternalDiversity(ISymbolicExpressionTree tree) {
|
---|
| 115 | var branchPoint = tree.IterateNodesPrefix().FirstOrDefault(x => x.SubtreeCount == 2);
|
---|
| 116 | if (branchPoint == null)
|
---|
| 117 | return 1;
|
---|
| 118 |
|
---|
| 119 | var s1 = branchPoint.GetSubtree(0);
|
---|
| 120 | var s2 = branchPoint.GetSubtree(1);
|
---|
| 121 |
|
---|
| 122 | // set parents to null because otherwise the bottom-up calculator will throw an exception
|
---|
| 123 | var p1 = s1.Parent;
|
---|
| 124 | s1.Parent = null;
|
---|
| 125 | var p2 = s2.Parent;
|
---|
| 126 | s2.Parent = null;
|
---|
| 127 |
|
---|
| 128 | var m = busCalculator.ComputeBottomUpMapping(s1, s2);
|
---|
| 129 | var diversity = 1 - 2.0 * m.Count / (s1.GetLength() + s2.GetLength());
|
---|
| 130 |
|
---|
| 131 | // restore parents
|
---|
| 132 | s1.Parent = p1;
|
---|
| 133 | s2.Parent = p2;
|
---|
| 134 |
|
---|
| 135 | return diversity;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[11224] | 138 |
|
---|
| 139 | internal class ReverseComparer : IComparer<DoubleValue> {
|
---|
| 140 | public int Compare(DoubleValue x, DoubleValue y) {
|
---|
| 141 | return y.CompareTo(x);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[11221] | 144 | }
|
---|