Changeset 5499 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators
- Timestamp:
- 02/16/11 19:01:00 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Property svn:ignore
-
old new 2 2 obj 3 3 HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs 4 *.user
-
- Property svn:ignore
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs
r5445 r5499 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 29 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 32 31 [StorableClass] 33 32 [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")] 34 33 public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator { 35 36 34 [StorableConstructor] 37 35 private ChangeNodeTypeManipulation(bool deserializing) : base(deserializing) { } … … 43 41 } 44 42 45 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree , ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {46 ChangeNodeType(random, symbolicExpressionTree , grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);43 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree) { 44 ChangeNodeType(random, symbolicExpressionTree); 47 45 } 48 46 49 public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree , ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) {47 public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree) { 50 48 51 49 // select any node as parent (except the root node) 52 50 var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1) 53 51 from subtree in parent.SubTrees 54 select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random); 52 select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) }) 53 .SelectRandom(random); 54 int subTreeCount = manipulationPoint.Node.SubTrees.Count(); 55 55 // find possible symbols for the node (also considering the existing branches below it) 56 56 var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index) 57 where manipulationPoint.Node.SubTrees.Count <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)58 where manipulationPoint.Node.SubTrees.Count >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)57 where subTreeCount <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol) 58 where subTreeCount >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol) 59 59 select symbol; 60 60 61 61 if (allowedSymbols.Count() == 0) { 62 success = false;63 62 return; 64 63 } 65 64 var node = manipulationPoint.Node; 66 65 // keep only symbols that are still possible considering the existing sub-trees 67 var constrainedSymbols = from symbol in allowedSymbols 68 let disallowedSubtrees = 69 from subtree in node.SubTrees 70 where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.SubTrees.IndexOf(subtree)) 71 select subtree 72 where disallowedSubtrees.Count() == 0 73 select symbol; 66 var constrainedSymbols = (from symbol in allowedSymbols 67 let disallowedSubtrees = 68 from subtree in node.SubTrees 69 where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.IndexOfSubTree(subtree)) 70 select subtree 71 where disallowedSubtrees.Count() == 0 72 select symbol) 73 .ToList(); 74 74 if (constrainedSymbols.Count() == 0) { 75 success = false;76 75 return; 77 76 } 78 var newSymbol = SelectRandomSymbol(random, constrainedSymbols); 77 var weights = constrainedSymbols.Select(s => s.InitialFrequency).ToList(); 78 var newSymbol = constrainedSymbols.SelectRandom(weights, random); 79 79 80 80 // replace the old node with the new node … … 86 86 manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index); 87 87 manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, newNode); 88 success = true;89 }90 91 private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {92 var symbolList = symbols.ToList();93 var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();94 if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");95 var r = random.NextDouble() * ticketsSum;96 double aggregatedTickets = 0;97 for (int i = 0; i < symbolList.Count; i++) {98 aggregatedTickets += symbolList[i].InitialFrequency;99 if (aggregatedTickets > r) {100 return symbolList[i];101 }102 }103 // this should never happen104 throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");105 88 } 106 89 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/FullTreeShaker.cs
r5445 r5499 24 24 using HeuristicLab.Data; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Parameters; 26 27 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 28 29 [StorableClass] 29 30 [Item("FullTreeShaker", "Manipulates all nodes that have local parameters.")] 30 31 public sealed class FullTreeShaker : SymbolicExpressionTreeManipulator { 32 private const string ShakingFactorParameterName = "ShakingFactor"; 33 #region parameter properties 34 public IValueLookupParameter<DoubleValue> ShakingFactorParameter { 35 get { return (IValueLookupParameter<DoubleValue>)Parameters[ShakingFactorParameterName]; } 36 } 37 #endregion 38 #region properties 39 public DoubleValue ShakingFactor { 40 get { return ShakingFactorParameter.ActualValue; } 41 } 42 #endregion 31 43 32 44 [StorableConstructor] 33 45 private FullTreeShaker(bool deserializing) : base(deserializing) { } 34 46 private FullTreeShaker(FullTreeShaker original, Cloner cloner) : base(original, cloner) { } 35 public FullTreeShaker() : base() { } 47 public FullTreeShaker() 48 : base() { 49 Parameters.Add(new ValueLookupParameter<DoubleValue>(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0))); 50 } 36 51 37 52 public override IDeepCloneable Clone(Cloner cloner) { … … 39 54 } 40 55 41 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {42 foreach (var node in symbolicExpressionTree.IterateNodesPrefix()){56 protected override void Manipulate(IRandom random, SymbolicExpressionTree tree) { 57 tree.Root.ForEachNodePostfix(node => { 43 58 if (node.HasLocalParameters) { 44 node.ShakeLocalParameters(random, 1.0);59 node.ShakeLocalParameters(random, ShakingFactor.Value); 45 60 } 46 } 47 success = true; 61 }); 48 62 } 49 63 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/MultiSymbolicExpressionTreeManipulator.cs
r5445 r5499 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;29 28 using HeuristicLab.Operators; 30 29 using HeuristicLab.Optimization; … … 33 32 using HeuristicLab.PluginInfrastructure; 34 33 35 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{34 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 36 35 [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")] 37 36 [StorableClass] 38 public sealed class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>, ISymbolicExpressionTreeManipulator, IStochasticOperator {39 private const string MaxTreeSizeParameterName = "MaxTreeSize";40 private const string MaxTreeHeightParameterName = "MaxTreeHeight";41 private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar";37 public sealed class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>, 38 ISymbolicExpressionTreeManipulator, 39 IStochasticOperator, 40 ISymbolicExpressionTreeSizeConstraintOperator { 42 41 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 42 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 43 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 43 44 44 45 public override bool CanChangeName { … … 49 50 } 50 51 51 #region ISymbolicExpressionTreeManipulator Members52 public I LookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {53 get { return (I LookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }52 #region Parameter Properties 53 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 54 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 54 55 } 55 #endregion 56 57 #region ISymbolicExpressionTreeOperator Members 58 public IValueLookupParameter<IntValue> MaxTreeSizeParameter { 59 get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; } 56 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 57 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 60 58 } 61 public IValueLookupParameter<IntValue> MaxTreeHeightParameter { 62 get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; } 63 } 64 public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter { 65 get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; } 59 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 60 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 66 61 } 67 62 #endregion … … 72 67 public MultiSymbolicExpressionTreeManipulator() 73 68 : base() { 74 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 75 Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree.")); 76 Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0).")); 77 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees.")); 69 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 70 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 71 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 78 72 79 73 foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) { … … 98 92 99 93 private void ParameterizeManipulators() { 94 foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) { 95 manipulator.RandomParameter.ActualName = RandomParameter.Name; 96 } 100 97 foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) { 101 manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;102 manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;103 manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;104 98 manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name; 105 99 } 106 107 foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {108 manipulator. RandomParameter.ActualName = RandomParameter.Name;100 foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) { 101 manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name; 102 manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name; 109 103 } 110 104 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/OnePointShaker.cs
r5445 r5499 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Parameters; 27 28 28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 29 30 [StorableClass] 30 31 [Item("OnePointShaker", "Selects a random node with local parameters and manipulates the selected node.")] 31 32 public sealed class OnePointShaker : SymbolicExpressionTreeManipulator { 33 private const string ShakingFactorParameterName = "ShakingFactor"; 34 #region parameter properties 35 public IValueLookupParameter<DoubleValue> ShakingFactorParameter { 36 get { return (IValueLookupParameter<DoubleValue>)Parameters[ShakingFactorParameterName]; } 37 } 38 #endregion 39 #region properties 40 public DoubleValue ShakingFactor { 41 get { return ShakingFactorParameter.ActualValue; } 42 } 43 #endregion 32 44 [StorableConstructor] 33 45 private OnePointShaker(bool deserializing) : base(deserializing) { } 34 46 private OnePointShaker(OnePointShaker original, Cloner cloner) : base(original, cloner) { } 35 public OnePointShaker() : base() { } 47 public OnePointShaker() 48 : base() { 49 Parameters.Add(new ValueLookupParameter<DoubleValue>(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0))); 50 } 36 51 37 52 public override IDeepCloneable Clone(Cloner cloner) { … … 39 54 } 40 55 41 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {42 var parametricNodes = from node in symbolicExpressionTree.IterateNodesPrefix()56 protected override void Manipulate(IRandom random, SymbolicExpressionTree tree) { 57 var parametricNodes = from node in tree.IterateNodesPrefix() 43 58 where node.HasLocalParameters 44 59 select node; 45 60 if (parametricNodes.Count() > 0) { 46 SymbolicExpressionTreeNodeselectedPoint = parametricNodes.SelectRandom(random);61 var selectedPoint = parametricNodes.SelectRandom(random); 47 62 48 selectedPoint.ShakeLocalParameters(random, 1.0); 49 success = true; 50 } else { 51 success = false; 63 selectedPoint.ShakeLocalParameters(random, ShakingFactor.Value); 52 64 } 53 65 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
r5445 r5499 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;30 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Parameters; 31 30 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 33 32 [StorableClass] 34 33 [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")] 35 public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator { 34 public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator { 35 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 36 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 37 #region Parameter Properties 38 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 39 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 40 } 41 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 42 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 43 } 44 #endregion 45 #region Properties 46 public IntValue MaximumSymbolicExpressionTreeLength { 47 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 48 } 49 public IntValue MaximumSymbolicExpressionTreeDepth { 50 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 51 } 52 #endregion 53 36 54 [StorableConstructor] 37 55 private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { } 38 56 private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { } 39 public ReplaceBranchManipulation() : base() { } 57 public ReplaceBranchManipulation() 58 : base() { 59 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 60 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 61 } 40 62 41 63 public override IDeepCloneable Clone(Cloner cloner) { … … 43 65 } 44 66 45 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {46 ReplaceRandomBranch(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);67 protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 68 ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 47 69 } 48 70 49 public static void ReplaceRandomBranch(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) { 50 success = false; 71 public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) { 51 72 // select any node as parent (except the root node) 52 73 var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1) 53 74 from subtree in parent.SubTrees 54 select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random); 75 select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) }) 76 .SelectRandom(random); 55 77 56 int max Size = maxTreeSize- symbolicExpressionTree.Size + manipulationPoint.Node.GetSize();57 int max Height = maxTreeHeight- symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight();78 int maxLength = maxTreeLength - symbolicExpressionTree.Size + manipulationPoint.Node.GetSize(); 79 int maxDepth = maxTreeDepth - symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight(); 58 80 // find possible symbols for the node (also considering the existing branches below it) 59 var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)60 where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxHeight61 where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxSize62 select symbol;81 var allowedSymbols = (from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index) 82 where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxDepth 83 where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxLength 84 select symbol).ToList(); 63 85 if (allowedSymbols.Count() == 0) return; 64 65 var seedSymbol = SelectRandomSymbol(random, allowedSymbols); // replace the old node with the new node 86 var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); 87 var seedSymbol = allowedSymbols.SelectRandom(weights, random); 88 // replace the old node with the new node 66 89 var seedNode = seedSymbol.CreateTreeNode(); 67 90 if (seedNode.HasLocalParameters) … … 70 93 manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index); 71 94 manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode); 72 seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxSize, maxHeight, 0, 0); 73 success = true; 74 } 75 76 private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) { 77 var symbolList = symbols.ToList(); 78 var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum(); 79 if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero."); 80 var r = random.NextDouble() * ticketsSum; 81 double aggregatedTickets = 0; 82 for (int i = 0; i < symbolList.Count; i++) { 83 aggregatedTickets += symbolList[i].InitialFrequency; 84 if (aggregatedTickets > r) { 85 return symbolList[i]; 86 } 87 } 88 // this should never happen 89 throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols."); 95 seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth, 0, 0); 90 96 } 91 97 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/SymbolicExpressionTreeManipulator.cs
r5494 r5499 33 33 [StorableClass] 34 34 public abstract class SymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator { 35 private const string FailedManipulationEventsParameterName = "FailedManipulationEvents";36 35 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 37 36 38 37 #region Parameter Properties 39 public IValueParameter<IntValue> FailedManipulationEventsParameter {40 get { return (IValueParameter<IntValue>)Parameters[FailedManipulationEventsParameterName]; }41 }42 38 public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter { 43 39 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } … … 46 42 47 43 #region Properties 48 public IntValue FailedManipulationEvents {49 get { return FailedManipulationEventsParameter.Value; }50 }51 44 public SymbolicExpressionTree SymbolicExpressionTree { 52 45 get { return SymbolicExpressionTreeParameter.ActualValue; } … … 59 52 public SymbolicExpressionTreeManipulator() 60 53 : base() { 61 Parameters.Add(new ValueParameter<IntValue>(FailedManipulationEventsParameterName, "The number of failed manipulation events.", new IntValue()));62 54 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 63 55 } … … 65 57 public sealed override IOperation Apply() { 66 58 SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue; 67 bool success; 68 Manipulate(RandomParameter.ActualValue, tree, SymbolicExpressionGrammarParameter.ActualValue, 69 MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success); 59 Manipulate(RandomParameter.ActualValue, tree); 70 60 71 if (!success) FailedManipulationEvents.Value++;72 61 return base.Apply(); 73 62 } 74 63 75 protected abstract void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, 76 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success); 64 protected abstract void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree); 77 65 } 78 66 }
Note: See TracChangeset
for help on using the changeset viewer.