Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs @ 13977

Last change on this file since 13977 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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