Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SymbolicExpressionTreeDiversityAnalyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPhenotypicDiversityAnalyzer.cs @ 12969

Last change on this file since 12969 was 12086, checked in by bburlacu, 10 years ago

#2326: Moved phenotypic diversity analyzers one level up (since they can be applied to both single- and multiobjective problems). Added wiring in the multiobjective problems. Changed base class to SolutionSimilarityCalculator and adjusted analyzers.

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