Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/TracingSymbolicExpressionTreeCrossover.cs @ 7479

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

#1772: Implemented an initial set of features: individual ancestry, genealogy tracking via customized genetic operators and data structures.

File size: 6.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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29using TreeCacheType = HeuristicLab.Core.ItemList<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree>;
30using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree,
31                                                      HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree>;
32using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree,
33                                                      HeuristicLab.Core.IItemArray<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree>>;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  /// <summary>
37  /// A base class for operators that perform a crossover of symbolic expression trees.
38  /// </summary>
39  [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
40  [StorableClass]
41  public abstract class TracingSymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
42    private const string ParentsParameterName = "Parents";
43    private const string ChildParameterName = "Child";
44    private const string GlobalTraceMapParameterName = "GlobalTraceMap";
45    private const string GlobalCloneMapParameterName = "GlobalCloneMap";
46    #region Parameter Properties
47    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
48      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
49    }
50    public ILookupParameter<ISymbolicExpressionTree> ChildParameter {
51      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; }
52    }
53    public LookupParameter<CloneMapType> GlobalCloneMapParameter {
54      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
55    }
56    public LookupParameter<TraceMapType> GlobalTraceMapParameter {
57      get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
58    }
59    #endregion
60    #region Properties
61    public ItemArray<ISymbolicExpressionTree> Parents {
62      get { return ParentsParameter.ActualValue; }
63    }
64    public ISymbolicExpressionTree Child {
65      get { return ChildParameter.ActualValue; }
66      set { ChildParameter.ActualValue = value; }
67    }
68    public CloneMapType GlobalCloneMap {
69      get { return GlobalCloneMapParameter.ActualValue; }
70    }
71    public TraceMapType GlobalTraceMap {
72      get { return GlobalTraceMapParameter.ActualValue; }
73    }
74    #endregion
75    [StorableConstructor]
76    protected TracingSymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { }
77    protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { }
78    protected TracingSymbolicExpressionTreeCrossover()
79      : base() {
80      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
81      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
82      Parameters.Add(new LookupParameter<ItemDictionary<ISymbolicExpressionTree, ISymbolicExpressionTree>>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
83      Parameters.Add(new LookupParameter<ItemDictionary<ISymbolicExpressionTree, IItemArray<ISymbolicExpressionTree>>>(GlobalTraceMapParameterName, "A global cache containing tracing info."));
84    }
85
86    public sealed override IOperation Apply() {
87      if (Parents.Length != 2)
88        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
89      // add global trace cache if not already present in global scope
90      if (GlobalTraceMap == null) {
91        var gScope = ExecutionContext.Scope;
92        while (gScope.Parent != null) gScope = gScope.Parent;
93        gScope.Variables.Add(new Variable(GlobalTraceMapParameterName, new ItemDictionary<ISymbolicExpressionTree, IItemArray<ISymbolicExpressionTree>>()));
94      }
95      var originalParents = new ItemArray<ISymbolicExpressionTree>(Parents.Select(x => GlobalCloneMap[x]));
96      // prevent incestuous crossover (an individual mating with itself)
97      if (originalParents[0] == originalParents[1]) {
98        Child = Parents[0];
99        return base.Apply();
100      }
101      ISymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]);
102
103      Child = result;
104      GlobalTraceMap.Add(Child, originalParents);
105
106      return base.Apply();
107    }
108
109    protected abstract ISymbolicExpressionTree Cross(IRandom random,
110      ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
111  }
112}
Note: See TracBrowser for help on using the repository browser.