[3418] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3418] | 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;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[3418] | 26 | using HeuristicLab.Core;
|
---|
[4068] | 27 | using HeuristicLab.Data;
|
---|
[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 |
|
---|
[5499] | 34 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3534] | 35 | [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
|
---|
[3419] | 36 | [StorableClass]
|
---|
[5499] | 37 | public sealed class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>,
|
---|
| 38 | ISymbolicExpressionTreeManipulator,
|
---|
| 39 | IStochasticOperator,
|
---|
| 40 | ISymbolicExpressionTreeSizeConstraintOperator {
|
---|
[3534] | 41 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
[5499] | 42 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 43 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
[3534] | 44 |
|
---|
[3418] | 45 | public override bool CanChangeName {
|
---|
| 46 | get { return false; }
|
---|
| 47 | }
|
---|
[3425] | 48 | protected override bool CreateChildOperation {
|
---|
| 49 | get { return true; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[5499] | 52 | #region Parameter Properties
|
---|
| 53 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 54 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
[3418] | 55 | }
|
---|
[5499] | 56 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 57 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
[3418] | 58 | }
|
---|
[5499] | 59 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 60 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[3534] | 61 | }
|
---|
| 62 | #endregion
|
---|
[3418] | 63 |
|
---|
| 64 | [StorableConstructor]
|
---|
[3534] | 65 | private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 66 | private MultiSymbolicExpressionTreeManipulator(MultiSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3534] | 67 | public MultiSymbolicExpressionTreeManipulator()
|
---|
[3418] | 68 | : base() {
|
---|
[5499] | 69 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
| 70 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 71 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
[3674] | 72 |
|
---|
[5719] | 73 | CheckedItemList<ISymbolicExpressionTreeManipulator> list = new CheckedItemList<ISymbolicExpressionTreeManipulator>();
|
---|
[3674] | 74 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
|
---|
[5719] | 75 | if (!typeof(IMultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type) &&
|
---|
| 76 | !typeof(ISymbolicExpressionTreeArchitectureAlteringOperator).IsAssignableFrom(type))
|
---|
| 77 | list.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type), true);
|
---|
[3674] | 78 | }
|
---|
[5719] | 79 | Operators = list.AsReadOnly();
|
---|
| 80 | Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems));
|
---|
[3418] | 81 | }
|
---|
| 82 |
|
---|
[4722] | 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new MultiSymbolicExpressionTreeManipulator(this, cloner);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[3534] | 87 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
[3445] | 88 | base.Operators_ItemsReplaced(sender, e);
|
---|
[3534] | 89 | ParameterizeManipulators();
|
---|
[3425] | 90 | }
|
---|
| 91 |
|
---|
[3534] | 92 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
[3445] | 93 | base.Operators_ItemsAdded(sender, e);
|
---|
[3534] | 94 | ParameterizeManipulators();
|
---|
[3418] | 95 | }
|
---|
| 96 |
|
---|
[3534] | 97 | private void ParameterizeManipulators() {
|
---|
[5499] | 98 | foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
|
---|
| 99 | manipulator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 100 | }
|
---|
[3534] | 101 | foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
| 102 | manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
[3418] | 103 | }
|
---|
[5499] | 104 | foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
| 105 | manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
|
---|
| 106 | manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
|
---|
[3418] | 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|