1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HEAL.Attic;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace 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 = checkedItemList;
|
---|
85 | Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems));
|
---|
86 |
|
---|
87 | SelectedOperatorParameter.ActualName = "SelectedManipulationOperator";
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | return new MultiSymbolicExpressionTreeManipulator(this, cloner);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
95 | double sum = Operators.CheckedItems.Sum(o => Probabilities[o.Index]);
|
---|
96 | if (sum.IsAlmost(0)) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
|
---|
97 | double r = random.NextDouble() * sum;
|
---|
98 | sum = 0;
|
---|
99 | int index = -1;
|
---|
100 | foreach (var indexedItem in Operators.CheckedItems) {
|
---|
101 | sum += Probabilities[indexedItem.Index];
|
---|
102 | if (sum > r) {
|
---|
103 | index = indexedItem.Index;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | Operators[index].Manipulate(random, symbolicExpressionTree);
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
111 | base.Operators_ItemsReplaced(sender, e);
|
---|
112 | ParameterizeManipulators();
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>> e) {
|
---|
116 | base.Operators_ItemsAdded(sender, e);
|
---|
117 | ParameterizeManipulators();
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void ParameterizeManipulators() {
|
---|
121 | foreach (IStochasticOperator manipulator in Operators.OfType<IStochasticOperator>()) {
|
---|
122 | manipulator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
123 | }
|
---|
124 | foreach (ISymbolicExpressionTreeManipulator manipulator in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
125 | manipulator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
126 | }
|
---|
127 | foreach (ISymbolicExpressionTreeSizeConstraintOperator manipulator in Operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
128 | manipulator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
|
---|
129 | manipulator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|