Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/15/12 19:09:09 (12 years ago)
Author:
gkronber
Message:

#1847 added operator for replacing branches with semantically similar branches

Location:
branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves
Files:
9 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMoveGenerator.cs

    r7802 r7832  
    3030
    3131namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    32   [Item("ChangeNodeTypeMoveGenerator", "Operator that generates moves for changing the symbol of a single symbolic expression tree node.")]
     32  [Item("ChangeNodeTypeExhaustiveMoveGenerator", "Operator that generates moves for changing the symbol of a single symbolic expression tree node.")]
    3333  [StorableClass]
    34   public class ChangeNodeTypeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IMoveGenerator, IStochasticOperator {
     34  public class ChangeNodeTypeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IExhaustiveMoveGenerator, IStochasticOperator {
    3535    public override bool CanChangeName {
    3636      get { return false; }
     
    3939      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    4040    }
    41     public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    42       get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     41    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     42      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    4343    }
    4444    public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
     
    5555      : base() {
    5656      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
    57       Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
     57      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
    5858      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
    5959      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
     
    6666
    6767    public override IOperation Apply() {
    68       SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
     68      var tree = SymbolicExpressionTreeParameter.ActualValue;
    6969      var moves = GenerateMoves(tree);
    7070      string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
     
    8282    }
    8383
    84     public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(SymbolicExpressionTree tree) {
     84    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree) {
    8585      var g = tree.Root.Grammar;
    8686      return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
  • branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMoveMaker.cs

    r7802 r7832  
    4545      get { return (ILookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; }
    4646    }
    47     public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    48       get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     47    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     48      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    4949    }
    5050
     
    5757      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The move to evaluate."));
    5858      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The relative quality of the move."));
    59       Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree on which the move should be applied."));
     59      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree on which the move should be applied."));
    6060    }
    6161
     
    6666    public override IOperation Apply() {
    6767      ChangeNodeTypeMove move = ChangeNodeTypeMoveParameter.ActualValue;
    68       SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
     68      var tree = SymbolicExpressionTreeParameter.ActualValue;
    6969      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
    7070      DoubleValue quality = QualityParameter.ActualValue;
  • branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMultiMoveGenerator.cs

    r7802 r7832  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     
    4645      set { SampleSizeParameter.Value = value; }
    4746    }
    48     public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    49       get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     47    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     48      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    5049    }
    5150    public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
     
    6463      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
    6564
    66       Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
     65      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
    6766      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
    6867      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
     
    7574
    7675    public override IOperation Apply() {
    77       SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
    78       var moves = ChangeNodeTypeMoveGenerator.GenerateMoves(tree).ToArray();
     76      var tree = SymbolicExpressionTreeParameter.ActualValue;
     77
     78
    7979      string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
    8080      var moveScopes = new List<Scope>();
     
    8282      int n = SampleSizeParameter.ActualValue.Value;
    8383
    84       var rand = Enumerable.Range(0, moves.Length).Select(i => random.NextDouble()).ToArray();
    85       Array.Sort(rand, moves);
    86       n = Math.Min(n, moves.Length);
     84      var moves = GenerateMoves(tree, random, n);
    8785
    88       foreach (var m in moves.Take(n)) {
     86      foreach (var m in moves) {
    8987        if (m.NewChild.HasLocalParameters)
    9088          m.NewChild.ResetLocalParameters(random);
     
    9694      return base.Apply();
    9795    }
     96
     97    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree, IRandom random, int n) {
     98      int count = 0;
     99      var g = tree.Root.Grammar;
     100      var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
     101                              from i in Enumerable.Range(0, parent.SubtreeCount)
     102                              from s in g.GetAllowedChildSymbols(parent.Symbol, i)
     103                              where s.Enabled
     104                              where s.InitialFrequency > 0.0
     105                              let currentChild = parent.GetSubtree(i)
     106                              where s.MinimumArity >= currentChild.SubtreeCount
     107                              where s.MaximumArity <= currentChild.SubtreeCount
     108                              where (from j in Enumerable.Range(0, currentChild.SubtreeCount)
     109                                     let childOfChild = currentChild.GetSubtree(j)
     110                                     select g.IsAllowedChildSymbol(s, childOfChild.Symbol, j)).All(p => p)
     111                              select new { parent, i, s }).ToArray();
     112      if (possibleChildren.Length < n) n = possibleChildren.Length;
     113      while (count < n) {
     114        var selected = possibleChildren[random.Next(possibleChildren.Length)];
     115        yield return new ChangeNodeTypeMove(tree, selected.parent, selected.i, selected.s.CreateTreeNode());
     116        count++;
     117      }
     118    }
    98119  }
    99120}
  • branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ShakeMoveGenerator.cs

    r7802 r7832  
    3030
    3131namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    32   [Item("ShakeMoveGenerator", "Operator that generates moves for shaking a single symbolic expression tree node.")]
     32  [Item("ShakeExhaustiveMoveGenerator", "Operator that generates moves for shaking a single symbolic expression tree node.")]
    3333  [StorableClass]
    34   public class ShakeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IMoveGenerator, IStochasticOperator {
     34  public class ShakeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IExhaustiveMoveGenerator, IStochasticOperator {
    3535    public override bool CanChangeName {
    3636      get { return false; }
     
    3939      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    4040    }
    41     public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    42       get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     41    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     42      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    4343    }
    4444    public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
     
    5555      : base() {
    5656      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
    57       Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
     57      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
    5858      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
    5959      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
     
    6666
    6767    public override IOperation Apply() {
    68       SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
     68      var tree = SymbolicExpressionTreeParameter.ActualValue;
    6969      var moves = GenerateMoves(tree);
    7070      string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
     
    8181    }
    8282
    83     public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(SymbolicExpressionTree tree) {
    84       var g = tree.Root.Grammar;
     83    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree) {
    8584      return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
    8685              from i in Enumerable.Range(0, parent.SubtreeCount)
  • branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ShakeMultiMoveGenerator.cs

    r7802 r7832  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     
    4645      set { SampleSizeParameter.Value = value; }
    4746    }
    48     public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    49       get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     47    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     48      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
    5049    }
    5150    public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
     
    6463      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
    6564
    66       Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
     65      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
    6766      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
    6867      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
     
    7574
    7675    public override IOperation Apply() {
    77       SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
    78       var moves = ChangeNodeTypeMoveGenerator.GenerateMoves(tree).ToArray();
     76      var tree = SymbolicExpressionTreeParameter.ActualValue;
     77      var random = RandomParameter.ActualValue;
     78      int n = SampleSizeParameter.ActualValue.Value;
     79      var moves = ChangeNodeTypeMultiMoveGenerator.GenerateMoves(tree, random, n);
    7980      string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
    8081      var moveScopes = new List<Scope>();
    81       var random = RandomParameter.ActualValue;
    82       int n = SampleSizeParameter.ActualValue.Value;
    8382
    84       var rand = Enumerable.Range(0, moves.Length).Select(i => random.NextDouble()).ToArray();
    85       Array.Sort(rand, moves);
    86       n = Math.Min(n, moves.Length);
    87 
    88       foreach (var m in moves.Take(n)) {
     83      foreach (var m in moves) {
    8984        m.NewChild.ShakeLocalParameters(random, 1);
    9085        var moveScope = new Scope(moveScopes.Count.ToString());
     
    9590      return base.Apply();
    9691    }
     92
     93    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree, IRandom random, int n) {
     94      int count = 0;
     95      var g = tree.Root.Grammar;
     96      var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
     97                              from i in Enumerable.Range(0, parent.SubtreeCount)
     98                              let currentChild = parent.GetSubtree(i)
     99                              where currentChild.HasLocalParameters
     100                              select new { parent, i, currentChild }).ToArray();
     101      if (possibleChildren.Length < n) n = possibleChildren.Length;
     102      while (count < n) {
     103        var selected = possibleChildren[random.Next(possibleChildren.Length)];
     104        yield return new ChangeNodeTypeMove(tree, selected.parent, selected.i, (ISymbolicExpressionTreeNode)selected.currentChild.Clone());
     105        count++;
     106      }
     107    }
    97108  }
    98109}
Note: See TracChangeset for help on using the changeset viewer.