Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Manipulators/MultiSymbolicExpressionTreeManipulator.cs @ 15221

Last change on this file since 15221 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.5 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
35  [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
36  [StorableClass]
37  public class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>, ISymbolicExpressionTreeManipulator, 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
43    public override bool CanChangeName {
44      get { return false; }
45    }
46    protected override bool CreateChildOperation {
47      get { return true; }
48    }
49
50    #region ISymbolicExpressionTreeManipulator Members
51    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
52      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
53    }
54    #endregion
55
56    #region ISymbolicExpressionTreeOperator Members
57    public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
59    }
60    public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
61      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
62    }
63    public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
64      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
65    }
66    #endregion
67
68
69    [StorableConstructor]
70    private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
71    public MultiSymbolicExpressionTreeManipulator()
72      : base() {
73      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
74      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
75      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
76      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
77
78      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
79        if (!typeof(MultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type) && !typeof(ISymbolicExpressionTreeArchitectureManipulator).IsAssignableFrom(type))
80          Operators.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type), true);
81      }
82    }
83
84    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
85      base.Operators_ItemsReplaced(sender, e);
86      ParameterizeManipulators();
87    }
88
89    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
90      base.Operators_ItemsAdded(sender, e);
91      ParameterizeManipulators();
92    }
93
94    private void ParameterizeManipulators() {
95      foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
96        manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;
97        manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;
98        manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;
99        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
100      }
101
102      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
103        manipulator.RandomParameter.ActualName = RandomParameter.Name;
104      }
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.