Changeset 7075


Ignore:
Timestamp:
11/25/11 11:41:48 (10 years ago)
Author:
bburlacu
Message:

#1682: Updated crossovers

Location:
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionContextAwareCrossover.cs

    r7072 r7075  
    8888
    8989      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
    90       var rootSymbol = new ProgramRootSymbol();
    91       var startSymbol = new StartSymbol();
    92 
    9390      ISymbolicExpressionTreeNode selectedBranch = null;
    9491
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs

    r7035 r7075  
    3333
    3434namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    35   public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover where T : class, IDataAnalysisProblemData {
     35  public abstract class SymbolicDataAnalysisExpressionCrossover<T> : SymbolicExpressionTreeCrossover, ISymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3636    private const string RandomParameterName = "Random";
    3737    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
    3838    private const string ProblemDataParameterName = "ProblemData";
    3939    private const string EstimationLimitsParameterName = "EstimationLimits";
    40     private const string EvaluationPartitionParameterName = "EvaluationPartition";
     40    private const string EvaluatorParameterName = "Evaluator";
     41    private const string SymbolicDataAnalysisEvaluationPartitionParameterName = "EvaluationPartition";
    4142    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
    4243    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     
    5253      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
    5354    }
    54     public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
    55       get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
     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]; }
    5660    }
    5761    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
     
    8690      : base() {
    8791      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"));
    8893      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."));
     94      Parameters.Add(new ValueLookupParameter<IntRange>(SymbolicDataAnalysisEvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
    9095      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."));
    9196      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."));
    9299    }
    93100
     
    100107    /// <param name="startSymbol"></param>
    101108    /// <returns></returns>
    102     protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ProgramRootSymbol rootSymbol, StartSymbol startSymbol) {
    103       ISymbolicExpressionTreeNode rootNode = rootSymbol.CreateTreeNode();
     109    protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ISymbolicExpressionGrammar grammar) {
     110      SymbolicExpressionTree tree = new SymbolicExpressionTree();
     111      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
    104112      if (rootNode.HasLocalParameters)
    105113        rootNode.ResetLocalParameters(random);
    106       ISymbolicExpressionTreeNode startNode = startSymbol.CreateTreeNode();
     114      rootNode.SetGrammar((ISymbolicExpressionTreeGrammar)grammar);
     115      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     116      startNode.SetGrammar((ISymbolicExpressionTreeGrammar)grammar);
    107117      if (startNode.HasLocalParameters)
    108118        startNode.ResetLocalParameters(random);
    109119      rootNode.AddSubtree(startNode);
    110       startNode.AddSubtree(node);
     120
     121      rootNode.AddSubtree(startNode);
     122      if (startNode.Grammar.IsAllowedChildSymbol(startNode.Symbol, node.Symbol))
     123        startNode.AddSubtree(node);
     124      else
     125        throw new InvalidOperationException("The given node cannot be a child of the start symbol.");
    111126      return new SymbolicExpressionTree(rootNode);
    112127    }
     
    118133    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
    119134      IEnumerable<int> rows;
    120       int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
    121       int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
     135      int samplesStart = SymbolicDataAnalysisEvaluationPartitionParameter.ActualValue.Start;
     136      int samplesEnd = SymbolicDataAnalysisEvaluationPartitionParameter.ActualValue.End;
    122137      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
    123138      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs

    r7035 r7075  
    3737    [StorableConstructor]
    3838    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { }
    39     private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner)
    40       : base(original, cloner) {
    41     }
    42     public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover()
    43       : base() {
    44     }
    45     public override IDeepCloneable Clone(Cloner cloner) {
    46       return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner);
    47     }
     39    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner)
     40      : base(original, cloner) { }
     41    public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() : base() { }
     42    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); }
     43 
    4844    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4945      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
    5046      IEnumerable<int> rows = GenerateRowsToEvaluate();
    5147      T problemData = ProblemDataParameter.ActualValue;
    52       return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
     48      var grammar = parent0.Root.Grammar;
     49      return Cross(random, parent0, parent1, interpreter, problemData,
     50                   rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
    5351    }
    5452
     
    6058    /// choose the second crosspoint via a random weighted selection procedure.
    6159    /// </summary>
    62     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
     60    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 
    6361                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
    6462      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
     
    8987
    9088      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
    91       var rootSymbol = new ProgramRootSymbol();
    92       var startSymbol = new StartSymbol();
    93       var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
     89      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
    9490      IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
    9591      double min0 = estimatedValues0.Min();
     
    9894      List<double> weights = new List<double>();
    9995      foreach (var node in allowedBranches) {
    100         var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
     96        var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
    10197        IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
    10298        double min1 = estimatedValues1.Min();
     
    109105
    110106      ISymbolicExpressionTreeNode selectedBranch = SelectRandomBranch(random, allowedBranches, weights);
     107      swap(crossoverPoint0, selectedBranch);
    111108
    112       // perform the actual swap
    113       if (crossoverPoint0.Child != null) {
     109      return parent0;
     110    }
     111
     112    private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
     113      if (crossoverPoint.Child != null) {
    114114        // manipulate the tree of parent0 in place
    115115        // replace the branch in tree0 with the selected branch from tree1
    116         crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
     116        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
    117117        if (selectedBranch != null) {
    118           crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
     118          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
    119119        }
    120120      } else {
    121121        // child is null (additional child should be added under the parent)
    122122        if (selectedBranch != null) {
    123           crossoverPoint0.Parent.AddSubtree(selectedBranch);
     123          crossoverPoint.Parent.AddSubtree(selectedBranch);
    124124        }
    125125      }
    126 
    127       return parent0;
    128126    }
    129127
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs

    r7036 r7075  
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3333
    34   [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on behavioral similarity")]
     34  [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on semantic similarity.")]
    3535  public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
    3636  {
    3737    [StorableConstructor]
    3838    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
    39     private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner)
    40       : base(original, cloner) {
    41     }
    42     public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover()
    43       : base() {
    44     }
    45     public override IDeepCloneable Clone(Cloner cloner) {
    46       return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner);
    47     }
     39    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner) : base(original, cloner) { }
     40    public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() : base() { }
     41    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); }
     42 
    4843    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4944      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
     
    5651    /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
    5752    /// </summary>
    58     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
     53    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 
    5954                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
    6055      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
     
    8277        return parent0;
    8378
    84      var dataset = problemData.Dataset;
     79      var dataset = problemData.Dataset;
    8580
    8681      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
    87       var rootSymbol = new ProgramRootSymbol();
    88       var startSymbol = new StartSymbol();
    89       var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
     82      //var rootSymbol = new ProgramRootSymbol();
     83      //var startSymbol = new StartSymbol();
     84      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
    9085      IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
    9186
     
    9489      // pick the first node that fulfills the semantic similarity conditions
    9590      foreach (var node in allowedBranches) {
    96         var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
     91        var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
    9792        IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
    9893
     
    109104
    110105      // perform the actual swap
    111       if (crossoverPoint0.Child != null) {
     106      swap(crossoverPoint0, selectedBranch);
     107
     108      return parent0;
     109    }
     110
     111    private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
     112      // perform the actual swap
     113      if (crossoverPoint.Child != null) {
    112114        // manipulate the tree of parent0 in place
    113115        // replace the branch in tree0 with the selected branch from tree1
    114         crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
     116        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
    115117        if (selectedBranch != null) {
    116           crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
     118          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
    117119        }
    118120      } else {
    119121        // child is null (additional child should be added under the parent)
    120122        if (selectedBranch != null) {
    121           crossoverPoint0.Parent.AddSubtree(selectedBranch);
     123          crossoverPoint.Parent.AddSubtree(selectedBranch);
    122124        }
    123125      }
    124       return parent0;
    125126    }
     127
    126128  }
    127129}
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionCrossover.cs

    r7072 r7075  
    2727
    2828namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    29   public interface ISymbolicDataAnalysisExpressionCrossover : INamedItem {
     29  public interface ISymbolicDataAnalysisExpressionCrossover<T> : IOperator {
    3030    IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { get; }
    3131  }
Note: See TracChangeset for help on using the changeset viewer.