[16978] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[16978] | 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.Linq;
|
---|
| 23 | using HEAL.Attic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[16981] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
[16978] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 33 | [StorableType("F30B6562-ECEC-42A0-B7D6-E55390FC0412")]
|
---|
[16981] | 34 | [NonDiscoverableType]
|
---|
[16978] | 35 | public class SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator : InstrumentedOperator {
|
---|
| 36 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
| 37 | private const string AverageSimilarityParameterName = "AverageSimilarity";
|
---|
| 38 | private const string StrictSimilarityParameterName = "StrictSimilarity";
|
---|
| 39 |
|
---|
| 40 | public SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator() {
|
---|
| 41 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
|
---|
| 42 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(AverageSimilarityParameterName));
|
---|
| 43 | Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | protected SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator(SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator rhs, Cloner cloner) { }
|
---|
| 47 |
|
---|
| 48 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 49 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
|
---|
| 53 | get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public bool StrictSimilarity {
|
---|
| 57 | get { return StrictSimilarityParameter.Value.Value; }
|
---|
| 58 | set { StrictSimilarityParameter.Value.Value = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public IScopeTreeLookupParameter<DoubleValue> AverageSimilarityParameter {
|
---|
| 62 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[AverageSimilarityParameterName]; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 66 | return new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator(this, cloner);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | [StorableConstructor]
|
---|
| 70 | protected SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
| 71 |
|
---|
| 72 | public override IOperation InstrumentedApply() {
|
---|
| 73 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 74 | var similarityMatrix = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(trees, false, StrictSimilarity);
|
---|
| 75 | DoubleValue averageSimilarity(int i) => new DoubleValue(Enumerable.Range(0, trees.Length).Where(j => i != j).Average(j => similarityMatrix[i, j]));
|
---|
| 76 | AverageSimilarityParameter.ActualValue = new ItemArray<DoubleValue>(Enumerable.Range(0, trees.Length).Select(averageSimilarity));
|
---|
| 77 | return base.InstrumentedApply();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|