Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionCrossover.cs @ 7072

Last change on this file since 7072 was 7072, checked in by bburlacu, 12 years ago

#1682: New versions of crossover (work-in-progress).

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Common;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
35  public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover where T : class, IDataAnalysisProblemData {
36    private const string RandomParameterName = "Random";
37    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
38    private const string ProblemDataParameterName = "ProblemData";
39    private const string EstimationLimitsParameterName = "EstimationLimits";
40    private const string EvaluationPartitionParameterName = "EvaluationPartition";
41    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
42    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
43    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
44
45    public override bool CanChangeName { get { return false; } }
46
47    #region Parameter properties
48    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
49      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
50    }
51    public IValueLookupParameter<T> ProblemDataParameter {
52      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
53    }
54    public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
55      get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
56    }
57    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
58      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
59    }
60    public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
61      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
62    }
63    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
64      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
65    }
66    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
67      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
68    }
69    #endregion
70
71    #region Properties
72    public IntValue MaximumSymbolicExpressionTreeLength {
73      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
74    }
75    public IntValue MaximumSymbolicExpressionTreeDepth {
76      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
77    }
78    #endregion
79
80    [StorableConstructor]
81    protected SymbolicDataAnalysisExpressionCrossover(bool deserializing) : base(deserializing) { }
82    protected SymbolicDataAnalysisExpressionCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
83      : base(original, cloner) {
84    }
85    public SymbolicDataAnalysisExpressionCrossover()
86      : base() {
87      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
88      Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
89      Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
90      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."));
91      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."));
92    }
93
94    /// <summary>
95    /// Creates a SymbolicExpressionTreeNode reusing the root and start symbols (since they are expensive to create).
96    /// </summary>
97    /// <param name="random"></param>
98    /// <param name="node"></param>
99    /// <param name="rootSymbol"></param>
100    /// <param name="startSymbol"></param>
101    /// <returns></returns>
102    protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ProgramRootSymbol rootSymbol, StartSymbol startSymbol) {
103      ISymbolicExpressionTreeNode rootNode = rootSymbol.CreateTreeNode();
104      if (rootNode.HasLocalParameters)
105        rootNode.ResetLocalParameters(random);
106      ISymbolicExpressionTreeNode startNode = startSymbol.CreateTreeNode();
107      if (startNode.HasLocalParameters)
108        startNode.ResetLocalParameters(random);
109      rootNode.AddSubtree(startNode);
110      startNode.AddSubtree(node);
111      return new SymbolicExpressionTree(rootNode);
112    }
113
114    protected IEnumerable<int> GenerateRowsToEvaluate() {
115      return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
116    }
117
118    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
119      IEnumerable<int> rows;
120      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
121      int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
122      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
123      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
124
125      if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
126
127      if (percentageOfRows.IsAlmost(1.0))
128        rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
129      else {
130        int seed = RandomParameter.ActualValue.Next();
131        int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
132        if (count == 0) count = 1;
133        rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
134      }
135
136      return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.