[7784] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Analysis;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Calculates the complexity of all trees in the population.
|
---|
| 39 | /// </summary>
|
---|
| 40 | [Item("SymbolicDataAnalysisComplexityAnalyzer", "Calculates the complexity of all trees in the population.")]
|
---|
| 41 | [StorableClass]
|
---|
| 42 | public sealed class SymbolicDataAnalysisComplexityAnalyzer : SymbolicDataAnalysisAnalyzer {
|
---|
| 43 | private const string ComplexityParameterName = "Complexity";
|
---|
| 44 | private const string WeightsParameterName = "Weights";
|
---|
| 45 |
|
---|
| 46 | public override bool EnabledByDefault {
|
---|
| 47 | get { return false; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | #region parameter properties
|
---|
| 51 | public IScopeTreeLookupParameter<DoubleValue> ComplexityParameter {
|
---|
| 52 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[ComplexityParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | public IValueParameter<ItemDictionary<StringValue, DoubleValue>> WeightsParameter {
|
---|
| 55 | get { return (IValueParameter<ItemDictionary<StringValue, DoubleValue>>)Parameters[WeightsParameterName]; }
|
---|
| 56 | }
|
---|
| 57 | #endregion
|
---|
| 58 |
|
---|
| 59 | [StorableConstructor]
|
---|
| 60 | private SymbolicDataAnalysisComplexityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 61 | private SymbolicDataAnalysisComplexityAnalyzer(SymbolicDataAnalysisComplexityAnalyzer original, Cloner cloner)
|
---|
| 62 | : base(original, cloner) {
|
---|
| 63 | }
|
---|
| 64 | public SymbolicDataAnalysisComplexityAnalyzer()
|
---|
| 65 | : base() {
|
---|
| 66 | var defaultWeights = new Dictionary<string, double>()
|
---|
| 67 | {
|
---|
| 68 | { "StartSymbol", 0.0},
|
---|
| 69 | { "ProgramRootSymbol", 0.0},
|
---|
| 70 | { "Addition", 1.0 },
|
---|
| 71 | { "AiryA", 10.0 },
|
---|
| 72 | { "AiryB", 10.0 },
|
---|
| 73 | { "And", 2.0 },
|
---|
| 74 | { "Average", 2.0 },
|
---|
| 75 | { "Bessel", 10.0 },
|
---|
| 76 | { "Constant", 1.0 },
|
---|
| 77 | { "Cosine", 5.0 },
|
---|
| 78 | { "CosineIntegral", 10.0 },
|
---|
| 79 | { "Dawson", 10.0 },
|
---|
| 80 | { "Derivative", 5.0 },
|
---|
| 81 | { "Division", 2.0 },
|
---|
| 82 | { "Erf", 10.0 },
|
---|
| 83 | { "Exponential", 5.0 },
|
---|
| 84 | { "ExponentialIntegralEi", 10.0 },
|
---|
| 85 | { "FresnelCosineIntegral", 10.0 },
|
---|
| 86 | { "FresnelSineIntegral", 10.0 },
|
---|
| 87 | { "Gamma", 10.0 },
|
---|
| 88 | { "GreaterThan", 2.0 },
|
---|
| 89 | { "HyperbolicCosineIntegral", 10.0 },
|
---|
| 90 | { "HyperbolicSineIntegral", 10.0 },
|
---|
| 91 | { "IfThenElse", 3.0 },
|
---|
| 92 | { "Integral", 5.0},
|
---|
| 93 | { "LaggedVariable", 2.0 },
|
---|
| 94 | { "LessThan", 2.0 },
|
---|
| 95 | { "Logarithm", 5.0 },
|
---|
| 96 | { "Multiplication", 2.0 },
|
---|
| 97 | { "Norm", 10.0 },
|
---|
| 98 | { "Not", 2.0 },
|
---|
| 99 | { "Or", 1.0 },
|
---|
| 100 | { "Power", 5.0 },
|
---|
| 101 | { "Psi", 10.0 },
|
---|
| 102 | { "Root", 5.0 },
|
---|
| 103 | { "Sine", 5.0 },
|
---|
| 104 | { "SineIntegral", 10.0 },
|
---|
| 105 | { "Square", 2.0 },
|
---|
| 106 | { "SquareRoot", 2.0 },
|
---|
| 107 | { "Subtraction", 1.0 },
|
---|
| 108 | { "Tangent", 3.0 },
|
---|
| 109 | { "TimeLag", 3.0 },
|
---|
| 110 | { "Variable", 1.0 },
|
---|
| 111 | { "Variable Condition", 5.0 },
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | var defaultWeightsTable = new ItemDictionary<StringValue, DoubleValue>();
|
---|
| 115 | foreach (var p in defaultWeights) {
|
---|
| 116 | defaultWeightsTable.Add(new StringValue(p.Key), new DoubleValue(p.Value));
|
---|
| 117 | }
|
---|
| 118 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(ComplexityParameterName, "The complexity of the tree."));
|
---|
| 119 | Parameters.Add(new ValueParameter<ItemDictionary<StringValue, DoubleValue>>(WeightsParameterName, "A table with complexity weights for each symbol.", defaultWeightsTable));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 123 | return new SymbolicDataAnalysisComplexityAnalyzer(this, cloner);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public override IOperation Apply() {
|
---|
| 127 | ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 128 | var weightsTable = WeightsParameter.Value;
|
---|
| 129 | var weights = new Dictionary<string, double>();
|
---|
| 130 | foreach (var p in weightsTable)
|
---|
| 131 | weights.Add(p.Key.Value, p.Value.Value);
|
---|
| 132 | var complexities = from t in expressions
|
---|
| 133 | select CalculateComplexity(t, weights);
|
---|
| 134 | ComplexityParameter.ActualValue = new ItemArray<DoubleValue>(complexities.Select(x => new DoubleValue(x)).ToArray());
|
---|
| 135 | return base.Apply();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private double CalculateComplexity(ISymbolicExpressionTree t, Dictionary<string, double> weights) {
|
---|
| 139 | double c = 0.0;
|
---|
| 140 | foreach (var n in t.Root.IterateNodesPrefix()) {
|
---|
| 141 | if (!weights.ContainsKey(n.Symbol.Name)) throw new ArgumentException("Weight for symbol " + n.Symbol.Name + " is not defined.");
|
---|
| 142 | c += weights[n.Symbol.Name];
|
---|
| 143 | }
|
---|
| 144 | return c;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|