Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Manipulators/MultiSymbolicExpressionTreeManipulator.cs @ 3534

Last change on this file since 3534 was 3534, checked in by gkronber, 14 years ago

Added interfaces for symbolic expression tree operators and added multi manipulation operators. #937 (Data types and operators for symbolic expression tree encoding)

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.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
34  [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
35  [StorableClass]
36  public class MultiSymbolicExpressionTreeManipulator : StochasticMultiOperator<ISymbolicExpressionTreeManipulator>, ISymbolicExpressionTreeManipulator, IStochasticOperator {
37    private const string MaxTreeSizeParameterName = "MaxTreeSize";
38    private const string MaxTreeHeightParameterName = "MaxTreeHeight";
39    private const string SymbolicExpressionGrammarParameterName = "SymbolicExpressionGrammar";
40    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
41
42    public override bool CanChangeName {
43      get { return false; }
44    }
45    protected override bool CreateChildOperation {
46      get { return true; }
47    }
48
49    #region ISymbolicExpressionTreeManipulator Members
50    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
51      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
52    }
53    #endregion
54
55    #region ISymbolicExpressionTreeOperator Members
56    public IValueLookupParameter<IntValue> MaxTreeSizeParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeSizeParameterName]; }
58    }
59    public IValueLookupParameter<IntValue> MaxTreeHeightParameter {
60      get { return (IValueLookupParameter<IntValue>)Parameters[MaxTreeHeightParameterName]; }
61    }
62    public ILookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionGrammarParameter {
63      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
64    }
65    #endregion
66
67
68    [StorableConstructor]
69    private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
70    public MultiSymbolicExpressionTreeManipulator()
71      : base() {
72      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
73      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeSizeParameterName, "The maximal size (number of nodes) of the symbolic expression tree."));
74      Parameters.Add(new ValueLookupParameter<IntValue>(MaxTreeHeightParameterName, "The maximal height of the symbolic expression tree (a tree with one node has height = 0)."));
75      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionGrammarParameterName, "The grammar that defines the allowed symbols and syntax of the symbolic expression trees."));
76
77      Operators.Add(new FullTreeShaker());
78      Operators.Add(new OnePointShaker());
79      Operators.Add(new ChangeNodeTypeManipulation());
80    }
81
82    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
83      base.Operators_ItemsReplaced(sender, e);
84      ParameterizeManipulators();
85    }
86
87    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
88      base.Operators_ItemsAdded(sender, e);
89      ParameterizeManipulators();
90    }
91
92    private void ParameterizeManipulators() {
93      foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
94        manipulator.MaxTreeSizeParameter.ActualName = MaxTreeSizeParameter.Name;
95        manipulator.MaxTreeHeightParameter.ActualName = MaxTreeHeightParameter.Name;
96        manipulator.SymbolicExpressionGrammarParameter.ActualName = SymbolicExpressionGrammarParameter.Name;
97        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
98      }
99
100      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
101        manipulator.RandomParameter.ActualName = RandomParameter.Name;
102      }
103    }
104
105    public override IOperation Apply() {
106      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one symbolic expression tree manipulator to choose from.");
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.