Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/MultiSymbolicExpressionTreeManipulator.cs @ 17106

Last change on this file since 17106 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  [Item("MultiSymbolicExpressionTreeManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
37  [StorableType("0A0ED8D8-69D4-4F58-A4FD-6D97F27B4009")]
38  public sealed class MultiSymbolicExpressionTreeManipulator : StochasticMultiBranch<ISymbolicExpressionTreeManipulator>,
39    ISymbolicExpressionTreeManipulator,
40    IStochasticOperator,
41    ISymbolicExpressionTreeSizeConstraintOperator {
42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
43    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
44    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
45
46    public override bool CanChangeName {
47      get { return false; }
48    }
49    protected override bool CreateChildOperation {
50      get { return true; }
51    }
52
53    #region Parameter Properties
54    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
56    }
57    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
59    }
60    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
61      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
62    }
63    #endregion
64
65    [StorableConstructor]
66    private MultiSymbolicExpressionTreeManipulator(StorableConstructorFlag _) : base(_) { }
67    private MultiSymbolicExpressionTreeManipulator(MultiSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
68    public MultiSymbolicExpressionTreeManipulator()
69      : base() {
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)."));
73
74      List<ISymbolicExpressionTreeManipulator> list = new List<ISymbolicExpressionTreeManipulator>();
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;
78        if (typeof(ISymbolicExpressionTreeArchitectureAlteringOperator).IsAssignableFrom(type)) continue;
79        list.Add((ISymbolicExpressionTreeManipulator)Activator.CreateInstance(type));
80      }
81      CheckedItemList<ISymbolicExpressionTreeManipulator> checkedItemList = new CheckedItemList<ISymbolicExpressionTreeManipulator>();
82      checkedItemList.AddRange(list.OrderBy(op => op.Name));
83      Operators = checkedItemList.AsReadOnly();
84      Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems));
85
86      SelectedOperatorParameter.ActualName = "SelectedManipulationOperator";
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new MultiSymbolicExpressionTreeManipulator(this, cloner);
91    }
92
93    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
94      base.Operators_ItemsReplaced(sender, e);
95      ParameterizeManipulators();
96    }
97
98    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
99      base.Operators_ItemsAdded(sender, e);
100      ParameterizeManipulators();
101    }
102
103    private void ParameterizeManipulators() {
104      foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
105        manipulator.RandomParameter.ActualName = RandomParameter.Name;
106      }
107      foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
108        manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
109      }
110      foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
111        manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
112        manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
113      }
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.