Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/MultiSymbolicExpressionTreeArchitectureManipulator.cs @ 7227

Last change on this file since 7227 was 7052, checked in by mkommend, 13 years ago

#1654: Changed discovery in multi operators as the discovery by specifying a plugin description does not work in the LightWeightApplicationManager and therefore the classes were not useable in unit tests.

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  [Item("MultiSymbolicExpressionTreeArchitectureManipulator", "Randomly selects and applies one of its architecture manipulators every time it is called.")]
37  [StorableClass]
38  public sealed class MultiSymbolicExpressionTreeArchitectureManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>,
39    ISymbolicExpressionTreeArchitectureManipulator,
40    ISymbolicExpressionTreeSizeConstraintOperator,
41    IStochasticOperator {
42    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
43    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
44    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
45    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
46    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
47
48    public override bool CanChangeName {
49      get { return false; }
50    }
51    protected override bool CreateChildOperation {
52      get { return true; }
53    }
54    #region Parameter properties
55    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
56      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
57    }
58    public IValueLookupParameter<IntValue> MaximumFunctionDefinitionsParameter {
59      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
60    }
61    public IValueLookupParameter<IntValue> MaximumFunctionArgumentsParameter {
62      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
63    }
64    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
65      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
66    }
67    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
68      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
69    }
70    #endregion
71    #region Parameter Properties
72    public IntValue MaximumFunctionDefinitions {
73      get { return MaximumFunctionDefinitionsParameter.ActualValue; }
74    }
75    public IntValue MaximumFunctionArguments {
76      get { return MaximumFunctionArgumentsParameter.ActualValue; }
77    }
78    public IntValue MaximumSymbolicExpressionTreeLength {
79      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
80    }
81    public IntValue MaximumSymbolicExpressionTreeDepth {
82      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
83    }
84    #endregion
85
86
87    [StorableConstructor]
88    private MultiSymbolicExpressionTreeArchitectureManipulator(bool deserializing) : base(deserializing) { }
89    private MultiSymbolicExpressionTreeArchitectureManipulator(MultiSymbolicExpressionTreeArchitectureManipulator original, Cloner cloner) : base(original, cloner) { }
90    public MultiSymbolicExpressionTreeArchitectureManipulator()
91      : base() {
92      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
93      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionDefinitionsParameterName, "The maximal allowed number of automatically defined functions."));
94      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumFunctionArgumentsParameterName, "The maximal allowed number of arguments of a automatically defined functions."));
95      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
96      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
97
98      List<ISymbolicExpressionTreeManipulator> list = new List<ISymbolicExpressionTreeManipulator>();
99      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
100        if (this.GetType().Assembly != type.Assembly) continue;
101        if (typeof(IMultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type)) continue;
102        list.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type));
103      }
104      CheckedItemList<ISymbolicExpressionTreeManipulator> checkedItemList = new CheckedItemList<ISymbolicExpressionTreeManipulator>();
105      checkedItemList.AddRange(list.OrderBy(op => op.Name));
106      Operators = checkedItemList.AsReadOnly();
107      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems));
108    }
109
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new MultiSymbolicExpressionTreeArchitectureManipulator(this, cloner);
112    }
113
114    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
115      base.Operators_ItemsReplaced(sender, e);
116      ParameterizeManipulators();
117    }
118
119    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
120      base.Operators_ItemsAdded(sender, e);
121      ParameterizeManipulators();
122    }
123
124    private void ParameterizeManipulators() {
125      foreach (ISymbolicExpressionTreeArchitectureManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
126        manipulator.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
127        manipulator.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
128      }
129      foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
130        manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
131        manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
132      }
133      foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
134        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
135      }
136      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
137        manipulator.RandomParameter.ActualName = RandomParameter.Name;
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.