[3418] | 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 |
|
---|
[3527] | 22 | using System;
|
---|
[3425] | 23 | using System.Linq;
|
---|
[3418] | 24 | using HeuristicLab.Collections;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[4068] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
|
---|
[3418] | 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3569] | 32 | using HeuristicLab.PluginInfrastructure;
|
---|
[3418] | 33 |
|
---|
[3534] | 34 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
|
---|
| 35 | [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
|
---|
[3419] | 36 | [StorableClass]
|
---|
[3593] | 37 | public class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>, ISymbolicExpressionTreeManipulator, IStochasticOperator {
|
---|
[3534] | 38 | private const string MaxTreeSizeParameterName = "MaxTreeSize";
|
---|
| 39 | private const string MaxTreeHeightParameterName = "MaxTreeHeight";
|
---|
| 40 | private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar";
|
---|
| 41 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
| 42 |
|
---|
[3418] | 43 | public override bool CanChangeName {
|
---|
| 44 | get { return false; }
|
---|
| 45 | }
|
---|
[3425] | 46 | protected override bool CreateChildOperation {
|
---|
| 47 | get { return true; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[3534] | 50 | #region ISymbolicExpressionTreeManipulator Members
|
---|
| 51 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 52 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[3418] | 53 | }
|
---|
[3534] | 54 | #endregion
|
---|
[3418] | 55 |
|
---|
[3534] | 56 | #region ISymbolicExpressionTreeOperator Members
|
---|
| 57 | public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
|
---|
| 58 | get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
|
---|
[3418] | 59 | }
|
---|
[3534] | 60 | public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
|
---|
| 61 | get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
|
---|
| 62 | }
|
---|
| 63 | public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
|
---|
| 64 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
|
---|
| 65 | }
|
---|
| 66 | #endregion
|
---|
[3418] | 67 |
|
---|
[3534] | 68 |
|
---|
[3418] | 69 | [StorableConstructor]
|
---|
[3534] | 70 | private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 71 | public MultiSymbolicExpressionTreeManipulator()
|
---|
[3418] | 72 | : base() {
|
---|
[3534] | 73 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
| 74 | Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
|
---|
| 75 | Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
|
---|
| 76 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
|
---|
[3674] | 77 |
|
---|
| 78 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
|
---|
| 79 | if (!typeof(MultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type) && !typeof(ISymbolicExpressionTreeArchitectureManipulator).IsAssignableFrom(type))
|
---|
| 80 | Operators.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type), true);
|
---|
| 81 | }
|
---|
[3418] | 82 | }
|
---|
| 83 |
|
---|
[3534] | 84 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
[3445] | 85 | base.Operators_ItemsReplaced(sender, e);
|
---|
[3534] | 86 | ParameterizeManipulators();
|
---|
[3425] | 87 | }
|
---|
| 88 |
|
---|
[3534] | 89 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
[3445] | 90 | base.Operators_ItemsAdded(sender, e);
|
---|
[3534] | 91 | ParameterizeManipulators();
|
---|
[3418] | 92 | }
|
---|
| 93 |
|
---|
[3534] | 94 | private void ParameterizeManipulators() {
|
---|
| 95 | foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
| 96 | manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;
|
---|
| 97 | manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;
|
---|
| 98 | manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;
|
---|
| 99 | manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
[3418] | 100 | }
|
---|
[3534] | 101 |
|
---|
| 102 | foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
|
---|
| 103 | manipulator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
[3418] | 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|