Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs @ 18027

Last change on this file since 18027 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.8 KB
RevLine 
[16978]1#region License Information
2/* HeuristicLab
[17181]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
22using System.Linq;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
[17098]30using HeuristicLab.PluginInfrastructure;
[16978]31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [StorableType("F30B6562-ECEC-42A0-B7D6-E55390FC0412")]
[17098]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}
Note: See TracBrowser for help on using the repository browser.