1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
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>,
|
---|
39 | ITracingSymbolicExpressionTreeOperator,
|
---|
40 | ISymbolicExpressionTreeManipulator,
|
---|
41 | IStochasticOperator,
|
---|
42 | ISymbolicExpressionTreeSizeConstraintOperator {
|
---|
43 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
44 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
45 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
46 | private const string SymbolicExpressionTreeNodeComparerParameterName = "SymbolicExpressionTreeNodeComparer";
|
---|
47 | private const string SymbolicExpressionTreeNodeComparerParameterDescription = "The comparison operator used to check if two symbolic expression tree nodes are equal or similar.";
|
---|
48 |
|
---|
49 | public override bool CanChangeName {
|
---|
50 | get { return false; }
|
---|
51 | }
|
---|
52 | protected override bool CreateChildOperation {
|
---|
53 | get { return true; }
|
---|
54 | }
|
---|
55 | public ValueParameter<ISymbolicExpressionTreeNodeComparer> SymbolicExpressionTreeNodeComparerParameter {
|
---|
56 | get { return (ValueParameter<ISymbolicExpressionTreeNodeComparer>)Parameters[SymbolicExpressionTreeNodeComparerParameterName]; }
|
---|
57 | }
|
---|
58 | public ISymbolicExpressionTreeNodeComparer SymbolicExpressionTreeNodeComparer {
|
---|
59 | get { return (ISymbolicExpressionTreeNodeComparer)SymbolicExpressionTreeNodeComparerParameter.ActualValue; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | #region Parameter Properties
|
---|
63 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
64 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
67 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
68 | }
|
---|
69 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
70 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | [StorableConstructor]
|
---|
75 | private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
|
---|
76 | private MultiSymbolicExpressionTreeManipulator(MultiSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
77 | public MultiSymbolicExpressionTreeManipulator()
|
---|
78 | : base() {
|
---|
79 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
80 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
81 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
82 | Parameters.Add(new ValueParameter<ISymbolicExpressionTreeNodeComparer>(SymbolicExpressionTreeNodeComparerParameterName, SymbolicExpressionTreeNodeComparerParameterDescription));
|
---|
83 |
|
---|
84 | List<ISymbolicExpressionTreeManipulator> list = new List<ISymbolicExpressionTreeManipulator>();
|
---|
85 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
|
---|
86 | if (this.GetType().Assembly != type.Assembly) continue;
|
---|
87 | if (typeof(IMultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type)) continue;
|
---|
88 | if (typeof(ISymbolicExpressionTreeArchitectureAlteringOperator).IsAssignableFrom(type)) continue;
|
---|
89 | list.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type));
|
---|
90 | }
|
---|
91 | CheckedItemList<ISymbolicExpressionTreeManipulator> checkedItemList = new CheckedItemList<ISymbolicExpressionTreeManipulator>();
|
---|
92 | checkedItemList.AddRange(list.OrderBy(op => op.Name));
|
---|
93 | Operators = checkedItemList.AsReadOnly();
|
---|
94 | Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems));
|
---|
95 | }
|
---|
96 |
|
---|
97 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
98 | return new MultiSymbolicExpressionTreeManipulator(this, cloner);
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
102 | base.Operators_ItemsReplaced(sender, e);
|
---|
103 | ParameterizeManipulators();
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
107 | base.Operators_ItemsAdded(sender, e);
|
---|
108 | ParameterizeManipulators();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void ParameterizeManipulators() {
|
---|
112 | foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
|
---|
113 | manipulator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
114 | }
|
---|
115 | foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
116 | manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
117 | }
|
---|
118 | foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
119 | manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
|
---|
120 | manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
|
---|
121 | }
|
---|
122 | var comparers = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeNodeComparer>().ToList();
|
---|
123 | if (comparers.Count > 0)
|
---|
124 | foreach (var manipulator in Operators.OfType<ITracingSymbolicExpressionTreeOperator>()) {
|
---|
125 | manipulator.SymbolicExpressionTreeNodeComparerParameter.ActualValue = comparers.First();
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|