Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Introduced base class and interface for tracing operators. Introduced SymbolicExpressionTreeNodeComparer interface to be implemented by node matching operators. Fixed bug in the TracingSymbolicExpressionTreeManipulator where nodes modified by the Full- and OnePoint tree shakers were not correctly detected. The SymbolicExpressionTreeNodeSimilarityComparer is now injected in the tracing genetic operators as a parameter.

File size: 4.5 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.EvolutionaryTracking;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
31  /// <summary>
32  /// A base class for operators that perform a crossover of symbolic expression trees.
33  /// </summary>
34  [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
35  [StorableClass]
36  public abstract class TracingSymbolicExpressionTreeCrossover : TracingSymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
37    private const string ParentsParameterName = "Parents";
38    private const string ChildParameterName = "Child";
39
40
41    #region Parameter Properties
42    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
43      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
44    }
45    public ILookupParameter<ISymbolicExpressionTree> ChildParameter {
46      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; }
47    }
48    #endregion
49
50    #region Properties
51    public ItemArray<ISymbolicExpressionTree> Parents {
52      get { return ParentsParameter.ActualValue; }
53    }
54    public ISymbolicExpressionTree Child {
55      get { return ChildParameter.ActualValue; }
56      set { ChildParameter.ActualValue = value; }
57    }
58    #endregion
59
60    [StorableConstructor]
61    protected TracingSymbolicExpressionTreeCrossover(bool deserializing)
62      : base(deserializing) {
63    }
64    protected TracingSymbolicExpressionTreeCrossover(TracingSymbolicExpressionTreeCrossover original, Cloner cloner)
65      : base(original, cloner) {
66    }
67    protected TracingSymbolicExpressionTreeCrossover()
68      : base() {
69      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
70      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));
71
72    }
73    public sealed override IOperation Apply() {
74      if (Parents.Length != 2)
75        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
76
77      AddTracingVariablesToGlobalScope();
78
79      // save the nodes of parent0 in a list so we can track what modifications are made by crossover
80      var nodes0 = Parents[0].IterateNodesBreadth().ToList();
81
82      // perform crossover
83      Child = Crossover(Random, Parents[0], Parents[1]);
84
85      // create another list of parent0's nodes, so we can compare it with the first list to see what changed
86      var nodes1 = Child.IterateNodesBreadth().ToList();
87
88      // compare the two node lists and check the difference (comparing node references so we avoid false functional identity).
89      int i = 0, min = Math.Min(nodes0.Count, nodes1.Count);
90      while (i != min && ReferenceEquals(nodes0[i], nodes1[i])) ++i;
91
92      // add heredity information into the global variables
93      GlobalTraceMap[Child] = new ItemList<IItem>(Parents.Select(x => GlobalCloneMap[x])); ; // map child to its corresponding parents from the previous generation
94      GlobalFragmentMap[Child] = new GenericWrapper<ISymbolicExpressionTreeNode>(i == min ? null : nodes1[i]); // map child to the index of its fragment
95      return base.Apply();
96    }
97
98    public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
99  }
100}
Note: See TracBrowser for help on using the repository browser.