Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1682: Implemented the MultiSymbolicDataAnalysisExpressionTreeCrossover

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