Changeset 7832 for branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves
- Timestamp:
- 05/15/12 19:09:09 (12 years ago)
- 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 30 30 31 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 32 [Item("ChangeNodeType MoveGenerator", "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.")] 33 33 [StorableClass] 34 public class ChangeNodeTypeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, I MoveGenerator, IStochasticOperator {34 public class ChangeNodeTypeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IExhaustiveMoveGenerator, IStochasticOperator { 35 35 public override bool CanChangeName { 36 36 get { return false; } … … 39 39 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 40 40 } 41 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {42 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }41 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 42 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } 43 43 } 44 44 public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter { … … 55 55 : base() { 56 56 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.")); 58 58 Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes.")); 59 59 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes.")); … … 66 66 67 67 public override IOperation Apply() { 68 SymbolicExpressionTreetree = SymbolicExpressionTreeParameter.ActualValue;68 var tree = SymbolicExpressionTreeParameter.ActualValue; 69 69 var moves = GenerateMoves(tree); 70 70 string moveParameterName = ChangeNodeTypeMoveParameter.ActualName; … … 82 82 } 83 83 84 public static IEnumerable<ChangeNodeTypeMove> GenerateMoves( SymbolicExpressionTree tree) {84 public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree) { 85 85 var g = tree.Root.Grammar; 86 86 return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix() -
branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMoveMaker.cs
r7802 r7832 45 45 get { return (ILookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; } 46 46 } 47 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {48 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }47 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } 49 49 } 50 50 … … 57 57 Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The move to evaluate.")); 58 58 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.")); 60 60 } 61 61 … … 66 66 public override IOperation Apply() { 67 67 ChangeNodeTypeMove move = ChangeNodeTypeMoveParameter.ActualValue; 68 SymbolicExpressionTreetree = SymbolicExpressionTreeParameter.ActualValue;68 var tree = SymbolicExpressionTreeParameter.ActualValue; 69 69 DoubleValue moveQuality = MoveQualityParameter.ActualValue; 70 70 DoubleValue quality = QualityParameter.ActualValue; -
branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMultiMoveGenerator.cs
r7802 r7832 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Linq; … … 46 45 set { SampleSizeParameter.Value = value; } 47 46 } 48 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {49 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }47 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } 50 49 } 51 50 public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter { … … 64 63 Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate.")); 65 64 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.")); 67 66 Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes.")); 68 67 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes.")); … … 75 74 76 75 public override IOperation Apply() { 77 SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue; 78 var moves = ChangeNodeTypeMoveGenerator.GenerateMoves(tree).ToArray(); 76 var tree = SymbolicExpressionTreeParameter.ActualValue; 77 78 79 79 string moveParameterName = ChangeNodeTypeMoveParameter.ActualName; 80 80 var moveScopes = new List<Scope>(); … … 82 82 int n = SampleSizeParameter.ActualValue.Value; 83 83 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); 87 85 88 foreach (var m in moves .Take(n)) {86 foreach (var m in moves) { 89 87 if (m.NewChild.HasLocalParameters) 90 88 m.NewChild.ResetLocalParameters(random); … … 96 94 return base.Apply(); 97 95 } 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 } 98 119 } 99 120 } -
branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ShakeMoveGenerator.cs
r7802 r7832 30 30 31 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 32 [Item("Shake MoveGenerator", "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.")] 33 33 [StorableClass] 34 public class ShakeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, I MoveGenerator, IStochasticOperator {34 public class ShakeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IExhaustiveMoveGenerator, IStochasticOperator { 35 35 public override bool CanChangeName { 36 36 get { return false; } … … 39 39 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 40 40 } 41 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {42 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }41 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 42 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } 43 43 } 44 44 public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter { … … 55 55 : base() { 56 56 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.")); 58 58 Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes.")); 59 59 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes.")); … … 66 66 67 67 public override IOperation Apply() { 68 SymbolicExpressionTreetree = SymbolicExpressionTreeParameter.ActualValue;68 var tree = SymbolicExpressionTreeParameter.ActualValue; 69 69 var moves = GenerateMoves(tree); 70 70 string moveParameterName = ChangeNodeTypeMoveParameter.ActualName; … … 81 81 } 82 82 83 public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(SymbolicExpressionTree tree) { 84 var g = tree.Root.Grammar; 83 public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree) { 85 84 return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix() 86 85 from i in Enumerable.Range(0, parent.SubtreeCount) -
branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ShakeMultiMoveGenerator.cs
r7802 r7832 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Linq; … … 46 45 set { SampleSizeParameter.Value = value; } 47 46 } 48 public ILookupParameter< SymbolicExpressionTree> SymbolicExpressionTreeParameter {49 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }47 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } 50 49 } 51 50 public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter { … … 64 63 Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate.")); 65 64 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.")); 67 66 Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes.")); 68 67 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes.")); … … 75 74 76 75 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); 79 80 string moveParameterName = ChangeNodeTypeMoveParameter.ActualName; 80 81 var moveScopes = new List<Scope>(); 81 var random = RandomParameter.ActualValue;82 int n = SampleSizeParameter.ActualValue.Value;83 82 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) { 89 84 m.NewChild.ShakeLocalParameters(random, 1); 90 85 var moveScope = new Scope(moveScopes.Count.ToString()); … … 95 90 return base.Apply(); 96 91 } 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 } 97 108 } 98 109 }
Note: See TracChangeset
for help on using the changeset viewer.