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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
|
---|
35 | [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class MultiSymbolicExpressionTreeArchitectureManipulator : StochasticMultiBranch<ISymbolicExpressionTreeArchitectureManipulator>, ISymbolicExpressionTreeArchitectureManipulator, IStochasticOperator {
|
---|
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 | private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
|
---|
43 | private const string MaxFunctionDefiningBranchesParameterName = "MaxFunctionDefiningBranches";
|
---|
44 |
|
---|
45 | public override bool CanChangeName {
|
---|
46 | get { return false; }
|
---|
47 | }
|
---|
48 | protected override bool CreateChildOperation {
|
---|
49 | get { return true; }
|
---|
50 | }
|
---|
51 | #region ISymbolicExpressionTreeArchitectureManipulator Members
|
---|
52 | public IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {
|
---|
53 | get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionDefiningBranchesParameterName]; }
|
---|
54 | }
|
---|
55 | public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter {
|
---|
56 | get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 |
|
---|
61 | #region ISymbolicExpressionTreeManipulator Members
|
---|
62 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
63 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | #region ISymbolicExpressionTreeOperator Members
|
---|
68 | public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
|
---|
69 | get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
|
---|
70 | }
|
---|
71 | public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
|
---|
72 | get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
|
---|
73 | }
|
---|
74 | public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
|
---|
75 | get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 |
|
---|
80 | [StorableConstructor]
|
---|
81 | private MultiSymbolicExpressionTreeArchitectureManipulator(bool deserializing) : base(deserializing) { }
|
---|
82 | public MultiSymbolicExpressionTreeArchitectureManipulator()
|
---|
83 | : base() {
|
---|
84 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
85 | Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefiningBranchesParameterName, "The maximal allowed number of function defining branches."));
|
---|
86 | Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "The maximal allowed number of arguments of a newly created function."));
|
---|
87 | Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
|
---|
89 | Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
|
---|
90 |
|
---|
91 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeArchitectureManipulator))) {
|
---|
92 | if (!typeof(MultiOperator<ISymbolicExpressionTreeArchitectureManipulator>).IsAssignableFrom(type))
|
---|
93 | Operators.Add((ISymbolicExpressionTreeArchitectureManipulator)Activator.CreateInstance(type), true);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
|
---|
98 | base.Operators_ItemsReplaced(sender, e);
|
---|
99 | ParameterizeManipulators();
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeArchitectureManipulator>> e) {
|
---|
103 | base.Operators_ItemsAdded(sender, e);
|
---|
104 | ParameterizeManipulators();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void ParameterizeManipulators() {
|
---|
108 | foreach (ISymbolicExpressionTreeArchitectureManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
|
---|
109 | manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;
|
---|
110 | manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;
|
---|
111 | manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;
|
---|
112 | manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
113 | manipulator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
|
---|
114 | manipulator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
|
---|
115 | }
|
---|
116 |
|
---|
117 | foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
|
---|
118 | manipulator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | #region ISymbolicExpressionTreeArchitectureManipulator Members
|
---|
123 | public void ModifyArchitecture(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, out bool success) {
|
---|
124 | var op = Operators.SelectRandom(random);
|
---|
125 | op.ModifyArchitecture(random, symbolicExpressionTree, grammar, maxTreeSize, maxTreeHeight, maxFunctionDefiningBranches, maxFunctionArguments, out success);
|
---|
126 | }
|
---|
127 | #endregion
|
---|
128 | }
|
---|
129 | }
|
---|