Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/MultiSymbolicExpressionTreeArchitectureManipulator.cs @ 3535

Last change on this file since 3535 was 3534, checked in by gkronber, 14 years ago

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

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators {
34  [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")]
35  [StorableClass]
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; }
46    }
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]; }
56    }
57    #endregion
58
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()
82      : base() {
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());
96    }
97
98    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
99      base.Operators_ItemsReplaced(sender, e);
100      ParameterizeManipulators();
101    }
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
134  }
135}
Note: See TracBrowser for help on using the repository browser.