Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs @ 12547

Last change on this file since 12547 was 12480, checked in by mkommend, 10 years ago

#2320: Fixed error in symbolicexpressiontree crossover regarding the wiring of lookup parameters if persisted file is loaded.

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  /// <summary>
30  /// A base class for operators that perform a crossover of symbolic expression trees.
31  /// </summary>
32  [Item("SymbolicExpressionTreeCrossover", "A base class for operators that perform a crossover of symbolic expression trees.")]
33  [StorableClass]
34  public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover {
35    private const string ParentsParameterName = "Parents";
36    #region Parameter Properties
37    public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter {
38      get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; }
39    }
40    #endregion
41    #region Properties
42    private ItemArray<ISymbolicExpressionTree> Parents {
43      get { return ParentsParameter.ActualValue; }
44    }
45    private ISymbolicExpressionTree Child {
46      get { return SymbolicExpressionTreeParameter.ActualValue; }
47      set { SymbolicExpressionTreeParameter.ActualValue = value; }
48    }
49    #endregion
50    [StorableConstructor]
51    protected SymbolicExpressionTreeCrossover(bool deserializing) : base(deserializing) { }
52    protected SymbolicExpressionTreeCrossover(SymbolicExpressionTreeCrossover original, Cloner cloner) : base(original, cloner) { }
53    protected SymbolicExpressionTreeCrossover()
54      : base() {
55      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));
56      ParentsParameter.ActualName = "SymbolicExpressionTree";
57    }
58
59    public sealed override IOperation InstrumentedApply() {
60      if (Parents.Length != 2)
61        throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators.");
62
63      ISymbolicExpressionTree result = Crossover(RandomParameter.ActualValue, Parents[0], Parents[1]);
64
65      Child = result;
66      return base.InstrumentedApply();
67    }
68
69    public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1);
70
71
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() {
74      // BackwardsCompatibility3.4
75      #region Backwards compatible code, remove with 3.5
76      if (Parameters.ContainsKey("Child")) {
77        var oldChildParameter = (ILookupParameter<ISymbolicExpressionTree>)Parameters["Child"];
78        Parameters.Remove("Child");
79        SymbolicExpressionTreeParameter.ActualName = oldChildParameter.ActualName;
80      }
81      #endregion
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.