Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7481 was 7481, checked in by mkommend, 13 years ago

#1682: Corrected gp-crossover code.

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