Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1682: Overhauled the crossover operators, fixed bug in the DeterministicBestCrossover.

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