source: branches/SpectralKernelForGaussianProcesses/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/MultiSymbolicExpressionTreeArchitectureManipulator.cs @ 10479

Last change on this file since 10479 was 10479, checked in by gkronber, 6 years ago

#2124 merged all changes from trunk to prepare for trunk-reintegration

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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      SelectedOperatorParameter.ActualName = "SelectedManipulationOperator";
110    }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new MultiSymbolicExpressionTreeArchitectureManipulator(this, cloner);
114    }
115
116    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
117      base.Operators_ItemsReplaced(sender, e);
118      ParameterizeManipulators();
119    }
120
121    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
122      base.Operators_ItemsAdded(sender, e);
123      ParameterizeManipulators();
124    }
125
126    private void ParameterizeManipulators() {
127      foreach (ISymbolicExpressionTreeArchitectureManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
128        manipulator.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
129        manipulator.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
130      }
131      foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
132        manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
133        manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
134      }
135      foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
136        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
137      }
138      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
139        manipulator.RandomParameter.ActualName = RandomParameter.Name;
140      }
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.