Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs @ 14593

Last change on this file since 14593 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  [StorableClass]
35  public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover, ISymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
36    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
37    private const string ProblemDataParameterName = "ProblemData";
38    private const string EvaluatorParameterName = "Evaluator";
39    private const string EvaluationPartitionParameterName = "EvaluationPartition";
40    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
41    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
42    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
43
44    #region Parameter properties
45    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
46      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
47    }
48    public IValueLookupParameter<T> ProblemDataParameter {
49      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
50    }
51    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
52      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
53    }
54    public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
55      get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
56    }
57    public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
58      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
59    }
60    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
61      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
62    }
63    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
64      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
65    }
66    #endregion
67
68    #region Properties
69    public IntValue MaximumSymbolicExpressionTreeLength {
70      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
71    }
72    public IntValue MaximumSymbolicExpressionTreeDepth {
73      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
74    }
75    #endregion
76
77    [StorableConstructor]
78    protected SymbolicDataAnalysisExpressionCrossover(bool deserializing) : base(deserializing) { }
79    protected SymbolicDataAnalysisExpressionCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
80      : base(original, cloner) {
81    }
82    public SymbolicDataAnalysisExpressionCrossover()
83      : base() {
84      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
85      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(EvaluatorParameterName, "The single objective solution evaluator"));
86      Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
87      Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
88      Parameters.Add(new ValueLookupParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index."));
89      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximum tree depth."));
90      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximum tree length."));
91
92      EvaluatorParameter.Hidden = true;
93      EvaluationPartitionParameter.Hidden = true;
94      SymbolicDataAnalysisTreeInterpreterParameter.Hidden = true;
95      ProblemDataParameter.Hidden = true;
96      RelativeNumberOfEvaluatedSamplesParameter.Hidden = true;
97    }
98
99    /// <summary>
100    /// Creates a SymbolicExpressionTreeNode reusing the root and start symbols (since they are expensive to create).
101    /// </summary>
102    /// <param name="random"></param>
103    /// <param name="node"></param>
104    /// <param name="rootSymbol"></param>
105    /// <param name="startSymbol"></param>
106    /// <returns></returns>
107    protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ISymbol rootSymbol, ISymbol startSymbol) {
108      var rootNode = new SymbolicExpressionTreeTopLevelNode(rootSymbol);
109      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
110
111      var startNode = new SymbolicExpressionTreeTopLevelNode(startSymbol);
112      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
113
114      startNode.AddSubtree(node);
115      rootNode.AddSubtree(startNode);
116
117      return new SymbolicExpressionTree(rootNode);
118    }
119
120    protected IEnumerable<int> GenerateRowsToEvaluate() {
121      return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
122    }
123
124    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
125      IEnumerable<int> rows;
126      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
127      int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
128      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
129      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
130
131      if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
132
133      if (percentageOfRows.IsAlmost(1.0))
134        rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
135      else {
136        int seed = RandomParameter.ActualValue.Next();
137        int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
138        if (count == 0) count = 1;
139        rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
140      }
141
142      return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
143    }
144
145    protected static void Swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
146      if (crossoverPoint.Child != null) {
147        // manipulate the tree of parent0 in place
148        // replace the branch in tree0 with the selected branch from tree1
149        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
150        if (selectedBranch != null) {
151          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
152        }
153      } else {
154        // child is null (additional child should be added under the parent)
155        if (selectedBranch != null) {
156          crossoverPoint.Parent.AddSubtree(selectedBranch);
157        }
158      }
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.