Free cookie consent management tool by TermsFeed Policy Generator

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

#1682: Updated crossovers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.