Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/TracingSymbolicExpressionTreeManipulator.cs @ 7514

Last change on this file since 7514 was 7514, checked in by bburlacu, 12 years ago

#1772: Merged latest trunk changes to SymbolicExpressionTreeEncoding. Deleted unneeded obj folder.

File size: 5.0 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
29using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  /// <summary>
33  /// A base class for operators that manipulate real-valued vectors.
34  /// </summary>
35  [Item("TracingSymbolicExpressionTreeManipulator", "A base class for operators that manipulate symbolic expression trees.")]
36  [StorableClass]
37  public abstract class TracingSymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator {
38    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
39    private const string GlobalTraceMapParameterName = "GlobalTraceMap";
40    private const string GlobalCloneMapParameterName = "GlobalCloneMap";
41
42    #region Parameter Properties
43    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
44      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
45    }
46    public LookupParameter<CloneMapType> GlobalCloneMapParameter {
47      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
48    }
49    public LookupParameter<TraceMapType> GlobalTraceMapParameter {
50      get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
51    }
52    #endregion
53
54    #region Properties
55    public ISymbolicExpressionTree SymbolicExpressionTree {
56      get { return SymbolicExpressionTreeParameter.ActualValue; }
57    }
58    public CloneMapType GlobalCloneMap {
59      get { return GlobalCloneMapParameter.ActualValue; }
60    }
61    public TraceMapType GlobalTraceMap {
62      get { return GlobalTraceMapParameter.ActualValue; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected TracingSymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { }
68    protected TracingSymbolicExpressionTreeManipulator(TracingSymbolicExpressionTreeManipulator original, Cloner cloner) : base(original, cloner) { }
69    public TracingSymbolicExpressionTreeManipulator()
70      : base() {
71      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));
72      Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
73      Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing tracing info."));
74    }
75
76    public sealed override IOperation Apply() {
77      ISymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
78
79      if (GlobalTraceMap == null) {
80        var gScope = ExecutionContext.Scope;
81        while (gScope.Parent != null) gScope = gScope.Parent;
82        gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new TraceMapType()));
83      }
84      if (GlobalTraceMap.ContainsKey(tree)) {
85        // tree was affected by crossover before mutation
86        // if the tree to be manipulated is already a product of crossover (in the current reproduction phase), then there exists no relationship with the original "parent".
87        // In order to preserve information, a tree clone is created before mutation and added to the trace map.
88        var clone = (IItem)tree.Clone();
89        GlobalTraceMap[clone] = GlobalTraceMap[tree];
90        GlobalTraceMap[tree] = new ItemList<IItem> { clone };
91      } else {
92        var original = GlobalCloneMap[tree];
93        GlobalTraceMap[tree] = new ItemList<IItem> { original };
94      }
95      Manipulate(RandomParameter.ActualValue, tree);
96
97      return base.Apply();
98    }
99
100    protected abstract void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree);
101  }
102}
Note: See TracBrowser for help on using the repository browser.