[7479] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
[7514] | 22 | using System;
|
---|
[8557] | 23 | using System.Collections.Generic;
|
---|
[8213] | 24 | using System.Linq;
|
---|
[7479] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// A base class for operators that manipulate real-valued vectors.
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("TracingSymbolicExpressionTreeManipulator", "A base class for operators that manipulate symbolic expression trees.")]
|
---|
| 35 | [StorableClass]
|
---|
[8557] | 36 | public abstract class TracingSymbolicExpressionTreeManipulator : TracingSymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator {
|
---|
[7479] | 37 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
[9083] | 38 | [Storable]
|
---|
| 39 | private readonly Dictionary<Type, byte> manipulationTypes = new Dictionary<Type, byte>();
|
---|
[7479] | 40 |
|
---|
| 41 | #region Parameter Properties
|
---|
| 42 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 43 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 44 | }
|
---|
| 45 | #endregion
|
---|
| 46 |
|
---|
| 47 | #region Properties
|
---|
| 48 | public ISymbolicExpressionTree SymbolicExpressionTree {
|
---|
| 49 | get { return SymbolicExpressionTreeParameter.ActualValue; }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
| 53 | [StorableConstructor]
|
---|
| 54 | protected TracingSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
|
---|
[9083] | 55 | protected TracingSymbolicExpressionTreeManipulator(TracingSymbolicExpressionTreeManipulator original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
| 57 | this.manipulationTypes = new Dictionary<Type, byte>(original.manipulationTypes);
|
---|
| 58 | }
|
---|
[7479] | 59 | public TracingSymbolicExpressionTreeManipulator()
|
---|
| 60 | : base() {
|
---|
| 61 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
|
---|
[9083] | 62 |
|
---|
| 63 | manipulationTypes.Add(typeof(FullTreeShaker), ManipulationType.FullTreeShaker);
|
---|
| 64 | manipulationTypes.Add(typeof(OnePointShaker), ManipulationType.OnePointShaker);
|
---|
| 65 | manipulationTypes.Add(typeof(ChangeNodeTypeManipulation), ManipulationType.ChangeNodeTypeManipulation);
|
---|
| 66 | manipulationTypes.Add(typeof(ReplaceBranchManipulation), ManipulationType.ReplaceBranchManipulation);
|
---|
[9237] | 67 | manipulationTypes.Add(typeof(RemoveBranchManipulation), ManipulationType.RemoveBranchManipulation);
|
---|
[7479] | 68 | }
|
---|
| 69 |
|
---|
| 70 | public sealed override IOperation Apply() {
|
---|
| 71 | ISymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 72 |
|
---|
[8557] | 73 | AddTracingVariablesToGlobalScope();
|
---|
[7792] | 74 |
|
---|
[8557] | 75 | /* The following block needs to be explained in more detail;
|
---|
| 76 | * If the tree was already affected by crossover (before mutation), then:
|
---|
| 77 | * - it will already be contained in the GlobalTraceMap
|
---|
| 78 | * - In order to preserve information, a clone of the tree is created
|
---|
| 79 | * - The clone represents the intermediate stage of the tree, before mutation is applied
|
---|
| 80 | * - The clone therefore represents the child after crossover so GlobalTraceMap[clone] will get tree's parents
|
---|
| 81 | * and GlobalFragmentMap[clone] will get the subtree from clone that represents the cloned fragment
|
---|
| 82 | * - After tree is mutated, we set GlobalTraceMap[tree] = clone and GlobalFragmentMap[tree] = modified node from tree
|
---|
| 83 | */
|
---|
[9083] | 84 | Fragment fragment = null;
|
---|
[7514] | 85 | if (GlobalTraceMap.ContainsKey(tree)) {
|
---|
[9083] | 86 | var clone = (IItem)tree.Clone();
|
---|
| 87 | GlobalCloneMap[clone] = GlobalCloneMap[tree];
|
---|
| 88 | GlobalCloneMap[tree] = clone;
|
---|
[8213] | 89 | GlobalTraceMap[clone] = GlobalTraceMap[tree]; // clone gets parents of tree
|
---|
| 90 | GlobalTraceMap[tree] = new ItemList<IItem> { clone }; // tree gets clone as parent
|
---|
[9083] | 91 | fragment = (Fragment)GlobalFragmentMap[tree];
|
---|
| 92 | int index = tree.IterateNodesBreadth().ToList().IndexOf(fragment.Root); // returns -1 if fragment is not present in tree (can happen if fragment is null)
|
---|
| 93 | GlobalFragmentMap[clone] = new Fragment(index == -1 ? null : ((ISymbolicExpressionTree)clone).IterateNodesBreadth().ElementAt(index));
|
---|
[7514] | 94 | } else {
|
---|
[8557] | 95 | GlobalTraceMap[tree] = new ItemList<IItem> { GlobalCloneMap[tree] };
|
---|
[7514] | 96 | }
|
---|
[9237] | 97 | var comparer = SymbolicExpressionTreeNodeComparer;
|
---|
| 98 | var clonedTree = (ISymbolicExpressionTree)tree.Clone();
|
---|
| 99 | var nodes0 = clonedTree.IterateNodesBreadth().ToList();
|
---|
[7479] | 100 | Manipulate(RandomParameter.ActualValue, tree);
|
---|
[9237] | 101 | var nodes1 = tree.IterateNodesBreadth().ToList();
|
---|
| 102 |
|
---|
[9083] | 103 | int min = Math.Min(nodes0.Count, nodes1.Count);
|
---|
[9237] | 104 | int i = 0; while (i != min && comparer.Equals(nodes0[i], nodes1[i])) ++i;
|
---|
| 105 | fragment = new Fragment(i == min ? null : nodes1[i], 1);
|
---|
| 106 | GlobalFragmentMap[tree] = fragment;
|
---|
[7479] | 107 | return base.Apply();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | protected abstract void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree);
|
---|
[9083] | 111 |
|
---|
| 112 | private struct ManipulationType {
|
---|
| 113 | public const byte FullTreeShaker = 1;
|
---|
| 114 | public const byte OnePointShaker = 2;
|
---|
| 115 | public const byte ChangeNodeTypeManipulation = 3;
|
---|
| 116 | public const byte ReplaceBranchManipulation = 4;
|
---|
[9237] | 117 | public const byte RemoveBranchManipulation = 5;
|
---|
[9083] | 118 | }
|
---|
[7479] | 119 | }
|
---|
[8213] | 120 | }
|
---|