Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/11 19:01:00 (14 years ago)
Author:
gkronber
Message:

#1418 changes in symbolic expression tree encoding.

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  
        22obj
        33HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs
         4*.user
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs

    r5445 r5499  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2928using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3029
    31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3231  [StorableClass]
    3332  [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
    3433  public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
    35 
    3634    [StorableConstructor]
    3735    private ChangeNodeTypeManipulation(bool deserializing) : base(deserializing) { }
     
    4341    }
    4442
    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);
    4745    }
    4846
    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) {
    5048
    5149      // select any node as parent (except the root node)
    5250      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
    5351                               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();
    5555      // find possible symbols for the node (also considering the existing branches below it)
    5656      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)
    5959                           select symbol;
    6060
    6161      if (allowedSymbols.Count() == 0) {
    62         success = false;
    6362        return;
    6463      }
    6564      var node = manipulationPoint.Node;
    6665      // 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();
    7474      if (constrainedSymbols.Count() == 0) {
    75         success = false;
    7675        return;
    7776      }
    78       var newSymbol = SelectRandomSymbol(random, constrainedSymbols);
     77      var weights = constrainedSymbols.Select(s => s.InitialFrequency).ToList();
     78      var newSymbol =  constrainedSymbols.SelectRandom(weights, random);
    7979
    8080      // replace the old node with the new node
     
    8686      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
    8787      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 happen
    104       throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");
    10588    }
    10689  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/FullTreeShaker.cs

    r5445 r5499  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Parameters;
    2627
    27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2829  [StorableClass]
    2930  [Item("FullTreeShaker", "Manipulates all nodes that have local parameters.")]
    3031  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
    3143
    3244    [StorableConstructor]
    3345    private FullTreeShaker(bool deserializing) : base(deserializing) { }
    3446    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    }
    3651
    3752    public override IDeepCloneable Clone(Cloner cloner) {
     
    3954    }
    4055
    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 => {
    4358        if (node.HasLocalParameters) {
    44           node.ShakeLocalParameters(random, 1.0);
     59          node.ShakeLocalParameters(random, ShakingFactor.Value);
    4560        }
    46       }
    47       success = true;
     61      });
    4862    }
    4963  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/MultiSymbolicExpressionTreeManipulator.cs

    r5445 r5499  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    2928using HeuristicLab.Operators;
    3029using HeuristicLab.Optimization;
     
    3332using HeuristicLab.PluginInfrastructure;
    3433
    35 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3635  [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
    3736  [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 {
    4241    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
     42    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     43    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
    4344
    4445    public override bool CanChangeName {
     
    4950    }
    5051
    51     #region ISymbolicExpressionTreeManipulator Members
    52     public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    53       get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     52    #region Parameter Properties
     53    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     54      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
    5455    }
    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]; }
    6058    }
    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]; }
    6661    }
    6762    #endregion
     
    7267    public MultiSymbolicExpressionTreeManipulator()
    7368      : 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)."));
    7872
    7973      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
     
    9892
    9993    private void ParameterizeManipulators() {
     94      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
     95        manipulator.RandomParameter.ActualName = RandomParameter.Name;
     96      }
    10097      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;
    10498        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
    10599      }
    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;
    109103      }
    110104    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/OnePointShaker.cs

    r5445 r5499  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Parameters;
    2728
    28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2930  [StorableClass]
    3031  [Item("OnePointShaker", "Selects a random node with local parameters and manipulates the selected node.")]
    3132  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
    3244    [StorableConstructor]
    3345    private OnePointShaker(bool deserializing) : base(deserializing) { }
    3446    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    }
    3651
    3752    public override IDeepCloneable Clone(Cloner cloner) {
     
    3954    }
    4055
    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()
    4358                            where node.HasLocalParameters
    4459                            select node;
    4560      if (parametricNodes.Count() > 0) {
    46         SymbolicExpressionTreeNode selectedPoint = parametricNodes.SelectRandom(random);
     61        var selectedPoint = parametricNodes.SelectRandom(random);
    4762
    48         selectedPoint.ShakeLocalParameters(random, 1.0);
    49         success = true;
    50       } else {
    51         success = false;
     63        selectedPoint.ShakeLocalParameters(random, ShakingFactor.Value);
    5264      }
    5365    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs

    r5445 r5499  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3028using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using HeuristicLab.Parameters;
    3130
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3332  [StorableClass]
    3433  [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
    3654    [StorableConstructor]
    3755    private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { }
    3856    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    }
    4062
    4163    public override IDeepCloneable Clone(Cloner cloner) {
     
    4365    }
    4466
    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);
    4769    }
    4870
    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) {
    5172      // select any node as parent (except the root node)
    5273      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
    5374                               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);
    5577
    56       int maxSize = maxTreeSize - symbolicExpressionTree.Size + manipulationPoint.Node.GetSize();
    57       int maxHeight = maxTreeHeight - symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight();
     78      int maxLength = maxTreeLength - symbolicExpressionTree.Size + manipulationPoint.Node.GetSize();
     79      int maxDepth = maxTreeDepth - symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight();
    5880      // 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) <= maxHeight
    61                            where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxSize
    62                            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();
    6385      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
    6689      var seedNode = seedSymbol.CreateTreeNode();
    6790      if (seedNode.HasLocalParameters)
     
    7093      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
    7194      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);
    9096    }
    9197  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/SymbolicExpressionTreeManipulator.cs

    r5494 r5499  
    3333  [StorableClass]
    3434  public abstract class SymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator {
    35     private const string FailedManipulationEventsParameterName = "FailedManipulationEvents";
    3635    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
    3736
    3837    #region Parameter Properties
    39     public IValueParameter<IntValue> FailedManipulationEventsParameter {
    40       get { return (IValueParameter<IntValue>)Parameters[FailedManipulationEventsParameterName]; }
    41     }
    4238    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    4339      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     
    4642
    4743    #region Properties
    48     public IntValue FailedManipulationEvents {
    49       get { return FailedManipulationEventsParameter.Value; }
    50     }
    5144    public SymbolicExpressionTree SymbolicExpressionTree {
    5245      get { return SymbolicExpressionTreeParameter.ActualValue; }
     
    5952    public SymbolicExpressionTreeManipulator()
    6053      : base() {
    61       Parameters.Add(new ValueParameter<IntValue>(FailedManipulationEventsParameterName, "The number of failed manipulation events.", new IntValue()));
    6254      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
    6355    }
     
    6557    public sealed override IOperation Apply() {
    6658      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);
    7060
    71       if (!success) FailedManipulationEvents.Value++;
    7261      return base.Apply();
    7362    }
    7463
    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);
    7765  }
    7866}
Note: See TracChangeset for help on using the changeset viewer.