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