Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/MultiSymbolicExpressionTreeArchitectureManipulator.cs @ 5386

Last change on this file since 5386 was 5015, checked in by mkommend, 13 years ago

Moved check for max expression depth from tree node to the manipulator and corrected `MultiSymbolicExpressionTreeArchitectureManipulator' (ticket #1315).

File size: 7.7 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.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
36  [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")]
37  [StorableClass]
38  public sealed class MultiSymbolicExpressionTreeArchitectureManipulator : StochasticMultiBranch<ISymbolicExpressionTreeArchitectureManipulator>, ISymbolicExpressionTreeArchitectureManipulator, IStochasticOperator {
39    private const string MaxTreeSizeParameterName = "MaxTreeSize";
40    private const string MaxTreeHeightParameterName = "MaxTreeHeight";
41    private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar";
42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
43    private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
44    private const string MaxFunctionDefiningBranchesParameterName = "MaxFunctionDefiningBranches";
45
46    public override bool CanChangeName {
47      get { return false; }
48    }
49    protected override bool CreateChildOperation {
50      get { return true; }
51    }
52    #region ISymbolicExpressionTreeArchitectureManipulator Members
53    public IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {
54      get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionDefiningBranchesParameterName]; }
55    }
56    public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
58    }
59    #endregion
60
61    #region ISymbolicExpressionTreeManipulator Members
62    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
63      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
64    }
65    #endregion
66
67    #region ISymbolicExpressionTreeOperator Members
68    public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
69      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
70    }
71    public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
72      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
73    }
74    public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
75      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
76    }
77    #endregion
78
79
80    [StorableConstructor]
81    private MultiSymbolicExpressionTreeArchitectureManipulator(bool deserializing) : base(deserializing) { }
82    private MultiSymbolicExpressionTreeArchitectureManipulator(MultiSymbolicExpressionTreeArchitectureManipulator original, Cloner cloner) : base(original, cloner) { }
83    public MultiSymbolicExpressionTreeArchitectureManipulator()
84      : base() {
85      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
86      Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefiningBranchesParameterName, "The maximal allowed number of function defining branches."));
87      Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "The maximal allowed number of arguments of a newly created function."));
88      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
89      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
90      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
91
92      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeArchitectureManipulator))) {
93        if (!typeof(MultiOperator<ISymbolicExpressionTreeArchitectureManipulator>).IsAssignableFrom(type))
94          Operators.Add((ISymbolicExpressionTreeArchitectureManipulator)Activator.CreateInstance(type), true);
95      }
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new MultiSymbolicExpressionTreeArchitectureManipulator(this, cloner);
100    }
101
102    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
103      base.Operators_ItemsReplaced(sender, e);
104      ParameterizeManipulators();
105    }
106
107    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
108      base.Operators_ItemsAdded(sender, e);
109      ParameterizeManipulators();
110    }
111
112    private void ParameterizeManipulators() {
113      foreach (ISymbolicExpressionTreeArchitectureManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
114        manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;
115        manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;
116        manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;
117        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
118        manipulator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
119        manipulator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
120      }
121
122      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
123        manipulator.RandomParameter.ActualName = RandomParameter.Name;
124      }
125    }
126
127    #region ISymbolicExpressionTreeArchitectureManipulator Members
128    public void ModifyArchitecture(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, out bool success) {
129      var op = Operators.SelectRandom(random);
130      op.ModifyArchitecture(random, symbolicExpressionTree, grammar, maxTreeSize, maxTreeHeight, maxFunctionDefiningBranches, maxFunctionArguments, out success);
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.