Changeset 12422 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators
- Timestamp:
- 06/10/15 11:29:34 (10 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
r12012 r12422 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 using HeuristicLab.Parameters;28 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 27 using HeuristicLab.PluginInfrastructure; … … 36 34 ISymbolicExpressionTreeSizeConstraintOperator, 37 35 ISymbolicExpressionTreeGrammarBasedOperator { 38 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";39 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";40 41 #region Parameter Properties42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }44 }45 46 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {47 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }48 }49 #endregion50 #region Properties51 public IntValue MaximumSymbolicExpressionTreeDepth {52 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }53 }54 55 public IntValue MaximumSymbolicExpressionTreeLength {56 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }57 }58 59 #endregion60 36 61 37 [StorableConstructor] … … 63 39 protected FullTreeCreator(FullTreeCreator original, Cloner cloner) : base(original, cloner) { } 64 40 65 public FullTreeCreator() 66 : base() { 67 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, 68 "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored).")); 69 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, 70 "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 71 } 41 public FullTreeCreator() : base() { } 72 42 73 43 public override IDeepCloneable Clone(Cloner cloner) { … … 77 47 78 48 protected override ISymbolicExpressionTree Create(IRandom random) { 79 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 49 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 50 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 80 51 } 81 52 … … 131 102 .ToList(); 132 103 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 104 105 #pragma warning disable 612, 618 133 106 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 107 #pragma warning restore 612, 618 108 134 109 var tree = selectedSymbol.CreateTreeNode(); 135 110 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 163 138 throw new InvalidOperationException("No symbols are available for the tree."); 164 139 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 140 141 #pragma warning disable 612, 618 165 142 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 143 #pragma warning restore 612, 618 144 166 145 var tree = selectedSymbol.CreateTreeNode(); 167 146 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 169 148 } 170 149 171 foreach (var subTree in root.Subtrees) 172 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0) 173 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 150 //additional levels should only be added if the maximum depth is not reached yet 151 if (maxDepth > currentDepth) { 152 foreach (var subTree in root.Subtrees) 153 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0) 154 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 155 } 174 156 } 175 157 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs
r12012 r12422 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 using HeuristicLab.Parameters;28 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 27 using HeuristicLab.PluginInfrastructure; … … 33 31 [StorableClass] 34 32 [Item("GrowTreeCreator", "An operator that creates new symbolic expression trees using the 'Grow' method")] 35 public class GrowTreeCreator : SymbolicExpressionTreeCreator, 36 ISymbolicExpressionTreeSizeConstraintOperator, 37 ISymbolicExpressionTreeGrammarBasedOperator { 38 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 39 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 40 41 #region Parameter Properties 42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 44 } 45 46 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 47 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 48 } 49 50 #endregion 51 #region Properties 52 public IntValue MaximumSymbolicExpressionTreeDepth { 53 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 54 } 55 56 public IntValue MaximumSymbolicExpressionTreeLength { 57 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 58 } 59 #endregion 60 33 public class GrowTreeCreator : SymbolicExpressionTreeCreator { 61 34 [StorableConstructor] 62 35 protected GrowTreeCreator(bool deserializing) : base(deserializing) { } 63 36 protected GrowTreeCreator(GrowTreeCreator original, Cloner cloner) : base(original, cloner) { } 64 37 65 public GrowTreeCreator() 66 : base() { 67 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, 68 "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored).")); 69 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, 70 "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 71 } 38 public GrowTreeCreator() : base() { } 72 39 73 40 public override IDeepCloneable Clone(Cloner cloner) { … … 78 45 protected override ISymbolicExpressionTree Create(IRandom random) { 79 46 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 80 MaximumSymbolicExpressionTreeLength .Value, MaximumSymbolicExpressionTreeDepth.Value);47 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 81 48 } 82 49 … … 122 89 throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 123 90 124 var allowedSymbols = seedNode.Grammar.AllowedSymbols 125 .Where(s => s.InitialFrequency > 0.0) 126 .ToList(); 91 var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 127 92 128 93 for (var i = 0; i < arity; i++) { 129 var possibleSymbols = allowedSymbols 130 .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)) 131 .ToList(); 94 var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList(); 132 95 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 96 97 #pragma warning disable 612, 618 133 98 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 99 #pragma warning restore 612, 618 100 134 101 var tree = selectedSymbol.CreateTreeNode(); 135 102 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 146 113 private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) { 147 114 var arity = SampleArity(random, root); 148 if (arity <= 0)149 throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");115 if (arity == 0) 116 return; 150 117 151 118 var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 152 119 153 120 for (var i = 0; i < arity; i++) { 154 var possibleSymbols = allowedSymbols 155 .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && 156 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth) 157 .ToList(); 121 var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && 122 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList(); 158 123 159 124 if (!possibleSymbols.Any()) 160 125 throw new InvalidOperationException("No symbols are available for the tree."); 126 161 127 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 128 #pragma warning disable 612, 618 162 129 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 130 #pragma warning restore 612, 618 131 163 132 var tree = selectedSymbol.CreateTreeNode(); 164 133 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 166 135 } 167 136 168 foreach (var subTree in root.Subtrees) 169 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0) 170 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 137 if (maxDepth > currentDepth) 138 foreach (var subTree in root.Subtrees) 139 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0) 140 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 171 141 } 172 142 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r12012 r12422 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 using HeuristicLab.Parameters;29 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 28 using HeuristicLab.PluginInfrastructure; … … 37 35 ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator { 38 36 private const int MAX_TRIES = 100; 39 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";40 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";41 #region Parameter Properties42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }44 }45 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {46 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }47 }48 #endregion49 #region Properties50 public IntValue MaximumSymbolicExpressionTreeLength {51 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }52 }53 public IntValue MaximumSymbolicExpressionTreeDepth {54 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }55 }56 #endregion57 37 58 38 [StorableConstructor] … … 61 41 public ProbabilisticTreeCreator() 62 42 : base() { 63 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 64 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 43 65 44 } 66 45 … … 71 50 72 51 protected override ISymbolicExpressionTree Create(IRandom random) { 73 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 52 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 53 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 74 54 } 75 55 … … 186 166 if (allowedSymbols.Count == 0) return false; 187 167 var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList(); 168 169 #pragma warning disable 612, 618 188 170 var selectedSymbol = allowedSymbols.SelectRandom(weights, random); 171 #pragma warning restore 612, 618 172 189 173 ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode(); 190 174 if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random); … … 232 216 select g).First().ToList(); 233 217 var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList(); 218 219 #pragma warning disable 612, 618 234 220 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 221 #pragma warning restore 612, 618 222 235 223 var tree = selectedSymbol.CreateTreeNode(); 236 224 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/RampedHalfAndHalfTreeCreator.cs
r12012 r12422 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 using HeuristicLab.Parameters;26 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 25 using HeuristicLab.PluginInfrastructure; … … 31 29 [StorableClass] 32 30 [Item("RampedHalfAndHalfTreeCreator", "An operator that creates new symbolic expression trees in an alternate way: half the trees are created usign the 'Grow' method while the other half are created using the 'Full' method")] 33 public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator, 34 ISymbolicExpressionTreeSizeConstraintOperator, 35 ISymbolicExpressionTreeGrammarBasedOperator { 36 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 37 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 38 39 #region Parameter Properties 40 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 41 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 42 } 43 44 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 45 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 46 } 47 48 #endregion 49 #region Properties 50 public IntValue MaximumSymbolicExpressionTreeDepth { 51 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 52 } 53 54 public IntValue MaximumSymbolicExpressionTreeLength { 55 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 56 } 57 #endregion 58 31 public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator { 59 32 [StorableConstructor] 60 33 protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { } 61 34 protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { } 62 35 63 public RampedHalfAndHalfTreeCreator() 64 : base() { 65 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, 66 "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored).")); 67 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, 68 "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 69 } 36 public RampedHalfAndHalfTreeCreator() : base() { } 70 37 71 38 public override IDeepCloneable Clone(Cloner cloner) { … … 74 41 75 42 protected override ISymbolicExpressionTree Create(IRandom random) { 76 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 43 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 44 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 77 45 } 78 46 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs
r12012 r12422 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 24 25 using HeuristicLab.Parameters; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 32 33 [StorableClass] 33 34 public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator { 34 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 35 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 36 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 37 35 38 private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar"; 36 39 private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar"; 37 40 38 41 #region Parameter Properties 39 public I LookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {40 get { return (I LookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 41 44 } 42 45 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 47 } 43 48 public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter { 44 49 get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; } … … 55 60 protected SymbolicExpressionTreeCreator() 56 61 : base() { 57 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be created.")); 58 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, 59 "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 60 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, 61 "An immutable clone of the concrete grammar that is actually used to create and manipulate trees.")); 62 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 63 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 64 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 65 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees.")); 62 66 } 63 67 … … 78 82 (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone())); 79 83 } 80 SymbolicExpressionTreeParameter.ActualValue = Create(Random );84 SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue); 81 85 return base.InstrumentedApply(); 82 86 }
Note: See TracChangeset
for help on using the changeset viewer.