Free cookie consent management tool by TermsFeed Policy Generator

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