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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
34 | [Item("SymbolicRegressionPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic distance between trees")]
|
---|
35 | [StorableType("BEF0E48C-CF40-453C-8110-C5A2F168679C")]
|
---|
36 | public class SymbolicRegressionPhenotypicDiversityAnalyzer : PopulationSimilarityAnalyzer,
|
---|
37 | ISymbolicDataAnalysisBoundedOperator, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
|
---|
38 | #region parameter names
|
---|
39 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
40 | private const string EvaluatedValuesParameterName = "EstimatedValues";
|
---|
41 | private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
42 | private const string ProblemDataParameterName = "ProblemData";
|
---|
43 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region parameter properties
|
---|
47 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
48 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
49 | }
|
---|
50 | private IScopeTreeLookupParameter<DoubleArray> EvaluatedValuesParameter {
|
---|
51 | get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters[EvaluatedValuesParameterName]; }
|
---|
52 | }
|
---|
53 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
|
---|
54 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
|
---|
55 | }
|
---|
56 | public IValueLookupParameter<IRegressionProblemData> ProblemDataParameter {
|
---|
57 | get { return (IValueLookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
58 | }
|
---|
59 | public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
60 | get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | public SymbolicRegressionPhenotypicDiversityAnalyzer(IEnumerable<ISolutionSimilarityCalculator> validSimilarityCalculators)
|
---|
65 | : base(validSimilarityCalculators) {
|
---|
66 | #region add parameters
|
---|
67 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees."));
|
---|
68 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes."));
|
---|
69 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
|
---|
70 | Parameters.Add(new ValueLookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
71 | 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."));
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | UpdateCounterParameter.ActualName = "PhenotypicDiversityAnalyzerUpdateCounter";
|
---|
75 | DiversityResultName = "Phenotypic Diversity";
|
---|
76 | }
|
---|
77 |
|
---|
78 | [StorableConstructor]
|
---|
79 | protected SymbolicRegressionPhenotypicDiversityAnalyzer(bool deserializing)
|
---|
80 | : base(deserializing) {
|
---|
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 | }
|
---|