Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/MultiSymbolicExpressionTreeManipulator.cs @ 9456

Last change on this file since 9456 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.1 KB
RevLine 
[3418]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3418]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
[3527]22using System;
[6929]23using System.Collections.Generic;
[3425]24using System.Linq;
[3418]25using HeuristicLab.Collections;
[4722]26using HeuristicLab.Common;
[3418]27using HeuristicLab.Core;
[4068]28using HeuristicLab.Data;
[3418]29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3569]33using HeuristicLab.PluginInfrastructure;
[3418]34
[5499]35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[3534]36  [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
[3419]37  [StorableClass]
[5499]38  public sealed class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>,
39    ISymbolicExpressionTreeManipulator,
40    IStochasticOperator,
41    ISymbolicExpressionTreeSizeConstraintOperator {
[3534]42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
[5499]43    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
44    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
[3534]45
[3418]46    public override bool CanChangeName {
47      get { return false; }
48    }
[3425]49    protected override bool CreateChildOperation {
50      get { return true; }
51    }
52
[5499]53    #region Parameter Properties
54    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
[3418]56    }
[5499]57    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
[3418]59    }
[5499]60    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
61      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
[3534]62    }
63    #endregion
[3418]64
65    [StorableConstructor]
[3534]66    private MultiSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
[4722]67    private MultiSymbolicExpressionTreeManipulator(MultiSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
[3534]68    public MultiSymbolicExpressionTreeManipulator()
[3418]69      : base() {
[5499]70      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
71      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
72      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
[3674]73
[6929]74      List<ISymbolicExpressionTreeManipulator> list = new List<ISymbolicExpressionTreeManipulator>();
[7052]75      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicExpressionTreeManipulator))) {
76        if (this.GetType().Assembly != type.Assembly) continue;
77        if (typeof(IMultiOperator<ISymbolicExpressionTreeManipulator>).IsAssignableFrom(type)) continue;
[7054]78        if (typeof(ISymbolicExpressionTreeArchitectureAlteringOperator).IsAssignableFrom(type)) continue;
[7052]79        list.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type));
[3674]80      }
[6929]81      CheckedItemList<ISymbolicExpressionTreeManipulator> checkedItemList = new CheckedItemList<ISymbolicExpressionTreeManipulator>();
82      checkedItemList.AddRange(list.OrderBy(op => op.Name));
83      Operators = checkedItemList.AsReadOnly();
[5719]84      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems));
[3418]85    }
86
[4722]87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new MultiSymbolicExpressionTreeManipulator(this, cloner);
89    }
90
[3534]91    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
[3445]92      base.Operators_ItemsReplaced(sender, e);
[3534]93      ParameterizeManipulators();
[3425]94    }
95
[3534]96    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
[3445]97      base.Operators_ItemsAdded(sender, e);
[3534]98      ParameterizeManipulators();
[3418]99    }
100
[3534]101    private void ParameterizeManipulators() {
[5499]102      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
103        manipulator.RandomParameter.ActualName = RandomParameter.Name;
104      }
[3534]105      foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
106        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
[3418]107      }
[5499]108      foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
109        manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
110        manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
[3418]111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.