Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7497


Ignore:
Timestamp:
02/21/12 11:08:58 (12 years ago)
Author:
bburlacu
Message:

#1682:

  • Added names and detailed descriptions to all crossover operators
  • Minor code improvements (selection of allowed branches and cutpoints)
Location:
branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/MultiSymbolicDataAnalysisExpressionCrossover.cs

    r7489 r7497  
    108108      Operators = checkedItemList;
    109109      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeCrossover>>(Operators.CheckedItems));
     110      Name = "MultiSymbolicDataAnalysisExpressionCrossover";
    110111    }
    111112
  • branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs

    r7490 r7497  
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3333
    34   [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range.")]
     34  [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range. The range parameter controls the crossover behavior:\n" +
     35                                     "- HighLevel (upper 25% of the tree)\n" +
     36                                     "- Standard (mid 50% of the tree)\n" +
     37                                     "- LowLevel (lower 25% of the tree)")]
    3538  public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> :
    3639    SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
     
    6063      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Standard)).AsReadOnly());
    6164      DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.LowLevel)).AsReadOnly());
     65      Name = "DepthConstrainedCrossover";
    6266    }
    6367    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
     
    8387    /// <returns></returns>
    8488    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength, string mode) {
    85       int depth = parent0.Root.GetDepth() - 1; // subtract 1 because the tree levels are counted from 0
     89      int depth = parent0.Root.GetDepth() - 1; // substract 1 because the tree levels are counted from 0
    8690      var depthRange = new IntRange();
    8791      const int depthOffset = 2; // skip the first 2 levels (root + startNode)
     
    118122
    119123      var allowedBranches = (from s in GetNodesAtDepth(parent1, depthRange)
    120                              where
    121                                crossoverPoint0.IsMatchingPointType(s) &&
    122                                s.GetDepth() + level <= maxDepth &&
    123                                s.GetLength() + length <= maxLength
     124                             where s.GetDepth() + level <= maxDepth
     125                             where s.GetLength() + length <= maxLength
     126                             where crossoverPoint0.IsMatchingPointType(s)
    124127                             select s).ToList();
    125128      if (allowedBranches.Count == 0) return parent0;
    126129      var selectedBranch = allowedBranches.SelectRandom(random);
    127       swap(crossoverPoint0, selectedBranch);
     130      Swap(crossoverPoint0, selectedBranch);
    128131      return parent0;
    129132    }
     
    136139             where depth <= depthRange.End
    137140             select node;
    138 
    139       //var list = new List<Tuple<ISymbolicExpressionTreeNode, int>> { new Tuple<ISymbolicExpressionTreeNode, int>(root, 0) };
    140       //int offset = 0;
    141       //int level = 0;
    142       //while (level < range.End) {
    143       //  ++level;
    144       //  int count = list.Count;
    145       //  for (int i = offset; i != count; ++i) {
    146       //    if (list[i].Item1.Subtrees.Any())
    147       //      list.AddRange(from s in list[i].Item1.Subtrees select new Tuple<ISymbolicExpressionTreeNode, int>(s, level));
    148       //  }
    149       //  offset = count;
    150       //}
    151       //// taking advantage of the fact that the list is already sorted by level
    152       //for (int i = list.Count - 1; i >= 0; --i) {
    153       //  if (list[i].Item2 >= range.Start)
    154       //    yield return list[i].Item1;
    155       //  else break;
    156       //}
    157     }
    158 
    159     private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
    160       // perform the actual swap
    161       if (crossoverPoint.Child != null) {
    162         // manipulate the tree of parent0 in place
    163         // replace the branch in tree0 with the selected branch from tree1
    164         crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
    165         if (selectedBranch != null) {
    166           crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
    167         }
    168       } else {
    169         // child is null (additional child should be added under the parent)
    170         if (selectedBranch != null) {
    171           crossoverPoint.Parent.AddSubtree(selectedBranch);
    172         }
    173       }
    174141    }
    175142  }
  • branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs

    r7481 r7497  
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3131
    32   [Item("DeterministicBestCrossover", "An operator which performs subtree swapping in a deterministic way, by choosing the best subtree to be swapped in a certain position.")]
     32  [Item("DeterministicBestCrossover", "An operator which performs subtree swapping by choosing the best subtree to be swapped in a certain position:\n" +
     33                                      "- Take two parent individuals P0 and P1\n" +
     34                                      "- Randomly choose a crossover point C from P0\n" +
     35                                      "- Test all nodes from P1 to determine the one that produces the best child when inserted at place C in P0")]
    3336  public sealed class SymbolicDataAnalysisExpressionDeterministicBestCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3437    [StorableConstructor]
     
    6265      var crossoverPoints0 = new List<CutPoint>();
    6366      parent0.Root.ForEachNodePostfix((n) => {
    64         if (n.Subtrees.Any() && n != parent0.Root)
    65           crossoverPoints0.AddRange(from s in n.Subtrees select new CutPoint(n, s));
     67        if (n.Parent != null && n.Parent != parent0.Root)
     68          crossoverPoints0.Add(new CutPoint(n.Parent, n));
    6669      });
    6770      CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random);
     
    7174      var allowedBranches = new List<ISymbolicExpressionTreeNode>();
    7275      parent1.Root.ForEachNodePostfix((n) => {
    73         if (n.Subtrees.Any() && n != parent1.Root)
    74           allowedBranches.AddRange(from s in n.Subtrees
    75                                    where crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength
    76                                    select s);
     76        if (n.Parent != null && n.Parent != parent1.Root) {
     77          if (n.GetDepth() + level <= maxDepth && n.GetLength() + length <= maxLength && crossoverPoint0.IsMatchingPointType(n))
     78            allowedBranches.Add(n);
     79        }
    7780      });
    7881
     
    8992      foreach (var node in allowedBranches) {
    9093        var parent = node.Parent;
    91         swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent
     94        Swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent
    9295        double quality = evaluator.Evaluate(context, parent0, problemData, rows);
    93         swap(crossoverPoint0, originalChild); // swap the child back (so that the next swap will not affect the currently swapped node from parent1)
     96        Swap(crossoverPoint0, originalChild); // swap the child back (so that the next swap will not affect the currently swapped node from parent1)
    9497        nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality));
    9598        node.Parent = parent; // restore correct parent
     
    108111
    109112      // swap the node that would create the best offspring
    110       swap(crossoverPoint0, selectedBranch);
    111 
     113      Swap(crossoverPoint0, selectedBranch);
    112114      return parent0;
    113     }
    114 
    115     private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
    116       if (crossoverPoint.Child != null) {
    117         // manipulate the tree of parent0 in place
    118         // replace the branch in tree0 with the selected branch from tree1
    119         crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
    120         if (selectedBranch != null) {
    121           crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
    122         }
    123       } else {
    124         // child is null (additional child should be added under the parent)
    125         if (selectedBranch != null) {
    126           crossoverPoint.Parent.AddSubtree(selectedBranch);
    127         }
    128       }
    129115    }
    130116  }
Note: See TracChangeset for help on using the changeset viewer.