Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs @ 7506

Last change on this file since 7506 was 7506, checked in by mkommend, 12 years ago

#1682: Integrated new gp crossovers into the trunk and corrected the parameter wiring.

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