Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/26/10 15:47:10 (14 years ago)
Author:
gkronber
Message:

Added interfaces for symbolic expression tree operators and added multi manipulation operators. #937 (Data types and operators for symbolic expression tree encoding)

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/MultiSymbolicExpressionTreeArchitectureManipulator.cs

    r3529 r3534  
    2222using System;
    2323using System.Linq;
    24 using HeuristicLab.Common;
     24using HeuristicLab.Collections;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Data;
    2726using HeuristicLab.Operators;
    2827using HeuristicLab.Optimization;
    2928using HeuristicLab.Parameters;
    3029using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    32 using System.Collections.Generic;
    33 using System.Text;
     30using HeuristicLab.Data;
     31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    3432
    3533namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators {
    36   /// <summary>
    37   /// Manipulates a symbolic expression by applying one architecture altering operator randomly.
    38   /// </summary>
    39   [Item("RandomArchitectureAlteringOperator", "Manipulates a symbolic expression by applying one architecture altering operator randomly.")]
     34  [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")]
    4035  [StorableClass]
    41   public class RandomArchitectureAlteringOperator : SymbolicExpressionTreeArchitectureAlteringOperator {
    42     #region Parameter Properties
    43     public IValueParameter<ItemList<SymbolicExpressionTreeArchitectureAlteringOperator>> OperatorsParameter {
    44       get { return (IValueParameter<ItemList<SymbolicExpressionTreeArchitectureAlteringOperator>>)Parameters["Operators"]; }
     36  public class MultiSymbolicExpressionTreeArchitectureManipulator : StochasticMultiOperator<ISymbolicExpressionTreeArchitectureManipulator>, ISymbolicExpressionTreeArchitectureManipulator, IStochasticOperator {
     37    private const string MaxTreeSizeParameterName = "MaxTreeSize";
     38    private const string MaxTreeHeightParameterName = "MaxTreeHeight";
     39    private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar";
     40    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
     41    private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
     42    private const string MaxFunctionDefiningBranchesParameterName = "MaxFunctionDefiningBranches";
     43
     44    public override bool CanChangeName {
     45      get { return false; }
    4546    }
    46     #endregion
    47     #region Properties
    48     public ItemList<SymbolicExpressionTreeArchitectureAlteringOperator> Operators {
    49       get { return OperatorsParameter.Value; }
     47    protected override bool CreateChildOperation {
     48      get { return true; }
     49    }
     50    #region ISymbolicExpressionTreeArchitectureManipulator Members
     51    public IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {
     52      get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionDefiningBranchesParameterName]; }
     53    }
     54    public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter {
     55      get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
    5056    }
    5157    #endregion
    5258
    53     public RandomArchitectureAlteringOperator()
     59
     60    #region ISymbolicExpressionTreeManipulator Members
     61    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
     62      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     63    }
     64    #endregion
     65
     66    #region ISymbolicExpressionTreeOperator Members
     67    public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
     68      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
     69    }
     70    public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
     71      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
     72    }
     73    public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
     74      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
     75    }
     76    #endregion
     77
     78
     79    [StorableConstructor]
     80    private MultiSymbolicExpressionTreeArchitectureManipulator(bool deserializing) : base(deserializing) { }
     81    public MultiSymbolicExpressionTreeArchitectureManipulator()
    5482      : base() {
    55       var operators = new ItemList<SymbolicExpressionTreeArchitectureAlteringOperator>();
    56       operators.Add(new ArgumentCreater());
    57       operators.Add(new ArgumentDeleter());
    58       operators.Add(new ArgumentDuplicater());
    59       operators.Add(new SubroutineCreater());
    60       operators.Add(new SubroutineDeleter());
    61       operators.Add(new SubroutineDuplicater());
    62       Parameters.Add(new ValueParameter<ItemList<SymbolicExpressionTreeArchitectureAlteringOperator>>("Operators",
    63         "The list of architecture altering operators from which a random operator should be called.",
    64         operators));
     83      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
     84      Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefiningBranchesParameterName, "The maximal allowed number of function defining branches."));
     85      Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "The maximal allowed number of arguments of a newly created function."));
     86      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
     87      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
     88      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
     89
     90      Operators.Add(new ArgumentCreater());
     91      Operators.Add(new ArgumentDeleter());
     92      Operators.Add(new ArgumentDuplicater());
     93      Operators.Add(new SubroutineCreater());
     94      Operators.Add(new SubroutineDeleter());
     95      Operators.Add(new SubroutineDuplicater());
    6596    }
    6697
    67     public override void ModifyArchitecture(IRandom random, SymbolicExpressionTree tree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, out bool success) {
    68       var selectedOperator = Operators.SelectRandom(random);
    69       selectedOperator.ModifyArchitecture(random, tree, grammar, maxTreeSize, maxTreeHeight, maxFunctionDefiningBranches, maxFunctionArguments, out success);
     98    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
     99      base.Operators_ItemsReplaced(sender, e);
     100      ParameterizeManipulators();
    70101    }
     102
     103    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
     104      base.Operators_ItemsAdded(sender, e);
     105      ParameterizeManipulators();
     106    }
     107
     108    private void ParameterizeManipulators() {
     109      foreach (ISymbolicExpressionTreeArchitectureManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
     110        manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;
     111        manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;
     112        manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;
     113        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
     114        manipulator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
     115        manipulator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
     116      }
     117
     118      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
     119        manipulator.RandomParameter.ActualName = RandomParameter.Name;
     120      }
     121    }
     122
     123    public override IOperation Apply() {
     124      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one symbolic expression tree architecture manipulator to choose from.");
     125      return base.Apply();
     126    }
     127
     128    #region ISymbolicExpressionTreeArchitectureManipulator Members
     129    public void ModifyArchitecture(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, out bool success) {
     130      var op = Operators.SelectRandom(random);
     131      op.ModifyArchitecture(random, symbolicExpressionTree, grammar, maxTreeSize, maxTreeHeight, maxFunctionDefiningBranches, maxFunctionArguments, out success);
     132    }
     133    #endregion
    71134  }
    72135}
Note: See TracChangeset for help on using the changeset viewer.