Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPhenotypicDiversityAnalyzer.cs @ 16641

Last change on this file since 16641 was 16641, checked in by gkronber, 6 years ago

#2971: merged r16527:16625 from trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression to branch/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression (resolving all conflicts)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HEAL.Attic;
32using HEAL.Attic;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
35  [Item("SymbolicRegressionPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic distance between trees")]
36  [StorableType("A72624D2-6628-45E6-8EBF-4AAD88CC01F5")]
37  public class SymbolicRegressionPhenotypicDiversityAnalyzer : PopulationSimilarityAnalyzer,
38    ISymbolicDataAnalysisBoundedOperator, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
39    #region parameter names
40    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
41    private const string EvaluatedValuesParameterName = "EstimatedValues";
42    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
43    private const string ProblemDataParameterName = "ProblemData";
44    private const string EstimationLimitsParameterName = "EstimationLimits";
45    #endregion
46
47    #region parameter properties
48    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
49      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
50    }
51    private IScopeTreeLookupParameter<DoubleArray> EvaluatedValuesParameter {
52      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters[EvaluatedValuesParameterName]; }
53    }
54    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
55      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
56    }
57    public IValueLookupParameter<IRegressionProblemData> ProblemDataParameter {
58      get { return (IValueLookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
59    }
60    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
61      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
62    }
63    #endregion
64
65    public SymbolicRegressionPhenotypicDiversityAnalyzer(IEnumerable<ISolutionSimilarityCalculator> validSimilarityCalculators)
66      : base(validSimilarityCalculators) {
67      #region add parameters
68      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees."));
69      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes."));
70      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
71      Parameters.Add(new ValueLookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
72      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees."));
73      #endregion
74
75      UpdateCounterParameter.ActualName = "PhenotypicDiversityAnalyzerUpdateCounter";
76      DiversityResultName = "Phenotypic Diversity";
77    }
78
79    [StorableConstructor]
80    protected SymbolicRegressionPhenotypicDiversityAnalyzer(StorableConstructorFlag _) : base(_) {
81    }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new SymbolicRegressionPhenotypicDiversityAnalyzer(this, cloner);
85    }
86
87    protected SymbolicRegressionPhenotypicDiversityAnalyzer(SymbolicRegressionPhenotypicDiversityAnalyzer original, Cloner cloner)
88      : base(original, cloner) {
89    }
90
91    public override IOperation Apply() {
92      int updateInterval = UpdateIntervalParameter.Value.Value;
93      IntValue updateCounter = UpdateCounterParameter.ActualValue;
94
95      if (updateCounter == null) {
96        updateCounter = new IntValue(updateInterval);
97        UpdateCounterParameter.ActualValue = updateCounter;
98      }
99
100      if (updateCounter.Value == updateInterval) {
101        var trees = SymbolicExpressionTreeParameter.ActualValue;
102        var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
103        var ds = ProblemDataParameter.ActualValue.Dataset;
104        var rows = ProblemDataParameter.ActualValue.TrainingIndices;
105
106        var evaluatedValues = new ItemArray<DoubleArray>(trees.Select(t => new DoubleArray(interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray())));
107        EvaluatedValuesParameter.ActualValue = evaluatedValues;
108      }
109      return base.Apply();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.