Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5499


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
Files:
3 added
50 edited
3 moved

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/Analyzers/MinAverageMaxSymbolicExpressionTreeLengthAnalyzer.cs

    r5494 r5499  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    2827using HeuristicLab.Operators;
    2928using HeuristicLab.Parameters;
    3029using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3130
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers {
     31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3332  /// <summary>
    34   /// An operator that tracks the min average and max tree size.
     33  /// An operator that tracks the min average and max length of symbolic expression trees.
    3534  /// </summary>
    36   [Item("MinAverageMaxSymbolicExpressionTreeSizeAnalyzer", "An operator that tracks the min avgerage and max tree size.")]
     35  [Item("MinAverageMaxSymbolicExpressionTreeLengthAnalyzer", "An operator that tracks the min avgerage and max length of symbolic expression trees.")]
    3736  [StorableClass]
    38   public sealed class MinAverageMaxSymbolicExpressionTreeSizeAnalyzer : AlgorithmOperator, ISymbolicExpressionTreeAnalyzer {
     37  public sealed class MinAverageMaxSymbolicExpressionTreeLengthAnalyzer : AlgorithmOperator, ISymbolicExpressionTreeAnalyzer {
    3938    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
    40     private const string SymbolicExpressionTreeSizeParameterName = "SymbolicExpressionTreeSize";
    41     private const string SymbolicExpressionTreeSizesParameterName = "Symbolic expression tree size";
    42     private const string MinTreeSizeParameterName = "Min tree size";
    43     private const string AverageTreeSizeParameterName = "Average tree size";
    44     private const string MaxTreeSizeParameterName = "Max tree size";
     39    private const string SymbolicExpressionTreeLengthParameterName = "SymbolicExpressionTreeLength";
     40    private const string SymbolicExpressionTreeLengthsParameterName = "Symbolic expression tree length";
     41    private const string MinTreeLengthParameterName = "Minimal symbolic expression tree length";
     42    private const string AverageTreeLengthParameterName = "Average symbolic expression tree length";
     43    private const string MaxTreeLengthParameterName = "Maximal symbolic expression tree length";
    4544    private const string ResultsParameterName = "Results";
    4645
     
    4948      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    5049    }
    51     public ScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeSizeParameter {
    52       get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeSizeParameterName]; }
     50    public ScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeLengthParameter {
     51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeLengthParameterName]; }
    5352    }
    54     public ValueLookupParameter<DataTable> SymbolicExpressionTreeSizesParameter {
    55       get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeSizesParameterName]; }
     53    public ValueLookupParameter<DataTable> SymbolicExpressionTreeLengthsParameter {
     54      get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName]; }
    5655    }
    5756    public ValueLookupParameter<VariableCollection> ResultsParameter {
     
    6564
    6665    #endregion
     66
    6767    [StorableConstructor]
    68     private MinAverageMaxSymbolicExpressionTreeSizeAnalyzer(bool deserializing) : base() { }
    69     private MinAverageMaxSymbolicExpressionTreeSizeAnalyzer(MinAverageMaxSymbolicExpressionTreeSizeAnalyzer original, Cloner cloner)
     68    private MinAverageMaxSymbolicExpressionTreeLengthAnalyzer(bool deserializing) : base() { }
     69    private MinAverageMaxSymbolicExpressionTreeLengthAnalyzer(MinAverageMaxSymbolicExpressionTreeLengthAnalyzer original, Cloner cloner)
    7070      : base(original, cloner) {
    7171      AfterDeserialization();
    7272    }
    73     public MinAverageMaxSymbolicExpressionTreeSizeAnalyzer()
     73    public MinAverageMaxSymbolicExpressionTreeLengthAnalyzer()
    7474      : base() {
    75       Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose size should be calculated."));
    76       Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeSizeParameterName, "The tree size of the symbolic expression tree."));
    77       Parameters.Add(new ValueLookupParameter<DataTable>(SymbolicExpressionTreeSizesParameterName, "The data table to store the tree sizes."));
     75      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));
     76      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeLengthParameterName, "The length of the symbolic expression tree."));
     77      Parameters.Add(new ValueLookupParameter<DataTable>(SymbolicExpressionTreeLengthsParameterName, "The data table to store the symbolic expression tree lengths."));
    7878      Parameters.Add(new ValueLookupParameter<VariableCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
    7979
    8080      subScopesProcessor = new UniformSubScopesProcessor();
    81       SymbolicExpressionTreeSizeCalculator sizeCalculator = new SymbolicExpressionTreeSizeCalculator();
     81      SymbolicExpressionTreeLengthCalculator lengthCalculator = new SymbolicExpressionTreeLengthCalculator();
    8282      valueAnalyzer = new MinAverageMaxValueAnalyzer();
    8383
    8484      subScopesProcessor.Depth.Value = SymbolicExpressionTreeParameter.Depth;
    85       sizeCalculator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
    86       sizeCalculator.SymbolicExpressionTreeSizeParameter.ActualName = SymbolicExpressionTreeSizeParameter.Name;
    87       valueAnalyzer.ValueParameter.ActualName = sizeCalculator.SymbolicExpressionTreeSizeParameter.Name;
    88       valueAnalyzer.ValueParameter.Depth = SymbolicExpressionTreeSizeParameter.Depth;
    89       valueAnalyzer.AverageValueParameter.ActualName = AverageTreeSizeParameterName;
     85      lengthCalculator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
     86      lengthCalculator.SymbolicExpressionTreeLengthParameter.ActualName = SymbolicExpressionTreeLengthParameter.Name;
     87      valueAnalyzer.ValueParameter.ActualName = lengthCalculator.SymbolicExpressionTreeLengthParameter.Name;
     88      valueAnalyzer.ValueParameter.Depth = SymbolicExpressionTreeLengthParameter.Depth;
     89      valueAnalyzer.AverageValueParameter.ActualName = AverageTreeLengthParameterName;
    9090      valueAnalyzer.CollectAverageValueInResultsParameter.Value = new BoolValue(false);
    91       valueAnalyzer.MaxValueParameter.ActualName = MaxTreeSizeParameterName;
     91      valueAnalyzer.MaxValueParameter.ActualName = MaxTreeLengthParameterName;
    9292      valueAnalyzer.CollectMaxValueInResultsParameter.Value = new BoolValue(false);
    93       valueAnalyzer.MinValueParameter.ActualName = MinTreeSizeParameterName;
     93      valueAnalyzer.MinValueParameter.ActualName = MinTreeLengthParameterName;
    9494      valueAnalyzer.CollectMinValueInResultsParameter.Value = new BoolValue(false);
    95       valueAnalyzer.ValuesParameter.ActualName = SymbolicExpressionTreeSizesParameter.Name;
     95      valueAnalyzer.ValuesParameter.ActualName = SymbolicExpressionTreeLengthsParameter.Name;
    9696
    9797      OperatorGraph.InitialOperator = subScopesProcessor;
    98       subScopesProcessor.Operator = sizeCalculator;
    99       sizeCalculator.Successor = null;
     98      subScopesProcessor.Operator = lengthCalculator;
     99      lengthCalculator.Successor = null;
    100100      subScopesProcessor.Successor = valueAnalyzer;
    101101      valueAnalyzer.Successor = null;
     
    108108    private void AfterDeserialization() {
    109109      SymbolicExpressionTreeParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeParameter_DepthChanged);
    110       SymbolicExpressionTreeSizeParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeSizeParameter_DepthChanged);
     110      SymbolicExpressionTreeLengthParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeSizeParameter_DepthChanged);
    111111    }
    112112
    113113    public override IDeepCloneable Clone(Cloner cloner) {
    114       return new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer(this, cloner);
     114      return new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer(this, cloner);
    115115    }
    116116
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs

    r5445 r5499  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    2827using HeuristicLab.Operators;
    2928using HeuristicLab.Optimization;
     
    3130using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3231
    33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers {
     32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3433  /// <summary>
    35   /// An operator that tracks the frequencies of distinc symbols.
     34  /// An operator that tracks the frequencies of distinct symbols in symbolic expression trees.
    3635  /// </summary>
    37   [Item("SymbolicExpressionSymbolFrequencyAnalyzer", "An operator that tracks frequencies of symbols.")]
     36  [Item("SymbolicExpressionSymbolFrequencyAnalyzer", "An operator that tracks frequencies of symbols in symbolic expression trees.")]
    3837  [StorableClass]
    3938  public class SymbolicExpressionSymbolFrequencyAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
     
    6766      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
    6867      Parameters.Add(new ValueLookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies."));
    69       Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
     68      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the symbol frequencies should be stored."));
    7069    }
    7170    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionTreeLengthCalculator.cs

    r5494 r5499  
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828
    29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3030  /// <summary>
    31   /// An operator that outputs the tree size of a symbolic expression tree.
     31  /// An operator that outputs the length of a symbolic expression tree.
    3232  /// </summary>
    33   [Item("SymbolicExpressionTreeSizeCalculator", "An operator that outputs the tree size of a symbolic expression tree.")]
     33  [Item("SymbolicExpressionTreeLengthCalculator", "An operator that outputs the length of a symbolic expression tree.")]
    3434  [StorableClass]
    35   public sealed class SymbolicExpressionTreeSizeCalculator : SingleSuccessorOperator {
     35  public sealed class SymbolicExpressionTreeLengthCalculator : SingleSuccessorOperator {
    3636    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
    37     private const string SymbolicExpressionTreeSizeParameterName = "SymbolicExpressionTreeSize";
     37    private const string SymbolicExpressionTreeLengthParameterName = "SymbolicExpressionTreeLength";
    3838
    3939    #region parameter properties
     
    4141      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    4242    }
    43     public ILookupParameter<DoubleValue> SymbolicExpressionTreeSizeParameter {
    44       get { return (ILookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeSizeParameterName]; }
     43    public ILookupParameter<DoubleValue> SymbolicExpressionTreeLengthParameter {
     44      get { return (ILookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeLengthParameterName]; }
    4545    }
    4646    #endregion
     
    5050      get { return SymbolicExpressionTreeParameter.ActualValue; }
    5151    }
    52     public DoubleValue SymbolicExpressionTreeSize {
    53       get { return SymbolicExpressionTreeSizeParameter.ActualValue; }
    54       set { SymbolicExpressionTreeSizeParameter.ActualValue = value; }
     52    public DoubleValue SymbolicExpressionTreeLength {
     53      get { return SymbolicExpressionTreeLengthParameter.ActualValue; }
     54      set { SymbolicExpressionTreeLengthParameter.ActualValue = value; }
    5555    }
    5656    #endregion
    5757
    5858    [StorableConstructor]
    59     private SymbolicExpressionTreeSizeCalculator(bool deserializing) : base(deserializing) { }
    60     private SymbolicExpressionTreeSizeCalculator(SymbolicExpressionTreeSizeCalculator original, Cloner cloner) : base(original, cloner) { }
    61     public SymbolicExpressionTreeSizeCalculator()
     59    private SymbolicExpressionTreeLengthCalculator(bool deserializing) : base(deserializing) { }
     60    private SymbolicExpressionTreeLengthCalculator(SymbolicExpressionTreeLengthCalculator original, Cloner cloner) : base(original, cloner) { }
     61    public SymbolicExpressionTreeLengthCalculator()
    6262      : base() {
    63       Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose size should be calculated."));
    64       Parameters.Add(new LookupParameter<DoubleValue>(SymbolicExpressionTreeSizeParameterName, "The tree size of the symbolic expression tree."));
     63      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));
     64      Parameters.Add(new LookupParameter<DoubleValue>(SymbolicExpressionTreeLengthParameterName, "The length of the symbolic expression tree."));
    6565    }
    6666
    6767    public override IOperation Apply() {
    68       SymbolicExpressionTree tree = SymbolicExpressionTree;
    69       SymbolicExpressionTreeSize = new DoubleValue(tree.Size);
     68      SymbolicExpressionTreeLength = new DoubleValue(SymbolicExpressionTree.Size);
    7069      return base.Apply();
    7170    }
    7271
    7372    public override IDeepCloneable Clone(Cloner cloner) {
    74       return new SymbolicExpressionTreeSizeCalculator(this, cloner);
     73      return new SymbolicExpressionTreeLengthCalculator(this, cloner);
    7574    }
    7675  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.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.ArchitectureManipulators {
     30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3231  /// <summary>
    3332  /// Creates a new argument within one function-defining branch of a symbolic expression tree.
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDeleter.cs

    r5445 r5499  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Data;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2827
    29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3029  /// <summary>
    3130  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 112
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDuplicater.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.ArchitectureManipulators {
     30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3231  /// <summary>
    3332  /// Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch.
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs

    r5445 r5499  
    2121
    2222using System.Collections.Generic;
    23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2423
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
     24namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2625  public static class GrammarModifier {
    27     public static void AddDynamicSymbol(ISymbolicExpressionGrammar grammar, Symbol classRepresentative, string symbolName, int nArgs) {
     26    public static void AddDynamicSymbol(ISymbolicExpressionTreeGrammar grammar, ISymbol classRepresentative, string symbolName, int nArgs) {
    2827      var invokeSym = new InvokeFunction(symbolName);
    2928      grammar.AddSymbol(invokeSym);
     
    3433
    3534
    36     public static void AddDynamicArguments(ISymbolicExpressionGrammar grammar, Symbol classRepresentative, IEnumerable<int> argumentIndexes) {
     35    public static void AddDynamicArguments(ISymbolicExpressionTreeGrammar grammar, ISymbol classRepresentative, IEnumerable<int> argumentIndexes) {
    3736      foreach (int argIndex in argumentIndexes) {
    3837        var argSymbol = new Argument(argIndex);
     
    4443    }
    4544
    46     private static void SetSyntaxConstraints(ISymbolicExpressionGrammar grammar, Symbol classRepresentative, Symbol newSymbol) {
     45    private static void SetSyntaxConstraints(ISymbolicExpressionTreeGrammar grammar, ISymbol classRepresentative, Symbol newSymbol) {
    4746      // allow symbol as child of the representative first to make sure that the symbol
    4847      // is in the list of parents and children
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/MultiSymbolicExpressionTreeArchitectureManipulator.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.ArchitectureManipulators {
     34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3635  [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")]
    3736  [StorableClass]
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs

    r5445 r5499  
    2727using HeuristicLab.Core;
    2828using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3029using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3130
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
     31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3332  /// <summary>
    3433  /// Manipulates a symbolic expression by adding one new function-defining branch containing
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDeleter.cs

    r5445 r5499  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2927using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3028using System.Collections.Generic;
    3129
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
     30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3331  /// <summary>
    3432  /// Manipulates a symbolic expression by deleting a preexisting function-defining branch.
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs

    r5445 r5499  
    2727using HeuristicLab.Core;
    2828using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3029using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3130
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
     31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3332  /// <summary>
    3433  /// Manipulates a symbolic expression by duplicating a preexisting function-defining branch.
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SymbolicExpressionTreeArchitectureManipulator.cs

    r5445 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
    2725using HeuristicLab.Parameters;
    2826using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2927
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3129  /// <summary>
    3230  /// Base class for architecture altering operators for symbolic expression trees.
     
    3432  [StorableClass]
    3533  public abstract class SymbolicExpressionTreeArchitectureManipulator : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeArchitectureManipulator {
    36     private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
    37     private const string MaxFunctionDefiningBranchesParameterName = "MaxFunctionDefiningBranches";
     34    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
     35    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
    3836    public override bool CanChangeName {
    3937      get { return false; }
    4038    }
    41 
    42     public IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {
    43       get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionDefiningBranchesParameterName]; }
     39    #region parameter properties
     40    public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter {
     41      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
    4442    }
    45     public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter {
    46       get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
     43    public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter {
     44      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
    4745    }
    48     public IntValue MaxFunctionDefiningBranches {
    49       get { return MaxFunctionDefinitionsParameter.ActualValue; }
     46    #endregion
     47    #region properties
     48    public IntValue MaximumFunctionDefinitions {
     49      get { return MaximumFunctionDefinitionsParameter.ActualValue; }
    5050    }
    51     public IntValue MaxFunctionArguments {
    52       get { return MaxFunctionArgumentsParameter.ActualValue; }
     51    public IntValue MaximumFunctionArguments {
     52      get { return MaximumFunctionArgumentsParameter.ActualValue; }
    5353    }
     54    #endregion
    5455    [StorableConstructor]
    5556    protected SymbolicExpressionTreeArchitectureManipulator(bool deserializing) : base(deserializing) { }
     
    5758    public SymbolicExpressionTreeArchitectureManipulator()
    5859      : base() {
    59       Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefiningBranchesParameterName, "The maximal allowed number of function defining branches."));
    60       Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "The maximal allowed number of arguments of a newly created function."));
     60      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximal allowed number of automatically defined functions."));
     61      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximal allowed number of arguments of a automatically defined functions."));
    6162    }
    6263
    63     protected override sealed void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
    64       ModifyArchitecture(random, symbolicExpressionTree, grammar, maxTreeSize, maxTreeHeight, MaxFunctionDefiningBranches, MaxFunctionArguments, out success);
     64    protected override sealed void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree) {
     65      ModifyArchitecture(random, symbolicExpressionTree, MaximumFunctionDefinitions, MaximumFunctionArguments);
    6566    }
    6667
     
    6869      IRandom random,
    6970      SymbolicExpressionTree tree,
    70       ISymbolicExpressionGrammar grammar,
    71       IntValue maxTreeSize,
    72       IntValue maxTreeHeight,
    7371      IntValue maxFunctionDefinitions,
    74       IntValue maxFunctionArguments,
    75       out bool success
    76       );
     72      IntValue maxFunctionArguments);
    7773  }
    7874}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/Instruction.cs

    r5445 r5499  
    2121
    2222
    23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Compiler {
    24   // total size of this struct should be small to improve cache access while executing the code
    25   // should be aligned to 8/16/32 byte
    26   // size = 4(8) + 1 + 1 + 2 = 8 (12)
     23namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     24  // total size of this class should be small to improve cache access while executing the code
    2725  public class Instruction {
    2826    // the tree node can hold additional data that is necessary for the execution of this instruction
    29     public SymbolicExpressionTreeNode dynamicNode;
     27    public ISymbolicExpressionTreeNode dynamicNode;
    3028    // op code of the function that determines what operation should be executed
    3129    public byte opCode;
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs

    r5445 r5499  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2625
    27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Compiler {
     26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2827  public class SymbolicExpressionTreeCompiler {
    2928    private Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
    3029    private List<Func<Instruction, Instruction>> postInstructionCompiledHooks = new List<Func<Instruction, Instruction>>();
    3130
    32     public Instruction[] Compile(SymbolicExpressionTree tree, Func<SymbolicExpressionTreeNode, byte> opCodeMapper) {
     31    public Instruction[] Compile(SymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
    3332      List<Instruction> code = new List<Instruction>();
    3433      entryPoint.Clear();
    3534      // compile main body branches
    36       foreach (var branch in tree.Root.SubTrees[0].SubTrees) {
     35      foreach (var branch in tree.Root.GetSubTree(0).SubTrees) {
    3736        code.AddRange(Compile(branch, opCodeMapper));
    3837      }
     
    6059    }
    6160
    62     private IEnumerable<Instruction> Compile(SymbolicExpressionTreeNode branch, Func<SymbolicExpressionTreeNode, byte> opCodeMapper) {
     61    private IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
    6362      foreach (var node in branch.IterateNodesPrefix()) {
    6463        Instruction instr = new Instruction();
    65         if (node.SubTrees.Count > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
    66         instr.nArguments = (byte)node.SubTrees.Count;
     64        int subTreesCount = node.SubTrees.Count();
     65        if (subTreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
     66        instr.nArguments = (byte)subTreesCount;
    6767        instr.opCode = opCodeMapper(node);
    6868        if (node.Symbol is Argument) {
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r5445 r5499  
    2727using HeuristicLab.Core;
    2828using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
    30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3129using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    32 
    33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators {
     30using HeuristicLab.Parameters;
     31
     32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3433  [StorableClass]
    3534  [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")]
    36   public sealed class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator {
     35  public sealed class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator,
     36    ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator, ISymbolicExpressionTreeArchitectureAlteringOperator {
    3737    private const int MAX_TRIES = 100;
     38    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     39    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     40    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
     41    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
     42    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
     43    #region Parameter Properties
     44    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     45      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
     46    }
     47    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
     48      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
     49    }
     50    public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter {
     51      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
     52    }
     53    public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter {
     54      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
     55    }
     56    public IValueLookupParameter<ISymbolicExpressionTreeGrammar> SymbolicExpressionTreeGrammarParameter {
     57      get { return (IValueLookupParameter<ISymbolicExpressionTreeGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
     58    }
     59    #endregion
     60    #region Properties
     61    public IntValue MaximumSymbolicExpressionTreeLength {
     62      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
     63    }
     64    public IntValue MaximumSymbolicExpressionTreeDepth {
     65      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
     66    }
     67    public IntValue MaximumFunctionDefinitions {
     68      get { return MaximumFunctionDefinitionsParameter.ActualValue; }
     69    }
     70    public IntValue MaximumFunctionArguments {
     71      get { return MaximumFunctionArgumentsParameter.ActualValue; }
     72    }
     73    public ISymbolicExpressionTreeGrammar SymbolicExpressionTreeGrammar {
     74      get { return SymbolicExpressionTreeGrammarParameter.ActualValue; }
     75    }
     76    #endregion
     77
    3878    [StorableConstructor]
    3979    private ProbabilisticTreeCreator(bool deserializing) : base(deserializing) { }
    4080    private ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
    41     public ProbabilisticTreeCreator() : base() { }
     81    public ProbabilisticTreeCreator()
     82      : base() {
     83      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
     84      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     85      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximum allowed number of automatically defined functions."));
     86      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximum allowed number of arguments of automatically defined functions."));
     87      Parameters.Add(new ValueLookupParameter<SymbolicExpressionTreeGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     88    }
    4289
    4390    public override IDeepCloneable Clone(Cloner cloner) {
     
    4592    }
    4693
    47     protected override SymbolicExpressionTree Create(
    48       IRandom random,
    49       ISymbolicExpressionGrammar grammar,
    50       IntValue maxTreeSize, IntValue maxTreeHeight,
    51       IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
    52       return Create(random, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
    53     }
    54 
    55     public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar,
     94    protected override SymbolicExpressionTree Create(IRandom random) {
     95      return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value,
     96        MaximumFunctionDefinitions.Value, MaximumFunctionArguments.Value);
     97    }
     98
     99    public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar,
    56100      int maxTreeSize, int maxTreeHeight,
    57101      int maxFunctionDefinitions, int maxFunctionArguments
     
    66110
    67111    private class TreeExtensionPoint {
    68       public SymbolicExpressionTreeNode Parent { get; set; }
     112      public ISymbolicExpressionTreeNode Parent { get; set; }
    69113      public int ChildIndex { get; set; }
    70114      public int ExtensionPointDepth { get; set; }
    71115    }
    72116
    73     public static SymbolicExpressionTreeNode PTC2(IRandom random, SymbolicExpressionTreeNode seedNode,
     117    public static ISymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
    74118      int maxTreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
    75119      // tree size is limited by the grammar and by the explicit size constraints
     
    79123      while (tries++ < MAX_TRIES) {
    80124        // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
    81         int treeSize = random.Next(allowedMinSize, allowedMaxSize + 1);
     125        int treeSize;
     126        treeSize = random.Next(allowedMinSize, allowedMaxSize + 1);
    82127        if (treeSize <= 1 || maxDepth <= 1) return seedNode;
    83128
     
    89134        } else {
    90135          // clean seedNode
    91           while (seedNode.SubTrees.Count > 0) seedNode.RemoveSubTree(0);
     136          while (seedNode.SubTrees.Count() > 0) seedNode.RemoveSubTree(0);
    92137        }
    93138        // try a different size MAX_TRIES times
     
    96141    }
    97142
    98     private static bool CreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, ISymbolicExpressionGrammar globalGrammar,
     143    private static bool CreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
    99144      int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
    100145      try {
     
    105150    }
    106151
    107     private static void TryCreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, ISymbolicExpressionGrammar globalGrammar,
     152    private static void TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar,
    108153      int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
    109154      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
     
    122167        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
    123168        extensionPoints.RemoveAt(randomIndex);
    124         SymbolicExpressionTreeNode parent = nextExtension.Parent;
     169        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
    125170        int argumentIndex = nextExtension.ChildIndex;
    126171        int extensionDepth = nextExtension.ExtensionPointDepth;
     
    128173          ReplaceWithMinimalTree(random, root, parent, argumentIndex, maxFunctionDefinitions, maxFunctionArguments);
    129174        } else {
    130           var allowedSymbols = from s in parent.Grammar.Symbols
    131                                where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
    132                                where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth
    133                                where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize
    134                                select s;
    135           Symbol selectedSymbol = SelectRandomSymbol(random, allowedSymbols);
    136           SymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
     175          var allowedSymbols = (from s in parent.Grammar.Symbols
     176                                where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
     177                                where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth
     178                                where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize
     179                                select s)
     180                               .ToList();
     181          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();
     182          var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
     183          ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
    137184          if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
    138185          parent.RemoveSubTree(argumentIndex);
     
    159206        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
    160207        extensionPoints.RemoveAt(randomIndex);
    161         SymbolicExpressionTreeNode parent = nextExtension.Parent;
     208        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
    162209        int a = nextExtension.ChildIndex;
    163210        int d = nextExtension.ExtensionPointDepth;
     
    166213    }
    167214
    168     private static void ReplaceWithMinimalTree(IRandom random, SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode parent, int argumentIndex, int maxFunctionDefinitions, int maxFunctionArguments) {
     215    private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
     216      int argumentIndex, int maxFunctionDefinitions, int maxFunctionArguments) {
    169217      // determine possible symbols that will lead to the smallest possible tree
    170218      var possibleSymbols = (from s in parent.GetAllowedSymbols(argumentIndex)
    171219                             group s by parent.Grammar.GetMinExpressionLength(s) into g
    172220                             orderby g.Key
    173                              select g).First();
    174       var selectedSymbol = SelectRandomSymbol(random, possibleSymbols);
     221                             select g).First().ToList();
     222      var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
     223      var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
    175224      var tree = selectedSymbol.CreateTreeNode();
    176225      if (tree.HasLocalParameters) tree.ResetLocalParameters(random);
     
    187236    }
    188237
    189     private static void InitializeNewTreeNode(IRandom random, SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) {
     238    private static void InitializeNewTreeNode(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) {
    190239      // NB it is assumed that defuns are only allowed as children of root and nowhere else
    191240      // also assumes that newTree is already attached to root somewhere
    192241      if (IsTopLevelBranch(root, newTree)) {
    193         ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionGrammar)root.Grammar.Clone());
     242        ((SymbolicExpressionTreeTopLevelNode)newTree).SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
    194243
    195244        // allow invokes of existing ADFs with higher index
    196         int argIndex = root.SubTrees.IndexOf(newTree);
    197         for (int i = argIndex + 1; i < root.SubTrees.Count; i++) {
    198           var otherDefunNode = root.SubTrees[i] as DefunTreeNode;
     245        int argIndex = root.IndexOfSubTree(newTree);
     246        for (int i = argIndex + 1; i < root.SubTrees.Count(); i++) {
     247          var otherDefunNode = root.GetSubTree(i) as DefunTreeNode;
    199248          if (otherDefunNode != null) {
    200249            GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
     
    219268        }
    220269        // in existing branches with smaller index allow invoke of current function
    221         int argIndex = root.SubTrees.IndexOf(newTree);
     270        int argIndex = root.IndexOfSubTree(newTree);
    222271        for (int i = 0; i < argIndex; i++) {
    223272          // if not dummy node
    224           if (root.SubTrees[i].Symbol != null) {
    225             var existingBranch = root.SubTrees[i];
     273          if (root.GetSubTree(i).Symbol != null) {
     274            var existingBranch = root.GetSubTree(i);
    226275            GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs);
    227276          }
     
    230279    }
    231280
    232     private static bool IsTopLevelBranch(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode branch) {
     281    private static bool IsTopLevelBranch(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode branch) {
    233282      return branch is SymbolicExpressionTreeTopLevelNode;
    234283    }
    235284
    236     private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
    237       var symbolList = symbols.ToList();
    238       var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();
    239       if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");
    240       var r = random.NextDouble() * ticketsSum;
    241       double aggregatedTickets = 0;
    242       for (int i = 0; i < symbolList.Count; i++) {
    243         aggregatedTickets += symbolList[i].InitialFrequency;
    244         if (aggregatedTickets > r) {
    245           return symbolList[i];
    246         }
    247       }
    248       // this should never happen
    249       throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");
    250     }
    251 
    252     private static int SampleArity(IRandom random, SymbolicExpressionTreeNode node, int targetSize) {
     285    private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetSize) {
    253286      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
    254287      int minArity = node.GetMinSubtreeCount();
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs

    r5445 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    2625using HeuristicLab.Parameters;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2827
    29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3029  /// <summary>
    3130  /// A base class for operators creating symbolic expression trees.
     
    3433  [StorableClass]
    3534  public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator {
    36     private const string MaxFunctionDefinitionsParameterName = "MaxFunctionDefinitions";
    37     private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
    3835    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
    3936    #region Parameter Properties
    40     public IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {
    41       get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionDefinitionsParameterName]; }
    42     }
    43     public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter {
    44       get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
    45     }
    4637    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    4738      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     
    5041
    5142    #region Propeties
    52     public IntValue MaxFunctionDefinitions {
    53       get { return MaxFunctionDefinitionsParameter.ActualValue; }
    54     }
    55     public IntValue MaxFunctionArguments {
    56       get { return MaxFunctionArgumentsParameter.ActualValue; }
    57     }
    5843    public SymbolicExpressionTree SymbolicExpressionTree {
    5944      get { return SymbolicExpressionTreeParameter.ActualValue; }
     
    6752    protected SymbolicExpressionTreeCreator()
    6853      : base() {
    69       Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefinitionsParameterName, "Maximal number of function definitions in the symbolic expression tree."));
    70       Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "Maximal number of arguments of automatically defined functions in the symbolic expression tree."));
    7154      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be created."));
    7255    }
    7356
    7457    public sealed override IOperation Apply() {
    75       SymbolicExpressionTree = Create(Random, SymbolicExpressionGrammar,
    76         MaxTreeSize, MaxTreeHeight, MaxFunctionDefinitions, MaxFunctionArguments);
     58      SymbolicExpressionTree = Create(Random);
    7759      return base.Apply();
    7860    }
    7961
    80     protected abstract SymbolicExpressionTree Create(
    81       IRandom random,
    82       ISymbolicExpressionGrammar grammar,
    83       IntValue maxTreeSize, IntValue maxTreeHeight,
    84       IntValue maxFunctionDefinitions, IntValue maxFunctionArguments
    85       );
     62    protected abstract SymbolicExpressionTree Create(IRandom random);
    8663  }
    8764}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/CrossoverPoint.cs

    r5445 r5499  
    2121
    2222
    23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
    24   public class CrossoverPoint {
     23namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     24  internal class CrossoverPoint {
    2525    public SymbolicExpressionTreeNode Parent { get; set; }
    2626    public SymbolicExpressionTreeNode Child { get; set; }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs

    r5445 r5499  
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3030
    31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
     31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3232  /// <summary>
    3333  /// Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
     
    3838  [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")]
    3939  [StorableClass]
    40   public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover {
     40  public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
     41    private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
     42    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     43    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     44    #region Parameter Properties
    4145    public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
    42       get { return (IValueLookupParameter<PercentValue>)Parameters["InternalCrossoverPointProbability"]; }
    43     }
     46      get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
     47    }
     48    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     49      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
     50    }
     51    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
     52      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
     53    }
     54    #endregion
     55    #region Properties
     56    public PercentValue InternalCrossoverPointProbability {
     57      get { return InternalCrossoverPointProbabilityParameter.ActualValue; }
     58    }
     59    public IntValue MaximumSymbolicExpressionTreeLength {
     60      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
     61    }
     62    public IntValue MaximumSymbolicExpressionTreeDepth {
     63      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
     64    }
     65    #endregion
    4466    [StorableConstructor]
    4567    private SubtreeCrossover(bool deserializing) : base(deserializing) { }
     
    4769    public SubtreeCrossover()
    4870      : base() {
    49       Parameters.Add(new ValueLookupParameter<PercentValue>("InternalCrossoverPointProbability", "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
     71      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
     72      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     73      Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
    5074    }
    5175
     
    5579
    5680    protected override SymbolicExpressionTree Cross(IRandom random,
    57       SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
    58       IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
    59       return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
     81      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1) {
     82      return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value,
     83        MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
    6084    }
    6185
    6286    public static SymbolicExpressionTree Cross(IRandom random,
    6387      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
    64       double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight, out bool success) {
     88      double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight) {
    6589      // select a random crossover point in the first parent
    6690      SymbolicExpressionTreeNode crossoverPoint0;
     
    81105
    82106      if (allowedBranches.Count == 0) {
    83         success = false;
    84107        return parent0;
    85108      } else {
     
    90113        crossoverPoint0.RemoveSubTree(replacedSubtreeIndex);
    91114        crossoverPoint0.InsertSubTree(replacedSubtreeIndex, selectedBranch);
    92         success = true;
    93115        return parent0;
    94116      }
     
    190212    }
    191213
    192     private static int GetBranchLevel(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode point) {
     214    private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
    193215      if (root == point) return 0;
    194216      foreach (var subtree in root.SubTrees) {
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs

    r5445 r5499  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Data;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    2726using HeuristicLab.Parameters;
    2827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2928
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3130  /// <summary>
    3231  /// A base class for operators that perform a crossover of symbolic expression trees.
     
    3736    private const string ParentsParameterName = "Parents";
    3837    private const string ChildParameterName = "Child";
    39     private const string FailedCrossoverEventsParameterName = "FailedCrossoverEvents";
     38    #region Parameter Properties
    4039    public ILookupParameter<ItemArray<SymbolicExpressionTree>> ParentsParameter {
    4140      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[ParentsParameterName]; }
     
    4443      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ChildParameterName]; }
    4544    }
    46     public IValueParameter<IntValue> FailedCrossoverEventsParameter {
    47       get { return (ValueParameter<IntValue>)Parameters[FailedCrossoverEventsParameterName]; }
     45    #endregion
     46    #region Properties
     47    public ItemArray<SymbolicExpressionTree> Parents {
     48      get { return ParentsParameter.ActualValue; }
    4849    }
    49 
    50     public IntValue FailedCrossoverEvents {
    51       get { return FailedCrossoverEventsParameter.Value; }
     50    public SymbolicExpressionTree Child {
     51      get { return ChildParameter.ActualValue; }
     52      set { ChildParameter.ActualValue = value; }
    5253    }
     54    #endregion
    5355    [StorableConstructor]
    5456    protected SymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { }
     
    5860      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
    5961      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
    60       Parameters.Add(new ValueParameter<IntValue>(FailedCrossoverEventsParameterName, "The number of failed crossover events (child is an exact copy of a parent)", new IntValue()));
    6162    }
    6263
    6364    public sealed override IOperation Apply() {
    64       if (ParentsParameter.ActualValue.Length != 2)
     65      if (Parents.Length != 2)
    6566        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
    6667
    67       SymbolicExpressionTree parent0 = ParentsParameter.ActualValue[0];
    68       SymbolicExpressionTree parent1 = ParentsParameter.ActualValue[1];
     68      SymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]);
    6969
    70       IRandom random = RandomParameter.ActualValue;
    71 
    72       bool success;
    73       SymbolicExpressionTree result = Cross(random, parent0, parent1,
    74         MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success);
    75 
    76       if (!success) FailedCrossoverEvents.Value++;
    77 
    78       ChildParameter.ActualValue = result;
     70      Child = result;
    7971      return base.Apply();
    8072    }
    8173
    8274    protected abstract SymbolicExpressionTree Cross(IRandom random,
    83       SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
    84       IntValue maxTreeSize, IntValue maxTreeHeight, out bool success);
     75      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1);
    8576  }
    8677}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/DefaultSymbolicExpressionGrammar.cs

    r5473 r5499  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2928
     
    3736  [StorableClass]
    3837  [Item("DefaultSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
    39   public abstract class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionGrammar {
     38  public abstract class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionTreeGrammar {
    4039
    4140    #region properties for separation between implementation and persistence
     
    133132    }
    134133
    135     protected DefaultSymbolicExpressionGrammar(ISymbolicExpressionGrammar grammar)
     134    protected DefaultSymbolicExpressionGrammar(ISymbolicExpressionTreeGrammar grammar)
    136135      : base() {
    137136      Cloner cloner = new Cloner();
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeStringFormatter.cs

    r5445 r5499  
    2121
    2222using System.Text;
     23using System.Linq;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2627
    27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters {
     28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2829
    2930  [Item("SymbolicExpressionTreeStringFormatter", "The default string formatter for symbolic expression trees.")]
     
    4950    }
    5051
    51     private string FormatRecursively(SymbolicExpressionTreeNode node, int indentLength) {
     52    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength) {
    5253      StringBuilder strBuilder = new StringBuilder();
    5354      if (Indent) strBuilder.Append(' ', indentLength);
    5455      strBuilder.Append("(");
    5556      // internal nodes or leaf nodes?
    56       if (node.SubTrees.Count > 0) {
     57      if (node.SubTrees.Count() > 0) {
    5758        // symbol on same line as '('
    5859        strBuilder.AppendLine(node.ToString());
     
    7576      return new SymbolicExpressionTreeStringFormatter(this, cloner);
    7677    }
    77 
    7878  }
    79 
    8079}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/GlobalSymbolicExpressionGrammar.cs

    r5473 r5499  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2827
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj

    r5494 r5499  
    123123    <Compile Include="Compiler\Instruction.cs" />
    124124    <Compile Include="Compiler\SymbolicExpressionTreeCompiler.cs" />
     125    <Compile Include="Interfaces\ISymbolicExpressionTreeGrammar.cs" />
     126    <Compile Include="Interfaces\Operators\ISymbolicExpressionTreeArchitectureAlteringOperator.cs" />
     127    <Compile Include="Interfaces\Operators\ISymbolicExpressionTreeGrammarBasedOperator.cs" />
     128    <Compile Include="SymbolicExpressionTreeSizeConstraintOperator.cs" />
    125129    <Compile Include="Creators\SymbolicExpressionTreeCreator.cs" />
    126130    <Compile Include="Crossovers\CrossoverPoint.cs" />
     
    155159    <Compile Include="SymbolicExpressionTreeOperator.cs" />
    156160    <Compile Include="SymbolicExpressionTreeTerminalNode.cs" />
    157     <Compile Include="Interfaces\ISymbolicExpressionGrammar.cs" />
    158161    <Compile Include="Creators\ProbabilisticTreeCreator.cs" />
    159162    <Compile Include="HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs" />
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs

    r5494 r5499  
    2525  public interface ISymbol : INamedItem {
    2626    ISymbolicExpressionTreeNode CreateTreeNode();
     27    double InitialFrequency;
    2728  }
    2829}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTree.cs

    r5494 r5499  
    2626  public interface ISymbolicExpressionTree : IItem {
    2727    ISymbolicExpressionTreeNode Root { get; }
    28     uint Size { get; }
    29     uint Height { get; }
     28    int Size { get; }
     29    int Height { get; }
    3030
    3131    IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix();
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeGrammar.cs

    r5494 r5499  
    2424
    2525namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    26   public interface ISymbolicExpressionGrammar : IItem {
     26  public interface ISymbolicExpressionTreeGrammar : IItem {
    2727    ISymbol StartSymbol { get; }
    2828    void AddSymbol(ISymbol symbol);
    2929    void RemoveSymbol(ISymbol symbol);
    30     IEnumerable<ISymbol> ISymbol { get; }
     30    IEnumerable<ISymbol> Symbols { get; }
    3131    bool ContainsSymbol(ISymbol symbol);
    3232    void SetAllowedChild(ISymbol parent, ISymbol child, int argumentIndex);
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs

    r5494 r5499  
    2121using System.Collections.Generic;
    2222using HeuristicLab.Core;
     23using System;
    2324namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2425  public interface ISymbolicExpressionTreeNode : IItem {
    25     ISymbolicExpressionGrammar Grammar { get; }
     26    ISymbolicExpressionTreeGrammar Grammar { get; }
    2627
    2728    ISymbol Symbol { get; }
     
    3637    IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix();
    3738    IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix();
     39    void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a);
     40    void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a);
    3841
    39     IEnumerable<SymbolicExpressionTreeNode> SubTrees { get; }
     42    IEnumerable<ISymbolicExpressionTreeNode> SubTrees { get; }
     43    ISymbolicExpressionTreeNode GetSubTree(int index);
     44    int IndexOfSubTree(ISymbolicExpressionTreeNode tree);
    4045    void AddSubTree(ISymbolicExpressionTreeNode tree);
    4146    void InsertSubTree(int index, ISymbolicExpressionTreeNode tree);
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeArchitectureManipulator.cs

    r5494 r5499  
    2727  /// Interface for operators that manipulate symbolic expression trees.
    2828  /// </summary>
    29   public interface ISymbolicExpressionTreeArchitectureManipulator : ISymbolicExpressionTreeManipulator {
    30     IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter { get; }
    31     IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter { get; }
     29  public interface ISymbolicExpressionTreeArchitectureManipulator : ISymbolicExpressionTreeManipulator, ISymbolicExpressionTreeArchitectureAlteringOperator {
    3230  }
    3331}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeSizeConstraintOperator.cs

    r5494 r5499  
    2525namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  public interface ISymbolicExpressionTreeSizeConstraintOperator : ISymbolicExpressionTreeOperator {
    27     IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepth { get; }
    28     IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLength { get; }
     27    IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { get; }
     28    IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { get; }
    2929  }
    3030}
  • 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}
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTree.cs

    r5473 r5499  
    3030  [StorableClass]
    3131  [Item("SymbolicExpressionTree", "Represents a symbolic expression tree.")]
    32   public class SymbolicExpressionTree : Item {
     32  public class SymbolicExpressionTree : Item, ISymbolicExpressionTree {
    3333    public override Image ItemImage {
    3434      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
    3535    }
    3636    [Storable]
    37     private SymbolicExpressionTreeNode root;
    38     public SymbolicExpressionTreeNode Root {
     37    private ISymbolicExpressionTreeNode root;
     38    public ISymbolicExpressionTreeNode Root {
    3939      get { return root; }
    4040      set {
     
    7575    }
    7676
    77     public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
     77    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() {
    7878      if (root == null)
    7979        return new SymbolicExpressionTreeNode[0];
    8080      return root.IterateNodesPrefix();
    8181    }
    82     public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
     82    public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() {
    8383      if (root == null)
    8484        return new SymbolicExpressionTreeNode[0];
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r5473 r5499  
    2525namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  public sealed class SymbolicExpressionTreeGrammar : DefaultSymbolicExpressionGrammar {
    27     public SymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar)
     27    public SymbolicExpressionTreeGrammar(ISymbolicExpressionTreeGrammar grammar)
    2828      : base(grammar) {
    2929    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs

    r5473 r5499  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
    27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2928
    3029namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3130  [StorableClass]
    32   public class SymbolicExpressionTreeNode : DeepCloneable {
     31  public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode {
    3332    [Storable]
    3433    private IList<SymbolicExpressionTreeNode> subTrees;
     
    9695    }
    9796
    98     public virtual ISymbolicExpressionGrammar Grammar {
     97    public virtual ISymbolicExpressionTreeGrammar Grammar {
    9998      get { return parent.Grammar; }
    10099    }
     
    175174    }
    176175
    177     public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
     176    public IEnumerable<ISymbol> GetAllowedSymbols(int argumentIndex) {
    178177      return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
    179178    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeOperator.cs

    r5473 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    2625using HeuristicLab.Operators;
    2726using HeuristicLab.Optimization;
     
    3736  public abstract class SymbolicExpressionTreeOperator : SingleSuccessorOperator, IStochasticOperator, ISymbolicExpressionTreeOperator {
    3837    private const string RandomParameterName = "Random";
    39     private const string MaxTreeSizeParameterName = "MaxTreeSize";
    40     private const string MaxTreeHeightParameterName = "MaxTreeHeight";
    41     private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar";
    4238
    4339    public override bool CanChangeName {
     
    4945      get { return (LookupParameter<IRandom>)Parameters[RandomParameterName]; }
    5046    }
    51     public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
    52       get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
    53     }
    54     public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
    55       get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
    56     }
    57     public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
    58       get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
    59     }
    6047    #endregion
    6148
     
    6350    public IRandom Random {
    6451      get { return RandomParameter.ActualValue; }
    65     }
    66     public IntValue MaxTreeSize {
    67       get { return MaxTreeSizeParameter.ActualValue; }
    68     }
    69     public IntValue MaxTreeHeight {
    70       get { return MaxTreeHeightParameter.ActualValue; }
    71     }
    72     public ISymbolicExpressionGrammar SymbolicExpressionGrammar {
    73       get { return SymbolicExpressionGrammarParameter.ActualValue; }
    7452    }
    7553    #endregion
     
    8159      : base() {
    8260      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The pseudo random number generator which should be used for symbolic expression tree operators."));
    83       Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
    84       Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
    85       Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
    8661    }
    8762  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTerminalNode.cs

    r5473 r5499  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Common;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2726
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTopLevelNode.cs

    r5473 r5499  
    2121
    2222using HeuristicLab.Common;
    23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2423using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2524
     
    2827  public class SymbolicExpressionTreeTopLevelNode : SymbolicExpressionTreeNode {
    2928    [Storable]
    30     private ISymbolicExpressionGrammar grammar;
    31     public override ISymbolicExpressionGrammar Grammar {
     29    private ISymbolicExpressionTreeGrammar grammar;
     30    public override ISymbolicExpressionTreeGrammar Grammar {
    3231      get { return grammar; }
    3332    }
    34     public void SetGrammar(ISymbolicExpressionGrammar grammar) {
     33    public void SetGrammar(ISymbolicExpressionTreeGrammar grammar) {
    3534      this.grammar = grammar;
    3635    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Argument.cs

    r5445 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  /// <summary>
    2727  /// Symbol for function arguments
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/ArgumentTreeNode.cs

    r5445 r5499  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  [StorableClass]
    2727  public sealed class ArgumentTreeNode : SymbolicExpressionTreeTerminalNode {
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Defun.cs

    r5445 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  /// <summary>
    2727  /// Symbol for function defining branches
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/DefunTreeNode.cs

    r5445 r5499  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     24namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2525  [StorableClass]
    2626  public sealed class DefunTreeNode : SymbolicExpressionTreeTopLevelNode {
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunction.cs

    r5445 r5499  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2727  /// <summary>
    2828  /// Symbol for invoking automatically defined functions
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunctionTreeNode.cs

    r5445 r5499  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  [StorableClass]
    2727  public sealed class InvokeFunctionTreeNode : SymbolicExpressionTreeNode {
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/ProgramRootSymbol.cs

    r5445 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  [StorableClass]
    2727  [Item(ProgramRootSymbol.ProgramRootSymbolName, ProgramRootSymbol.ProgramRootSymbolDescription)]
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/ReadOnlySymbol.cs

    r5445 r5499  
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2525
    26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2727  [StorableClass]
    2828  [Item("ReadOnlySymbol", "Represents a symbol in a symbolic function tree that cannot be modified.")]
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/StartSymbol.cs

    r5445 r5499  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2626  [StorableClass]
    2727  [Item(StartSymbol.StartSymbolName, StartSymbol.StartSymbolDescription)]
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs

    r5445 r5499  
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2626
    27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols {
     27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2828  [StorableClass]
    2929  [Item("Symbol", "Represents a symbol in a symbolic function tree.")]
    30   public abstract class Symbol : NamedItem {
     30  public abstract class Symbol : NamedItem, ISymbol {
    3131    #region Properties
    3232    [Storable]
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.ArtificialAnt/3.4/ArtificialAntProblem.cs

    r5473 r5499  
    311311      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
    312312      operators.Add(new BestAntTrailAnalyzer());
    313       operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
     313      operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
    314314      operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    315315      ParameterizeAnalyzers();
Note: See TracChangeset for help on using the changeset viewer.