Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Manipulators/MultiSymbolicExpressionTreeManipulator.cs @ 15529

Last change on this file since 15529 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Manipulators {
36  [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
37  [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";
42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
43
44    public override bool CanChangeName {
45      get { return false; }
46    }
47    protected override bool CreateChildOperation {
48      get { return true; }
49    }
50
51    #region ISymbolicExpressionTreeManipulator Members
52    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
53      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
54    }
55    #endregion
56
57    #region ISymbolicExpressionTreeOperator Members
58    public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
59      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
60    }
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]; }
66    }
67    #endregion
68
69    [StorableConstructor]
70    private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
71    private MultiSymbolicExpressionTreeManipulator(MultiSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
72    public MultiSymbolicExpressionTreeManipulator()
73      : 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."));
78
79      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
80        if (!typeof(MultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type) && !typeof(ISymbolicExpressionTreeArchitectureManipulator).IsAssignableFrom(type))
81          Operators.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type), true);
82      }
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new MultiSymbolicExpressionTreeManipulator(this, cloner);
87    }
88
89    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
90      base.Operators_ItemsReplaced(sender, e);
91      ParameterizeManipulators();
92    }
93
94    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
95      base.Operators_ItemsAdded(sender, e);
96      ParameterizeManipulators();
97    }
98
99    private void ParameterizeManipulators() {
100      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;
104        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
105      }
106
107      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
108        manipulator.RandomParameter.ActualName = RandomParameter.Name;
109      }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.