Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs @ 17578

Last change on this file since 17578 was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
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;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [StorableType("F30B6562-ECEC-42A0-B7D6-E55390FC0412")]
34  [NonDiscoverableType]
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.