Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/23/12 16:05:57 (12 years ago)
Author:
mkommend
Message:

#1682: Integrated new gp crossovers into the trunk and corrected the parameter wiring.

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers

    • Property svn:ignore set to
      *.bak
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs

    r7259 r7506  
    3636  /// until a valid configuration is found.
    3737  /// </summary> 
    38   [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")]
     38  [Item("SubtreeSwappingCrossover", "An operator which performs subtree swapping crossover.")]
    3939  [StorableClass]
    40   public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
     40  public class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
    4141    private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
    4242    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
    4343    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     44
    4445    #region Parameter Properties
    4546    public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
     
    6566    #endregion
    6667    [StorableConstructor]
    67     private SubtreeCrossover(bool deserializing) : base(deserializing) { }
    68     private SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
     68    protected SubtreeCrossover(bool deserializing) : base(deserializing) { }
     69    protected SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
    6970    public SubtreeCrossover()
    7071      : base() {
     
    7879    }
    7980
    80     protected override ISymbolicExpressionTree Cross(IRandom random,
     81    public override ISymbolicExpressionTree Crossover(IRandom random,
    8182      ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    8283      return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value,
     
    9495      // calculate the max length and depth that the inserted branch can have
    9596      int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength);
    96       int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0.Parent);
     97      int maxInsertedBranchDepth = maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent);
    9798
    9899      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
    99100      parent1.Root.ForEachNodePostfix((n) => {
    100101        if (n.GetLength() <= maxInsertedBranchLength &&
    101             n.GetDepth() <= maxInsertedBranchDepth &&
    102             IsMatchingPointType(crossoverPoint0, n))
     102            n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n))
    103103          allowedBranches.Add(n);
    104104      });
    105105      // empty branch
    106       if (IsMatchingPointType(crossoverPoint0, null)) allowedBranches.Add(null);
     106      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    107107
    108108      if (allowedBranches.Count == 0) {
     
    128128    }
    129129
    130     private static bool IsMatchingPointType(CutPoint cutPoint, ISymbolicExpressionTreeNode newChild) {
    131       var parent = cutPoint.Parent;
    132       if (newChild == null) {
    133         // make sure that one subtree can be removed and that only the last subtree is removed
    134         return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&
    135           cutPoint.ChildIndex == parent.SubtreeCount - 1;
    136       } else {
    137         // check syntax constraints of direct parent - child relation
    138         if (!parent.Grammar.ContainsSymbol(newChild.Symbol) ||
    139             !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, cutPoint.ChildIndex)) return false;
    140 
    141         bool result = true;
    142         // check point type for the whole branch
    143         newChild.ForEachNodePostfix((n) => {
    144           result =
    145             result &&
    146             parent.Grammar.ContainsSymbol(n.Symbol) &&
    147             n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
    148             n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
    149         });
    150         return result;
    151       }
    152     }
    153 
    154130    private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out CutPoint crossoverPoint) {
    155131      if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
     
    157133      List<CutPoint> leafCrossoverPoints = new List<CutPoint>();
    158134      parent0.Root.ForEachNodePostfix((n) => {
    159         if (n.Subtrees.Any() && n != parent0.Root) {
     135        if (n.SubtreeCount > 0 && n != parent0.Root) {
    160136          foreach (var child in n.Subtrees) {
    161137            if (child.GetLength() <= maxBranchLength &&
    162138                child.GetDepth() <= maxBranchDepth) {
    163               if (child.Subtrees.Any())
     139              if (child.SubtreeCount > 0)
    164140                internalCrossoverPoints.Add(new CutPoint(n, child));
    165141              else
     
    167143            }
    168144          }
     145
    169146          // add one additional extension point if the number of sub trees for the symbol is not full
    170147          if (n.SubtreeCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {
     
    173150          }
    174151        }
    175       });
     152      }
     153    );
    176154
    177155      if (random.NextDouble() < internalNodeProbability) {
     
    200178        // select internal node if possible
    201179        allowedInternalBranches = (from branch in branches
    202                                    where branch != null && branch.Subtrees.Any()
     180                                   where branch != null && branch.SubtreeCount > 0
    203181                                   select branch).ToList();
    204182        if (allowedInternalBranches.Count > 0) {
     
    207185          // no internal nodes allowed => select leaf nodes
    208186          allowedLeafBranches = (from branch in branches
    209                                  where branch == null || !branch.Subtrees.Any()
     187                                 where branch == null || branch.SubtreeCount == 0
    210188                                 select branch).ToList();
    211189          return allowedLeafBranches.SelectRandom(random);
     
    214192        // select leaf node if possible
    215193        allowedLeafBranches = (from branch in branches
    216                                where branch == null || !branch.Subtrees.Any()
     194                               where branch == null || branch.SubtreeCount == 0
    217195                               select branch).ToList();
    218196        if (allowedLeafBranches.Count > 0) {
     
    220198        } else {
    221199          allowedInternalBranches = (from branch in branches
    222                                      where branch != null && branch.Subtrees.Any()
     200                                     where branch != null && branch.SubtreeCount > 0
    223201                                     select branch).ToList();
    224202          return allowedInternalBranches.SelectRandom(random);
     
    226204      }
    227205    }
    228 
    229     private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
    230       if (root == point) return 0;
    231       foreach (var subtree in root.Subtrees) {
    232         int branchLevel = GetBranchLevel(subtree, point);
    233         if (branchLevel < int.MaxValue) return 1 + branchLevel;
    234       }
    235       return int.MaxValue;
    236     }
    237206  }
    238207}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs

    r7259 r7506  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
    25 using HeuristicLab.Data;
    2625using HeuristicLab.Parameters;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    6665        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
    6766
    68       ISymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]);
     67      ISymbolicExpressionTree result = Crossover(Random, Parents[0], Parents[1]);
    6968
    7069      Child = result;
     
    7271    }
    7372
    74     protected abstract ISymbolicExpressionTree Cross(IRandom random,
    75       ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
     73    public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
    7674  }
    7775}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/CutPoint.cs

    r7259 r7506  
    2222
    2323namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    24   internal class CutPoint {
     24  public class CutPoint {
    2525    public ISymbolicExpressionTreeNode Parent { get; set; }
    2626    public ISymbolicExpressionTreeNode Child { get; set; }
     
    3939      this.Child = null;
    4040    }
     41
     42    public bool IsMatchingPointType(ISymbolicExpressionTreeNode newChild) {
     43      var parent = this.Parent;
     44      if (newChild == null) {
     45        // make sure that one subtree can be removed and that only the last subtree is removed
     46        return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&
     47          this.ChildIndex == parent.SubtreeCount - 1;
     48      } else {
     49        // check syntax constraints of direct parent - child relation
     50        if (!parent.Grammar.ContainsSymbol(newChild.Symbol) ||
     51            !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, this.ChildIndex))
     52          return false;
     53
     54        bool result = true;
     55        // check point type for the whole branch
     56        newChild.ForEachNodePostfix((n) => {
     57          result =
     58            result &&
     59            parent.Grammar.ContainsSymbol(n.Symbol) &&
     60            n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
     61            n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
     62        });
     63        return result;
     64      }
     65    }
    4166  }
    4267}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs

    r7259 r7506  
    125125    }
    126126
     127    public int GetBranchLevel(ISymbolicExpressionTreeNode child) {
     128      return GetBranchLevel(this, child);
     129    }
     130
     131    private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
     132      if (root == point)
     133        return 0;
     134      foreach (var subtree in root.Subtrees) {
     135        int branchLevel = GetBranchLevel(subtree, point);
     136        if (branchLevel < int.MaxValue)
     137          return 1 + branchLevel;
     138      }
     139      return int.MaxValue;
     140    }
     141
    127142    public virtual void ResetLocalParameters(IRandom random) { }
    128143    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
Note: See TracChangeset for help on using the changeset viewer.